immutable-array-methods
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -6,2 +6,5 @@ "use strict"; | ||
}); | ||
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } | ||
var push = exports.push = function push(array, value) { | ||
@@ -27,4 +30,11 @@ return array.concat([value]); | ||
var set = exports.set = function set(array, index, value) { | ||
return array.slice(0, index).concat([value]).concat(array.slice(index + 1)); | ||
return array[index] === value ? array : array.slice(0, index).concat([value]).concat(array.slice(index + 1)); | ||
}; | ||
exports.default = { push: push, pop: pop, shift: shift, unshift: unshift, splice: splice, set: set }; | ||
var flatten = exports.flatten = function flatten(array) { | ||
var _ref; | ||
return array.some(function (value) { | ||
return Array.isArray(value); | ||
}) ? (_ref = []).concat.apply(_ref, _toConsumableArray(array)) : array; | ||
}; | ||
exports.default = { push: push, pop: pop, shift: shift, unshift: unshift, splice: splice, set: set, flatten: flatten }; |
10
index.js
@@ -8,3 +8,9 @@ export const push = (array, value) => array.concat([value]); | ||
export const set = (array, index, value) => | ||
array.slice(0, index).concat([value]).concat(array.slice(index + 1)); | ||
export default {push, pop, shift, unshift, splice, set}; | ||
array[index] === value | ||
? array | ||
: array.slice(0, index).concat([value]).concat(array.slice(index + 1)); | ||
export const flatten = array => | ||
array.some(value => Array.isArray(value)) | ||
? [].concat(...array) | ||
: array; | ||
export default {push, pop, shift, unshift, splice, set, flatten}; |
{ | ||
"name": "immutable-array-methods", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Immutable versions of normally mutable array methods, such as pop(), push(), splice()", | ||
@@ -22,3 +22,3 @@ "main": "dist/index.js", | ||
"devDependencies": { | ||
"ava": "^0.11.0", | ||
"ava": "^0.14.0", | ||
"babel-cli": "^6.4.5", | ||
@@ -25,0 +25,0 @@ "babel-core": "^6.4.5", |
41
test.js
import test from 'ava'; | ||
import 'babel-core/register'; | ||
import {push, pop, unshift, shift, splice, set} from './index'; | ||
import {push, pop, unshift, shift, splice, set, flatten} from './index'; | ||
import arrayMethods from './index'; | ||
test('push()', t => { | ||
t.same(push(['a', 'b', 'c'], 'd'), ['a', 'b', 'c', 'd']); | ||
t.same(push(['a', 'b', 'c'], ['d']), ['a', 'b', 'c', ['d']]); | ||
t.deepEqual(push(['a', 'b', 'c'], 'd'), ['a', 'b', 'c', 'd']); | ||
t.deepEqual(push(['a', 'b', 'c'], ['d']), ['a', 'b', 'c', ['d']]); | ||
}); | ||
test('unshift()', t => { | ||
t.same(unshift(['a', 'b', 'c'], 'd'), ['d', 'a', 'b', 'c']); | ||
t.same(unshift(['a', 'b', 'c'], ['d']), [['d'], 'a', 'b', 'c']); | ||
t.deepEqual(unshift(['a', 'b', 'c'], 'd'), ['d', 'a', 'b', 'c']); | ||
t.deepEqual(unshift(['a', 'b', 'c'], ['d']), [['d'], 'a', 'b', 'c']); | ||
}); | ||
test('pop()', t => { | ||
t.same(pop(['a', 'b', 'c']), ['a', 'b']); | ||
t.deepEqual(pop(['a', 'b', 'c']), ['a', 'b']); | ||
}); | ||
test('shift()', t => { | ||
t.same(shift(['a', 'b', 'c']), ['b', 'c']); | ||
t.deepEqual(shift(['a', 'b', 'c']), ['b', 'c']); | ||
}); | ||
test('splice()', t => { | ||
t.same(splice(['a', 'b', 'c', 'd'], 2, 1), ['a', 'b', 'd']); | ||
t.same(splice(['a', 'b', 'c', 'd'], 2, 0, 'e'), ['a', 'b', 'e', 'c', 'd']); | ||
t.deepEqual(splice(['a', 'b', 'c', 'd'], 2, 1), ['a', 'b', 'd']); | ||
t.deepEqual(splice(['a', 'b', 'c', 'd'], 2, 0, 'e'), ['a', 'b', 'e', 'c', 'd']); | ||
}); | ||
test('set', t => { | ||
t.same(set([0, 1, 2], 0, 666), [666, 1, 2]); | ||
t.same(set([], 0, [666]), [[666]]); | ||
t.deepEqual(set([0, 1, 2], 0, 666), [666, 1, 2]); | ||
t.deepEqual(set([], 0, [666]), [[666]]); | ||
const input = [0, 1, 2]; | ||
const actual = set(input, 1, 1); | ||
const expected = input; | ||
t.is(actual, expected); | ||
}); | ||
test('flatten, no nested arrays', t => { | ||
const input = [1, 2, 3]; | ||
const actual = flatten(input); | ||
const expected = input; | ||
t.is(actual, expected); | ||
}); | ||
test('flatten, with nested arrays', t => { | ||
const input = Object.freeze([1, [2, 3], [4, [5]]]); | ||
const actual = flatten(input); | ||
const expected = [1, 2, 3, 4, [5]]; | ||
t.deepEqual(actual, expected); | ||
}); | ||
test('default import', t => { | ||
@@ -41,2 +59,3 @@ t.is(arrayMethods.push, push); | ||
t.is(arrayMethods.set, set); | ||
t.is(arrayMethods.flatten, flatten); | ||
}); |
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
6724
110