code-stringify
Advanced tools
Comparing version 1.1.0 to 1.2.0
152
index.js
'use strict' | ||
module.exports = code_stringify | ||
code_stringify.Code = Code | ||
module.exports = stringify | ||
stringify.Code = Code | ||
@@ -14,3 +14,3 @@ var node_util = require('util') | ||
code_stringify.QUOTE = '\"' | ||
stringify.QUOTE = '\"' | ||
@@ -27,5 +27,12 @@ | ||
function code_stringify(value, replacer, space, indent) { | ||
var type = typeof value | ||
function stringify (value, replacer, space, indent) { | ||
space = make_sure_spaces(space) | ||
indent = make_sure_spaces(indent) | ||
value = apply_replacer(value, replacer) | ||
return code_stringify(value, space, indent) | ||
} | ||
function code_stringify(value, space, indent) { | ||
if(value === undefined){ | ||
@@ -39,2 +46,4 @@ return 'undefined' | ||
var type = typeof value | ||
if(type === 'number'){ | ||
@@ -57,3 +66,3 @@ return String(value) | ||
if(node_util.isArray(value)){ | ||
return array_to_code(value, replacer, space, indent) | ||
return array_to_code(value, space, indent) | ||
} | ||
@@ -69,6 +78,88 @@ | ||
return object_to_code(value, replacer, space, indent) | ||
return object_to_code(value, space, indent) | ||
} | ||
function apply_replacer (value, replacer) { | ||
if (node_util.isFunction(replacer)) { | ||
var input = { | ||
'': value | ||
} | ||
return apply_function_replacer(input, replacer)[''] | ||
} | ||
if ( | ||
node_util.isArray(replacer) | ||
// Array replacer only works for plain object | ||
&& is_plain_object(value) | ||
) { | ||
return apply_function_replacer(value, function (k, v) { | ||
return ~replacer.indexOf(k) | ||
? v | ||
: undefined | ||
}) | ||
} | ||
return value | ||
} | ||
function apply_function_replacer (value, replacer) { | ||
if (node_util.isArray(value)) { | ||
return apply_array_function_replacer(value, replacer) | ||
} | ||
if (is_plain_object(value)) { | ||
return apply_object_function_replacer(value, replacer) | ||
} | ||
return value | ||
} | ||
function apply_array_function_replacer (value, replacer) { | ||
return value.map(function (i, v) { | ||
v = replacer.call(value, i, v) | ||
return apply_function_replacer(v, replacer) | ||
}) | ||
} | ||
function apply_object_function_replacer (value, replacer) { | ||
var k | ||
var v | ||
for (k in value) { | ||
v = value[k] | ||
v = replacer.call(value, k, v) | ||
if (v === undefined) { | ||
delete value[k] | ||
continue | ||
} | ||
value[k] = apply_function_replacer(v, replacer) | ||
} | ||
return value | ||
} | ||
function is_plain_object (object) { | ||
return typeof object === 'object' | ||
&& object.constructor === {}.constructor | ||
} | ||
function make_sure_spaces (space) { | ||
var type = typeof space | ||
// Support string-type `space` | ||
return type === 'string' | ||
? space | ||
: type === 'number' | ||
? create_spaces(space) | ||
: '' | ||
} | ||
function create_spaces(n){ | ||
@@ -94,11 +185,8 @@ if(!n){ | ||
// @param {number} indent | ||
function object_to_code(object, replacer, space, indent) { | ||
space = space || 0 | ||
indent = indent || 0 | ||
function object_to_code(object, space, indent) { | ||
var key | ||
var value | ||
var indent_string = create_spaces(indent) | ||
var joiner = (space ? '\n' + create_spaces(space) : '') + indent_string | ||
var indent_string = indent | ||
var joiner = (space ? '\n' + space : '') + indent_string | ||
@@ -119,14 +207,6 @@ var key_value_joiner = space | ||
if(replacer){ | ||
value = replacer(key, value) | ||
if(value === undefined){ | ||
continue | ||
} | ||
} | ||
code.push( | ||
string_to_key(key) + | ||
':' + key_value_joiner + | ||
code_stringify(value, replacer, space, space + indent) | ||
code_stringify(value, space, space + indent) | ||
) | ||
@@ -145,5 +225,5 @@ } | ||
function string_to_code(string){ | ||
return code_stringify.QUOTE | ||
return stringify.QUOTE | ||
+ escape_string(string) | ||
+ code_stringify.QUOTE | ||
+ stringify.QUOTE | ||
} | ||
@@ -173,3 +253,3 @@ | ||
.replace(/\\/g, '\\\\') | ||
.replace(new RegExp(code_stringify.QUOTE, 'g'), '\\' + code_stringify.QUOTE) | ||
.replace(new RegExp(stringify.QUOTE, 'g'), '\\' + stringify.QUOTE) | ||
} | ||
@@ -179,11 +259,8 @@ | ||
// @param {Array} array | ||
function array_to_code(array, replacer, space, indent){ | ||
space = space || 0 | ||
indent = indent || 0 | ||
function array_to_code (array, space, indent) { | ||
var key | ||
var value | ||
var indent_string = create_spaces(indent) | ||
var joiner = (space ? '\n' + create_spaces(space) : '') + indent_string | ||
var indent_string = indent | ||
var joiner = (space ? '\n' + space : '') + indent_string | ||
@@ -204,14 +281,3 @@ var start = PRE_BRANKET + joiner | ||
value = array[i] | ||
if(replacer){ | ||
value = replacer( String(i), value ) | ||
if(value === undefined){ | ||
// If `replacer` returns undefined then null is used instead, as well as `JSON.stringify` | ||
value = null | ||
} | ||
} | ||
code.push( code_stringify(value, replacer, space, indent + space) ) | ||
code.push(code_stringify(value, space, indent + space)) | ||
} | ||
@@ -218,0 +284,0 @@ |
{ | ||
"name": "code-stringify", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "code-stringify is node.js module that converts JavaScript variables into source codes. Unlike JSON.stringify, code-stringify converts things into strings of code, not JSON.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -5,5 +5,5 @@ [![Build Status](https://travis-ci.org/kaelzhang/node-code-stringify.svg?branch=master)](https://travis-ci.org/kaelzhang/node-code-stringify) | ||
code-stringify is the node.js module that converts JavaScript variables into source codes. | ||
`code-stringify` is the node.js module that converts JavaScript variables into source codes. | ||
Unlike `JSON.stringify`, code-stringify also deals with reference(object) types of variables, and it converts JavaScript variables into strings of codes, not JSON. | ||
Unlike `JSON.stringify`, `code-stringify` also deals with reference(object) types of variables, and it converts JavaScript variables into strings of codes, not JSON. | ||
@@ -53,8 +53,21 @@ ## Installation | ||
##### replacer `function(key, value)` | ||
##### replacer `function(key, value)|Array` | ||
The `replacer` argument acts just like the second parameter of `JSON.stringify`. | ||
##### space `number` | ||
```js | ||
code({ | ||
a: 1, | ||
b: 2 | ||
}, function (key, value) { | ||
return key === 'b' | ||
? undefined | ||
: value | ||
}) | ||
// '{a:1}' | ||
``` | ||
##### space `number|string` | ||
The `space` argument acts just like the third parameter of `JSON.stringify`. | ||
@@ -90,2 +103,17 @@ | ||
## Versus `JSON.stringify()` | ||
value | JSON.stringify(value) | code(value) | | ||
----- | --------------------- | ----------- | ---------- | ||
`1` | `'1'` | `'1'` | | ||
`'1'` | `"1"` | `"'1'"` | you can change quote style by `code.QUOTE = '"'` | ||
`undefined` | `undefined` | `'undefined'` | | ||
`null` | `'null'` | `'null'` | | ||
[undefined] | `[null]` | `[undefined]` | | ||
[null] | `[null]` | `[null]` | | ||
- `JSON.stringify` makes JSON. | ||
- `code-stringify` makes JavaScript code. | ||
## Known Issues | ||
@@ -92,0 +120,0 @@ |
@@ -1,5 +0,5 @@ | ||
'use strict'; | ||
'use strict' | ||
var code = require('..'); | ||
var expect = require('chai').expect; | ||
var code = require('..') | ||
var expect = require('chai').expect | ||
var fs = require('fs') | ||
@@ -14,3 +14,3 @@ var node_path = require('path') | ||
def: true, | ||
'g-h': function(n){return n;}, | ||
'g-h': function(n){return n}, | ||
$i: [ | ||
@@ -48,2 +48,14 @@ { | ||
}) | ||
it('should not use normal `toCode` property', function () { | ||
expect(code({toCode: function(){return 3}})).to.equal('{toCode:function (){return 3}}') | ||
}) | ||
it('if toCode is not a function', function () { | ||
function F () { | ||
} | ||
F.prototype.toCode = 3 | ||
var f = new F | ||
expect(code(f)).to.equal('{}') | ||
}) | ||
}) | ||
@@ -53,28 +65,28 @@ | ||
it("number", function(){ | ||
var a = 1; | ||
expect( code(a) ).to.equal('1'); | ||
}); | ||
var a = 1 | ||
expect( code(a) ).to.equal('1') | ||
}) | ||
describe("string", function(){ | ||
it("no quotes", function(){ | ||
var a = 'a'; | ||
expect( code(a) ).to.equal("'a'"); | ||
}); | ||
var a = 'a' | ||
expect( code(a) ).to.equal("'a'") | ||
}) | ||
it("with double quotes", function(){ | ||
var a = '"a"'; | ||
expect( code(a) ).to.equal("'\"a\"'"); | ||
}); | ||
var a = '"a"' | ||
expect( code(a) ).to.equal("'\"a\"'") | ||
}) | ||
it("with single quotes", function(){ | ||
var a = "'a'"; | ||
expect( code(a) ).to.equal("'\\'a\\''"); | ||
}); | ||
}); | ||
var a = "'a'" | ||
expect( code(a) ).to.equal("'\\'a\\''") | ||
}) | ||
}) | ||
it("boolean", function(){ | ||
expect(code(true)).to.equal('true'); | ||
expect(code(false)).to.equal('false'); | ||
}); | ||
}); | ||
expect(code(true)).to.equal('true') | ||
expect(code(false)).to.equal('false') | ||
}) | ||
}) | ||
@@ -84,27 +96,27 @@ describe("reference types", function(){ | ||
it("no indent", function(){ | ||
expect(code([1, '2', true])).to.equal("[1,'2',true]"); | ||
}); | ||
expect(code([1, '2', true])).to.equal("[1,'2',true]") | ||
}) | ||
it("with indents", function(){ | ||
expect(code([1, '2', true], null, 4)).to.equal("[\n 1,\n '2',\n true\n]"); | ||
}); | ||
expect(code([1, '2', true], null, 4)).to.equal("[\n 1,\n '2',\n true\n]") | ||
}) | ||
it("empty array", function(){ | ||
expect(code([])).to.equal('[]'); | ||
expect(code({a:[]})).to.equal("{a:[]}"); | ||
}); | ||
}); | ||
expect(code([])).to.equal('[]') | ||
expect(code({a:[]})).to.equal("{a:[]}") | ||
}) | ||
}) | ||
it("functions", function(){ | ||
expect(code(function(a){return a;})).to.equal('function (a){return a;}'); | ||
}); | ||
expect(code(function(a){return a})).to.equal('function (a){return a}') | ||
}) | ||
it("regexp", function(){ | ||
expect(code(/abc/)).to.equal('/abc/'); | ||
}); | ||
expect(code(/abc/)).to.equal('/abc/') | ||
}) | ||
describe("objects", function(){ | ||
it("no indent", function(){ | ||
expect(code({a:1,b:true,cd:2,'c-d':3})).to.equal("{a:1,b:true,cd:2,'c-d':3}"); | ||
}); | ||
expect(code({a:1,b:true,cd:2,'c-d':3})).to.equal("{a:1,b:true,cd:2,'c-d':3}") | ||
}) | ||
@@ -120,15 +132,41 @@ it('number key object', function () { | ||
it("with indents", function(){ | ||
expect(code({a:1,b:true}, null, 4)).to.equal("{\n a: 1,\n b: true\n}"); | ||
}); | ||
}); | ||
}); | ||
expect(code({a:1,b:true}, null, 4)).to.equal("{\n a: 1,\n b: true\n}") | ||
}) | ||
}) | ||
}) | ||
describe("mixtures", function(){ | ||
it("array of objects", function(){ | ||
expect(code([1,{a:1}])).to.equal("[1,{a:1}]"); | ||
}); | ||
expect(code([1,{a:1}])).to.equal("[1,{a:1}]") | ||
}) | ||
it("object contains arrays", function(){ | ||
expect(code({a:[1,'a']})).to.equal("{a:[1,'a']}"); | ||
}); | ||
}); | ||
expect(code({a:[1,'a']})).to.equal("{a:[1,'a']}") | ||
}) | ||
}) | ||
describe('replacer', function () { | ||
it('removes object property if returns undefined, remove all', function () { | ||
expect(code({a: 1}, function () {})).to.equal('undefined') | ||
}) | ||
it('removes object property if returns undefined, remove a', function () { | ||
expect(code({a: 1}, function (k, v) { | ||
return k === 'a' | ||
? undefined | ||
: v | ||
})).to.equal('{}') | ||
}) | ||
it('replacer this', function () { | ||
var value = {a: 1} | ||
code(value, function (k, v) { | ||
if (!k) { | ||
expect(this).to.deep.equal({'': value}) | ||
} | ||
if (k === 'a') { | ||
expect(this).to.equal(value) | ||
} | ||
}) | ||
}) | ||
}) |
Sorry, the diff of this file is not supported yet
14265
344
123