Skip to content

Commit

Permalink
Feat. New option ignoreFunction according to issue#32 (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
realdennis authored and okuryu committed Sep 4, 2019
1 parent c65dd4a commit 9b47a96
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Expand Up @@ -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:
Expand Down
21 changes: 21 additions & 0 deletions index.js
Expand Up @@ -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 = {});

Expand All @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand Down
23 changes: 23 additions & 0 deletions test/unit/serialize.js
Expand Up @@ -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 () {
Expand Down

0 comments on commit 9b47a96

Please sign in to comment.