Skip to content

Commit

Permalink
Code formatted & Docs update
Browse files Browse the repository at this point in the history
  • Loading branch information
realdennis committed Sep 2, 2019
1 parent 55df724 commit fc9e435
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]];
}
}

Expand Down Expand Up @@ -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".
Expand Down
20 changes: 10 additions & 10 deletions test/unit/serialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"}'
);
});
});
Expand Down

0 comments on commit fc9e435

Please sign in to comment.