dot-object
Advanced tools
Comparing version 0.7.0 to 0.8.0
# ChangeLog | ||
## 2015-03-06, Version 0.8.0 | ||
* [[`5ce0fe8836`](https://github.com/rhalff/dot-object/commit/5ce0fe8836)] - Simplify API | ||
## 2015-03-06, Version 0.7.0 | ||
@@ -4,0 +8,0 @@ |
15
index.js
@@ -15,2 +15,9 @@ 'use strict'; | ||
var dotDefault = new DotObject('.', false); | ||
function wrap(method) { | ||
return function() { | ||
return dotDefault[method].apply(dotDefault, arguments); | ||
}; | ||
} | ||
DotObject.prototype._fill = function(a, obj, v, mod) { | ||
@@ -262,2 +269,10 @@ var k = a.shift(); | ||
DotObject.pick = wrap('pick'); | ||
DotObject.move = wrap('move'); | ||
DotObject.transfer = wrap('transfer'); | ||
DotObject.copy = wrap('copy'); | ||
DotObject.object = wrap('object'); | ||
DotObject.str = wrap('str'); | ||
DotObject.set = wrap('set'); | ||
module.exports = DotObject; |
{ | ||
"name": "dot-object", | ||
"description": "dot-object makes it possible to transform and read (JSON) objects using dot notation.", | ||
"version": "0.7.0", | ||
"version": "0.8.0", | ||
"author": { | ||
@@ -18,14 +18,15 @@ "name": "Rob Halff", | ||
"scripts": { | ||
"test": "gulp test" | ||
"test": "jshint index.js && jshint test/*.js && gulp test" | ||
}, | ||
"devDependencies": { | ||
"underscore.string": "latest", | ||
"mocha": "1.x.x", | ||
"should": "1.x.x", | ||
"gulp": "^3.8.10", | ||
"gulp-jscs": "^1.3.0", | ||
"gulp-jshint": "^1.9.0", | ||
"gulp-mocha": "^1.1.1", | ||
"gulp": "^3.8.10", | ||
"gulp-jscs": "^1.3.0", | ||
"jscs": "^1.7.3", | ||
"jscs-jsdoc": "0.0.23" | ||
"jscs-jsdoc": "0.0.23", | ||
"jshint": "^2.6.3", | ||
"mocha": "1.x.x", | ||
"should": "1.x.x", | ||
"underscore.string": "latest" | ||
}, | ||
@@ -32,0 +33,0 @@ "keywords": [ |
@@ -11,6 +11,4 @@ [![Build Status](https://travis-ci.org/rhalff/dot-object.png)](https://travis-ci.org/rhalff/dot-object) | ||
```javascript | ||
var DJ = require('dot-object'); | ||
var dot = require('dot-object'); | ||
var dj = new DJ(); | ||
var obj = { | ||
@@ -21,4 +19,4 @@ 'first_name': 'John', | ||
dj.move('first_name', 'contact.firstname', obj); | ||
dj.move('last_name', 'contact.lastname', obj); | ||
dot.move('first_name', 'contact.firstname', obj); | ||
dot.move('last_name', 'contact.lastname', obj); | ||
@@ -36,9 +34,53 @@ console.log(obj); | ||
#### Copy property from one object to another | ||
```javascript | ||
var dot = require('dot-object'); | ||
var src = { | ||
name: 'John', | ||
stuff: { | ||
phone: { | ||
brand: 'iphone', | ||
version: 6 | ||
} | ||
} | ||
}; | ||
var tgt = {name: 'Brandon'}; | ||
dot.copy('stuff.phone', 'wanna.haves.phone', src, tgt); | ||
console.log(tgt); | ||
{ | ||
name: 'Brandon', | ||
wanna: { | ||
haves: { | ||
phone: { | ||
brand: 'iphone', | ||
version: 6 | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
#### Transfer property from one object to another | ||
Does the same as copy but removes the value from the source object: | ||
```javascript | ||
dot.transfer('stuff.phone', 'wanna.haves.phone', src, tgt); | ||
// src: {"name":"John","stuff":{}} | ||
// tgt: {"name":"Brandon","wanna":{"haves":{"phone":{"brand":"iphone","version":6}}} | ||
``` | ||
#### Transform an object | ||
```javascript | ||
var DJ = require('dot-object'); | ||
var dot = require('dot-object'); | ||
var dj = new DJ(); | ||
var row = { | ||
@@ -52,3 +94,3 @@ 'id': 2, | ||
dj.object(row); | ||
dot.object(row); | ||
@@ -76,11 +118,9 @@ console.log(row); | ||
```javascript | ||
var DJ = require('dot-object'); | ||
var dot = require('dot-object'); | ||
var dj = new DJ(); | ||
var tgt = { val: 'test' }; | ||
dot.str('this.is.my.string', 'value', tgt); | ||
var obj = { val: 'test' }; | ||
dj.str('this.is.my.string', 'value', obj); | ||
console.log(tgt); | ||
console.log(obj); | ||
{ | ||
@@ -100,2 +140,4 @@ "val": "test", | ||
``` | ||
var dot = require('dot-object'); | ||
var obj = { | ||
@@ -109,3 +151,3 @@ some: { | ||
var val = dj.pick('some.nested.key', obj); | ||
var val = dot.pick('some.nested.key', obj); | ||
console.log(val); | ||
@@ -125,6 +167,4 @@ | ||
```javascript | ||
var DJ = require('dot-object'); | ||
var dot = require('dot-object'); | ||
var dj = new DJ(); | ||
var _s = require('underscore.string'); | ||
@@ -141,3 +181,3 @@ | ||
dj.object(row, mods); | ||
dot.object(row, mods); | ||
@@ -160,13 +200,11 @@ console.log(row); | ||
var DJ = require('dot-object'); | ||
var dot = require('dot-object'); | ||
var _s = require('underscore.string'); | ||
var obj = { id: 100 }; | ||
var dj = new DJ(); | ||
// use one modifier | ||
dj.str('my.title', 'this is my title', obj, _s.slugify); | ||
dot.str('my.title', 'this is my title', obj, _s.slugify); | ||
// multiple modifiers | ||
dj.str('my.title', ' this is my title ', obj, [_s.trim, _s.slugify]); | ||
dot.str('my.title', ' this is my title ', obj, [_s.trim, _s.slugify]); | ||
@@ -190,5 +228,5 @@ console.log(obj); | ||
```javascript | ||
var DJ = require('dot-object'); | ||
var Dot = require('dot-object'); | ||
var dj = new DJ('->'); | ||
var dot = new Dot('->'); | ||
@@ -206,3 +244,3 @@ var _s = require('underscore.string'); | ||
dj.object(row, mods); | ||
dot.object(row, mods); | ||
@@ -209,0 +247,0 @@ console.log(row); |
@@ -5,10 +5,8 @@ 'use strict'; | ||
var _s = require('underscore.string'); | ||
var DJ = require('../index.js'); | ||
var Dot = require('../index.js'); | ||
describe('Dot Object test:', function() { | ||
describe('Object test:', function() { | ||
it('Should Dot object keys', function() { | ||
it('Should expand dotted keys', function() { | ||
var dj = new DJ(); | ||
var row = { | ||
@@ -22,3 +20,3 @@ 'id': 2, | ||
dj.object(row); | ||
Dot.object(row); | ||
@@ -43,11 +41,9 @@ row.should.eql({ | ||
it('Should Dot Object a string', function() { | ||
it('Should expand dotted string', function() { | ||
var obj = {}; | ||
var tgt = {}; | ||
var dj = new DJ(); | ||
Dot.str('this.is.my.string', 'value', tgt); | ||
dj.str('this.is.my.string', 'value', obj); | ||
obj.should.eql({ | ||
tgt.should.eql({ | ||
'this': { | ||
@@ -64,5 +60,5 @@ 'is': { | ||
it('DJ.str Redefinition should fail', function() { | ||
it('Dot.str Redefinition should fail', function() { | ||
var obj = { | ||
var tgt = { | ||
'already': 'set' | ||
@@ -73,6 +69,4 @@ }; | ||
var dj = new DJ(); | ||
Dot.str('already.new', 'value', tgt); | ||
dj.str('already.new', 'value', obj); | ||
}).should.throw('Trying to redefine `already` which is a string'); | ||
@@ -82,11 +76,9 @@ | ||
it('DJ.str should process a modifier', function() { | ||
it('Dot.str should process a modifier', function() { | ||
var obj = {}; | ||
var tgt = {}; | ||
var dj = new DJ(); | ||
Dot.str('this.is.my.string', 'value', tgt, _s.capitalize); | ||
dj.str('this.is.my.string', 'value', obj, _s.capitalize); | ||
obj.should.eql({ | ||
tgt.should.eql({ | ||
'this': { | ||
@@ -103,15 +95,13 @@ 'is': { | ||
it('DOT.str should process multiple modifiers', function() { | ||
it('Dot.str should process multiple modifiers', function() { | ||
var obj = {}; | ||
var tgt = {}; | ||
var dj = new DJ(); | ||
dj.str( | ||
Dot.str( | ||
'this.is.my.string', | ||
' this is a test ', | ||
obj, [_s.trim, _s.underscored] | ||
tgt, [_s.trim, _s.underscored] | ||
); | ||
obj.should.eql({ | ||
tgt.should.eql({ | ||
'this': { | ||
@@ -128,3 +118,3 @@ 'is': { | ||
it('DJ.object should process a modifier', function() { | ||
it('Dot.object should process a modifier', function() { | ||
@@ -141,6 +131,4 @@ var row = { | ||
var dj = new DJ(); | ||
Dot.object(row, mods); | ||
dj.object(row, mods); | ||
row.should.eql({'page': {'title': 'My Page', 'slug': 'my-page'}}); | ||
@@ -157,4 +145,3 @@ | ||
var dj = new DJ(); | ||
dj.object(row, mods); | ||
Dot.object(row, mods); | ||
@@ -166,3 +153,3 @@ row.should.eql({'title': 'my page', 'slug': 'My Page'}); | ||
it('DJ.object should process multiple modifiers', function() { | ||
it('Dot.object should process multiple modifiers', function() { | ||
@@ -173,4 +160,3 @@ var row = {'page.name': ' My Page '}; | ||
var dj = new DJ(); | ||
dj.object(row, mods); | ||
Dot.object(row, mods); | ||
@@ -181,3 +167,3 @@ row.should.eql({'page': {'name': 'my_page'}}); | ||
it('DJ.object should work with a different seperator', function() { | ||
it('Dot.object should work with a different seperator', function() { | ||
@@ -188,4 +174,4 @@ var row = {'page=>name': ' My Page '}; | ||
var dj = new DJ('=>', false); | ||
dj.object(row, mods); | ||
var dot = new Dot('=>', false); | ||
dot.object(row, mods); | ||
@@ -192,0 +178,0 @@ row.should.eql({'page': {'name': 'my_page'}}); |
'use strict'; | ||
require('should'); | ||
var dj = require('../index.js')(); | ||
var Dot = require('../index.js'); | ||
@@ -30,3 +30,3 @@ describe('Should be able to merge:', function() { | ||
dj.move('other', 'things', link, true); | ||
Dot.move('other', 'things', link, true); | ||
@@ -65,3 +65,3 @@ link.should.eql(expected); | ||
dj.move('other', 'things.target', link, true); | ||
Dot.move('other', 'things.target', link, true); | ||
@@ -100,3 +100,3 @@ link.should.eql(expected); | ||
dj.move('other', 'things.target', link, true); | ||
Dot.move('other', 'things.target', link, true); | ||
@@ -103,0 +103,0 @@ link.should.eql(expected); |
'use strict'; | ||
require('should'); | ||
var dj = require('../index.js')(); | ||
var Dot = require('../index.js'); | ||
describe('DJ value picker:', function() { | ||
describe('Move test:', function() { | ||
@@ -24,6 +24,6 @@ it('Should be able to move properties', function() { | ||
dj.move('source', 'source.id', link); | ||
dj.move('out', 'source.port', link); | ||
dj.move('target', 'target.id', link); | ||
dj.move('in', 'target.port', link); | ||
Dot.move('source', 'source.id', link); | ||
Dot.move('out', 'source.port', link); | ||
Dot.move('target', 'target.id', link); | ||
Dot.move('in', 'target.port', link); | ||
@@ -49,6 +49,6 @@ link.should.eql(expected); | ||
dj.move('source', 'source.id', link); | ||
dj.move('out.er.nope', 'source.port', link); | ||
dj.move('target.bla.di.bla', 'target.id', link); | ||
dj.move('in', 'target.port', link); | ||
Dot.move('source', 'source.id', link); | ||
Dot.move('out.er.nope', 'source.port', link); | ||
Dot.move('target.bla.di.bla', 'target.id', link); | ||
Dot.move('in', 'target.port', link); | ||
@@ -55,0 +55,0 @@ link.should.eql(expected); |
@@ -5,9 +5,9 @@ 'use strict'; | ||
var _s = require('underscore.string'); | ||
var DJ = require('../index.js'); | ||
var Dot = require('../index.js'); | ||
describe('DJ test:', function() { | ||
describe('Override test:', function() { | ||
it('Redefinition should _not_ fail if override is true', function() { | ||
var dj = new DJ('.', true); | ||
var dot = new Dot('.', true); | ||
@@ -19,3 +19,3 @@ var obj = { | ||
dj.str('already.new', 'value', obj); | ||
dot.str('already.new', 'value', obj); | ||
@@ -29,5 +29,5 @@ obj.should.eql({ | ||
it('Redefinition should _not_ fail if override is true', function() { | ||
it('Redefinition should _not_ fail if override is true (2)', function() { | ||
var dj = new DJ('.', true); | ||
var dot = new Dot('.', true); | ||
@@ -39,4 +39,4 @@ var obj = { | ||
dj.str('already.new', 'value', obj); | ||
dj.str('some', 'new_value', obj); | ||
dot.str('already.new', 'value', obj); | ||
dot.str('some', 'new_value', obj); | ||
@@ -53,3 +53,3 @@ obj.should.eql({ | ||
var dj = new DJ('.', true); | ||
var dot = new Dot('.', true); | ||
@@ -66,3 +66,3 @@ var row = { | ||
dj.object(row, mods); | ||
dot.object(row, mods); | ||
@@ -69,0 +69,0 @@ row.should.eql({ |
'use strict'; | ||
/*jshint -W030 */ | ||
require('should'); | ||
var DJ = require('../index.js'); | ||
var Dot = require('../index.js'); | ||
describe('DJ value picker:', function() { | ||
describe('Pick:', function() { | ||
it('Should be able to pick a value', function() { | ||
var dj = new DJ('.', true); | ||
var obj = { | ||
@@ -17,3 +17,3 @@ 'some': 'value', | ||
var val = dj.pick('some', obj); | ||
var val = Dot.pick('some', obj); | ||
@@ -26,4 +26,2 @@ val.should.eql('value'); | ||
var dj = new DJ(); | ||
var obj = { | ||
@@ -35,3 +33,3 @@ 'some': { | ||
var val = dj.pick('some.other', obj); | ||
var val = Dot.pick('some.other', obj); | ||
@@ -44,4 +42,2 @@ val.should.eql('value'); | ||
var dj = new DJ(); | ||
var obj = { | ||
@@ -51,3 +47,3 @@ 'some': null | ||
var val = dj.pick('some', obj); | ||
var val = Dot.pick('some', obj); | ||
@@ -60,4 +56,2 @@ (val === null).should.be.true; | ||
var dj = new DJ(); | ||
var obj = { | ||
@@ -67,3 +61,3 @@ 'some': null | ||
var val = dj.pick('other', obj); | ||
var val = Dot.pick('other', obj); | ||
@@ -74,20 +68,18 @@ (val === undefined).should.be.true; | ||
it('Should return undefined when picking an non-existing dotted value', function() { | ||
it('Should return undefined when picking an non-existing dotted value', | ||
function() { | ||
var dj = new DJ(); | ||
var obj = { | ||
'some': null | ||
}; | ||
var obj = { | ||
'some': null | ||
}; | ||
var val = Dot.pick('some.other', obj); | ||
var val = dj.pick('some.other', obj); | ||
(val === undefined).should.be.true; | ||
(val === undefined).should.be.true; | ||
} | ||
); | ||
}); | ||
it('Should check down the object\'s prototype chain', function() { | ||
var dj = new DJ(); | ||
var obj = { | ||
@@ -99,7 +91,7 @@ 'some': { | ||
var objIns = Object.create(obj); | ||
var objIns = Object.create(obj); | ||
objIns.should.have.property('some'); | ||
var val = dj.pick('some.other', objIns); | ||
var val = Dot.pick('some.other', objIns); | ||
val.should.be.an.Object; | ||
@@ -106,0 +98,0 @@ |
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
23788
608
258
10