-
Notifications
You must be signed in to change notification settings - Fork 305
/
es7.js
58 lines (51 loc) · 1.36 KB
/
es7.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
'use strict';
/***
* @module ES7
* @description Polyfills that provide basic ES7 compatibility. This module
* provides the base for Sugar functionality, but is not a full
* polyfill suite.
*
***/
/*** @namespace Array ***/
function sameValueZero(a, b) {
if (isRealNaN(a)) {
return isRealNaN(b);
}
return a === b ? a !== 0 || 1 / a === 1 / b : false;
}
defineInstancePolyfill(sugarArray, {
/***
* @method includes(search, [fromIndex] = 0)
* @returns Boolean
* @polyfill ES7
* @short Returns true if `search` is contained within the array.
* @extra Search begins at [fromIndex], which defaults to the beginning of the
* array.
*
* @example
*
* [1,2,3].includes(2) -> true
* [1,2,3].includes(4) -> false
* [1,2,3].includes(2, 3) -> false
*
***/
'includes': function(search) {
// Force compiler to respect argument length.
var argLen = arguments.length, fromIndex = arguments[1];
var arr = this, len;
if (isString(arr)) {
return arr.includes(search, fromIndex);
}
fromIndex = fromIndex ? fromIndex.valueOf() : 0;
len = arr.length;
if (fromIndex < 0) {
fromIndex = max(0, fromIndex + len);
}
for (var i = fromIndex; i < len; i++) {
if (sameValueZero(search, arr[i])) {
return true;
}
}
return false;
}
});