Skip to content

Commit

Permalink
Merge pull request #29 from zetorama/master
Browse files Browse the repository at this point in the history
Add possibility to bind object once
  • Loading branch information
mariocasciaro committed Jan 29, 2015
2 parents 57e379d + 11d5cc7 commit 1e0746b
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 4 deletions.
14 changes: 14 additions & 0 deletions README.md
Expand Up @@ -84,6 +84,20 @@ objectPath.del(obj, ["a","c",0]); // obj.a.c is now ['f']
objectPath.has(obj, "a.b"); // true
objectPath.has(obj, ["a","d"]); // false

//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

```

### Credits
Expand Down
4 changes: 2 additions & 2 deletions bower.json
@@ -1,6 +1,6 @@
{
"name": "object-path",
"version": "0.6.0",
"version": "0.8.2",
"main": "index.js",
"keywords": [
"deep",
Expand All @@ -14,4 +14,4 @@
"coverage",
"*.yml"
]
}
}
10 changes: 9 additions & 1 deletion index.js
Expand Up @@ -134,7 +134,15 @@
return obj;
}

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) {
if (isEmpty(obj)) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{
"name": "object-path",
"description": "Access deep properties using a path",
"version": "0.8.1",
"version": "0.8.2",
"author": {
"name": "Mario Casciaro"
},
Expand Down
166 changes: 166 additions & 0 deletions test.js
Expand Up @@ -508,3 +508,169 @@ describe('has', function () {
expect(objectPath.has(obj, ['1a'])).to.be.true;
});
});



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;
});

});

0 comments on commit 1e0746b

Please sign in to comment.