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 38d38a2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
6 changes: 5 additions & 1 deletion README.md
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
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
24 changes: 12 additions & 12 deletions test/unit/serialize.js
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 = {
num:123,
str:'str',
fn
var obj2 = {
num: 123,
str: 'str',
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 38d38a2

Please sign in to comment.