UNPKG

vuikit

Version:

A Vuejs component library based on UIkit

61 lines (54 loc) 1.25 kB
/** * Vuikit 0.7.0 * (c) 2018 Miljan Aleksic * @license MIT */ import { merge, isObject, isFunction } from 'vuikit/core/util' /* * Safely and quickly serialize JavaScript objects * https://github.com/davidmarkclements/fast-safe-stringify */ export default function (obj) { if (isObject(obj) && !isFunction(obj.toJSON)) { decirc(merge({}, obj), '', [], null) } return JSON.stringify(obj) } function Circle (val, k, parent) { this.val = val this.k = k this.parent = parent this.count = 1 } Circle.prototype.toJSON = function toJSON () { if (--this.count === 0) { this.parent[this.k] = this.val } return '[Circular]' } function decirc (val, k, stack, parent) { var keys, len, i if (typeof val !== 'object' || val === null) { // not an object, nothing to do return } else if (val instanceof Circle) { val.count++ return } else if (typeof val.toJSON === 'function' && !val.toJSON.forceDecirc) { return } else if (parent) { if (~stack.indexOf(val)) { parent[k] = new Circle(val, k, parent) return } } stack.push(val) keys = Object.keys(val) len = keys.length i = 0 for (; i < len; i++) { k = keys[i] decirc(val[k], k, stack, val) } stack.pop() }