immutable-ops
Advanced tools
Comparing version 0.3.0 to 0.4.0
@@ -35,2 +35,10 @@ 'use strict'; | ||
function fastArrayCopy(arr) { | ||
var copied = new Array(arr.length); | ||
for (var i = 0; i < arr.length; i++) { | ||
copied[i] = arr[i]; | ||
} | ||
return copied; | ||
} | ||
function canMutate(obj) { | ||
@@ -272,3 +280,6 @@ return obj.hasOwnProperty(MUTABILITY_TAG); | ||
var newArr = [].concat(_toConsumableArray(arr.slice(0, index)), [value], _toConsumableArray(arr.slice(index + 1))); | ||
if (arr[index] === value) return arr; | ||
var newArr = fastArrayCopy(arr); | ||
newArr[index] = value; | ||
prepareNewObject(opts, newArr); | ||
@@ -292,5 +303,11 @@ | ||
var keys = forceArray(_keys); | ||
var keysInObj = keys.filter(function (key) { | ||
return obj.hasOwnProperty(key); | ||
}); | ||
// None of the keys were in the object, so we can return `obj`. | ||
if (keysInObj.length === 0) return obj; | ||
var newObj = Object.assign({}, obj); | ||
keys.forEach(function (key) { | ||
keysInObj.forEach(function (key) { | ||
delete newObj[key]; | ||
@@ -357,2 +374,5 @@ }); | ||
var newArr = arr.filter(func); | ||
if (newArr.length === arr.length) return arr; | ||
prepareNewObject(opts, newArr); | ||
@@ -359,0 +379,0 @@ return newArr; |
@@ -208,2 +208,18 @@ 'use strict'; | ||
it('deepMerges and returns initial object when no values changed', function () { | ||
var baseObj = (0, _deepFreeze2['default'])({ | ||
deep: { | ||
dontChange: 'John' | ||
} | ||
}); | ||
var mergeObj = (0, _deepFreeze2['default'])({ | ||
deep: { | ||
dontChange: 'John' | ||
} | ||
}); | ||
var result = ops.deepMerge(mergeObj, baseObj); | ||
expect(result).to.equal(baseObj); | ||
}); | ||
it('omits a single key', function () { | ||
@@ -222,2 +238,12 @@ var obj = (0, _deepFreeze2['default'])({ | ||
it('omits a single key, returns same object if no value changes', function () { | ||
var obj = (0, _deepFreeze2['default'])({ | ||
name: 'Tommi', | ||
age: 25 | ||
}); | ||
var result = ops.omit('location', obj); | ||
expect(result).to.equal(obj); | ||
}); | ||
it('omits an array of keys', function () { | ||
@@ -259,2 +285,18 @@ var obj = (0, _deepFreeze2['default'])({ | ||
}); | ||
it('sets a value in path but returns same object if no value changes', function () { | ||
var obj = (0, _deepFreeze2['default'])({ | ||
first: { | ||
second: { | ||
value: 'value', | ||
maintain: true | ||
}, | ||
maintain: true | ||
}, | ||
maintain: true | ||
}); | ||
var result = ops.setIn('first.second.value', 'value', obj); | ||
expect(result).to.equal(obj); | ||
}); | ||
}); | ||
@@ -375,2 +417,10 @@ }); | ||
it('filter with no effect should return initial array', function () { | ||
var arr = (0, _deepFreeze2['default'])([0, 1, 2, 3]); | ||
var result = ops.filter(function (item) { | ||
return item < 4; | ||
}, arr); | ||
expect(result).to.equal(arr); | ||
}); | ||
it('set', function () { | ||
@@ -386,2 +436,9 @@ var arr = (0, _deepFreeze2['default'])([1, 2, 987, 4]); | ||
it('set with no effect should return initial array', function () { | ||
var arr = (0, _deepFreeze2['default'])([1, 2, 3, 4]); | ||
var result = ops.set(2, 3, arr); | ||
expect(result).to.equal(arr); | ||
}); | ||
it('splice with deletions', function () { | ||
@@ -388,0 +445,0 @@ var splice = ops.splice; |
{ | ||
"name": "immutable-ops", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "A collection of functions to perform immutable operations on plain JavaScript objects", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -9,2 +9,10 @@ import forOwn from 'lodash/forOwn'; | ||
function fastArrayCopy(arr) { | ||
const copied = new Array(arr.length); | ||
for (let i = 0; i < arr.length; i++) { | ||
copied[i] = arr[i]; | ||
} | ||
return copied; | ||
} | ||
export function canMutate(obj) { | ||
@@ -248,3 +256,6 @@ return obj.hasOwnProperty(MUTABILITY_TAG); | ||
const newArr = [...arr.slice(0, index), value, ...arr.slice(index + 1)]; | ||
if (arr[index] === value) return arr; | ||
const newArr = fastArrayCopy(arr); | ||
newArr[index] = value; | ||
prepareNewObject(opts, newArr); | ||
@@ -268,5 +279,9 @@ | ||
const keys = forceArray(_keys); | ||
const keysInObj = keys.filter(key => obj.hasOwnProperty(key)); | ||
// None of the keys were in the object, so we can return `obj`. | ||
if (keysInObj.length === 0) return obj; | ||
const newObj = Object.assign({}, obj); | ||
keys.forEach(key => { | ||
keysInObj.forEach(key => { | ||
delete newObj[key]; | ||
@@ -333,2 +348,5 @@ }); | ||
const newArr = arr.filter(func); | ||
if (newArr.length === arr.length) return arr; | ||
prepareNewObject(opts, newArr); | ||
@@ -335,0 +353,0 @@ return newArr; |
@@ -190,2 +190,18 @@ import chai from 'chai'; | ||
it('deepMerges and returns initial object when no values changed', () => { | ||
const baseObj = freeze({ | ||
deep: { | ||
dontChange: 'John', | ||
}, | ||
}); | ||
const mergeObj = freeze({ | ||
deep: { | ||
dontChange: 'John', | ||
}, | ||
}); | ||
const result = ops.deepMerge(mergeObj, baseObj); | ||
expect(result).to.equal(baseObj); | ||
}); | ||
it('omits a single key', () => { | ||
@@ -204,2 +220,12 @@ const obj = freeze({ | ||
it('omits a single key, returns same object if no value changes', () => { | ||
const obj = freeze({ | ||
name: 'Tommi', | ||
age: 25, | ||
}); | ||
const result = ops.omit('location', obj); | ||
expect(result).to.equal(obj); | ||
}); | ||
it('omits an array of keys', () => { | ||
@@ -241,2 +267,18 @@ const obj = freeze({ | ||
}); | ||
it('sets a value in path but returns same object if no value changes', () => { | ||
const obj = freeze({ | ||
first: { | ||
second: { | ||
value: 'value', | ||
maintain: true, | ||
}, | ||
maintain: true, | ||
}, | ||
maintain: true, | ||
}); | ||
const result = ops.setIn('first.second.value', 'value', obj); | ||
expect(result).to.equal(obj); | ||
}); | ||
}); | ||
@@ -345,2 +387,8 @@ }); | ||
it('filter with no effect should return initial array', () => { | ||
const arr = freeze([0, 1, 2, 3]); | ||
const result = ops.filter(item => item < 4, arr); | ||
expect(result).to.equal(arr); | ||
}); | ||
it('set', () => { | ||
@@ -356,2 +404,9 @@ const arr = freeze([1, 2, 987, 4]); | ||
it('set with no effect should return initial array', () => { | ||
const arr = freeze([1, 2, 3, 4]); | ||
const result = ops.set(2, 3, arr); | ||
expect(result).to.equal(arr); | ||
}); | ||
it('splice with deletions', () => { | ||
@@ -358,0 +413,0 @@ const splice = ops.splice; |
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
69483
1502