-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add function to check if an object is serializable
- Loading branch information
Showing
5 changed files
with
164 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* @fileoverview Object serialization utils. | ||
* @author Bryan Mishkin | ||
*/ | ||
|
||
"use strict"; | ||
|
||
const isPlainObject = require("lodash.isplainobject"); | ||
|
||
/** | ||
* Check if a value is a primitive or plain object created by the Object constructor. | ||
* @param {any} val the value to check | ||
* @returns {boolean} true if so | ||
* @private | ||
*/ | ||
function isPrimitiveOrPlainObject(val) { | ||
return (val === null || typeof val === "string" || typeof val === "boolean" || typeof val === "number" || Array.isArray(val) || isPlainObject(val)); | ||
} | ||
|
||
/** | ||
* Check if an object is serializable. | ||
* Functions or objects like RegExp cannot be serialized by JSON.stringify(). | ||
* Inspired by: https://stackoverflow.com/questions/30579940/reliable-way-to-check-if-objects-is-serializable-in-javascript | ||
* @param {any} obj the object | ||
* @returns {boolean} true if the object is serializable | ||
*/ | ||
function isSerializable(obj) { | ||
if (!isPrimitiveOrPlainObject(obj)) { | ||
return false; | ||
} | ||
for (const property in obj) { | ||
if (Object.hasOwn(obj, property)) { | ||
if (!isPrimitiveOrPlainObject(obj[property])) { | ||
return false; | ||
} | ||
if (typeof obj[property] === "object") { | ||
if (!isSerializable(obj[property])) { | ||
return false; | ||
} | ||
} | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
module.exports = { | ||
isSerializable | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/** | ||
* @fileoverview Tests for object serialization utils. | ||
* @author Bryan Mishkin | ||
*/ | ||
|
||
"use strict"; | ||
|
||
const assert = require("chai").assert; | ||
const { isSerializable } = require("../../../lib/shared/serialization"); | ||
|
||
describe("serialization", () => { | ||
describe("isSerializable", () => { | ||
it("string", () => { | ||
assert.isTrue(isSerializable("")); | ||
assert.isTrue(isSerializable("abc")); | ||
}); | ||
|
||
it("boolean", () => { | ||
assert.isTrue(isSerializable(true)); | ||
assert.isTrue(isSerializable(false)); | ||
}); | ||
|
||
it("number", () => { | ||
assert.isTrue(isSerializable(123)); | ||
}); | ||
|
||
it("function", () => { | ||
assert.isFalse(isSerializable(() => {})); | ||
}); | ||
|
||
it("RegExp", () => { | ||
assert.isFalse(isSerializable(/abc/u)); | ||
}); | ||
|
||
it("null", () => { | ||
assert.isTrue(isSerializable(null)); | ||
}); | ||
|
||
it("undefined", () => { | ||
assert.isFalse(isSerializable(void 0)); | ||
}); | ||
|
||
describe("object", () => { | ||
it("plain objects", () => { | ||
assert.isTrue(isSerializable({})); | ||
assert.isTrue(isSerializable({ a: 123 })); | ||
assert.isTrue(isSerializable({ a: { b: 456 } })); | ||
}); | ||
|
||
it("object with function", () => { | ||
assert.isFalse(isSerializable({ a() {} })); | ||
assert.isFalse(isSerializable({ a: { b() {} } })); | ||
}); | ||
|
||
it("object with RegExp", () => { | ||
assert.isFalse(isSerializable({ a: /abc/u })); | ||
assert.isFalse(isSerializable({ a: { b: /abc/u } })); | ||
}); | ||
}); | ||
|
||
describe("array", () => { | ||
it("plain array", () => { | ||
assert.isTrue(isSerializable([])); | ||
assert.isTrue(isSerializable([1, 2, 3])); | ||
}); | ||
|
||
it("array with function", () => { | ||
assert.isFalse(isSerializable([function() {}])); | ||
}); | ||
|
||
it("array with RegExp", () => { | ||
assert.isFalse(isSerializable([/abc/u])); | ||
}); | ||
|
||
it("array with plain/nested objects", () => { | ||
assert.isTrue(isSerializable([{ a: 1 }, { b: 2 }, { c: { nested: true } }])); | ||
}); | ||
|
||
it("array with object with nested function", () => { | ||
assert.isFalse(isSerializable([{ a: { fn() {} } }])); | ||
}); | ||
}); | ||
}); | ||
}); |