serialize-to-js
Advanced tools
Comparing version 0.1.0 to 0.1.1
22
index.js
@@ -14,12 +14,2 @@ /* | ||
/** | ||
* wrap an object key | ||
* @api private | ||
* @param {String} key - objects key | ||
* @return {String} wrapped key in quotes if necessary | ||
*/ | ||
function _wrapkey(key) { | ||
return (/^[a-zA-Z$_][a-zA-Z$_0-9]+$/.test(key) ? key : "'"+key+"'"); | ||
} | ||
/** | ||
* serializes an object to javascript | ||
@@ -92,6 +82,6 @@ * | ||
else if (util.isDate(source)){ | ||
out += "new Date('" + source.toJSON() + "')"; | ||
out += 'new Date("' + source.toJSON() + '")'; | ||
} | ||
else if (util.isError(source)){ | ||
out += "new Error(" + (source.message ? "'"+source.message+"'" : '') + ")"; | ||
out += "new Error(" + (source.message ? '"'+source.message+'"' : '') + ")"; | ||
} | ||
@@ -111,3 +101,3 @@ else if (util.isBuffer(source)){ | ||
if (!opts._refs.hasReference(source[key])) { | ||
tmp.push(_wrapkey(key) + ': ' + serialize(source[key], opts)); | ||
tmp.push(Ref.wrapkey(key) + ': ' + serialize(source[key], opts)); | ||
} | ||
@@ -117,3 +107,3 @@ opts._refs.pop(); | ||
else { | ||
tmp.push(_wrapkey(key) + ': ' + serialize(source[key], opts)); | ||
tmp.push(Ref.wrapkey(key) + ': ' + serialize(source[key], opts)); | ||
} | ||
@@ -133,3 +123,3 @@ } | ||
else if (util.isString(source)) { | ||
out += "'" + source.replace(/'/g, "\\'") + "'"; | ||
out += '"' + source.replace(/"/g, '\\"') + '"'; | ||
} | ||
@@ -176,3 +166,3 @@ else { | ||
opts.references.forEach(function(i){ | ||
out += 'm.' + i[0] + ' = m.' + i[1] + ';\n'; | ||
out += 'm' + i[0] + ' = m' + i[1] + ';\n'; | ||
}); | ||
@@ -179,0 +169,0 @@ } |
@@ -8,2 +8,4 @@ /* | ||
var KEY = /^[a-zA-Z$_][a-zA-Z$_0-9]*$/; | ||
/** | ||
@@ -21,2 +23,12 @@ * handle references | ||
/** | ||
* wrap an object key | ||
* @api private | ||
* @param {String} key - objects key | ||
* @return {String} wrapped key in quotes if necessary | ||
*/ | ||
Ref.wrapkey = function(key) { | ||
return (KEY.test(key) ? key : '"'+key+'"'); | ||
}; | ||
Ref.prototype = { | ||
@@ -39,4 +51,18 @@ /** | ||
*/ | ||
joinKey: function() { | ||
return this.key.join('.'); | ||
join: function(key) { | ||
var out = ''; | ||
key = key || this.key; | ||
if (typeof key === 'string') { | ||
console.log('#',key) | ||
key = [ key ]; | ||
} | ||
key.forEach(function(k){ | ||
if (KEY.test(k)) { | ||
out += '.' + k; | ||
} else { | ||
out += '['+ Ref.wrapkey(k) +']'; | ||
} | ||
}); | ||
return out; | ||
}, | ||
@@ -52,3 +78,3 @@ /** | ||
if (~(idx = this.refs.indexOf(source))) { | ||
this.references.push([ this.joinKey(), this.keys[idx] ]); | ||
this.references.push([ this.join(), this.keys[idx] ]); | ||
return true; | ||
@@ -58,3 +84,3 @@ } | ||
this.refs.push(source); | ||
this.keys.push(this.joinKey()); | ||
this.keys.push(this.join()); | ||
} | ||
@@ -61,0 +87,0 @@ }, |
{ | ||
"name": "serialize-to-js", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "serialize objects to javascript", | ||
@@ -39,3 +39,6 @@ "main": "index.js", | ||
}, | ||
"homepage": "https://github.com/commenthol/serialize-to-js" | ||
} | ||
"homepage": "https://github.com/commenthol/serialize-to-js", | ||
"devDependencies": { | ||
"mocha": "~2.2.4" | ||
} | ||
} |
@@ -120,5 +120,1 @@ # serialize-to-js | ||
@@ -9,5 +9,9 @@ 'use strict'; | ||
function log (arg) { | ||
console.log(JSON.stringify(arg).replace(/"/g, '')); | ||
console.log(JSON.stringify(arg)); | ||
} | ||
function getObj(str) { | ||
return new Function('var module = {};\n' + str + '\n return m;')() | ||
} | ||
describe('#serialize simple', function(){ | ||
@@ -20,4 +24,4 @@ it('function only', function(){ | ||
it('string only', function(){ | ||
var res = M.serialize("string's\nnew\t line"); | ||
var exp = '\'string\\\'s\nnew\t line\''; | ||
var res = M.serialize("string's\n\"new\"\t line"); | ||
var exp = '"string\'s\n\\"new\\"\t line"'; | ||
assert.equal(res, exp); | ||
@@ -48,3 +52,3 @@ }); | ||
var res = M.serialize(d); | ||
var exp = "new Date('1970-01-13T00:00:00.000Z')"; | ||
var exp = 'new Date("1970-01-13T00:00:00.000Z")'; | ||
assert.equal(res, exp); | ||
@@ -55,3 +59,3 @@ }); | ||
var res = M.serialize(e); | ||
var exp = "new Error('error')"; | ||
var exp = 'new Error("error")'; | ||
assert.equal(res, exp); | ||
@@ -85,3 +89,3 @@ }); | ||
var res = M.serialize(a); | ||
var exp = "[true, false, undefined, 1, 3.1415, -17, 'string']"; | ||
var exp = '[true, false, undefined, 1, 3.1415, -17, "string"]'; | ||
assert.equal(res, exp); | ||
@@ -97,6 +101,6 @@ }); | ||
six: -17, | ||
seven: 'string' | ||
"se ven": 'string' | ||
}; | ||
var res = M.serialize(o); | ||
var exp = "{'5': 3.1415, one: true, two: false, 'thr-ee': undefined, four: 1, six: -17, seven: 'string'}"; | ||
var exp = '{"5": 3.1415, one: true, two: false, "thr-ee": undefined, four: 1, six: -17, "se ven": "string"}'; | ||
assert.equal(res, exp); | ||
@@ -116,6 +120,9 @@ }); | ||
"thr-ee": undefined, | ||
"3": "3", | ||
"4 four": "four" | ||
}; | ||
var o = { a: o1, b: o1 }; | ||
var res = M.serialize(o); | ||
var exp = "{'a': {one: true, 'thr-ee': undefined}, 'b': {one: true, 'thr-ee': undefined}}"; | ||
var exp = '{a: {"3": "3", one: true, "thr-ee": undefined, "4 four": "four"}, b: {"3": "3", one: true, "thr-ee": undefined, "4 four": "four"}}'; | ||
//~ console.log(res); | ||
assert.equal(res, exp); | ||
@@ -128,8 +135,17 @@ }); | ||
"thr-ee": undefined, | ||
"3": "3", | ||
"4 four": { "four": 4 } | ||
}; | ||
var o = { a: r, b: r, c: { d: r }}; | ||
var o = { a: r, b: r, c: { d: r, "0": r, "spa ce": r }, "0": r["4 four"], "spa ce": r}; | ||
var opts = { reference: true }; | ||
var res = M.serialize(o, opts); | ||
var exp = "{'a': {one: true, 'thr-ee': undefined}, 'c': {}}"; | ||
var refs = [ [ 'b', 'a' ], [ 'c.d', 'a' ] ]; | ||
var exp = '{"0": {four: 4}, a: {"3": "3", one: true, "thr-ee": undefined}, c: {}}'; | ||
var refs = [ | ||
[ '.a["4 four"]', '["0"]' ], | ||
[ '.b', '.a' ], | ||
[ '.c["0"]', '.a' ], | ||
[ '.c.d', '.a' ], | ||
[ '.c["spa ce"]', '.a' ], | ||
[ '["spa ce"]', '.a' ] | ||
]; | ||
//~ console.log(res); console.log(opts.references); | ||
@@ -155,5 +171,6 @@ assert.equal(res, exp); | ||
var res = M.serializeToModule(o); | ||
var exp = "var m = module.exports = {\n\t'a': {\n\t\tone: true,\n\t\t'thr-ee': undefined\n\t},\n\t'b': {\n\t\tone: true,\n\t\t'thr-ee': undefined\n\t},\n\t'c': {\n\t\t'd': {\n\t\t\tone: true,\n\t\t\t'thr-ee': undefined\n\t\t}\n\t}\n};"; | ||
var exp = "var m = module.exports = {\n\ta: {\n\t\tone: true,\n\t\t\"thr-ee\": undefined\n\t},\n\tb: {\n\t\tone: true,\n\t\t\"thr-ee\": undefined\n\t},\n\tc: {\n\t\td: {\n\t\t\tone: true,\n\t\t\t\"thr-ee\": undefined\n\t\t}\n\t}\n};"; | ||
//~ log(res); | ||
assert.equal(res, exp); | ||
assert.deepEqual(o, getObj(res)); | ||
}); | ||
@@ -165,7 +182,8 @@ it('object of objects using references', function(){ | ||
}; | ||
var o = { a: r, b: r, c: { d: r }}; | ||
var o = { a: r, b: r, c: { d: r, "0": r, "spa ce": r }, "0": r, "spa ce": r}; | ||
var res = M.serializeToModule(o, { reference: true, beautify: false }); | ||
var exp = "var m = module.exports = {'a': {one: true, 'thr-ee': undefined}, 'c': {}};\nm.b = m.a;\nm.c.d = m.a;\n"; | ||
var exp = "var m = module.exports = {\"0\": {one: true, \"thr-ee\": undefined}, c: {}};\nm.a = m[\"0\"];\nm.b = m[\"0\"];\nm.c[\"0\"] = m[\"0\"];\nm.c.d = m[\"0\"];\nm.c[\"spa ce\"] = m[\"0\"];\nm[\"spa ce\"] = m[\"0\"];\n"; | ||
//~ log(res); | ||
assert.equal(res, exp); | ||
assert.deepEqual(o, getObj(res)); | ||
}); | ||
@@ -179,6 +197,7 @@ it('object of objects - beautify', function(){ | ||
var res = M.serializeToModule(o, { reference: true }); | ||
var exp = "var m = module.exports = {\n\t'a': {\n\t\tone: true,\n\t\t'thr-ee': /^test$/\n\t},\n\t'c': {}\n};\nm.b = m.a;\nm.c.d = m.a;"; | ||
//~ console.log(res); | ||
var exp = "var m = module.exports = {\n\ta: {\n\t\tone: true,\n\t\t\"thr-ee\": /^test$/\n\t},\n\tc: {}\n};\nm.b = m.a;\nm.c.d = m.a;"; | ||
//~ log(res); | ||
assert.equal(res, exp); | ||
assert.deepEqual(o, getObj(res)); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17729
433
1
120
1