Skip to content

Commit 9b47a96

Browse files
realdennisokuryu
authored andcommitted
Feat. New option ignoreFunction according to issue#32 (#58)
1 parent c65dd4a commit 9b47a96

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,15 @@ This option is to signal `serialize()` that we want to do a straight conversion,
105105
serialize(obj, {unsafe: true});
106106
```
107107

108+
#### `options.ignoreFunction`
109+
110+
This option is to signal `serialize()` that we do not want serialize JavaScript function.
111+
Just treat function like `JSON.stringify` do, but other features will work as expected.
112+
113+
```js
114+
serialize(obj, {ignoreFunction: true});
115+
```
116+
108117
## Deserializing
109118

110119
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:

index.js

+21
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ function escapeUnsafeChars(unsafeChar) {
3131
return ESCAPED_CHARS[unsafeChar];
3232
}
3333

34+
function deleteFunctions(obj){
35+
var functionKeys = [];
36+
for (var key in obj) {
37+
if (typeof obj[key] === "function") {
38+
functionKeys.push(key);
39+
}
40+
}
41+
for (var i = 0; i < functionKeys.length; i++) {
42+
delete obj[functionKeys[i]];
43+
}
44+
}
45+
3446
module.exports = function serialize(obj, options) {
3547
options || (options = {});
3648

@@ -50,6 +62,11 @@ module.exports = function serialize(obj, options) {
5062
// which are later replaced by their string representation.
5163
function replacer(key, value) {
5264

65+
// For nested function
66+
if(options.ignoreFunction){
67+
deleteFunctions(value);
68+
}
69+
5370
if (!value && value !== undefined) {
5471
return value;
5572
}
@@ -125,6 +142,10 @@ module.exports = function serialize(obj, options) {
125142
return serializedFn;
126143
}
127144

145+
// Check if the parameter is function
146+
if (options.ignoreFunction && typeof obj === "function") {
147+
obj = undefined;
148+
}
128149
// Protects against `JSON.stringify()` returning `undefined`, by serializing
129150
// to the literal string: "undefined".
130151
if (obj === undefined) {

test/unit/serialize.js

+23
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,29 @@ describe('serialize( obj )', function () {
424424
expect(serialize(["<"], {space: 2})).to.equal('[\n "\\u003C"\n]');
425425
expect(serialize(["<"], {unsafe: true, space: 2})).to.equal('[\n "<"\n]');
426426
});
427+
428+
it("should accept a `ignoreFunction` option", function() {
429+
function fn() { return true; }
430+
var obj = {
431+
fn: fn,
432+
fn_arrow: () => {
433+
return true;
434+
}
435+
};
436+
var obj2 = {
437+
num: 123,
438+
str: 'str',
439+
fn: fn
440+
}
441+
// case 1. Pass function to serialize
442+
expect(serialize(fn, { ignoreFunction: true })).to.equal('undefined');
443+
// case 2. Pass function(arrow) in object to serialze
444+
expect(serialize(obj, { ignoreFunction: true })).to.equal('{}');
445+
// case 3. Other features should work
446+
expect(serialize(obj2, { ignoreFunction: true })).to.equal(
447+
'{"num":123,"str":"str"}'
448+
);
449+
});
427450
});
428451

429452
describe('backwards-compatability', function () {

0 commit comments

Comments
 (0)