Skip to content

Commit 61877a2

Browse files
committed
initial commit
0 parents  commit 61877a2

File tree

10 files changed

+273
-0
lines changed

10 files changed

+273
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
_ignore/
2+
node_modules/
3+
.DS_Store
4+
.npm-debug.log
5+
.project

.jshintrc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"curly": false,
3+
"noempty": true,
4+
"newcap": false,
5+
"eqeqeq": true,
6+
"eqnull": true,
7+
"undef": true,
8+
"devel": true,
9+
"node": true,
10+
"browser": true,
11+
"evil": false,
12+
"latedef": true,
13+
"nonew": true,
14+
"trailing": true,
15+
"immed": true,
16+
"smarttabs": true,
17+
"strict": true,
18+
"globals": {
19+
"define": true
20+
}
21+
}

.npmignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.git*
2+
_ignore/
3+
.DS_Store
4+
.gitignore
5+
.npm-debug.log
6+
.project
7+
.travis.yml

.travis.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
language: node_js
2+
node_js:
3+
- '0.10'
4+
before_script:
5+
- npm install -g gulp

LICENSE-MIT

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Copyright (c) 2014 skratchdot
2+
3+
Permission is hereby granted, free of charge, to any person
4+
obtaining a copy of this software and associated documentation
5+
files (the "Software"), to deal in the Software without
6+
restriction, including without limitation the rights to use,
7+
copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the
9+
Software is furnished to do so, subject to the following
10+
conditions:
11+
12+
The above copyright notice and this permission notice shall be
13+
included in all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22+
OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# object-path-set
2+
3+
[![NPM version](https://badge.fury.io/js/object-path-set.svg)](http://badge.fury.io/js/object-path-set)
4+
[![Build Status](https://travis-ci.org/skratchdot/object-path-set.png?branch=master)](https://travis-ci.org/skratchdot/object-path-set)
5+
[![devDependency Status](https://david-dm.org/skratchdot/object-path-set/dev-status.svg)](https://david-dm.org/skratchdot/object-path-set#info=devDependencies)
6+
7+
[![NPM](https://nodei.co/npm/object-path-set.svg)](https://npmjs.org/package/object-path-set)
8+
9+
10+
## Description
11+
12+
set values in javascript objects by specifying a path.
13+
14+
if the path doesn't exist yet, it will be created.
15+
16+
by using this library, you can help prevent the following error from occurring:
17+
```
18+
Cannot set property 'foo' of undefined
19+
```
20+
21+
NOTE: I've re-written / used this function so many different times, I decided to publish it
22+
as a module.
23+
24+
## Getting Started
25+
26+
Install the module with: `npm install object-path-set`
27+
28+
```javascript
29+
var setPath = require('object-path-set');
30+
var obj = {foo:{bar:'baz'}};
31+
obj = setPath(obj, 'foo.bar', 'newValue'); // result: {foo:{bar:'newValue'}}
32+
obj = setPath(obj, 'foo.invalidKey', 'cool'); // result: {foo:{bar:'newValue',invalidKey: 'cool'}}
33+
obj = setPath(obj, 'foo|bar', 'again', '|'); // result: {foo:{bar:'again',invalidKey: 'cool'}}
34+
```
35+
36+
## License
37+
38+
Copyright (c) 2014 skratchdot
39+
Licensed under the MIT license.

gulpfile.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
var gulp = require('gulp');
3+
var jshint = require('gulp-jshint');
4+
var nodeunit = require('gulp-nodeunit');
5+
6+
gulp.task('jshint', function () {
7+
gulp.src('*.js')
8+
.pipe(jshint())
9+
.pipe(jshint.reporter('default'));
10+
});
11+
12+
gulp.task('test', function () {
13+
gulp.src('test.js')
14+
.pipe(nodeunit());
15+
});
16+
17+
gulp.task('watch', function () {
18+
gulp.watch(['*.js'], ['jshint', 'test']);
19+
});
20+
21+
// setup default task
22+
gulp.task('default', ['jshint', 'test', 'watch']);
23+
24+
// handle errors
25+
process.on('uncaughtException', function (e) {
26+
console.error(e);
27+
});

index.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
var setPath = function (obj, path, value, delimiter) {
4+
var arr, key;
5+
if (!obj || typeof obj !== 'object') {
6+
obj = {};
7+
}
8+
if (typeof path === 'string') {
9+
arr = path.split(delimiter || '.');
10+
key = arr[0];
11+
if (arr.length > 1) {
12+
arr.shift();
13+
obj[key] = setPath(obj[key], arr.join(delimiter || '.'), value, delimiter);
14+
} else {
15+
obj[key] = value;
16+
}
17+
}
18+
return obj;
19+
};
20+
21+
module.exports = exports = setPath;

package.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "object-path-set",
3+
"version": "0.0.1",
4+
"description": "set values in javascript objects by specifying a path",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "gulp test"
8+
},
9+
"author": "skratchdot",
10+
"license": "MIT",
11+
"bugs": {
12+
"url": "https://github.com/skratchdot/object-path-set/issues"
13+
},
14+
"homepage": "https://github.com/skratchdot/object-path-set",
15+
"repository": {
16+
"type": "git",
17+
"url": "https://github.com/skratchdot/object-path-set"
18+
},
19+
"devDependencies": {
20+
"gulp": "^3.8.1",
21+
"gulp-jshint": "^1.6.3",
22+
"gulp-nodeunit": "0.0.5"
23+
},
24+
"keywords": [
25+
"object",
26+
"path",
27+
"helper",
28+
"deep"
29+
]
30+
}

test.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
'use strict';
2+
/*
3+
======== A Handy Little Nodeunit Reference ========
4+
https://github.com/caolan/nodeunit
5+
6+
Test methods:
7+
test.expect(numAssertions)
8+
test.done()
9+
Test assertions:
10+
test.ok(value, [message])
11+
test.equal(actual, expected, [message])
12+
test.notEqual(actual, expected, [message])
13+
test.deepEqual(actual, expected, [message])
14+
test.notDeepEqual(actual, expected, [message])
15+
test.strictEqual(actual, expected, [message])
16+
test.notStrictEqual(actual, expected, [message])
17+
test.throws(block, [error], [message])
18+
test.doesNotThrow(block, [error], [message])
19+
test.ifError(value)
20+
*/
21+
var setPath = require('./index.js');
22+
var now = new Date();
23+
var getDefaultObject = function () {
24+
return {
25+
nested: {
26+
thing: {
27+
foo: 'bar'
28+
},
29+
is: {
30+
cool: true
31+
}
32+
},
33+
dataUndefined: undefined,
34+
dataDate: now,
35+
dataNumber: 42,
36+
dataString: 'foo',
37+
dataNull: null,
38+
dataBoolean: true
39+
};
40+
};
41+
42+
exports['object-path-set'] = {
43+
setUp: function (done) {
44+
// setup here
45+
done();
46+
},
47+
'types': function (test) {
48+
var obj = getDefaultObject();
49+
test.expect(5);
50+
obj = setPath(obj, 'dataUndefined', 'newValue');
51+
test.equal(typeof obj.dataUndefined, 'string', 'typeof dataUndefined is wrong');
52+
obj = setPath(obj, 'dataDate', 'newValue');
53+
test.equal(typeof obj.dataDate, 'string', 'typeof dataDate is wrong');
54+
obj = setPath(obj, 'nested', 'newValue');
55+
test.equal(typeof obj.nested, 'string', 'typeof nested is wrong');
56+
obj = setPath(obj, 'nested.foo', 'newValue');
57+
test.equal(typeof obj.nested, 'object', 'typeof nested is wrong');
58+
test.equal(typeof obj.nested.foo, 'string', 'typeof nested.foo is wrong');
59+
test.done();
60+
},
61+
'convert to object': function (test) {
62+
test.expect(5);
63+
test.deepEqual(setPath(1234, 'a', 42), {a: 42}, '1234 should have been converted to an object');
64+
test.deepEqual(setPath(null, 'a', 42), {a: 42}, 'null should have been converted to an object');
65+
test.deepEqual(setPath(true, 'a', 42), {a: 42}, 'true should have been converted to an object');
66+
test.deepEqual(setPath({a: 123}, 'a.b', 42), {a: {b: 42}}, '{a: 123} should have been converted to an object');
67+
test.deepEqual(setPath(null, 'a.b.c.d', null), {a:{b:{c:{d:null}}}}, 'null should have been converted to an object with a.b.c.d');
68+
test.done();
69+
},
70+
'delimiter': function (test) {
71+
test.expect(3);
72+
test.deepEqual(setPath({}, 'a|b|c|d', 42), {"a|b|c|d": 42}, 'should not have used my custom delimiter');
73+
test.deepEqual(setPath({}, 'a|b|c|d', 42, '|'), {a:{b:{c:{d:42}}}}, 'should have used my custom delimiter');
74+
test.deepEqual(setPath({}, 'a.b.c.d', 42, '|'), {"a.b.c.d": 42}, 'should have used my custom delimiter');
75+
test.done();
76+
},
77+
'values': function (test) {
78+
test.expect(4);
79+
test.deepEqual(setPath({}, 'a.b', 42), {a:{b: 42}}, 'expecting a number');
80+
test.deepEqual(setPath({}, 'a.b', undefined), {a:{b: undefined}}, 'expecting undefined');
81+
test.deepEqual(setPath({}, 'a.b', true), {a:{b: true}}, 'expecting a boolean');
82+
test.deepEqual(setPath({}, 'a.b', 'wow'), {a:{b: 'wow'}}, 'expecting a string');
83+
test.done();
84+
},
85+
'multiple sets': function (test) {
86+
var obj;
87+
test.expect(1);
88+
obj = setPath(obj, 'a', 42);
89+
obj = setPath(obj, 'b', true);
90+
obj = setPath(obj, 'c.d', {});
91+
obj = setPath(obj, 'c.d.e', {});
92+
obj = setPath(obj, 'c.d.f', 'foo');
93+
test.deepEqual(obj, {a: 42, b: true, c:{d:{e:{}, f:'foo'}}}, 'should have created a deep nested object');
94+
test.done();
95+
}
96+
};

0 commit comments

Comments
 (0)