source: imaps-frontend/node_modules/@discoveryjs/json-ext/cjs/utils.cjs@ 79a0317

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 2.6 KB
Line 
1'use strict';
2
3function isIterable(value) {
4 return (
5 typeof value === 'object' &&
6 value !== null &&
7 (
8 typeof value[Symbol.iterator] === 'function' ||
9 typeof value[Symbol.asyncIterator] === 'function'
10 )
11 );
12}
13
14function replaceValue(holder, key, value, replacer) {
15 if (value && typeof value.toJSON === 'function') {
16 value = value.toJSON();
17 }
18
19 if (replacer !== null) {
20 value = replacer.call(holder, String(key), value);
21 }
22
23 switch (typeof value) {
24 case 'function':
25 case 'symbol':
26 value = undefined;
27 break;
28
29 case 'object':
30 if (value !== null) {
31 const cls = value.constructor;
32 if (cls === String || cls === Number || cls === Boolean) {
33 value = value.valueOf();
34 }
35 }
36 break;
37 }
38
39 return value;
40}
41
42function normalizeReplacer(replacer) {
43 if (typeof replacer === 'function') {
44 return replacer;
45 }
46
47 if (Array.isArray(replacer)) {
48 const allowlist = new Set(replacer
49 .map(item => {
50 const cls = item && item.constructor;
51 return cls === String || cls === Number ? String(item) : null;
52 })
53 .filter(item => typeof item === 'string')
54 );
55
56 return [...allowlist];
57 }
58
59 return null;
60}
61
62function normalizeSpace(space) {
63 if (typeof space === 'number') {
64 if (!Number.isFinite(space) || space < 1) {
65 return false;
66 }
67
68 return ' '.repeat(Math.min(space, 10));
69 }
70
71 if (typeof space === 'string') {
72 return space.slice(0, 10) || false;
73 }
74
75 return false;
76}
77
78function normalizeStringifyOptions(optionsOrReplacer, space) {
79 if (optionsOrReplacer === null || Array.isArray(optionsOrReplacer) || typeof optionsOrReplacer !== 'object') {
80 optionsOrReplacer = {
81 replacer: optionsOrReplacer,
82 space
83 };
84 }
85
86 let replacer = normalizeReplacer(optionsOrReplacer.replacer);
87 let getKeys = Object.keys;
88
89 if (Array.isArray(replacer)) {
90 const allowlist = replacer;
91
92 getKeys = () => allowlist;
93 replacer = null;
94 }
95
96 return {
97 ...optionsOrReplacer,
98 replacer,
99 getKeys,
100 space: normalizeSpace(optionsOrReplacer.space)
101 };
102}
103
104exports.isIterable = isIterable;
105exports.normalizeReplacer = normalizeReplacer;
106exports.normalizeSpace = normalizeSpace;
107exports.normalizeStringifyOptions = normalizeStringifyOptions;
108exports.replaceValue = replaceValue;
Note: See TracBrowser for help on using the repository browser.