Skip to content

Commit

Permalink
added name collision warning in browser
Browse files Browse the repository at this point in the history
  • Loading branch information
jhnns committed Feb 14, 2012
1 parent faa8e24 commit 1f703d7
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
nbproject
.idea
node_modules
nbproject/
.idea/
node_modules/
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,33 @@ code that can be evaled again.**
You can use it to serialize classes, modules or other programming objects
and reuse them in an other environment such as a browser. JSON.stringify doesnt work with programming objects (that contain functions, dates, etc.) because they're no legal JSONs.

Works with node.js (tested) or in the browser (not tested)

Installation
------------
`npm install toSrc`

Usage
-----
`require("toSrc")` returns a single function accepting these parameters:
* **Params**

1. **obj**: *The object to stringify. Can also be a primitive like `1` or `true`.*
2. **depth** (optional): *The depth to go. All nested structures like objects or arrays deeper than this will be undefined. Defaults to 1, meaning that every object or array will be undefined by default.*

* **In node.js**

`require("toSrc")(obj, depth);`

1. **obj**: *The object to stringify. Can also be a primitive like `1` or `true`.*
2. **depth** (optional): *The depth to go. All nested structures like objects or arrays deeper than this will be undefined. Defaults to 1, meaning that every object or array will be undefined by default.*
* **In the browser**

Just call `toSrc(obj, depth);`


Examples
-----

```javascript

var toSrc = require("toSrc");

// Primitives
Expand Down Expand Up @@ -76,6 +87,7 @@ Examples
}
}, 2);
// = '{"regEx": /regex/gi, "anotherObj": {"test": "test"}}'

```
For more examples, check out the `test/test.js`
Expand Down
6 changes: 5 additions & 1 deletion lib/toSrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,11 @@ function toSrc(obj, depth) {
}

if(typeof window !== 'undefined') { // IF TRUE: We're within a browser context
window.toSrc = toSrc;
if (window.toSrc === undefined) {
window.toSrc = toSrc;
} else {
console.log("Name collision: window.toSrc already exists...");
}
} else if(typeof module !== 'undefined'){ // IF TRUE: We're within a commonJS context (like node.js)
module.exports = toSrc;
}
Expand Down
4 changes: 1 addition & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,7 @@ assert.ok(
toSrc(new Date())
)
);
assert.equal(toSrc(function() {
var test = "hello";
}), 'function () {\n var test = "hello";\n}');
assert.equal(toSrc(function() { var test = "hello"; }), 'function () { var test = "hello"; }');
assert.equal(toSrc([1, 2, "3"]), '[1, 2, "3"]');
assert.equal(toSrc({
"1": 1,
Expand Down

0 comments on commit 1f703d7

Please sign in to comment.