Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

object-path-immutable

Package Overview
Dependencies
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

object-path-immutable - npm Package Compare versions

Comparing version 0.3.0 to 0.4.0

17

index.js

@@ -139,2 +139,19 @@ (function (root, factory){

}
objectPathImmutable.insert = function (obj, path, value, at) {
at = ~~at;
return changeImmutable(obj, path, function(clonedObj, finalPath) {
var arr = clonedObj[finalPath];
if (!isArray(arr)) {
if (arr != null && typeof arr !== 'undefined')
throw new Error('Expected ' + path + 'to be an array. Instead got ' + typeof path);
arr = [];
}
var first = arr.slice(0, at)
first.push(value)
clonedObj[finalPath] = first.concat(arr.slice(at))
return clonedObj
})
}

@@ -141,0 +158,0 @@ objectPathImmutable.del = function (obj, path, value, at){

2

package.json
{
"name": "object-path-immutable",
"version": "0.3.0",
"version": "0.4.0",
"description": "Modify deep object properties without modifying the original object (immutability). Works great with React and Redux.",

@@ -5,0 +5,0 @@ "author": "Mario Casciaro <mariocasciaro@gmail.com>",

@@ -66,3 +66,76 @@ 'use strict';

describe('insert', function(){
it('should insert value into existing array without modifying the object', function(){
var obj = {
a: ['a'],
c: {}
}
var newObj = op.insert(obj, 'a', 'b', 0)
expect(newObj).not.to.be.equal(obj)
expect(newObj.a).not.to.be.equal(obj.a)
expect(newObj.c).to.be.equal(obj.c)
expect(newObj.a).to.be.eql(['b', 'a'])
});
it('should create intermediary array', function(){
var obj = {
a: [],
c: {}
}
var newObj = op.insert(obj, 'a.0.1', 'b')
expect(newObj).not.to.be.equal(obj)
expect(newObj.a).not.to.be.equal(obj.a)
expect(newObj.c).to.be.equal(obj.c)
expect(newObj.a).to.be.eql([[, ['b']]])
})
it('should insert in another index', function(){
var obj = {
a: 'b',
b: {
c: [],
d: ['a', 'b'],
e: [{},{f: 'g'}],
f: 'i'
}
}
var newObj = op.insert(obj, 'b.d', 'asdf', 1)
expect(newObj).not.to.be.equal(obj)
expect(newObj.b.d).to.be.eql(['a', 'asdf', 'b'])
});
it('should handle sparse array', function(){
var obj = {
a: 'b',
b: {
c: [],
d: ['a', 'b'],
e: [{},{f: 'g'}],
f: 'i'
}
}
obj.b.d = new Array(4)
obj.b.d[0] = 'a'
obj.b.d[1] = 'b'
var newObj = op.insert(obj, 'b.d', 'asdf', 3)
expect(newObj).not.to.be.equal(obj)
expect(newObj.b.d).to.be.eql([
'a',
'b',
,
'asdf'
,
])
})
})
describe('push', function() {

@@ -69,0 +142,0 @@ it('should push values without modifying the object', function() {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc