Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
bumblehead committed Jul 4, 2013
0 parents commit 21e1684
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
README.html
21 changes: 21 additions & 0 deletions MITLICENSE.txt
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.
85 changes: 85 additions & 0 deletions README.md
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'
// }]
// }
// }]
// }
//}
```
31 changes: 31 additions & 0 deletions index.js
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;
};

}());
23 changes: 23 additions & 0 deletions package.json
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. "
}
34 changes: 34 additions & 0 deletions test/objobjwalk.spec.js
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');
});

});

0 comments on commit 21e1684

Please sign in to comment.