-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 21e1684
Showing
6 changed files
with
196 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
README.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
(The MIT License) | ||
|
||
Copyright (c) 2012 Bumblehead <chris@bumblehead.com> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the 'Software'), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies | ||
of the Software, and to permit persons to whom the Software is furnished to do | ||
so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | ||
IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
objobjwalk | ||
========== | ||
**(c)[Bumblehead][0], 2012,2013** [MIT-license](#license) | ||
|
||
### OVERVIEW: | ||
|
||
Walk the object definitions of an object, modify them with a function. It may be used in a browser environment or a node.js environment. | ||
|
||
[0]: http://www.bumblehead.com "bumblehead" | ||
|
||
--------------------------------------------------------- | ||
#### <a id="install"></a>INSTALL: | ||
|
||
Scroungejs may be downloaded directly or installed through `npm`. | ||
|
||
* **npm** | ||
|
||
```bash | ||
$ npm install objobjwalk | ||
``` | ||
|
||
* **Direct Download** | ||
|
||
```bash | ||
$ git clone https://github.com/iambumblehead/objobjwalk.git | ||
$ cd objobjwalk && npm install | ||
``` | ||
|
||
--------------------------------------------------------- | ||
#### <a id="test"></a>Test: | ||
|
||
to run tests, use `npm test` from a shell. | ||
|
||
```bash | ||
$ npm test | ||
``` | ||
|
||
--------------------------------------------------------- | ||
#### <a id="get-started">GET STARTED: | ||
|
||
|
||
```javascript | ||
var newObj = objobjwalk({ | ||
a : { | ||
b : [{ | ||
c : { | ||
d : [{ | ||
type : 'deep object', | ||
value : '1' | ||
}], | ||
e : [{ | ||
type : 'deep object', | ||
value : '1' | ||
}] | ||
} | ||
}] | ||
} | ||
}, function (obj) { | ||
if (obj.type === 'deep object') { | ||
obj.value = '0'; | ||
} | ||
return obj; | ||
}); | ||
console.log(newObj); | ||
//{ | ||
// a : { | ||
// b : [{ | ||
// c : { | ||
// d : [{ | ||
// type : 'deep object', | ||
// value : '0' | ||
// }], | ||
// e : [{ | ||
// type : 'deep object', | ||
// value : '0' | ||
// }] | ||
// } | ||
// }] | ||
// } | ||
//} | ||
``` | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
|
||
var objobjwalk = ((typeof module === 'object') ? module : {}).exports = (function () { | ||
|
||
// Douglas Crockford's | ||
function isArray (obj) { | ||
if (typeof obj === 'object' && obj) { | ||
if (!(obj.propertyIsEnumerable('length'))) { | ||
return (typeof obj.length === 'number'); | ||
} | ||
} | ||
return false; | ||
}; | ||
|
||
// recurse through array and object properties | ||
return function objObjWalk (obj, fn) { | ||
if (isArray(obj)) { | ||
obj.map(function (e) { | ||
e = objobjwalk(e, fn); | ||
}); | ||
} else if (typeof obj === 'object' && obj) { | ||
for (var o in obj) { | ||
if (obj.hasOwnProperty(o)) { | ||
obj[o] = objObjWalk(obj[o], fn); | ||
} | ||
} | ||
obj = fn(obj); | ||
} | ||
return obj; | ||
}; | ||
|
||
}()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"name": "objobjwalk", | ||
"version": "0.0.1", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/iambumblehead/objobjwalk.git" | ||
}, | ||
"keywords": [ | ||
"object", | ||
"properties", | ||
"walk", | ||
"recurse", | ||
"deep" | ||
], | ||
"devDependencies" : { | ||
"jasmine-node" : ">=1.3.0" | ||
}, | ||
"scripts" : { | ||
"test" : "node node_modules/jasmine-node/bin/jasmine-node --verbose test/" | ||
}, | ||
"readmeFilename": "README.md", | ||
"description": "walk the object definitions of an object, modify them with a function. " | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
var objobjwalk = require('../index.js'); | ||
|
||
describe("objobjwalk", function () { | ||
var result, resultExpected; | ||
|
||
it("should find deeply nested object properties", function () { | ||
|
||
var newObj = objobjwalk({ | ||
a : { | ||
b : [{ | ||
c : { | ||
d : [{ | ||
type : 'deep object', | ||
value : '1' | ||
}], | ||
e : [{ | ||
type : 'deep object', | ||
value : '1' | ||
}] | ||
} | ||
}] | ||
} | ||
}, function (obj) { | ||
if (obj.type === 'deep object') { | ||
obj.value = '0'; | ||
} | ||
return obj; | ||
}); | ||
|
||
expect(newObj.a.b[0].c.d[0].value).toBe('0'); | ||
expect(newObj.a.b[0].c.e[0].value).toBe('0'); | ||
}); | ||
|
||
}); |