source: trip-planner-front/node_modules/@discoveryjs/json-ext/src/utils.js@ 1ad8e64

Last change on this file since 1ad8e64 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.3 KB
RevLine 
[6a3a178]1const PrimitiveType = 1;
2const ObjectType = 2;
3const ArrayType = 3;
4const PromiseType = 4;
5const ReadableStringType = 5;
6const ReadableObjectType = 6;
7// https://tc39.es/ecma262/#table-json-single-character-escapes
8const escapableCharCodeSubstitution = { // JSON Single Character Escape Sequences
9 0x08: '\\b',
10 0x09: '\\t',
11 0x0a: '\\n',
12 0x0c: '\\f',
13 0x0d: '\\r',
14 0x22: '\\\"',
15 0x5c: '\\\\'
16};
17
18function isLeadingSurrogate(code) {
19 return code >= 0xD800 && code <= 0xDBFF;
20}
21
22function isTrailingSurrogate(code) {
23 return code >= 0xDC00 && code <= 0xDFFF;
24}
25
26function isReadableStream(value) {
27 return (
28 typeof value.pipe === 'function' &&
29 typeof value._read === 'function' &&
30 typeof value._readableState === 'object' && value._readableState !== null
31 );
32}
33
34function replaceValue(holder, key, value, replacer) {
35 if (value && typeof value.toJSON === 'function') {
36 value = value.toJSON();
37 }
38
39 if (replacer !== null) {
40 value = replacer.call(holder, String(key), value);
41 }
42
43 switch (typeof value) {
44 case 'function':
45 case 'symbol':
46 value = undefined;
47 break;
48
49 case 'object':
50 if (value !== null) {
51 const cls = value.constructor;
52 if (cls === String || cls === Number || cls === Boolean) {
53 value = value.valueOf();
54 }
55 }
56 break;
57 }
58
59 return value;
60}
61
62function getTypeNative(value) {
63 if (value === null || typeof value !== 'object') {
64 return PrimitiveType;
65 }
66
67 if (Array.isArray(value)) {
68 return ArrayType;
69 }
70
71 return ObjectType;
72}
73
74function getTypeAsync(value) {
75 if (value === null || typeof value !== 'object') {
76 return PrimitiveType;
77 }
78
79 if (typeof value.then === 'function') {
80 return PromiseType;
81 }
82
83 if (isReadableStream(value)) {
84 return value._readableState.objectMode ? ReadableObjectType : ReadableStringType;
85 }
86
87 if (Array.isArray(value)) {
88 return ArrayType;
89 }
90
91 return ObjectType;
92}
93
94function normalizeReplacer(replacer) {
95 if (typeof replacer === 'function') {
96 return replacer;
97 }
98
99 if (Array.isArray(replacer)) {
100 const allowlist = new Set(replacer
101 .map(item => {
102 const cls = item && item.constructor;
103 return cls === String || cls === Number ? String(item) : null;
104 })
105 .filter(item => typeof item === 'string')
106 );
107
108 return [...allowlist];
109 }
110
111 return null;
112}
113
114function normalizeSpace(space) {
115 if (typeof space === 'number') {
116 if (!Number.isFinite(space) || space < 1) {
117 return false;
118 }
119
120 return ' '.repeat(Math.min(space, 10));
121 }
122
123 if (typeof space === 'string') {
124 return space.slice(0, 10) || false;
125 }
126
127 return false;
128}
129
130module.exports = {
131 escapableCharCodeSubstitution,
132 isLeadingSurrogate,
133 isTrailingSurrogate,
134 type: {
135 PRIMITIVE: PrimitiveType,
136 PROMISE: PromiseType,
137 ARRAY: ArrayType,
138 OBJECT: ObjectType,
139 STRING_STREAM: ReadableStringType,
140 OBJECT_STREAM: ReadableObjectType
141 },
142
143 isReadableStream,
144 replaceValue,
145 getTypeNative,
146 getTypeAsync,
147 normalizeReplacer,
148 normalizeSpace
149};
Note: See TracBrowser for help on using the repository browser.