Comparing version 0.1.2 to 0.1.3
113
lib/toSrc.js
/** | ||
* <p><b>MODULE: toSrc</b></p> | ||
* Converts any object into valid source code. | ||
* | ||
* <p>Converts any object into valid source code.</p> | ||
* | ||
* <p>If you dont pass a depth-parameter, all objects within an object or an | ||
* array are converted to undefined.</p> | ||
* If you dont pass a depth-parameter, all objects within an object or an | ||
* array are converted to undefined. | ||
*/ | ||
@@ -13,7 +11,6 @@ | ||
// Requirements | ||
// Variables | ||
var knownObjs; // stores all nested structures that have been processed to detect circular references | ||
// Assigning a number to every type, so type-checking will be a bit faster | ||
@@ -44,24 +41,21 @@ var typeOfObject = 1, | ||
/** | ||
* <p>The only way to check for a type in JavaScript is to compare the | ||
* result of the internal .toString() method (returns something like [object Array])</p> | ||
* The only way to check for a type in JavaScript is to compare the | ||
* result of the internal .toString() method (returns something like [object Array]) | ||
* | ||
* <p>The type can be something of: | ||
* <ul> | ||
* <li>Object</li> | ||
* <li>Array</li> | ||
* <li>Date</li> | ||
* <li>Function</li> | ||
* <li>RegExp</li> | ||
* <li>Number</li> | ||
* <li>Boolean</li> | ||
* <li>String</li> | ||
* <li>Null</li> | ||
* <li>Undefined</li> | ||
* </ul> | ||
* The type can be something of: | ||
* | ||
* - Object | ||
* - Array | ||
* - Date | ||
* - Function | ||
* - RegExp | ||
* - Number | ||
* - Boolean | ||
* - String | ||
* - Null | ||
* - Undefined | ||
* | ||
* The first character of the type must be a capital letter (unlike you may be | ||
* used to the typeof operator) | ||
* </p> | ||
* | ||
* <p></p> | ||
* | ||
* @private | ||
@@ -108,6 +102,6 @@ * @param {*} obj | ||
/** | ||
* <p>Converts any object into valid source code.</p> | ||
* Converts any object into valid source code. | ||
* | ||
* <p>If you dont pass a depth-parameter, it will default to 1. In this case | ||
* all objects within an object or an array are converted to undefined.</p> | ||
* If you dont pass a depth-parameter, it will default to 1. In this case | ||
* all objects within an object or an array are converted to undefined. | ||
* | ||
@@ -132,4 +126,4 @@ * @private | ||
if (type === typeOfObject) { | ||
if(depth > 0) { | ||
if(knownObjs.indexOf(obj) !== -1) { | ||
if (depth > 0) { | ||
if (knownObjs.indexOf(obj) !== -1) { | ||
console.log('toSrc warning: Circular reference detected within object ', obj); | ||
@@ -145,3 +139,3 @@ | ||
}} | ||
if(objString.length > 1) { | ||
if (objString.length > 1) { | ||
objString = objString.substring(0, objString.length - 2); | ||
@@ -156,5 +150,6 @@ } | ||
} else if (type === typeOfArray) { | ||
if(depth > 0) { | ||
if(knownObjs.indexOf(obj) !== -1) { | ||
if (depth > 0) { | ||
if (knownObjs.indexOf(obj) !== -1) { | ||
console.log('toSrc warning: Circular reference detected within array ', obj); | ||
return 'undefined'; | ||
@@ -168,3 +163,3 @@ } else { | ||
} | ||
if(objString.length > 1) { | ||
if (objString.length > 1) { | ||
objString = objString.substring(0, objString.length - 2); | ||
@@ -224,21 +219,19 @@ } | ||
/** | ||
* <p>Converts any object into valid source code.</p> | ||
* Converts any object into valid source code. | ||
* | ||
* <p>If you dont pass a depth-parameter, it will default to 1. In this case | ||
* all objects within an object or an array are converted to undefined.</p> | ||
* If you dont pass a depth-parameter, it will default to 1. In this case | ||
* all objects within an object or an array are converted to undefined. | ||
* | ||
* <p>Types that can be converted by this module are: | ||
* <ul> | ||
* <li>Object</li> | ||
* <li>Array</li> | ||
* <li>Date</li> | ||
* <li>Function</li> | ||
* <li>RegExp</li> | ||
* <li>Number</li> | ||
* <li>Boolean</li> | ||
* <li>String</li> | ||
* <li>Null</li> | ||
* <li>Undefined</li> | ||
* </ul> | ||
* </p> | ||
* Types that can be converted by this module are: | ||
* | ||
* - Object | ||
* - Array | ||
* - Date | ||
* - Function | ||
* - RegExp | ||
* - Number | ||
* - Boolean | ||
* - String | ||
* - Null | ||
* - Undefined | ||
* | ||
@@ -258,11 +251,11 @@ * @param {*} obj | ||
if(typeof window !== 'undefined') { // IF TRUE: We're within a browser context | ||
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; | ||
} | ||
if (typeof module !== "undefined") { | ||
module.exports = toSrc; | ||
} else if (typeof window !== "undefined") { | ||
if (window.toSrc === undefined) { | ||
window.toSrc = toSrc; | ||
} else { | ||
console.log("Name collision: window.toSrc already exists..."); | ||
} | ||
} | ||
})(); |
{ | ||
"name": "toSrc", | ||
"description": "Turns every JavaScript object or primitive into valid source code.", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"homepage": "http://jhnns.github.com/toSrc", | ||
@@ -24,3 +24,3 @@ "repository": { | ||
"engines": { | ||
"node": "0.6.x" | ||
"node": "< 0.9.x" | ||
}, | ||
@@ -27,0 +27,0 @@ "scripts": { |
@@ -1,2 +0,2 @@ | ||
**toSrc** | ||
**toSrc** [![Build Status](https://secure.travis-ci.org/jhnns/toSrc.png?branch=master)](http://travis-ci.org/jhnns/toSrc) | ||
======== | ||
@@ -12,3 +12,3 @@ | ||
----------------------------------------------------------------- | ||
<br /> | ||
@@ -19,3 +19,3 @@ Installation | ||
----------------------------------------------------------------- | ||
<br /> | ||
@@ -55,3 +55,3 @@ Examples | ||
} | ||
toSrc(testFunc); /* = 'function () {\n var test = "hello";\n}' */ | ||
toSrc(testFunc); // = 'function testFunc() {\n var test = "hello";\n}' | ||
toSrc(String); // = 'String', native functions don't expose the source code | ||
@@ -62,3 +62,4 @@ | ||
toSrc([1, 2, "3"]); // = '[1, 2, "3"]' | ||
toSrc([1, 2, ["a", "b", "c"]]); // = '[1, 2, undefined]' because the depth is 1 by default | ||
toSrc([1, 2, ["a", "b", "c"]]); // = '[1, 2, undefined]' because the depth | ||
// is 1 by default | ||
toSrc([1, 2, ["a", "b", "c"]], 2); // = '[1, 2, ["a", "b", "c"]]' | ||
@@ -88,3 +89,3 @@ | ||
----------------------------------------------------------------- | ||
<br /> | ||
@@ -95,14 +96,25 @@ API | ||
- *{ * } obj*: The object to stringify. Can also be a primitive like `1` or `true`. | ||
- *{Number=1} depth*: 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 within `obj` will be undefined by default. | ||
- *{ * } obj*:<br /> | ||
The object to stringify. Can also be a primitive like `1` or `true`. | ||
- *{Number=1} depth*:<br /> | ||
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 within `obj` will be undefined by default. | ||
**In node.js** | ||
<br /> | ||
`require("toSrc")(obj, depth);` | ||
Usage | ||
----- | ||
**In the browser** | ||
### In node.js | ||
```javascript | ||
var toSrc = require("toSrc"); | ||
toSrc(obj, depth); | ||
``` | ||
### In the browser | ||
Just call `toSrc(obj, depth);` | ||
----------------------------------------------------------------- | ||
<br /> | ||
@@ -119,3 +131,3 @@ Notes | ||
----------------------------------------------------------------- | ||
<br /> | ||
@@ -122,0 +134,0 @@ ## License |
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
Found 1 instance in 1 package
7
150
20426
386
1