Skip to content

Commit 4c73882

Browse files
committed
initial commit
0 parents  commit 4c73882

File tree

10 files changed

+268
-0
lines changed

10 files changed

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

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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
3+
module.exports = exports = function (obj, path, defaultValue, delimiter) {
4+
var arr, i;
5+
if (typeof path === 'string') {
6+
arr = path.split(delimiter || '.');
7+
for (i = 0; i < arr.length; i++) {
8+
if (obj && obj.hasOwnProperty(arr[i])) {
9+
obj = obj[arr[i]];
10+
} else {
11+
return defaultValue;
12+
}
13+
}
14+
return obj;
15+
} else {
16+
return defaultValue;
17+
}
18+
};

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-get",
3+
"version": "0.0.1",
4+
"description": "get values from 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-get/issues"
13+
},
14+
"homepage": "https://github.com/skratchdot/object-path-get",
15+
"repository": {
16+
"type": "git",
17+
"url": "https://github.com/skratchdot/object-path-get"
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: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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 getPath = 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-get'] = {
43+
setUp: function (done) {
44+
// setup here
45+
done();
46+
},
47+
'types': function (test) {
48+
var obj = getDefaultObject();
49+
test.expect(8);
50+
test.equal(typeof getPath(obj, 'dataUndefined'), 'undefined', 'typeof dataUndefined is wrong');
51+
test.equal(typeof getPath(obj, 'dataDate'), 'object', 'typeof dataDate is wrong');
52+
test.equal(typeof getPath(obj, 'nested'), 'object', 'typeof nested is wrong');
53+
test.equal(typeof getPath(obj, 'nested.thing'), 'object', 'typeof nested.thing is wrong');
54+
test.equal(typeof getPath(obj, 'nested.thing.foo'), 'string', 'typeof nested.thing.foo is wrong');
55+
test.equal(typeof getPath(obj, 'dataNumber'), 'number', 'typeof dataNumber is wrong');
56+
test.equal(typeof getPath(obj, 'dataString'), 'string', 'typeof dataString is wrong');
57+
test.equal(typeof getPath(obj, 'dataBoolean'), 'boolean', 'typeof dataBoolean is wrong');
58+
test.done();
59+
},
60+
'missing': function (test) {
61+
var obj = getDefaultObject();
62+
test.expect(3);
63+
test.equal(getPath(obj, 'invalidKey', 'wow'), 'wow', 'invalidKey should return default value (string)');
64+
test.equal(getPath(obj, 'invalidKey', null), null, 'invalidKey should return default value (null)');
65+
test.equal(getPath(obj, 'nested.invalidKey', 'nested'), 'nested', 'nested.invalidKey should return default value (string)');
66+
test.done();
67+
},
68+
'delimiter': function (test) {
69+
var obj = getDefaultObject();
70+
test.expect(4);
71+
test.equal(getPath(obj, 'nested.is.cool', null, '.'), true, 'nested.is.cool should be true with . delimiter');
72+
test.equal(getPath(obj, 'nested|is|cool', null, '|'), true, 'nested|is|cool should be true with | delimiter');
73+
test.equal(getPath(obj, 'nested|is|cool', null, '.'), null, 'nested|is|cool should be null with . delimiter');
74+
test.equal(getPath(obj, 'nested|is|cool', null), null, 'nested|is|cool should be null with default delimiter');
75+
test.done();
76+
},
77+
'values': function (test) {
78+
var obj = getDefaultObject();
79+
test.expect(5);
80+
test.equal(getPath(obj, 'dataUndefined'), undefined, 'dataUndefined is wrong');
81+
test.equal(getPath(obj, 'dataDate'), now, 'dataDate is wrong');
82+
test.equal(getPath(obj, 'dataNumber'), 42, 'dataNumber is wrong');
83+
test.equal(getPath(obj, 'dataString'), 'foo', 'dataString is wrong');
84+
test.equal(getPath(obj, 'dataBoolean'), true, 'dataBoolean is wrong');
85+
test.done();
86+
},
87+
'nested': function (test) {
88+
var obj = getDefaultObject();
89+
test.expect(3);
90+
test.equal(typeof getPath(obj, 'nested'), 'object', 'nested should be an object');
91+
test.equal(getPath(obj, 'nested.thing.foo'), 'bar', 'nested.thing.foo should be bar');
92+
test.equal(getPath(obj, 'nested.is.cool'), true, 'nested.is.cool should be true');
93+
test.done();
94+
}
95+
};

0 commit comments

Comments
 (0)