-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
111 lines (102 loc) · 2.8 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/**
* stringifier
*
* https://github.com/twada/stringifier
*
* Copyright (c) 2014-2024 Takuto Wada
* Licensed under the MIT license.
* https://twada.mit-license.org/2014-2024
*/
'use strict';
const traverse = require('traverse');
const typeName = require('type-name');
const s = require('./strategies');
function defaultHandlers () {
return {
'null': s.always('null'),
'undefined': s.always('undefined'),
'function': s.prune(),
'string': s.json(),
'boolean': s.json(),
'number': s.number(),
'bigint': s.bigint(),
'symbol': s.toStr(),
'RegExp': s.toStr(),
'String': s.newLike(),
'Boolean': s.newLike(),
'Number': s.newLike(),
'Date': s.newLike(),
'Array': s.array(),
'Object': s.object(),
'Error': s.object(null, ['message', 'code']),
'@default': s.object()
};
}
function defaultOptions () {
return {
maxDepth: null,
indent: null,
anonymous: '@Anonymous',
circular: '#@Circular#',
snip: '..(snip)',
lineSeparator: '\n',
typeFun: typeName
};
}
function createStringifier (customOptions) {
const options = Object.assign({}, defaultOptions(), customOptions);
const handlers = Object.assign({}, defaultHandlers(), options.handlers);
return function stringifyAny (push, x) {
const context = this;
let handler = handlerFor(context.node, options, handlers);
const currentPath = '/' + context.path.map(String).join('/');
const customization = handlers[currentPath];
const acc = {
context: context,
options: options,
handlers: handlers,
push: push
};
if (typeof customization === 'function') {
handler = customization;
} else if (typeof customization === 'number') {
handler = s.flow.compose(s.filters.truncate(customization), handler);
} else if (context.parent && Array.isArray(context.parent.node) && !(context.key in context.parent.node)) {
// sparse arrays
handler = s.always('');
}
handler(acc, x);
return push;
};
}
function handlerFor (val, options, handlers) {
const tname = options.typeFun(val);
if (typeof handlers[tname] === 'function') {
return handlers[tname];
}
if (tname.endsWith('Error')) {
return handlers['Error'];
}
return handlers['@default'];
}
function walk (val, reducer) {
const buffer = [];
const push = function (str) {
buffer.push(str);
};
traverse(val).reduce(reducer, push);
return buffer.join('');
}
function stringify (val, options) {
return walk(val, createStringifier(options));
}
function stringifier (options) {
return function (val) {
return walk(val, createStringifier(options));
};
}
stringifier.stringify = stringify;
stringifier.strategies = s;
stringifier.defaultOptions = defaultOptions;
stringifier.defaultHandlers = defaultHandlers;
module.exports = stringifier;