UNPKG

foop

Version:

interfaces that describe their intentions.

74 lines (63 loc) 1.84 kB
const isArray = require('../is/array') const isFunction = require('../is/function') const isObj = require('../is/obj') const traverse = require('../traverse') // @TODO HYDRATE PERSIST MINIFY ... SERIALIZE CLASSES... METADATA... // @TODO: https://github.com/blakeembrey/javascript-stringify/blob/master/javascript-stringify.js function stringify(arg) { const trav = traverse(arg) let s = '' trav.before(function() { if (isArray(trav.iteratee)) s += '[' else if (isObj(trav.iteratee)) s += '{' }) trav.pre(function() { const key = trav.key || trav.path.join('') if (key && isObj(trav.iteratee) && !isArray(trav.iteratee)) { s += '"' + key + '"' + ':' } }) trav.after(function() { if (s.endsWith(',')) s = s.slice(0, -1) if (isArray(trav.iteratee)) s += ']' else if (isObj(trav.iteratee)) s += '}' }) trav.post(child => (s += ',')) /* prettier-ignore */ trav.forEach(function(key, node) { if (isFunction(node)) { s += 'null' } // aka isPrimitive else if (!isArray(node) && !isObj(node)) { s += node.toString() } }) return s } // function std(obj) { // if (obj && obj.toJSON) obj = obj.toJSON() // // switch (typeOf(obj)) { // case 'string': // return '"' + obj.replace(/[\x00-\x1f\\"]/g, escape) + '"' // case 'array': // return '[' + obj.map(JSON.encode).clean() + ']' // case 'object': // case 'hash': // var string = [] // Object.each(obj, function(value, key) { // var json = JSON.encode(value) // if (json) string.push(JSON.encode(key) + ':' + json) // }) // return '{' + string + '}' // case 'number': // case 'boolean': // return '' + obj // case 'null': // return 'null' // } // // return null // } module.exports = stringify