From fc9e435e1025058eb85cf2f3a3cc451b8a651862 Mon Sep 17 00:00:00 2001 From: realdennis Date: Mon, 2 Sep 2019 22:29:33 +0800 Subject: [PATCH] Code formatted & Docs update --- README.md | 6 +++++- index.js | 10 +++++----- test/unit/serialize.js | 20 ++++++++++---------- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 3d6edd2..810fb57 100644 --- a/README.md +++ b/README.md @@ -101,13 +101,17 @@ serialize(obj, {isJSON: true}); This option is to signal `serialize()` that we want to do a straight conversion, without the XSS protection. This options needs to be explicitly set to `true`. HTML characters and JavaScript line terminators will not be escaped. You will have to roll your own. +```js +serialize(obj, {unsafe: true}); +``` + #### `options.ignoreFunction` This option is to signal `serialize()` that we do not want serialize JavaScript function. Just treat function like `JSON.stringify` do, but other features will work as expected. ```js -serialize(obj, {unsafe: true}); +serialize(obj, {ignoreFunction: true}); ``` ## Deserializing diff --git a/index.js b/index.js index cb4d771..67cd2d2 100644 --- a/index.js +++ b/index.js @@ -34,12 +34,12 @@ function escapeUnsafeChars(unsafeChar) { function deleteFunctions(obj){ var functionKeys = []; for (var key in obj) { - if (typeof obj[key] === "function") { - functionKeys.push(key); - } + if (typeof obj[key] === "function") { + functionKeys.push(key); + } } for (var i = 0; i < functionKeys.length; i++) { - delete obj[functionKeys[i]]; + delete obj[functionKeys[i]]; } } @@ -144,7 +144,7 @@ module.exports = function serialize(obj, options) { // Check if the parameter is function if (options.ignoreFunction && typeof obj === "function") { - obj = undefined; + obj = undefined; } // Protects against `JSON.stringify()` returning `undefined`, by serializing // to the literal string: "undefined". diff --git a/test/unit/serialize.js b/test/unit/serialize.js index 43bd116..a418f7b 100644 --- a/test/unit/serialize.js +++ b/test/unit/serialize.js @@ -427,24 +427,24 @@ describe('serialize( obj )', function () { it("should accept a `ignoreFunction` option", function() { function fn() { return true; } - const obj = { - fn, - fn_arrow: () => { - return true; - } + var obj = { + fn: fn, + fn_arrow: () => { + return true; + } }; - const obj2 = { + var obj2 = { num:123, str:'str', - fn + fn: fn } // case 1. Pass function to serialize - expect(serialize(fn, { ignoreFunction: true })).to.equal(`undefined`); + expect(serialize(fn, { ignoreFunction: true })).to.equal('undefined'); // case 2. Pass function(arrow) in object to serialze - expect(serialize(obj, { ignoreFunction: true })).to.equal("{}"); + expect(serialize(obj, { ignoreFunction: true })).to.equal('{}'); // case 3. Other features should work expect(serialize(obj2, { ignoreFunction: true })).to.equal( - `{"num":123,"str":"str"}` + '{"num":123,"str":"str"}' ); }); });