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 5 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
5 changes: 5 additions & 0 deletions README.md
Expand Up @@ -101,6 +101,11 @@ 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.

#### `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, {unsafe: true});
```
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){
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I wrote this first

    Object.keys(obj)
    .filter(key => typeof obj[key] === "function")
    .forEach(functionKey => {
      delete obj[functionKey];
    });

But I have no idea what the lowest browser the code should support to,
so I avoid to use Object.keys

if(typeof obj[key] === 'function'){
functionKeys.push(key);
}
}
for(var i=0; i<functionKeys.length; i++){
realdennis marked this conversation as resolved.
Show resolved Hide resolved
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'){
realdennis marked this conversation as resolved.
Show resolved Hide resolved
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; }
const obj = {
realdennis marked this conversation as resolved.
Show resolved Hide resolved
fn,
fn_arrow: () => {
return true;
}
};
const obj2 = {
realdennis marked this conversation as resolved.
Show resolved Hide resolved
num:123,
str:'str',
fn
realdennis marked this conversation as resolved.
Show resolved Hide resolved
}
// case 1. Pass function to serialize
expect(serialize(fn, { ignoreFunction: true })).to.equal(`undefined`);
realdennis marked this conversation as resolved.
Show resolved Hide resolved
// 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"}`
realdennis marked this conversation as resolved.
Show resolved Hide resolved
);
});
});

describe('backwards-compatability', function () {
Expand Down