From 9b47a961c257f25f4eb1488ba375041a2a2aee52 Mon Sep 17 00:00:00 2001 From: Dennis Date: Wed, 4 Sep 2019 20:32:22 +0800 Subject: [PATCH] Feat. New option `ignoreFunction` according to issue#32 (#58) --- README.md | 9 +++++++++ index.js | 21 +++++++++++++++++++++ test/unit/serialize.js | 23 +++++++++++++++++++++++ 3 files changed, 53 insertions(+) diff --git a/README.md b/README.md index c0edf01..810fb57 100644 --- a/README.md +++ b/README.md @@ -105,6 +105,15 @@ This option is to signal `serialize()` that we want to do a straight conversion, 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, {ignoreFunction: true}); +``` + ## Deserializing For some use cases you might also need to deserialize the string. This is explicitly not part of this module. However, you can easily write it yourself: diff --git a/index.js b/index.js index 41a8999..67cd2d2 100644 --- a/index.js +++ b/index.js @@ -31,6 +31,18 @@ function escapeUnsafeChars(unsafeChar) { return ESCAPED_CHARS[unsafeChar]; } +function deleteFunctions(obj){ + var functionKeys = []; + for (var key in obj) { + if (typeof obj[key] === "function") { + functionKeys.push(key); + } + } + for (var i = 0; i < functionKeys.length; i++) { + delete obj[functionKeys[i]]; + } +} + module.exports = function serialize(obj, options) { options || (options = {}); @@ -50,6 +62,11 @@ module.exports = function serialize(obj, options) { // which are later replaced by their string representation. function replacer(key, value) { + // For nested function + if(options.ignoreFunction){ + deleteFunctions(value); + } + if (!value && value !== undefined) { return value; } @@ -125,6 +142,10 @@ module.exports = function serialize(obj, options) { return serializedFn; } + // Check if the parameter is function + if (options.ignoreFunction && typeof obj === "function") { + obj = undefined; + } // Protects against `JSON.stringify()` returning `undefined`, by serializing // to the literal string: "undefined". if (obj === undefined) { diff --git a/test/unit/serialize.js b/test/unit/serialize.js index e66977c..6b8c19c 100644 --- a/test/unit/serialize.js +++ b/test/unit/serialize.js @@ -424,6 +424,29 @@ describe('serialize( obj )', function () { expect(serialize(["<"], {space: 2})).to.equal('[\n "\\u003C"\n]'); expect(serialize(["<"], {unsafe: true, space: 2})).to.equal('[\n "<"\n]'); }); + + it("should accept a `ignoreFunction` option", function() { + function fn() { return true; } + var obj = { + fn: fn, + fn_arrow: () => { + return true; + } + }; + var obj2 = { + num: 123, + str: 'str', + fn: fn + } + // case 1. Pass function to serialize + expect(serialize(fn, { ignoreFunction: true })).to.equal('undefined'); + // case 2. Pass function(arrow) in object to serialze + expect(serialize(obj, { ignoreFunction: true })).to.equal('{}'); + // case 3. Other features should work + expect(serialize(obj2, { ignoreFunction: true })).to.equal( + '{"num":123,"str":"str"}' + ); + }); }); describe('backwards-compatability', function () {