@atlaspack/utils 2.14.5-canary.34 → 2.14.5-canary.340

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (197) hide show
  1. package/CHANGELOG.md +446 -0
  2. package/benchmark.js +23 -0
  3. package/dist/DefaultMap.js +41 -0
  4. package/dist/Deferred.js +16 -0
  5. package/dist/PromiseQueue.js +107 -0
  6. package/dist/TapStream.js +23 -0
  7. package/dist/alternatives.js +97 -0
  8. package/dist/ansi-html.js +12 -0
  9. package/dist/blob.js +29 -0
  10. package/dist/bundle-url.js +32 -0
  11. package/dist/collection.js +106 -0
  12. package/dist/config.js +138 -0
  13. package/dist/countLines.js +12 -0
  14. package/dist/debounce.js +15 -0
  15. package/dist/debug-tools.js +39 -0
  16. package/dist/dependency-location.js +22 -0
  17. package/dist/escape-html.js +19 -0
  18. package/dist/generateBuildMetrics.js +111 -0
  19. package/dist/generateCertificate.js +124 -0
  20. package/dist/getCertificate.js +13 -0
  21. package/dist/getExisting.js +20 -0
  22. package/dist/getModuleParts.js +27 -0
  23. package/dist/getRootDir.js +46 -0
  24. package/dist/getTimeId.js +13 -0
  25. package/dist/glob.js +129 -0
  26. package/dist/hash.js +45 -0
  27. package/dist/http-server.js +55 -0
  28. package/dist/index.js +148 -0
  29. package/dist/is-url.js +15 -0
  30. package/dist/isDirectoryInside.js +11 -0
  31. package/dist/objectHash.js +20 -0
  32. package/dist/openInBrowser.js +61 -0
  33. package/dist/parseCSSImport.js +14 -0
  34. package/dist/path.js +36 -0
  35. package/dist/prettifyTime.js +6 -0
  36. package/dist/prettyDiagnostic.js +104 -0
  37. package/dist/progress-message.js +31 -0
  38. package/dist/relativeBundlePath.js +13 -0
  39. package/dist/relativeUrl.js +11 -0
  40. package/dist/replaceBundleReferences.js +131 -0
  41. package/dist/schema.js +404 -0
  42. package/dist/shared-buffer.js +24 -0
  43. package/dist/sourcemap.js +121 -0
  44. package/dist/stream.js +69 -0
  45. package/dist/throttle.js +12 -0
  46. package/dist/urlJoin.js +22 -0
  47. package/lib/DefaultMap.js +42 -0
  48. package/lib/Deferred.js +30 -0
  49. package/lib/PromiseQueue.js +112 -0
  50. package/lib/TapStream.js +34 -0
  51. package/lib/alternatives.js +116 -0
  52. package/lib/ansi-html.js +20 -0
  53. package/lib/blob.js +40 -0
  54. package/lib/bundle-url.js +34 -0
  55. package/lib/collection.js +111 -0
  56. package/lib/config.js +174 -0
  57. package/lib/countLines.js +15 -0
  58. package/lib/debounce.js +18 -0
  59. package/lib/debug-tools.js +40 -0
  60. package/lib/dependency-location.js +21 -0
  61. package/lib/escape-html.js +22 -0
  62. package/lib/generateBuildMetrics.js +121 -0
  63. package/lib/generateCertificate.js +129 -0
  64. package/lib/getCertificate.js +18 -0
  65. package/lib/getExisting.js +25 -0
  66. package/lib/getModuleParts.js +30 -0
  67. package/lib/getRootDir.js +52 -0
  68. package/lib/getTimeId.js +10 -0
  69. package/lib/glob.js +110 -0
  70. package/lib/hash.js +50 -0
  71. package/lib/http-server.js +85 -0
  72. package/lib/index.js +643 -37310
  73. package/lib/is-url.js +24 -0
  74. package/lib/isDirectoryInside.js +18 -0
  75. package/lib/objectHash.js +26 -0
  76. package/lib/openInBrowser.js +74 -0
  77. package/lib/parseCSSImport.js +15 -0
  78. package/lib/path.js +39 -0
  79. package/lib/prettifyTime.js +9 -0
  80. package/lib/prettyDiagnostic.js +150 -0
  81. package/lib/progress-message.js +39 -0
  82. package/lib/relativeBundlePath.js +22 -0
  83. package/lib/relativeUrl.js +24 -0
  84. package/lib/replaceBundleReferences.js +182 -0
  85. package/lib/schema.js +353 -0
  86. package/lib/shared-buffer.js +31 -0
  87. package/lib/sourcemap.js +138 -0
  88. package/lib/stream.js +76 -0
  89. package/lib/throttle.js +15 -0
  90. package/lib/types/DefaultMap.d.ts +13 -0
  91. package/lib/types/Deferred.d.ts +8 -0
  92. package/lib/types/PromiseQueue.d.ts +25 -0
  93. package/lib/types/TapStream.d.ts +6 -0
  94. package/lib/types/alternatives.d.ts +3 -0
  95. package/lib/types/ansi-html.d.ts +1 -0
  96. package/lib/types/blob.d.ts +4 -0
  97. package/lib/types/bundle-url.d.ts +4 -0
  98. package/lib/types/collection.d.ts +33 -0
  99. package/lib/types/config.d.ts +17 -0
  100. package/lib/types/countLines.d.ts +1 -0
  101. package/lib/types/debounce.d.ts +1 -0
  102. package/lib/types/debug-tools.d.ts +8 -0
  103. package/lib/types/dependency-location.d.ts +14 -0
  104. package/lib/types/escape-html.d.ts +1 -0
  105. package/lib/types/generateBuildMetrics.d.ts +18 -0
  106. package/lib/types/generateCertificate.d.ts +5 -0
  107. package/lib/types/getCertificate.d.ts +5 -0
  108. package/lib/types/getExisting.d.ts +8 -0
  109. package/lib/types/getModuleParts.d.ts +4 -0
  110. package/lib/types/getRootDir.d.ts +2 -0
  111. package/lib/types/getTimeId.d.ts +1 -0
  112. package/lib/types/glob.d.ts +9 -0
  113. package/lib/types/hash.d.ts +7 -0
  114. package/lib/types/http-server.d.ts +19 -0
  115. package/lib/types/index.d.ts +49 -0
  116. package/lib/types/is-url.d.ts +1 -0
  117. package/lib/types/isDirectoryInside.d.ts +2 -0
  118. package/lib/types/objectHash.d.ts +3 -0
  119. package/lib/types/openInBrowser.d.ts +1 -0
  120. package/lib/types/parseCSSImport.d.ts +1 -0
  121. package/lib/types/path.d.ts +8 -0
  122. package/lib/types/prettifyTime.d.ts +1 -0
  123. package/lib/types/prettyDiagnostic.d.ts +17 -0
  124. package/lib/types/progress-message.d.ts +3 -0
  125. package/lib/types/relativeBundlePath.d.ts +4 -0
  126. package/lib/types/relativeUrl.d.ts +1 -0
  127. package/lib/types/replaceBundleReferences.d.ts +39 -0
  128. package/lib/types/schema.d.ts +107 -0
  129. package/lib/types/shared-buffer.d.ts +2 -0
  130. package/lib/types/sourcemap.d.ts +15 -0
  131. package/lib/types/stream.d.ts +8 -0
  132. package/lib/types/throttle.d.ts +1 -0
  133. package/lib/types/urlJoin.d.ts +5 -0
  134. package/lib/urlJoin.js +35 -0
  135. package/package.json +27 -18
  136. package/src/{DefaultMap.js → DefaultMap.ts} +8 -12
  137. package/src/Deferred.ts +26 -0
  138. package/src/{PromiseQueue.js → PromiseQueue.ts} +40 -35
  139. package/src/{TapStream.js → TapStream.ts} +8 -7
  140. package/src/{alternatives.js → alternatives.ts} +15 -16
  141. package/src/{ansi-html.js → ansi-html.ts} +1 -1
  142. package/src/{blob.js → blob.ts} +3 -5
  143. package/src/{bundle-url.js → bundle-url.ts} +3 -5
  144. package/src/{collection.js → collection.ts} +23 -20
  145. package/src/{config.js → config.ts} +20 -21
  146. package/src/{countLines.js → countLines.ts} +0 -2
  147. package/src/{debounce.js → debounce.ts} +3 -5
  148. package/src/debug-tools.ts +48 -0
  149. package/src/{dependency-location.js → dependency-location.ts} +15 -11
  150. package/src/{escape-html.js → escape-html.ts} +5 -3
  151. package/src/{generateBuildMetrics.js → generateBuildMetrics.ts} +19 -21
  152. package/src/{generateCertificate.js → generateCertificate.ts} +8 -6
  153. package/src/{getCertificate.js → getCertificate.ts} +6 -5
  154. package/src/{getExisting.js → getExisting.ts} +4 -3
  155. package/src/{getModuleParts.js → getModuleParts.ts} +3 -2
  156. package/src/{getRootDir.js → getRootDir.ts} +1 -3
  157. package/src/getTimeId.ts +12 -0
  158. package/src/{glob.js → glob.ts} +13 -14
  159. package/src/{hash.js → hash.ts} +23 -18
  160. package/src/{http-server.js → http-server.ts} +34 -40
  161. package/src/{index.js → index.ts} +14 -10
  162. package/src/{is-url.js → is-url.ts} +1 -2
  163. package/src/{isDirectoryInside.js → isDirectoryInside.ts} +1 -2
  164. package/src/{objectHash.js → objectHash.ts} +1 -4
  165. package/src/{openInBrowser.js → openInBrowser.ts} +2 -4
  166. package/src/{parseCSSImport.js → parseCSSImport.ts} +0 -2
  167. package/src/{path.js → path.ts} +2 -4
  168. package/src/{prettifyTime.js → prettifyTime.ts} +0 -2
  169. package/src/{prettyDiagnostic.js → prettyDiagnostic.ts} +34 -22
  170. package/src/progress-message.ts +43 -0
  171. package/src/{relativeBundlePath.js → relativeBundlePath.ts} +4 -4
  172. package/src/{relativeUrl.js → relativeUrl.ts} +0 -1
  173. package/src/{replaceBundleReferences.js → replaceBundleReferences.ts} +67 -51
  174. package/src/schema.ts +552 -0
  175. package/src/{shared-buffer.js → shared-buffer.ts} +5 -3
  176. package/src/{sourcemap.js → sourcemap.ts} +28 -10
  177. package/src/{stream.js → stream.ts} +30 -22
  178. package/src/throttle.ts +13 -0
  179. package/src/{urlJoin.js → urlJoin.ts} +1 -3
  180. package/test/{DefaultMap.test.js → DefaultMap.test.ts} +4 -6
  181. package/test/{PromiseQueue.test.js → PromiseQueue.test.ts} +5 -6
  182. package/test/{collection.test.js → collection.test.ts} +0 -2
  183. package/test/{config.test.js → config.test.ts} +0 -3
  184. package/test/{objectHash.test.js → objectHash.test.ts} +4 -5
  185. package/test/{prettifyTime.test.js → prettifyTime.test.ts} +0 -1
  186. package/test/{replaceBundleReferences.test.js → replaceBundleReferences.test.ts} +0 -32
  187. package/test/schema.test.ts +748 -0
  188. package/test/{sourcemap.test.js → sourcemap.test.ts} +0 -1
  189. package/test/{throttle.test.js → throttle.test.ts} +1 -3
  190. package/test/{urlJoin.test.js → urlJoin.test.ts} +0 -2
  191. package/tsconfig.json +36 -0
  192. package/tsconfig.tsbuildinfo +1 -0
  193. package/lib/index.js.map +0 -1
  194. package/src/Deferred.js +0 -23
  195. package/src/progress-message.js +0 -22
  196. package/src/schema.js +0 -504
  197. package/src/throttle.js +0 -15
package/src/Deferred.js DELETED
@@ -1,23 +0,0 @@
1
- // @flow strict-local
2
-
3
- import invariant from 'assert';
4
-
5
- export type Deferred<T> = {|
6
- resolve(T): void,
7
- reject(mixed): void,
8
- |};
9
-
10
- export function makeDeferredWithPromise<T>(): {|
11
- deferred: Deferred<T>,
12
- promise: Promise<T>,
13
- |} {
14
- let deferred: ?Deferred<T>;
15
- let promise = new Promise<T>((resolve, reject) => {
16
- deferred = {resolve, reject};
17
- });
18
-
19
- // Promise constructor callback executes synchronously, so this is defined
20
- invariant(deferred != null);
21
-
22
- return {deferred, promise};
23
- }
@@ -1,22 +0,0 @@
1
- // @flow strict-local
2
- import type {BuildProgressEvent} from '@atlaspack/types';
3
-
4
- import path from 'path';
5
-
6
- export function getProgressMessage(event: BuildProgressEvent): ?string {
7
- switch (event.phase) {
8
- case 'transforming':
9
- return `Building ${path.basename(event.filePath)}...`;
10
-
11
- case 'bundling':
12
- return 'Bundling...';
13
-
14
- case 'packaging':
15
- return `Packaging ${event.bundle.displayName}...`;
16
-
17
- case 'optimizing':
18
- return `Optimizing ${event.bundle.displayName}...`;
19
- }
20
-
21
- return null;
22
- }
package/src/schema.js DELETED
@@ -1,504 +0,0 @@
1
- // @flow strict-local
2
- import ThrowableDiagnostic, {
3
- generateJSONCodeHighlights,
4
- escapeMarkdown,
5
- encodeJSONKeyComponent,
6
- } from '@atlaspack/diagnostic';
7
- import type {Mapping} from '@mischnic/json-sourcemap';
8
- import nullthrows from 'nullthrows';
9
- import * as levenshtein from 'fastest-levenshtein';
10
-
11
- export type SchemaEntity =
12
- | SchemaObject
13
- | SchemaArray
14
- | SchemaBoolean
15
- | SchemaString
16
- | SchemaNumber
17
- | SchemaEnum
18
- | SchemaOneOf
19
- | SchemaAllOf
20
- | SchemaNot
21
- | SchemaAny;
22
- export type SchemaArray = {|
23
- type: 'array',
24
- items?: SchemaEntity,
25
- __type?: string,
26
- |};
27
- export type SchemaBoolean = {|
28
- type: 'boolean',
29
- __type?: string,
30
- |};
31
- export type SchemaOneOf = {|
32
- oneOf: Array<SchemaEntity>,
33
- |};
34
- export type SchemaAllOf = {|
35
- allOf: Array<SchemaEntity>,
36
- |};
37
- export type SchemaNot = {|
38
- not: SchemaEntity,
39
- __message: string,
40
- |};
41
- export type SchemaString = {|
42
- type: 'string',
43
- enum?: Array<string>,
44
- __validate?: (val: string) => ?string,
45
- __type?: string,
46
- |};
47
- export type SchemaNumber = {|
48
- type: 'number',
49
- enum?: Array<number>,
50
- __type?: string,
51
- |};
52
- export type SchemaEnum = {|
53
- enum: Array<mixed>,
54
- |};
55
- export type SchemaObject = {|
56
- type: 'object',
57
- properties: {[string]: SchemaEntity, ...},
58
- additionalProperties?: boolean | SchemaEntity,
59
- required?: Array<string>,
60
- __forbiddenProperties?: Array<string>,
61
- __type?: string,
62
- |};
63
- export type SchemaAny = {||};
64
- export type SchemaError =
65
- | {|
66
- type: 'type',
67
- expectedTypes: Array<string>,
68
- dataType: ?'key' | 'value',
69
-
70
- dataPath: string,
71
- ancestors: Array<SchemaEntity>,
72
- prettyType?: string,
73
- |}
74
- | {|
75
- type: 'enum',
76
- expectedValues: Array<mixed>,
77
- dataType: 'key' | 'value',
78
- actualValue: mixed,
79
-
80
- dataPath: string,
81
- ancestors: Array<SchemaEntity>,
82
- prettyType?: string,
83
- |}
84
- | {|
85
- type: 'forbidden-prop',
86
- prop: string,
87
- expectedProps: Array<string>,
88
- actualProps: Array<string>,
89
- dataType: 'key',
90
-
91
- dataPath: string,
92
- ancestors: Array<SchemaEntity>,
93
- prettyType?: string,
94
- |}
95
- | {|
96
- type: 'missing-prop',
97
- prop: string,
98
- expectedProps: Array<string>,
99
- actualProps: Array<string>,
100
- dataType: 'key' | 'value',
101
-
102
- dataPath: string,
103
- ancestors: Array<SchemaEntity>,
104
- prettyType?: string,
105
- |}
106
- | {|
107
- type: 'other',
108
- actualValue: mixed,
109
- dataType: ?'key' | 'value',
110
- message?: string,
111
- dataPath: string,
112
- ancestors: Array<SchemaEntity>,
113
- |};
114
-
115
- function validateSchema(schema: SchemaEntity, data: mixed): Array<SchemaError> {
116
- function walk(
117
- schemaAncestors,
118
- dataNode,
119
- dataPath,
120
- ): ?SchemaError | Array<SchemaError> {
121
- let [schemaNode] = schemaAncestors;
122
-
123
- if (schemaNode.type) {
124
- let type = Array.isArray(dataNode) ? 'array' : typeof dataNode;
125
- if (schemaNode.type !== type) {
126
- return {
127
- type: 'type',
128
- dataType: 'value',
129
- dataPath,
130
- expectedTypes: [schemaNode.type],
131
- ancestors: schemaAncestors,
132
- prettyType: schemaNode.__type,
133
- };
134
- } else {
135
- switch (schemaNode.type) {
136
- case 'array': {
137
- if (schemaNode.items) {
138
- let results: Array<SchemaError | Array<SchemaError>> = [];
139
- // $FlowFixMe type was already checked
140
- for (let i = 0; i < dataNode.length; i++) {
141
- let result = walk(
142
- [schemaNode.items].concat(schemaAncestors),
143
- // $FlowFixMe type was already checked
144
- dataNode[i],
145
- dataPath + '/' + i,
146
- );
147
- if (result) results.push(result);
148
- }
149
- if (results.length)
150
- return results.reduce((acc, v) => acc.concat(v), []);
151
- }
152
- break;
153
- }
154
- case 'string': {
155
- // $FlowFixMe type was already checked
156
- let value: string = dataNode;
157
- if (schemaNode.enum) {
158
- if (!schemaNode.enum.includes(value)) {
159
- return {
160
- type: 'enum',
161
- dataType: 'value',
162
- dataPath,
163
- expectedValues: schemaNode.enum,
164
- actualValue: value,
165
- ancestors: schemaAncestors,
166
- };
167
- }
168
- } else if (schemaNode.__validate) {
169
- let validationError = schemaNode.__validate(value);
170
- if (typeof validationError == 'string') {
171
- return {
172
- type: 'other',
173
- dataType: 'value',
174
- dataPath,
175
- message: validationError,
176
- actualValue: value,
177
- ancestors: schemaAncestors,
178
- };
179
- }
180
- }
181
- break;
182
- }
183
- case 'number': {
184
- // $FlowFixMe type was already checked
185
- let value: number = dataNode;
186
- if (schemaNode.enum) {
187
- if (!schemaNode.enum.includes(value)) {
188
- return {
189
- type: 'enum',
190
- dataType: 'value',
191
- dataPath,
192
- expectedValues: schemaNode.enum,
193
- actualValue: value,
194
- ancestors: schemaAncestors,
195
- };
196
- }
197
- }
198
- break;
199
- }
200
- case 'object': {
201
- let results: Array<Array<SchemaError> | SchemaError> = [];
202
- let invalidProps;
203
- if (schemaNode.__forbiddenProperties) {
204
- // $FlowFixMe type was already checked
205
- let keys = Object.keys(dataNode);
206
- invalidProps = schemaNode.__forbiddenProperties.filter((val) =>
207
- keys.includes(val),
208
- );
209
- results.push(
210
- ...invalidProps.map(
211
- (k) =>
212
- ({
213
- type: 'forbidden-prop',
214
- dataPath: dataPath + '/' + encodeJSONKeyComponent(k),
215
- dataType: 'key',
216
- prop: k,
217
- expectedProps: Object.keys(schemaNode.properties),
218
- actualProps: keys,
219
- ancestors: schemaAncestors,
220
- }: SchemaError),
221
- ),
222
- );
223
- }
224
- if (schemaNode.required) {
225
- // $FlowFixMe type was already checked
226
- let keys = Object.keys(dataNode);
227
- let missingKeys = schemaNode.required.filter(
228
- (val) => !keys.includes(val),
229
- );
230
- results.push(
231
- ...missingKeys.map(
232
- (k) =>
233
- ({
234
- type: 'missing-prop',
235
- dataPath,
236
- dataType: 'value',
237
- prop: k,
238
- expectedProps: schemaNode.required,
239
- actualProps: keys,
240
- ancestors: schemaAncestors,
241
- }: SchemaError),
242
- ),
243
- );
244
- }
245
- if (schemaNode.properties) {
246
- let {additionalProperties = true} = schemaNode;
247
- // $FlowFixMe type was already checked
248
- for (let k in dataNode) {
249
- if (invalidProps && invalidProps.includes(k)) {
250
- // Don't check type on forbidden props
251
- continue;
252
- } else if (k in schemaNode.properties) {
253
- let result = walk(
254
- [schemaNode.properties[k]].concat(schemaAncestors),
255
- // $FlowFixMe type was already checked
256
- dataNode[k],
257
- dataPath + '/' + encodeJSONKeyComponent(k),
258
- );
259
- if (result) results.push(result);
260
- } else {
261
- if (typeof additionalProperties === 'boolean') {
262
- if (!additionalProperties) {
263
- results.push({
264
- type: 'enum',
265
- dataType: 'key',
266
- dataPath: dataPath + '/' + encodeJSONKeyComponent(k),
267
- expectedValues: Object.keys(
268
- schemaNode.properties,
269
- ).filter(
270
- // $FlowFixMe type was already checked
271
- (p) => !(p in dataNode),
272
- ),
273
- actualValue: k,
274
- ancestors: schemaAncestors,
275
- prettyType: schemaNode.__type,
276
- });
277
- }
278
- } else {
279
- let result = walk(
280
- [additionalProperties].concat(schemaAncestors),
281
- // $FlowFixMe type was already checked
282
- dataNode[k],
283
- dataPath + '/' + encodeJSONKeyComponent(k),
284
- );
285
- if (result) results.push(result);
286
- }
287
- }
288
- }
289
- }
290
- if (results.length)
291
- return results.reduce((acc, v) => acc.concat(v), []);
292
- break;
293
- }
294
- case 'boolean':
295
- // NOOP, type was checked already
296
- break;
297
- default:
298
- throw new Error(`Unimplemented schema type ${type}?`);
299
- }
300
- }
301
- } else {
302
- if (schemaNode.enum && !schemaNode.enum.includes(dataNode)) {
303
- return {
304
- type: 'enum',
305
- dataType: 'value',
306
- dataPath: dataPath,
307
- expectedValues: schemaNode.enum,
308
- actualValue: schemaNode,
309
- ancestors: schemaAncestors,
310
- };
311
- }
312
-
313
- if (schemaNode.oneOf || schemaNode.allOf) {
314
- let list = schemaNode.oneOf || schemaNode.allOf;
315
- let results: Array<SchemaError | Array<SchemaError>> = [];
316
- for (let f of list) {
317
- let result = walk([f].concat(schemaAncestors), dataNode, dataPath);
318
- if (result) results.push(result);
319
- }
320
- if (
321
- schemaNode.oneOf
322
- ? results.length == schemaNode.oneOf.length
323
- : results.length > 0
324
- ) {
325
- // return the result with more values / longer key
326
- results.sort((a, b) =>
327
- Array.isArray(a) || Array.isArray(b)
328
- ? Array.isArray(a) && !Array.isArray(b)
329
- ? -1
330
- : !Array.isArray(a) && Array.isArray(b)
331
- ? 1
332
- : Array.isArray(a) && Array.isArray(b)
333
- ? b.length - a.length
334
- : 0
335
- : b.dataPath.length - a.dataPath.length,
336
- );
337
- return results[0];
338
- }
339
- } else if (schemaNode.not) {
340
- let result = walk(
341
- [schemaNode.not].concat(schemaAncestors),
342
- dataNode,
343
- dataPath,
344
- );
345
- if (!result || result.length == 0) {
346
- return {
347
- type: 'other',
348
- dataPath,
349
- dataType: null,
350
- message: schemaNode.__message,
351
- actualValue: dataNode,
352
- ancestors: schemaAncestors,
353
- };
354
- }
355
- }
356
- }
357
-
358
- return undefined;
359
- }
360
-
361
- let result = walk([schema], data, '');
362
- return Array.isArray(result) ? result : result ? [result] : [];
363
- }
364
- export default validateSchema;
365
-
366
- export function fuzzySearch(
367
- expectedValues: Array<string>,
368
- actualValue: string,
369
- ): Array<string> {
370
- let result = expectedValues
371
- .map((exp) => [exp, levenshtein.distance(exp, actualValue)])
372
- .filter(
373
- // Remove if more than half of the string would need to be changed
374
- ([, d]) => d * 2 < actualValue.length,
375
- );
376
- result.sort(([, a], [, b]) => a - b);
377
- return result.map(([v]) => v);
378
- }
379
-
380
- validateSchema.diagnostic = function (
381
- schema: SchemaEntity,
382
- data: {|
383
- ...
384
- | {|
385
- source?: ?string,
386
- data?: mixed,
387
- |}
388
- | {|
389
- source: string,
390
- map: {|
391
- data: mixed,
392
- pointers: {|[key: string]: Mapping|},
393
- |},
394
- |},
395
- filePath?: ?string,
396
- prependKey?: ?string,
397
- |},
398
- origin: string,
399
- message: string,
400
- ): void {
401
- if (
402
- 'source' in data &&
403
- 'data' in data &&
404
- typeof data.source !== 'string' &&
405
- !data
406
- ) {
407
- throw new Error(
408
- 'At least one of data.source and data.data must be defined!',
409
- );
410
- }
411
- let object = data.map
412
- ? data.map.data
413
- : // $FlowFixMe we can assume it's a JSON object
414
- data.data ?? JSON.parse(data.source);
415
- let errors = validateSchema(schema, object);
416
- if (errors.length) {
417
- let keys = errors.map((e) => {
418
- let message;
419
- if (e.type === 'enum') {
420
- let {actualValue} = e;
421
- let expectedValues = e.expectedValues.map(String);
422
- let likely =
423
- actualValue != null
424
- ? fuzzySearch(expectedValues, String(actualValue))
425
- : [];
426
-
427
- if (likely.length > 0) {
428
- message = `Did you mean ${likely
429
- .map((v) => JSON.stringify(v))
430
- .join(', ')}?`;
431
- } else if (expectedValues.length > 0) {
432
- message = `Possible values: ${expectedValues
433
- .map((v) => JSON.stringify(v))
434
- .join(', ')}`;
435
- } else {
436
- message = 'Unexpected value';
437
- }
438
- } else if (e.type === 'forbidden-prop') {
439
- let {prop, expectedProps, actualProps} = e;
440
- let likely = fuzzySearch(expectedProps, prop).filter(
441
- (v) => !actualProps.includes(v),
442
- );
443
- if (likely.length > 0) {
444
- message = `Did you mean ${likely
445
- .map((v) => JSON.stringify(v))
446
- .join(', ')}?`;
447
- } else {
448
- message = 'Unexpected property';
449
- }
450
- } else if (e.type === 'missing-prop') {
451
- let {prop, actualProps} = e;
452
- let likely = fuzzySearch(actualProps, prop);
453
- if (likely.length > 0) {
454
- message = `Did you mean ${JSON.stringify(prop)}?`;
455
- e.dataPath += '/' + likely[0];
456
- e.dataType = 'key';
457
- } else {
458
- message = `Missing property ${prop}`;
459
- }
460
- } else if (e.type === 'type') {
461
- if (e.prettyType != null) {
462
- message = `Expected ${e.prettyType}`;
463
- } else {
464
- message = `Expected type ${e.expectedTypes.join(', ')}`;
465
- }
466
- } else {
467
- message = e.message;
468
- }
469
- return {key: e.dataPath, type: e.dataType, message};
470
- });
471
- let map, code;
472
- if (data.map) {
473
- map = data.map;
474
- code = data.source;
475
- } else {
476
- // $FlowFixMe we can assume that data is valid JSON
477
- map = data.source ?? JSON.stringify(nullthrows(data.data), 0, '\t');
478
- code = map;
479
- }
480
- let codeFrames = [
481
- {
482
- filePath: data.filePath ?? undefined,
483
- language: 'json',
484
- code,
485
- codeHighlights: generateJSONCodeHighlights(
486
- map,
487
- keys.map(({key, type, message}) => ({
488
- key: (data.prependKey ?? '') + key,
489
- type: type,
490
- message: message != null ? escapeMarkdown(message) : message,
491
- })),
492
- ),
493
- },
494
- ];
495
-
496
- throw new ThrowableDiagnostic({
497
- diagnostic: {
498
- message: message,
499
- origin,
500
- codeFrames,
501
- },
502
- });
503
- }
504
- };
package/src/throttle.js DELETED
@@ -1,15 +0,0 @@
1
- // @flow strict-local
2
-
3
- export default function throttle<TArgs: Iterable<mixed>>(
4
- fn: (...args: TArgs) => mixed,
5
- delay: number,
6
- ): (...args: TArgs) => void {
7
- let lastCalled: ?number;
8
-
9
- return function throttled(...args: TArgs) {
10
- if (lastCalled == null || lastCalled + delay <= Date.now()) {
11
- fn.call(this, ...args);
12
- lastCalled = Date.now();
13
- }
14
- };
15
- }