mirror of
https://github.com/flynx/serialize.js.git
synced 2026-07-10 11:10:57 +00:00
refactoring...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
6c7f043d18
commit
9c36299e25
@ -22,31 +22,41 @@ var RECURSIVE = '<RECURSIVE%>'
|
|||||||
//---------------------------------------------------------------------
|
//---------------------------------------------------------------------
|
||||||
|
|
||||||
//
|
//
|
||||||
// serialize(obj[, space[, indent]])
|
// serialize(obj[, indent[, depth]])
|
||||||
// -> str
|
// -> str
|
||||||
//
|
//
|
||||||
// serialize(obj, path, seen, space, indent)
|
// indent can be:
|
||||||
|
// number - number of spaces to use for indent
|
||||||
|
// string - string to use for indenting
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// serialize(obj, path, seen, indent, depth)
|
||||||
// -> str
|
// -> str
|
||||||
//
|
//
|
||||||
// XXX add function support...
|
// XXX add function support...
|
||||||
// XXX need to destinguish between map key and value in path...
|
// XXX need to destinguish between map key and value in path...
|
||||||
var serialize =
|
var serialize =
|
||||||
module.serialize =
|
module.serialize =
|
||||||
function(obj, path=[], seen=new Map(), space, indent=0){
|
function(obj, path=[], seen=new Map(), indent, depth=0){
|
||||||
// args...
|
// args...
|
||||||
var args = [...arguments].slice(1)
|
var args = [...arguments].slice(1)
|
||||||
if(typeof(args[0]) == 'number'){
|
if(typeof(args[0]) == 'number'
|
||||||
space = args.shift() }
|
|| typeof(args[0]) == 'string'){
|
||||||
if(typeof(args[0]) == 'number'){
|
|
||||||
indent = args.shift() }
|
indent = args.shift() }
|
||||||
|
indent = typeof(indent) == 'number' ?
|
||||||
|
' '.repeat(indent)
|
||||||
|
: indent
|
||||||
|
if(typeof(args[0]) == 'number'){
|
||||||
|
depth = args.shift() }
|
||||||
;[path, seen] = args
|
;[path, seen] = args
|
||||||
path ??= []
|
path ??= []
|
||||||
seen ??= new Map()
|
seen ??= new Map()
|
||||||
|
|
||||||
|
|
||||||
// recursive...
|
// recursive...
|
||||||
var p = seen.get(obj)
|
var p = seen.get(obj)
|
||||||
if(p != null){
|
if(p != null){
|
||||||
// NOTE: serialize(..) is always printed flat here, regardless of space/indent...
|
// NOTE: serialize(..) is always printed flat here, regardless of indent/depth...
|
||||||
return RECURSIVE.replace('%', serialize(p)) }
|
return RECURSIVE.replace('%', serialize(p)) }
|
||||||
|
|
||||||
// XXX functions...
|
// XXX functions...
|
||||||
@ -65,7 +75,7 @@ function(obj, path=[], seen=new Map(), space, indent=0){
|
|||||||
INFINITY
|
INFINITY
|
||||||
: obj === -Infinity ?
|
: obj === -Infinity ?
|
||||||
NEG_INFINITY
|
NEG_INFINITY
|
||||||
: JSON.stringify(obj, null, space) }
|
: JSON.stringify(obj, null, indent) }
|
||||||
|
|
||||||
// objects...
|
// objects...
|
||||||
seen.set(obj, path)
|
seen.set(obj, path)
|
||||||
@ -80,14 +90,14 @@ function(obj, path=[], seen=new Map(), space, indent=0){
|
|||||||
for(var i=0; i < obj.length; i++){
|
for(var i=0; i < obj.length; i++){
|
||||||
elems.push(
|
elems.push(
|
||||||
i in obj ?
|
i in obj ?
|
||||||
serialize(obj[i], [...path, i], seen, space, indent+1)
|
serialize(obj[i], [...path, i], seen, indent, depth+1)
|
||||||
: EMPTY) }
|
: EMPTY) }
|
||||||
|
|
||||||
} else if(obj instanceof Map){
|
} else if(obj instanceof Map){
|
||||||
pre = 'Map(['
|
pre = 'Map(['
|
||||||
post = '])'
|
post = '])'
|
||||||
elems = [
|
elems = [
|
||||||
serialize([...obj], path, seen, space, indent)
|
serialize([...obj], path, seen, indent, depth)
|
||||||
.slice(1, -1)
|
.slice(1, -1)
|
||||||
.trim() ]
|
.trim() ]
|
||||||
|
|
||||||
@ -95,7 +105,7 @@ function(obj, path=[], seen=new Map(), space, indent=0){
|
|||||||
pre = 'Set(['
|
pre = 'Set(['
|
||||||
post = '])'
|
post = '])'
|
||||||
elems = [
|
elems = [
|
||||||
serialize([...obj], path, seen, space, indent)
|
serialize([...obj], path, seen, indent, depth)
|
||||||
.slice(1, -1)
|
.slice(1, -1)
|
||||||
.trim() ]
|
.trim() ]
|
||||||
|
|
||||||
@ -105,16 +115,16 @@ function(obj, path=[], seen=new Map(), space, indent=0){
|
|||||||
for(var [k, v] of Object.entries(obj)){
|
for(var [k, v] of Object.entries(obj)){
|
||||||
elems.push(`${
|
elems.push(`${
|
||||||
JSON.stringify(k)
|
JSON.stringify(k)
|
||||||
}:${ space != null ? ' ' : '' }${
|
}:${ indent != null ? ' ' : '' }${
|
||||||
serialize(v, [...path, k], seen, space, indent+1)
|
serialize(v, [...path, k], seen, indent, depth+1)
|
||||||
// relevant for pretty-printing only...
|
// relevant for pretty-printing only...
|
||||||
.trimLeft()
|
.trimLeft()
|
||||||
}`) } }
|
}`) } }
|
||||||
|
|
||||||
// handle indent...
|
// handle indent...
|
||||||
if(space != null){
|
if(indent != null){
|
||||||
i = ' '.repeat(indent * space)
|
i = indent.repeat(depth)
|
||||||
s = i + ' '.repeat(space)
|
s = i + indent
|
||||||
if(elems.length > 0){
|
if(elems.length > 0){
|
||||||
pre = pre + '\n' + s
|
pre = pre + '\n' + s
|
||||||
post = '\n' + i + post
|
post = '\n' + i + post
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user