Comparing version 0.10.3 to 0.10.4
@@ -5,2 +5,3 @@ 'use strict'; | ||
, isArguments = require('../../function/is-arguments') | ||
, isFunction = require('../../function/is-function') | ||
, toPosInt = require('../../number/to-pos-integer') | ||
@@ -11,59 +12,97 @@ , callable = require('../../object/valid-callable') | ||
, isArray = Array.isArray, call = Function.prototype.call; | ||
, isArray = Array.isArray, call = Function.prototype.call | ||
, desc = { configurable: true, enumerable: true, writable: true, value: null } | ||
, defineProperty = Object.defineProperty; | ||
module.exports = function (arrayLike/*, mapFn, thisArg*/) { | ||
var mapFn = arguments[1], thisArg = arguments[2], Constructor, i, j, arr, l, char, code, iterator | ||
, result, getIterator; | ||
var mapFn = arguments[1], thisArg = arguments[2], Constructor, i, j, arr, l, code, iterator | ||
, result, getIterator, isPlain, value; | ||
arrayLike = Object(validValue(arrayLike)); | ||
if (!this || !Array.isPrototypeOf(this)) Constructor = Array; | ||
else Constructor = this; | ||
if (mapFn != null) callable(mapFn); | ||
if (!this || (this === Array) || !isFunction(this)) { | ||
// Result: Plain array | ||
if (!mapFn) { | ||
if (isArguments(arrayLike)) { | ||
// Source: Arguments | ||
l = arrayLike.length; | ||
if (l !== 1) return Array.apply(null, arrayLike); | ||
arr = new Array(1); | ||
arr[0] = arrayLike[0]; | ||
return arr; | ||
} | ||
if (isArray(arrayLike)) { | ||
// Source: Array | ||
arr = new Array(l = arrayLike.length); | ||
for (i = 0; i < l; ++i) arr[i] = arrayLike[i]; | ||
return arr; | ||
} | ||
} | ||
arr = []; | ||
} else { | ||
// Result: Non plain array | ||
Constructor = this; | ||
} | ||
if (!isArray(arrayLike)) { | ||
if ((getIterator = arrayLike[iteratorSymbol]) !== undefined) { | ||
arr = new Constructor(); | ||
// Source: Iterator | ||
iterator = callable(getIterator).call(arrayLike); | ||
if (Constructor) arr = new Constructor(); | ||
result = iterator.next(); | ||
i = 0; | ||
while (!result.done) { | ||
arr[i] = mapFn ? call.call(mapFn, thisArg, result.value, i) : result.value; | ||
value = mapFn ? call.call(mapFn, thisArg, result.value, i) : result.value; | ||
if (!Constructor) { | ||
arr[i] = value | ||
} else { | ||
desc.value = value; | ||
defineProperty(arr, i, desc); | ||
} | ||
result = iterator.next(); | ||
++i; | ||
} | ||
return arr; | ||
} | ||
if (isString(arrayLike)) { | ||
l = i; | ||
} else if (isString(arrayLike)) { | ||
// Source: String | ||
l = arrayLike.length; | ||
arr = new Constructor(); | ||
if (Constructor) arr = new Constructor(); | ||
for (i = 0, j = 0; i < l; ++i) { | ||
char = arrayLike[i]; | ||
value = arrayLike[i]; | ||
if ((i + 1) < l) { | ||
code = char.charCodeAt(0); | ||
if ((code >= 0xD800) && (code <= 0xDBFF)) char += arrayLike[++i]; | ||
code = value.charCodeAt(0); | ||
if ((code >= 0xD800) && (code <= 0xDBFF)) value += arrayLike[++i]; | ||
} | ||
arr.push(mapFn ? call.call(mapFn, thisArg, char, j++) : char); | ||
value = mapFn ? call.call(mapFn, thisArg, value, j) : value; | ||
if (!Constructor) { | ||
arr[j] = value | ||
} else { | ||
desc.value = value; | ||
defineProperty(arr, j, desc); | ||
} | ||
++j; | ||
} | ||
return arr; | ||
l = j; | ||
} | ||
} | ||
l = toPosInt(arrayLike.length); | ||
if (mapFn != null) { | ||
arr = new Constructor(l); | ||
for (i = 0; i < l; ++i) arr[i] = call.call(mapFn, thisArg, arrayLike[i], i); | ||
return arr; | ||
if (l === undefined) { | ||
// Source: array or array-like | ||
l = toPosInt(arrayLike.length); | ||
if (Constructor) arr = new Constructor(l); | ||
for (i = 0; i < l; ++i) { | ||
value = mapFn ? call.call(mapFn, thisArg, arrayLike[i], i) : arrayLike[i]; | ||
if (!Constructor) { | ||
arr[i] = value | ||
} else { | ||
desc.value = value; | ||
defineProperty(arr, i, desc); | ||
} | ||
} | ||
} | ||
if (isArguments(arrayLike)) { | ||
if (l !== 1) return Constructor.apply(null, arrayLike); | ||
arr = new Constructor(1); | ||
arr[0] = arrayLike[0]; | ||
return arr; | ||
if (Constructor) { | ||
desc.value = null; | ||
arr.length = l; | ||
} | ||
arr = new Constructor(l); | ||
for (i = 0; i < l; ++i) arr[i] = arrayLike[i]; | ||
return arr; | ||
}; |
'use strict'; | ||
var slice = Array.prototype.slice; | ||
var isFunction = require('../../function/is-function') | ||
module.exports = function (/*…items*/) { return slice.call(arguments); }; | ||
, slice = Array.prototype.slice, defineProperty = Object.defineProperty | ||
, desc = { configurable: true, enumerable: true, writable: true, value: null }; | ||
module.exports = function (/*…items*/) { | ||
var result, i, l; | ||
if (!this || (this === Array) || !isFunction(this)) return slice.call(arguments); | ||
result = new this(l = arguments.length); | ||
for (i = 0; i < l; ++i) { | ||
desc.value = arguments[i]; | ||
defineProperty(result, i, desc); | ||
} | ||
desc.value = null; | ||
result.length = l; | ||
return result; | ||
}; |
{ | ||
"name": "es5-ext", | ||
"version": "0.10.3", | ||
"version": "0.10.4", | ||
"description": "ECMAScript 5 extensions and ES6 shims", | ||
@@ -5,0 +5,0 @@ "author": "Mariusz Nowak <medyk@medikoo.com> (http://www.medikoo.com/)", |
@@ -163,3 +163,3 @@ # es5-ext | ||
[_Introduced with ECMAScript 6_](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-array.from). | ||
Returns array representation of `arrayLike`. If `arrayLike` is already an array its copy is returned. | ||
Returns array representation of _iterable_ or _arrayLike_. If _arrayLike_ is an instance of array, its copy is returned. | ||
@@ -892,4 +892,4 @@ #### generate([length[, …fill]]) _(es5-ext/array/generate)_ | ||
Returns a string at given position in unicode safe manner. | ||
Based on [implementation by Matthias Bynes](https://github.com/mathiasbynens/String.prototype.at). | ||
Returns a string at given position in Unicode-safe manner. | ||
Based on [implementation by Mathias Bynens](https://github.com/mathiasbynens/String.prototype.at). | ||
@@ -913,2 +913,4 @@ #### str.camelToHyphen() _(es5-ext/string/#/camel-to-hyphen)_ | ||
Based on [implementation by Mathias Bynens](https://github.com/mathiasbynens/String.prototype.codePointAt). | ||
#### str.contains(searchString[, position]) _(es5-ext/string/#/contains)_ | ||
@@ -938,2 +940,8 @@ | ||
#### str.normalize([form]) _(es5-ext/string/#/normalize)_ | ||
[_Introduced with ECMAScript 6_](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize). | ||
Returns the Unicode Normalization Form of a given string. | ||
Based on Matsuza's version. Code used for integrated shim can be found at [github.com/walling/unorm](https://github.com/walling/unorm/blob/master/lib/unorm.js) | ||
#### str.pad(fill[, length]) _(es5-ext/string/#/pad)_ | ||
@@ -940,0 +948,0 @@ |
@@ -0,5 +1,7 @@ | ||
// Some tests taken from: https://github.com/mathiasbynens/Array.from/blob/master/tests/tests.js | ||
'use strict'; | ||
module.exports = function (t, a) { | ||
var o = [1, 2, 3]; | ||
var o = [1, 2, 3], MyType; | ||
a.not(t(o), o, "Array"); | ||
@@ -20,2 +22,40 @@ a.deep(t(o), o, "Array: same content"); | ||
a.deep(t(3), [], "Primitive"); | ||
a(t.length, 1, "Length"); | ||
a.deep(t({ 'length': 0 }), [], "No values Array-like"); | ||
a.deep(t({ 'length': -1 }), [], "Invalid length Array-like"); | ||
a.deep(t({ 'length': -Infinity }), [], "Invalid length Array-like #2"); | ||
a.throws(function () { t(undefined); }, TypeError, "Undefined"); | ||
a.throws(function () { t(null); }, TypeError, "Null"); | ||
a.deep(t(false), [], "Boolean"); | ||
a.deep(t(-Infinity), [], "Inifity"); | ||
a.deep(t(-0), [], "-0"); | ||
a.deep(t(+0), [], "+0"); | ||
a.deep(t(1), [], "1"); | ||
a.deep(t(+Infinity), [], "+Infinity"); | ||
a.deep(t({}), [], "Plain object"); | ||
a.deep(t({ 'length': 1 }), [undefined], "Sparse array-like"); | ||
a.deep(t({ '0': 'a', '1': 'b', 'length': 2 }, function (x) { return x + x; }), ['aa', 'bb'], | ||
"Map"); | ||
a.deep(t({ '0': 'a', '1': 'b', 'length': 2 }, function (x) { return String(this); }, undefined), | ||
['undefined', 'undefined'], "Map context"); | ||
a.deep(t({ '0': 'a', '1': 'b', 'length': 2 }, function (x) { return String(this); }, 'x'), | ||
['x', 'x'], "Map primitive context"); | ||
a.throws(function () { t({}, 'foo', 'x'); }, TypeError, "Non callable for map"); | ||
a.deep(t.call(null, { 'length': 1, '0': 'a' }), ['a'], "Null context"); | ||
a(t({ '__proto__': { '0': 'abc', 'length': 1 } })[0], 'abc', "Values on prototype"); | ||
a.throws(function () { t.call(function () { return Object.freeze({}); }, {}); }, | ||
TypeError, "Contructor producing freezed objects"); | ||
// Ensure no setters are called for the indexes | ||
// Ensure no setters are called for the indexes | ||
MyType = function () {}; | ||
Object.defineProperty(MyType.prototype, '0', { | ||
set: function (x) { throw new Error('Setter called: ' + x); } | ||
}); | ||
a.deep(t.call(MyType, { '0': 'abc', 'length': 1 }), { '0': 'abc', 'length': 1 }, | ||
"Defined not set"); | ||
}; |
@@ -0,5 +1,10 @@ | ||
// Most tests taken from https://github.com/mathiasbynens/Array.of/blob/master/tests/tests.js | ||
// Thanks @mathiasbynens | ||
'use strict'; | ||
var defineProperty = Object.defineProperty; | ||
module.exports = function (t, a) { | ||
var x = {}; | ||
var x = {}, testObject, MyType; | ||
@@ -10,2 +15,55 @@ a.deep(t(), [], "No arguments"); | ||
"Many arguments"); | ||
a(t.length, 0, "Length"); | ||
a.deep(t('abc'), ['abc'], "String"); | ||
a.deep(t(undefined), [undefined], "Undefined"); | ||
a.deep(t(null), [null], "Null"); | ||
a.deep(t(false), [false], "Boolean"); | ||
a.deep(t(-Infinity), [-Infinity], "Infinity"); | ||
a.deep(t(-0), [-0], "-0"); | ||
a.deep(t(+0), [+0], "+0"); | ||
a.deep(t(1), [1], "1"); | ||
a.deep(t(1, 2, 3), [1, 2, 3], "Numeric args"); | ||
a.deep(t(+Infinity), [+Infinity], "+Infinity"); | ||
a.deep(t({ '0': 'a', '1': 'b', '2': 'c', 'length': 3 }), | ||
[{ '0': 'a', '1': 'b', '2': 'c', 'length': 3 }], "Array like"); | ||
a.deep(t(undefined, null, false, -Infinity, -0, +0, 1, 2, +Infinity), | ||
[undefined, null, false, -Infinity, -0, +0, 1, 2, +Infinity], "Falsy arguments"); | ||
a.h1("Null context"); | ||
a.deep(t.call(null, 'abc'), ['abc'], "String"); | ||
a.deep(t.call(null, undefined), [undefined], "Undefined"); | ||
a.deep(t.call(null, null), [null], "Null"); | ||
a.deep(t.call(null, false), [false], "Boolean"); | ||
a.deep(t.call(null, -Infinity), [-Infinity], "-Infinity"); | ||
a.deep(t.call(null, -0), [-0], "-0"); | ||
a.deep(t.call(null, +0), [+0], "+0"); | ||
a.deep(t.call(null, 1), [1], "1"); | ||
a.deep(t.call(null, 1, 2, 3), [1, 2, 3], "Numeric"); | ||
a.deep(t.call(null, +Infinity), [+Infinity], "+Infinity"); | ||
a.deep(t.call(null, { '0': 'a', '1': 'b', '2': 'c', 'length': 3 }), | ||
[{ '0': 'a', '1': 'b', '2': 'c', 'length': 3 }], "Array-like"); | ||
a.deep(t.call(null, undefined, null, false, -Infinity, -0, +0, 1, 2, +Infinity), | ||
[undefined, null, false, -Infinity, -0, +0, 1, 2, +Infinity], "Falsy"); | ||
a.h1("Other constructor context"); | ||
a.deep(t.call(Object, 1, 2, 3), { '0': 1, '1': 2, '2': 3, 'length': 3 }, "Many arguments"); | ||
testObject = Object(3); | ||
testObject[0] = 1; | ||
testObject[1] = 2; | ||
testObject[2] = 3; | ||
testObject.length = 3; | ||
a.deep(t.call(Object, 1, 2, 3), testObject, "Test object"); | ||
a(t.call(Object).length, 0, "No arguments"); | ||
a.throws(function () { t.call(function () { return Object.freeze({}); }); }, TypeError, | ||
"Frozen instance"); | ||
// Ensure no setters are called for the indexes | ||
MyType = function () {}; | ||
defineProperty(MyType.prototype, '0', { | ||
set: function (x) { throw new Error('Setter called: ' + x); } | ||
}); | ||
a.deep(t.call(MyType, 'abc'), { '0': 'abc', 'length': 1 }, "Define, not set"); | ||
}; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
400730
6771
975