array-copywithin
Advanced tools
Comparing version 1.0.0 to 1.1.0
61
index.js
@@ -1,28 +0,3 @@ | ||
(function(root, arrayCopyWithin) { | ||
if (typeof exports !== 'undefined') { | ||
if (typeof module !== 'undefined' && module.exports) { | ||
exports = module.exports = arrayCopyWithin; | ||
} | ||
exports.arrayCopyWithin = arrayCopyWithin; | ||
} else if (typeof define === 'function' && define.amd) { | ||
define([], function() { | ||
return arrayCopyWithin; | ||
}); | ||
} | ||
if (!Array.prototype.copyWithin) { | ||
Array.prototype.copyWithin = function(target, start, end) { | ||
return arrayCopyWithin(this, target, start, end); | ||
}; | ||
} | ||
})(this, function (array, target, start, end) { | ||
if (array == null) { | ||
module.exports = function (array, target, start, end) { | ||
if (array === null || array === undefined) { | ||
throw new TypeError('this is null or undefined'); | ||
@@ -32,5 +7,3 @@ } | ||
var object = Object(array); | ||
// element (Array or Object) length | ||
var length = parseInt(object.length, 10); | ||
// start position to copy | ||
var target = parseInt(target, 10); | ||
@@ -46,27 +19,27 @@ | ||
if (start < 0) { | ||
start = length + start; | ||
} | ||
var to = target < 0 ? Math.max(length + target, 0) : Math.min(target, length); | ||
var from = start < 0 ? Math.max(length + start, 0) : Math.min(start, length); | ||
var last = end < 0 ? Math.max(length + end, 0) : Math.min(end, length); | ||
var count = Math.min(last - from, length - to); | ||
var direction = 1; | ||
if (end < 0) { | ||
end = length + end; | ||
if (from < to && to < (from + count)) { | ||
direction = -1; | ||
from += count - 1; | ||
to += count - 1; | ||
} | ||
var reverse = start > end; | ||
var count = (reverse ? start - end : end - start); | ||
while (count > 0) { | ||
count--; | ||
var from = !reverse ? start + count : end - count; | ||
var to = !reverse ? target + count : target - count; | ||
if (from in object) { | ||
object[to] = object[from]; | ||
} else { | ||
delete[to]; | ||
delete object[to]; | ||
} | ||
from += direction; | ||
to += direction; | ||
count--; | ||
} | ||
return object; | ||
}); | ||
}; |
{ | ||
"name": "array-copywithin", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Polyfill for Array.prototype.copyWithin", | ||
@@ -17,3 +17,5 @@ "main": "index.js", | ||
"keywords": [ | ||
"array" | ||
"array", | ||
"copywithin", | ||
"polyfill" | ||
], | ||
@@ -20,0 +22,0 @@ "author": { |
@@ -12,3 +12,3 @@ # array-copyWithin [![Build Status](https://travis-ci.org/1000ch/array-copyWithin.svg?branch=master)](https://travis-ci.org/1000ch/array-copyWithin) | ||
```javascript | ||
var copyWithin = require('array-copyWithin'); | ||
var copyWithin = require('array-copywithin'); | ||
@@ -24,3 +24,3 @@ var array = copyWithin([1, 2, 3, 4, 5], 0, 3); | ||
```javascript | ||
require('array-copyWithin'); | ||
require('array-copywithin/shim'); | ||
@@ -35,2 +35,2 @@ var array = [1, 2, 3, 4, 5].copyWithin(0, 3); | ||
MIT: http://1000ch.mit-license.org | ||
[MIT](https://1000ch.mit-license.org) © [Shogo Sensui](https://github.com/1000ch) |
var copyWithin = require('../'); | ||
var test = require('tape'); | ||
test('return [4, 5, 3, 4, 5]', function (t) { | ||
test('works with 2 args', function (t) { | ||
t.plan(4); | ||
t.plan(2); | ||
t.same(copyWithin([1, 2, 3, 4, 5], 0, 3), [4, 5, 3, 4, 5]); | ||
t.same(copyWithin([1, 2, 3, 4, 5], 1, 3), [1, 4, 5, 4, 5]); | ||
t.same(copyWithin([1, 2, 3, 4, 5], 1, 2), [1, 3, 4, 5, 5]); | ||
t.same(copyWithin([1, 2, 3, 4, 5], 2, 2), [1, 2, 3, 4, 5]); | ||
}); | ||
t.deepEqual( | ||
copyWithin([1, 2, 3, 4, 5], 0, 3), | ||
[4, 5, 3, 4, 5] | ||
); | ||
test('works with 3 args', function (t) { | ||
t.plan(3); | ||
t.deepEqual( | ||
[1, 2, 3, 4, 5].copyWithin(0, 3), | ||
[4, 5, 3, 4, 5] | ||
); | ||
t.same(copyWithin([1, 2, 3, 4, 5], 0, 3, 4), [4, 2, 3, 4, 5]); | ||
t.same(copyWithin([1, 2, 3, 4, 5], 1, 3, 4), [1, 4, 3, 4, 5]); | ||
t.same(copyWithin([1, 2, 3, 4, 5], 1, 2, 4), [1, 3, 4, 4, 5]); | ||
}); | ||
test('return [4, 2, 3, 4, 5]', function (t) { | ||
test('works with negative args', function (t) { | ||
t.plan(5); | ||
t.plan(2); | ||
t.deepEqual( | ||
copyWithin([1, 2, 3, 4, 5], 0, 3, 4), | ||
[4, 2, 3, 4, 5] | ||
); | ||
t.deepEqual( | ||
[1, 2, 3, 4, 5].copyWithin(0, 3, 4), | ||
[4, 2, 3, 4, 5] | ||
); | ||
t.same(copyWithin([1, 2, 3, 4, 5], 0, -2), [4, 5, 3, 4, 5]); | ||
t.same(copyWithin([1, 2, 3, 4, 5], 0, -2, -1), [4, 2, 3, 4, 5]); | ||
t.same(copyWithin([1, 2, 3, 4, 5], -4, -3, -2), [1, 3, 3, 4, 5]); | ||
t.same(copyWithin([1, 2, 3, 4, 5], -4, -3, -1), [1, 3, 4, 4, 5]); | ||
t.same(copyWithin([1, 2, 3, 4, 5], -4, -3), [1, 3, 4, 5, 5]); | ||
}); | ||
test('return [4, 2, 3, 4, 5]', function (t) { | ||
test('works with arraylike objects', function (t) { | ||
t.plan(4); | ||
t.plan(2); | ||
t.deepEqual( | ||
copyWithin([1, 2, 3, 4, 5], 0, -2, -1), | ||
[4, 2, 3, 4, 5] | ||
); | ||
t.deepEqual( | ||
[1, 2, 3, 4, 5].copyWithin(0, -2, -1), | ||
[4, 2, 3, 4, 5] | ||
); | ||
var args = (function () { return arguments; }(1, 2, 3)); | ||
t.same(Array.isArray(args), false); | ||
var argsClass = Object.prototype.toString.call(args); | ||
t.same(Array.prototype.slice.call(args), [1, 2, 3]); | ||
Array.prototype.copyWithin.call(args, -2, 0); | ||
t.same(Array.prototype.slice.call(args), [1, 1, 2]); | ||
t.same(Object.prototype.toString.call(args), argsClass); | ||
}); | ||
test('return [5, 4, 3, 4, 5]', function (t) { | ||
test('should delete the target key if the source key is not present', function (t) { | ||
t.plan(1); | ||
t.plan(2); | ||
t.deepEqual( | ||
copyWithin([1, 2, 3, 4, 5], 0, 4, 3), | ||
[4, 2, 3, 4, 5] | ||
); | ||
t.deepEqual( | ||
[1, 2, 3, 4, 5].copyWithin(0, 4, 3), | ||
[4, 2, 3, 4, 5] | ||
); | ||
t.same(copyWithin([undefined, 1, 2], 1, 0), [undefined, undefined, 1]); | ||
}); | ||
test('return {0: 1, 3: 1, length: 5}', function (t) { | ||
test('should check inherited properties as well', function (t) { | ||
t.plan(4); | ||
t.plan(2); | ||
var Parent = function Parent() {}; | ||
Parent.prototype[0] = 'foo'; | ||
var sparse = new Parent(); | ||
sparse[1] = 1; | ||
sparse[2] = 2; | ||
sparse.length = 3; | ||
var result = copyWithin(sparse, 1, 0); | ||
t.deepEqual( | ||
copyWithin({length: 5, 3: 1}, 0, 3), | ||
{0: 1, 3: 1, length: 5} | ||
); | ||
t.deepEqual( | ||
[].copyWithin.call({length: 5, 3: 1}, 0, 3), | ||
{0: 1, 3: 1, length: 5} | ||
); | ||
t.not(result['0'], undefined); | ||
t.false(Object.prototype.hasOwnProperty.call(result, 0)); | ||
t.true(Object.prototype.hasOwnProperty.call(result, 1)); | ||
t.same(result, { 1: 'foo', 2: 1, length: 3 }); | ||
}); |
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
7483
10
156