Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat. New option ignoreFunction according to issue#32 #58

Merged
merged 7 commits into from Sep 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.

realdennis marked this conversation as resolved.
Show resolved Hide resolved
```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