From 9a35039e68e92d278e5f7cfe3ebe940aaeb7cbe2 Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Thu, 15 Jan 2026 15:07:37 +0300 Subject: [PATCH] docs... Signed-off-by: Alex A. Naanou --- README.md | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ serialize.js | 16 ++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/README.md b/README.md index 568e8e8..2aa5172 100644 --- a/README.md +++ b/README.md @@ -53,12 +53,64 @@ Thus, care must be taken when serializing structures containing function. ### `serialize(..)` / `eJSON.stringify(..)` +Serialize a JavaScript value into a JSON or JSON-like string. +``` +serialize() +eJSON.stringify() + -> +``` + +More control: +``` +serialize(obj, options){ +serialize(obj, indent, depth=0, options){ + -> +``` + +Supported options: +- `indent` controls formatting and nested value indent, if set to a number + that number of spaces will be used to indent nested values if given a + string that string is used for indenting, note that only whitespace is + supported currently. + Default: `undefined` (disabled) +- `depth` if given is a number of `indent`'s, used to set top level indent + depth of the returned string, this can be useful when pretty-printing + or nesting the output. + Default: `0` +- `min_length_ref` sets the minimal length of a string or big-int value + for referencing when encountered repeatedly. + If set to `0` or `Infinity` referencing of strings and big-ints will + be is disabled. + Default: 'MIN_LENGTH_REF' +- `functions` if passed an array, encounterd functions will be pushed to + it and stored in the output by index. + Default: `undefined` + + ### `deserialize(..)` / 'eJSON.parse(..)' +``` +deserialize() +eJSON.parse() + -> +``` + + ### `deepCopy(..)` +``` +deepCopy() + -> +``` + + ### `partialDeepCopy(..)` +``` +partialDeepCopy() + -> +``` + ### `MIN_LENGTH_REF` / `.min_length_ref` diff --git a/serialize.js b/serialize.js index 456357f..596c5f3 100644 --- a/serialize.js +++ b/serialize.js @@ -93,6 +93,15 @@ module.MIN_LENGTH_REF = REFERENCE.length * 16 // _serialize(obj, base_path, seen, indent, depth, options) // -> str // +// Options format: +// { +// // Indent to use +// indent: undefined, +// depth: 0, +// min_length_ref: MIN_LENGTH_REF, +// functions: undefined, +// } +// // // Paths // A path is a chain of indexes/keys leading to a specific object in @@ -144,6 +153,8 @@ module.MIN_LENGTH_REF = REFERENCE.length * 16 var _serialize = module._serialize = function(obj, path=[], seen=new Map(), indent, depth=0, options={}){ + if(typeof(indent) == 'number'){ + indent = ' '.repeat(indent) } var min_length_ref = options.min_length_ref ?? module.MIN_LENGTH_REF @@ -260,6 +271,11 @@ function(obj, path=[], seen=new Map(), indent, depth=0, options={}){ var serialize = module.serialize = function(obj, indent, depth=0, options){ + if(indent + && typeof(indent) == 'object'){ + options = indent + indent = options.indent + depth = options.depth ?? 0 } return _serialize(obj, [], new Map(), indent, depth, options) }