Last change
on this file since bdd6491 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
907 bytes
|
Rev | Line | |
---|
[6a3a178] | 1 | exports = module.exports = stringify
|
---|
| 2 | exports.getSerialize = serializer
|
---|
| 3 |
|
---|
| 4 | function stringify(obj, replacer, spaces, cycleReplacer) {
|
---|
| 5 | return JSON.stringify(obj, serializer(replacer, cycleReplacer), spaces)
|
---|
| 6 | }
|
---|
| 7 |
|
---|
| 8 | function serializer(replacer, cycleReplacer) {
|
---|
| 9 | var stack = [], keys = []
|
---|
| 10 |
|
---|
| 11 | if (cycleReplacer == null) cycleReplacer = function(key, value) {
|
---|
| 12 | if (stack[0] === value) return "[Circular ~]"
|
---|
| 13 | return "[Circular ~." + keys.slice(0, stack.indexOf(value)).join(".") + "]"
|
---|
| 14 | }
|
---|
| 15 |
|
---|
| 16 | return function(key, value) {
|
---|
| 17 | if (stack.length > 0) {
|
---|
| 18 | var thisPos = stack.indexOf(this)
|
---|
| 19 | ~thisPos ? stack.splice(thisPos + 1) : stack.push(this)
|
---|
| 20 | ~thisPos ? keys.splice(thisPos, Infinity, key) : keys.push(key)
|
---|
| 21 | if (~stack.indexOf(value)) value = cycleReplacer.call(this, key, value)
|
---|
| 22 | }
|
---|
| 23 | else stack.push(value)
|
---|
| 24 |
|
---|
| 25 | return replacer == null ? value : replacer.call(this, key, value)
|
---|
| 26 | }
|
---|
| 27 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.