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

object-path

Package Overview
Dependencies
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

object-path - npm Package Compare versions

Comparing version 0.8.1 to 0.9.0

4

bower.json
{
"name": "object-path",
"version": "0.6.0",
"version": "0.8.2",
"main": "index.js",

@@ -17,2 +17,2 @@ "keywords": [

]
}
}

@@ -137,4 +137,12 @@ (function (root, factory){

var objectPath = {};
var objectPath = function(obj) {
return Object.keys(objectPath).reduce(function(proxy, prop) {
if (typeof objectPath[prop] === 'function') {
proxy[prop] = objectPath[prop].bind(objectPath, obj);
}
return proxy;
}, {});
};
objectPath.has = function (obj, path) {

@@ -141,0 +149,0 @@ if (isEmpty(obj)) {

{
"name": "object-path",
"description": "Access deep properties using a path",
"version": "0.8.1",
"version": "0.9.0",
"author": {

@@ -6,0 +6,0 @@ "name": "Mario Casciaro"

@@ -87,2 +87,16 @@

//bind object
var model = objectPath({
a: {
b: "d",
c: ["e", "f"]
}
});
//now any method from above is supported directly w/o passing an object
model.get("a.b"); //returns "d"
model.get(["a.c.b"], "DEFAULT"); //returns "DEFAULT"
model.del("a.b"); // obj.a.b is now undefined
model.has("a.b"); // false
```

@@ -89,0 +103,0 @@

@@ -511,1 +511,167 @@ var expect = require('chai').expect,

});
describe('bind object', function () {
// just get one scenario from each feature, so whole functionality is proxied well
it('should return the value under shallow object', function() {
var obj = getTestObj();
var model = objectPath(obj);
expect(model.get('a')).to.be.equal('b');
expect(model.get(['a'])).to.be.equal('b');
});
it('should set value under shallow object', function() {
var obj = getTestObj();
var model = objectPath(obj);
model.set('c', {m: 'o'});
expect(obj).to.have.deep.property('c.m', 'o');
obj = getTestObj();
model = objectPath(obj);
model.set(['c'], {m: 'o'});
expect(obj).to.have.deep.property('c.m', 'o');
});
it('should push value to existing array', function() {
var obj = getTestObj();
var model = objectPath(obj);
model.push('b.c', 'l');
expect(obj).to.have.deep.property('b.c.0', 'l');
obj = getTestObj();
model = objectPath(obj);
model.push(['b','c'], 'l');
expect(obj).to.have.deep.property('b.c.0', 'l');
});
it('should create the path if it does not exists', function() {
var obj = getTestObj();
var model = objectPath(obj);
var oldVal = model.ensureExists('b.g.1.l', 'test');
expect(oldVal).to.not.exist;
expect(obj).to.have.deep.property('b.g.1.l', 'test');
oldVal = model.ensureExists('b.g.1.l', 'test1');
expect(oldVal).to.be.equal('test');
expect(obj).to.have.deep.property('b.g.1.l', 'test');
});
it('should return the first non-undefined value', function(){
var obj = {
should: {have: 'prop'}
};
var model = objectPath(obj);
expect(model.coalesce([
'doesnt.exist',
['might','not','exist'],
'should.have'
])).to.equal('prop');
});
it('should empty each path according to their types', function(){
function Instance(){
this.notOwn = true;
}
/*istanbul ignore next: not part of code */
Instance.prototype.test = function(){};
/*istanbul ignore next: not part of code */
Instance.prototype.arr = [];
var
obj = {
string: 'some string',
array: ['some','array',[1,2,3]],
number: 21,
boolean: true,
object: {
some:'property',
sub: {
'property': true
}
},
instance: new Instance()
};
/*istanbul ignore next: not part of code */
obj['function'] = function(){};
var model = objectPath(obj);
model.empty(['array','2']);
expect(obj.array[2]).to.deep.equal([]);
model.empty('object.sub');
expect(obj.object.sub).to.deep.equal({});
model.empty('instance.test');
expect(obj.instance.test).to.equal(null);
expect(Instance.prototype.test).to.be.a('function');
model.empty('string');
model.empty('number');
model.empty('boolean');
model.empty('function');
model.empty('array');
model.empty('object');
model.empty('instance');
expect(obj.string).to.equal('');
expect(obj.array).to.deep.equal([]);
expect(obj.number).to.equal(0);
expect(obj.boolean).to.equal(false);
expect(obj.object).to.deep.equal({});
expect(obj.instance.notOwn).to.be.an('undefined');
expect(obj.instance.arr).to.be.an('array');
expect(obj['function']).to.equal(null);
});
it('should delete deep paths', function(){
var obj = getTestObj();
var model = objectPath(obj);
expect(model.del()).to.be.equal(obj);
model.set('b.g.1.0', 'test');
model.set('b.g.1.1', 'test');
model.set('b.h.az', 'test');
expect(obj).to.have.deep.property('b.g.1.0','test');
expect(obj).to.have.deep.property('b.g.1.1','test');
expect(obj).to.have.deep.property('b.h.az','test');
model.del('b.h.az');
expect(obj).to.not.have.deep.property('b.h.az');
expect(obj).to.have.deep.property('b.h');
model.del('b.g.1.1');
expect(obj).to.not.have.deep.property('b.g.1.1');
expect(obj).to.have.deep.property('b.g.1.0','test');
model.del(['b','g','1','0']);
expect(obj).to.not.have.deep.property('b.g.1.0');
expect(obj).to.have.deep.property('b.g.1');
expect(model.del(['b'])).to.not.have.deep.property('b.g');
expect(obj).to.be.deep.equal({'a':'b'});
});
it('should insert value into existing array', function(){
var obj = getTestObj();
var model = objectPath(obj);
model.insert('b.c', 'asdf');
expect(obj).to.have.deep.property('b.c.0', 'asdf');
expect(obj).to.not.have.deep.property('b.c.1');
});
it('should test under shallow object', function() {
var obj = getTestObj();
var model = objectPath(obj);
expect(model.has('a')).to.be.true;
expect(model.has(['a'])).to.be.true;
expect(model.has('z')).to.be.false;
expect(model.has(['z'])).to.be.false;
});
});
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