@docusaurus/utils 2.0.0-beta.ff31de0ff → 2.0.1

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 (111) hide show
  1. package/README.md +1 -1
  2. package/lib/constants.d.ts +73 -0
  3. package/lib/constants.d.ts.map +1 -0
  4. package/lib/constants.js +78 -0
  5. package/lib/constants.js.map +1 -0
  6. package/lib/dataFileUtils.d.ts +60 -0
  7. package/lib/dataFileUtils.d.ts.map +1 -0
  8. package/lib/dataFileUtils.js +91 -0
  9. package/lib/dataFileUtils.js.map +1 -0
  10. package/lib/emitUtils.d.ts +32 -0
  11. package/lib/emitUtils.d.ts.map +1 -0
  12. package/lib/emitUtils.js +80 -0
  13. package/lib/emitUtils.js.map +1 -0
  14. package/lib/gitUtils.d.ts +66 -0
  15. package/lib/gitUtils.d.ts.map +1 -0
  16. package/lib/gitUtils.js +63 -0
  17. package/lib/gitUtils.js.map +1 -0
  18. package/lib/globUtils.d.ts +40 -0
  19. package/lib/globUtils.d.ts.map +1 -0
  20. package/lib/globUtils.js +71 -0
  21. package/lib/globUtils.js.map +1 -0
  22. package/lib/hashUtils.d.ts +17 -0
  23. package/lib/hashUtils.d.ts.map +1 -0
  24. package/lib/hashUtils.js +42 -0
  25. package/lib/hashUtils.js.map +1 -0
  26. package/lib/i18nUtils.d.ts +53 -0
  27. package/lib/i18nUtils.d.ts.map +1 -0
  28. package/lib/i18nUtils.js +70 -0
  29. package/lib/i18nUtils.js.map +1 -0
  30. package/lib/index.d.ts +17 -82
  31. package/lib/index.d.ts.map +1 -0
  32. package/lib/index.js +91 -411
  33. package/lib/index.js.map +1 -0
  34. package/lib/jsUtils.d.ts +28 -0
  35. package/lib/jsUtils.d.ts.map +1 -0
  36. package/lib/jsUtils.js +57 -0
  37. package/lib/jsUtils.js.map +1 -0
  38. package/lib/markdownLinks.d.ts +49 -5
  39. package/lib/markdownLinks.d.ts.map +1 -0
  40. package/lib/markdownLinks.js +57 -13
  41. package/lib/markdownLinks.js.map +1 -0
  42. package/lib/markdownUtils.d.ts +112 -0
  43. package/lib/markdownUtils.d.ts.map +1 -0
  44. package/lib/markdownUtils.js +271 -0
  45. package/lib/markdownUtils.js.map +1 -0
  46. package/lib/pathUtils.d.ts +52 -0
  47. package/lib/pathUtils.d.ts.map +1 -0
  48. package/lib/pathUtils.js +115 -0
  49. package/lib/pathUtils.js.map +1 -0
  50. package/lib/shellUtils.d.ts +8 -0
  51. package/lib/shellUtils.d.ts.map +1 -0
  52. package/lib/shellUtils.js +21 -0
  53. package/lib/shellUtils.js.map +1 -0
  54. package/lib/slugger.d.ts +24 -0
  55. package/lib/slugger.d.ts.map +1 -0
  56. package/lib/slugger.js +23 -0
  57. package/lib/slugger.js.map +1 -0
  58. package/lib/tags.d.ts +59 -0
  59. package/lib/tags.d.ts.map +1 -0
  60. package/lib/tags.js +91 -0
  61. package/lib/tags.js.map +1 -0
  62. package/lib/urlUtils.d.ts +66 -0
  63. package/lib/urlUtils.d.ts.map +1 -0
  64. package/lib/urlUtils.js +207 -0
  65. package/lib/urlUtils.js.map +1 -0
  66. package/lib/webpackUtils.d.ts +35 -0
  67. package/lib/webpackUtils.d.ts.map +1 -0
  68. package/lib/webpackUtils.js +115 -0
  69. package/lib/webpackUtils.js.map +1 -0
  70. package/package.json +28 -10
  71. package/src/constants.ts +98 -0
  72. package/src/dataFileUtils.ts +122 -0
  73. package/src/deps.d.ts +10 -0
  74. package/src/emitUtils.ts +99 -0
  75. package/src/gitUtils.ts +146 -0
  76. package/src/globUtils.ts +85 -0
  77. package/src/hashUtils.ts +38 -0
  78. package/src/i18nUtils.ts +114 -0
  79. package/src/index.ts +98 -521
  80. package/src/jsUtils.ts +59 -0
  81. package/src/markdownLinks.ts +101 -30
  82. package/src/markdownUtils.ts +357 -0
  83. package/src/pathUtils.ts +123 -0
  84. package/src/shellUtils.ts +18 -0
  85. package/src/slugger.ts +36 -0
  86. package/src/tags.ts +130 -0
  87. package/src/urlUtils.ts +234 -0
  88. package/src/webpackUtils.ts +153 -0
  89. package/lib/.tsbuildinfo +0 -3928
  90. package/lib/codeTranslationsUtils.d.ts +0 -11
  91. package/lib/codeTranslationsUtils.js +0 -50
  92. package/lib/escapePath.d.ts +0 -17
  93. package/lib/escapePath.js +0 -25
  94. package/lib/markdownParser.d.ts +0 -28
  95. package/lib/markdownParser.js +0 -132
  96. package/lib/posixPath.d.ts +0 -14
  97. package/lib/posixPath.js +0 -28
  98. package/src/__tests__/__fixtures__/defaultCodeTranslations/en.json +0 -4
  99. package/src/__tests__/__fixtures__/defaultCodeTranslations/fr-FR.json +0 -5
  100. package/src/__tests__/__fixtures__/defaultCodeTranslations/fr.json +0 -4
  101. package/src/__tests__/__snapshots__/index.test.ts.snap +0 -8
  102. package/src/__tests__/codeTranslationsUtils.test.ts +0 -112
  103. package/src/__tests__/escapePath.test.ts +0 -25
  104. package/src/__tests__/index.test.ts +0 -681
  105. package/src/__tests__/markdownParser.test.ts +0 -772
  106. package/src/__tests__/posixPath.test.ts +0 -25
  107. package/src/codeTranslationsUtils.ts +0 -56
  108. package/src/escapePath.ts +0 -23
  109. package/src/markdownParser.ts +0 -177
  110. package/src/posixPath.ts +0 -27
  111. package/tsconfig.json +0 -9
@@ -1,681 +0,0 @@
1
- /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- */
7
-
8
- import path from 'path';
9
- import {
10
- fileToPath,
11
- simpleHash,
12
- docuHash,
13
- genComponentName,
14
- genChunkName,
15
- idx,
16
- getSubFolder,
17
- normalizeUrl,
18
- posixPath,
19
- objectWithKeySorted,
20
- aliasedSitePath,
21
- isValidPathname,
22
- addTrailingSlash,
23
- removeTrailingSlash,
24
- removeSuffix,
25
- removePrefix,
26
- getFilePathForRoutePath,
27
- addLeadingSlash,
28
- getElementsAround,
29
- mergeTranslations,
30
- mapAsyncSequencial,
31
- findAsyncSequential,
32
- findFolderContainingFile,
33
- getFolderContainingFile,
34
- updateTranslationFileMessages,
35
- parseMarkdownHeadingId,
36
- } from '../index';
37
- import {sum} from 'lodash';
38
-
39
- describe('load utils', () => {
40
- test('aliasedSitePath', () => {
41
- const asserts: Record<string, string> = {
42
- 'user/website/docs/asd.md': '@site/docs/asd.md',
43
- 'user/website/versioned_docs/foo/bar.md':
44
- '@site/versioned_docs/foo/bar.md',
45
- 'user/docs/test.md': '@site/../docs/test.md',
46
- };
47
- Object.keys(asserts).forEach((file) => {
48
- expect(posixPath(aliasedSitePath(file, 'user/website'))).toBe(
49
- asserts[file],
50
- );
51
- });
52
- });
53
-
54
- test('genComponentName', () => {
55
- const asserts: Record<string, string> = {
56
- '/': 'index',
57
- '/foo-bar': 'FooBar096',
58
- '/foo/bar': 'FooBar1Df',
59
- '/blog/2017/12/14/introducing-docusaurus':
60
- 'Blog20171214IntroducingDocusaurus8D2',
61
- '/blog/2017/12/14-introducing-docusaurus':
62
- 'Blog20171214IntroducingDocusaurus0Bc',
63
- '/blog/201712/14-introducing-docusaurus':
64
- 'Blog20171214IntroducingDocusaurusA93',
65
- };
66
- Object.keys(asserts).forEach((file) => {
67
- expect(genComponentName(file)).toBe(asserts[file]);
68
- });
69
- });
70
-
71
- test('simpleHash', () => {
72
- const asserts: Record<string, string> = {
73
- '': 'd41',
74
- '/foo-bar': '096',
75
- '/foo/bar': '1df',
76
- '/endi/lie': '9fa',
77
- '/endi-lie': 'fd3',
78
- '/yangshun/tay': '48d',
79
- '/yangshun-tay': 'f3b',
80
- };
81
- Object.keys(asserts).forEach((file) => {
82
- expect(simpleHash(file, 3)).toBe(asserts[file]);
83
- });
84
- });
85
-
86
- test('docuHash', () => {
87
- const asserts: Record<string, string> = {
88
- '': '-d41',
89
- '/': 'index',
90
- '/foo-bar': 'foo-bar-096',
91
- '/foo/bar': 'foo-bar-1df',
92
- '/endi/lie': 'endi-lie-9fa',
93
- '/endi-lie': 'endi-lie-fd3',
94
- '/yangshun/tay': 'yangshun-tay-48d',
95
- '/yangshun-tay': 'yangshun-tay-f3b',
96
- };
97
- Object.keys(asserts).forEach((file) => {
98
- expect(docuHash(file)).toBe(asserts[file]);
99
- });
100
- });
101
-
102
- test('fileToPath', () => {
103
- const asserts: Record<string, string> = {
104
- 'index.md': '/',
105
- 'hello/index.md': '/hello/',
106
- 'foo.md': '/foo',
107
- 'foo/bar.md': '/foo/bar',
108
- 'index.js': '/',
109
- 'hello/index.js': '/hello/',
110
- 'foo.js': '/foo',
111
- 'foo/bar.js': '/foo/bar',
112
- };
113
- Object.keys(asserts).forEach((file) => {
114
- expect(fileToPath(file)).toBe(asserts[file]);
115
- });
116
- });
117
-
118
- test('objectWithKeySorted', () => {
119
- const obj = {
120
- '/docs/adding-blog': '4',
121
- '/docs/versioning': '5',
122
- '/': '1',
123
- '/blog/2018': '3',
124
- '/youtube': '7',
125
- '/users/en/': '6',
126
- '/blog': '2',
127
- };
128
- expect(objectWithKeySorted(obj)).toMatchInlineSnapshot(`
129
- Object {
130
- "/": "1",
131
- "/blog": "2",
132
- "/blog/2018": "3",
133
- "/docs/adding-blog": "4",
134
- "/docs/versioning": "5",
135
- "/users/en/": "6",
136
- "/youtube": "7",
137
- }
138
- `);
139
- const obj2 = {
140
- b: 'foo',
141
- c: 'bar',
142
- a: 'baz',
143
- };
144
- expect(objectWithKeySorted(obj2)).toMatchInlineSnapshot(`
145
- Object {
146
- "a": "baz",
147
- "b": "foo",
148
- "c": "bar",
149
- }
150
- `);
151
- });
152
-
153
- test('genChunkName', () => {
154
- const firstAssert: Record<string, string> = {
155
- '/docs/adding-blog': 'docs-adding-blog-062',
156
- '/docs/versioning': 'docs-versioning-8a8',
157
- '/': 'index',
158
- '/blog/2018/04/30/How-I-Converted-Profilo-To-Docusaurus':
159
- 'blog-2018-04-30-how-i-converted-profilo-to-docusaurus-4f2',
160
- '/youtube': 'youtube-429',
161
- '/users/en/': 'users-en-f7a',
162
- '/blog': 'blog-c06',
163
- };
164
- Object.keys(firstAssert).forEach((str) => {
165
- expect(genChunkName(str)).toBe(firstAssert[str]);
166
- });
167
-
168
- // Don't allow different chunk name for same path.
169
- expect(genChunkName('path/is/similar', 'oldPrefix')).toEqual(
170
- genChunkName('path/is/similar', 'newPrefix'),
171
- );
172
-
173
- // Even with same preferred name, still different chunk name for different path
174
- const secondAssert: Record<string, string> = {
175
- '/blog/1': 'blog-85-f-089',
176
- '/blog/2': 'blog-353-489',
177
- };
178
- Object.keys(secondAssert).forEach((str) => {
179
- expect(genChunkName(str, undefined, 'blog')).toBe(secondAssert[str]);
180
- });
181
-
182
- // Only generate short unique id
183
- const thirdAssert: Record<string, string> = {
184
- a: '0cc175b9',
185
- b: '92eb5ffe',
186
- c: '4a8a08f0',
187
- d: '8277e091',
188
- };
189
- Object.keys(thirdAssert).forEach((str) => {
190
- expect(genChunkName(str, undefined, undefined, true)).toBe(
191
- thirdAssert[str],
192
- );
193
- });
194
- expect(genChunkName('d', undefined, undefined, true)).toBe('8277e091');
195
- });
196
-
197
- test('idx', () => {
198
- const a = {};
199
- const b = {hello: 'world'};
200
- const obj = {
201
- translation: {
202
- enabled: true,
203
- enabledLanguages: [
204
- {
205
- enabled: true,
206
- name: 'English',
207
- tag: 'en',
208
- },
209
- {
210
- enabled: true,
211
- name: '日本語',
212
- tag: 'ja',
213
- },
214
- ],
215
- },
216
- versioning: {
217
- enabled: false,
218
- versions: [],
219
- },
220
- };
221
- const test = {arr: [1, 2, 3]};
222
- const variable = 'enabledLanguages';
223
- expect(idx(a, ['b', 'c'])).toBeUndefined();
224
- expect(idx(b, ['hello'])).toEqual('world');
225
- expect(idx(b, 'hello')).toEqual('world');
226
- expect(idx(obj, 'typo')).toBeUndefined();
227
- expect(idx(obj, 'versioning')).toEqual({
228
- enabled: false,
229
- versions: [],
230
- });
231
- expect(idx(obj, ['translation', 'enabled'])).toEqual(true);
232
- expect(
233
- idx(obj, ['translation', variable]).map(
234
- (lang: {tag: string}) => lang.tag,
235
- ),
236
- ).toEqual(['en', 'ja']);
237
- expect(idx(test, ['arr', 0])).toEqual(1);
238
- expect(idx(undefined)).toBeUndefined();
239
- expect(idx(null)).toBeNull();
240
- });
241
-
242
- test('getSubFolder', () => {
243
- const testA = path.join('folder', 'en', 'test.md');
244
- const testB = path.join('folder', 'ja', 'test.md');
245
- const testC = path.join('folder', 'ja', 'en', 'test.md');
246
- const testD = path.join('docs', 'ro', 'test.md');
247
- const testE = path.join('docs', 'test.md');
248
- expect(getSubFolder(testA, 'folder')).toBe('en');
249
- expect(getSubFolder(testB, 'folder')).toBe('ja');
250
- expect(getSubFolder(testC, 'folder')).toBe('ja');
251
- expect(getSubFolder(testD, 'docs')).toBe('ro');
252
- expect(getSubFolder(testE, 'docs')).toBeNull();
253
- });
254
-
255
- test('normalizeUrl', () => {
256
- const asserts = [
257
- {
258
- input: ['/', ''],
259
- output: '/',
260
- },
261
- {
262
- input: ['', '/'],
263
- output: '/',
264
- },
265
- {
266
- input: ['/'],
267
- output: '/',
268
- },
269
- {
270
- input: [''],
271
- output: '',
272
- },
273
- {
274
- input: ['/', '/'],
275
- output: '/',
276
- },
277
- {
278
- input: ['/', 'docs'],
279
- output: '/docs',
280
- },
281
- {
282
- input: ['/', 'docs', 'en', 'next', 'blog'],
283
- output: '/docs/en/next/blog',
284
- },
285
- {
286
- input: ['/test/', '/docs', 'ro', 'doc1'],
287
- output: '/test/docs/ro/doc1',
288
- },
289
- {
290
- input: ['/test/', '/', 'ro', 'doc1'],
291
- output: '/test/ro/doc1',
292
- },
293
- {
294
- input: ['/', '/', '2020/02/29/leap-day'],
295
- output: '/2020/02/29/leap-day',
296
- },
297
- {
298
- input: ['', '/', 'ko', 'hello'],
299
- output: '/ko/hello',
300
- },
301
- {
302
- input: ['hello', 'world'],
303
- output: 'hello/world',
304
- },
305
- {
306
- input: ['http://www.google.com/', 'foo/bar', '?test=123'],
307
- output: 'http://www.google.com/foo/bar?test=123',
308
- },
309
- {
310
- input: ['http:', 'www.google.com///', 'foo/bar', '?test=123'],
311
- output: 'http://www.google.com/foo/bar?test=123',
312
- },
313
- {
314
- input: ['http://foobar.com', '', 'test'],
315
- output: 'http://foobar.com/test',
316
- },
317
- {
318
- input: ['http://foobar.com', '', 'test', '/'],
319
- output: 'http://foobar.com/test/',
320
- },
321
- {
322
- input: ['/', '', 'hello', '', '/', '/', '', '/', '/world'],
323
- output: '/hello/world',
324
- },
325
- {
326
- input: ['', '', '/tt', 'ko', 'hello'],
327
- output: '/tt/ko/hello',
328
- },
329
- {
330
- input: ['', '///hello///', '', '///world'],
331
- output: '/hello/world',
332
- },
333
- {
334
- input: ['', '/hello/', ''],
335
- output: '/hello/',
336
- },
337
- {
338
- input: ['', '/', ''],
339
- output: '/',
340
- },
341
- {
342
- input: ['///', '///'],
343
- output: '/',
344
- },
345
- {
346
- input: ['/', '/hello/world/', '///'],
347
- output: '/hello/world/',
348
- },
349
- ];
350
- asserts.forEach((testCase) => {
351
- expect(normalizeUrl(testCase.input)).toBe(testCase.output);
352
- });
353
-
354
- expect(() =>
355
- // @ts-expect-error undefined for test
356
- normalizeUrl(['http:example.com', undefined]),
357
- ).toThrowErrorMatchingInlineSnapshot(
358
- `"Url must be a string. Received undefined"`,
359
- );
360
- });
361
-
362
- test('isValidPathname', () => {
363
- expect(isValidPathname('/')).toBe(true);
364
- expect(isValidPathname('/hey')).toBe(true);
365
- expect(isValidPathname('/hey/ho')).toBe(true);
366
- expect(isValidPathname('/hey/ho/')).toBe(true);
367
- expect(isValidPathname('/hey/h%C3%B4/')).toBe(true);
368
- expect(isValidPathname('/hey///ho///')).toBe(true); // Unexpected but valid
369
- expect(isValidPathname('/hey/héllô you')).toBe(true);
370
-
371
- //
372
- expect(isValidPathname('')).toBe(false);
373
- expect(isValidPathname('hey')).toBe(false);
374
- expect(isValidPathname('/hey?qs=ho')).toBe(false);
375
- expect(isValidPathname('https://fb.com/hey')).toBe(false);
376
- expect(isValidPathname('//hey')).toBe(false);
377
- });
378
- });
379
-
380
- describe('addTrailingSlash', () => {
381
- test('should no-op', () => {
382
- expect(addTrailingSlash('/abcd/')).toEqual('/abcd/');
383
- });
384
- test('should add /', () => {
385
- expect(addTrailingSlash('/abcd')).toEqual('/abcd/');
386
- });
387
- });
388
-
389
- describe('addLeadingSlash', () => {
390
- test('should no-op', () => {
391
- expect(addLeadingSlash('/abc')).toEqual('/abc');
392
- });
393
- test('should add /', () => {
394
- expect(addLeadingSlash('abc')).toEqual('/abc');
395
- });
396
- });
397
-
398
- describe('removeTrailingSlash', () => {
399
- test('should no-op', () => {
400
- expect(removeTrailingSlash('/abcd')).toEqual('/abcd');
401
- });
402
- test('should remove /', () => {
403
- expect(removeTrailingSlash('/abcd/')).toEqual('/abcd');
404
- });
405
- });
406
-
407
- describe('removeSuffix', () => {
408
- test('should no-op 1', () => {
409
- expect(removeSuffix('abcdef', 'ijk')).toEqual('abcdef');
410
- });
411
- test('should no-op 2', () => {
412
- expect(removeSuffix('abcdef', 'abc')).toEqual('abcdef');
413
- });
414
- test('should no-op 3', () => {
415
- expect(removeSuffix('abcdef', '')).toEqual('abcdef');
416
- });
417
- test('should remove suffix', () => {
418
- expect(removeSuffix('abcdef', 'ef')).toEqual('abcd');
419
- });
420
- });
421
-
422
- describe('removePrefix', () => {
423
- test('should no-op 1', () => {
424
- expect(removePrefix('abcdef', 'ijk')).toEqual('abcdef');
425
- });
426
- test('should no-op 2', () => {
427
- expect(removePrefix('abcdef', 'def')).toEqual('abcdef');
428
- });
429
- test('should no-op 3', () => {
430
- expect(removePrefix('abcdef', '')).toEqual('abcdef');
431
- });
432
- test('should remove prefix', () => {
433
- expect(removePrefix('abcdef', 'ab')).toEqual('cdef');
434
- });
435
- });
436
-
437
- describe('getFilePathForRoutePath', () => {
438
- test('works for /', () => {
439
- expect(posixPath(getFilePathForRoutePath('/'))).toEqual('/index.html');
440
- });
441
- test('works for /somePath', () => {
442
- expect(posixPath(getFilePathForRoutePath('/somePath'))).toEqual(
443
- '/somePath/index.html',
444
- );
445
- });
446
- test('works for /somePath/', () => {
447
- expect(posixPath(getFilePathForRoutePath('/somePath/'))).toEqual(
448
- '/somePath/index.html',
449
- );
450
- });
451
- });
452
-
453
- describe('getElementsAround', () => {
454
- test('can return elements around', () => {
455
- expect(getElementsAround(['a', 'b', 'c', 'd'], 0)).toEqual({
456
- previous: undefined,
457
- next: 'b',
458
- });
459
- expect(getElementsAround(['a', 'b', 'c', 'd'], 1)).toEqual({
460
- previous: 'a',
461
- next: 'c',
462
- });
463
- expect(getElementsAround(['a', 'b', 'c', 'd'], 2)).toEqual({
464
- previous: 'b',
465
- next: 'd',
466
- });
467
- expect(getElementsAround(['a', 'b', 'c', 'd'], 3)).toEqual({
468
- previous: 'c',
469
- next: undefined,
470
- });
471
- });
472
-
473
- test('throws if bad index is provided', () => {
474
- expect(() =>
475
- getElementsAround(['a', 'b', 'c', 'd'], -1),
476
- ).toThrowErrorMatchingInlineSnapshot(
477
- `"Valid aroundIndex for array (of size 4) are between 0 and 3, but you provided aroundIndex=-1"`,
478
- );
479
- expect(() =>
480
- getElementsAround(['a', 'b', 'c', 'd'], 4),
481
- ).toThrowErrorMatchingInlineSnapshot(
482
- `"Valid aroundIndex for array (of size 4) are between 0 and 3, but you provided aroundIndex=4"`,
483
- );
484
- });
485
- });
486
-
487
- describe('mergeTranslations', () => {
488
- test('should merge translations', () => {
489
- expect(
490
- mergeTranslations([
491
- {
492
- T1: {message: 'T1 message', description: 'T1 desc'},
493
- T2: {message: 'T2 message', description: 'T2 desc'},
494
- T3: {message: 'T3 message', description: 'T3 desc'},
495
- },
496
- {
497
- T4: {message: 'T4 message', description: 'T4 desc'},
498
- },
499
- {T2: {message: 'T2 message 2', description: 'T2 desc 2'}},
500
- ]),
501
- ).toEqual({
502
- T1: {message: 'T1 message', description: 'T1 desc'},
503
- T2: {message: 'T2 message 2', description: 'T2 desc 2'},
504
- T3: {message: 'T3 message', description: 'T3 desc'},
505
- T4: {message: 'T4 message', description: 'T4 desc'},
506
- });
507
- });
508
- });
509
-
510
- describe('mapAsyncSequencial', () => {
511
- function sleep(timeout: number): Promise<void> {
512
- return new Promise((resolve) => setTimeout(resolve, timeout));
513
- }
514
-
515
- test('map sequentially', async () => {
516
- const itemToTimeout: Record<string, number> = {
517
- '1': 50,
518
- '2': 150,
519
- '3': 100,
520
- };
521
- const items = Object.keys(itemToTimeout);
522
-
523
- const itemMapStartsAt: Record<string, number> = {};
524
- const itemMapEndsAt: Record<string, number> = {};
525
-
526
- const timeBefore = Date.now();
527
- await expect(
528
- mapAsyncSequencial(items, async (item) => {
529
- const itemTimeout = itemToTimeout[item];
530
- itemMapStartsAt[item] = Date.now();
531
- await sleep(itemTimeout);
532
- itemMapEndsAt[item] = Date.now();
533
- return `${item} mapped`;
534
- }),
535
- ).resolves.toEqual(['1 mapped', '2 mapped', '3 mapped']);
536
- const timeAfter = Date.now();
537
-
538
- const timeTotal = timeAfter - timeBefore;
539
-
540
- const totalTimeouts = sum(Object.values(itemToTimeout));
541
- expect(timeTotal > totalTimeouts);
542
-
543
- expect(itemMapStartsAt['1'] > 0);
544
- expect(itemMapStartsAt['2'] > itemMapEndsAt['1']);
545
- expect(itemMapStartsAt['3'] > itemMapEndsAt['2']);
546
- });
547
- });
548
-
549
- describe('findAsyncSequencial', () => {
550
- function sleep(timeout: number): Promise<void> {
551
- return new Promise((resolve) => setTimeout(resolve, timeout));
552
- }
553
-
554
- test('find sequentially', async () => {
555
- const items = ['1', '2', '3'];
556
-
557
- const findFn = jest.fn(async (item: string) => {
558
- await sleep(50);
559
- return item === '2';
560
- });
561
-
562
- const timeBefore = Date.now();
563
- await expect(findAsyncSequential(items, findFn)).resolves.toEqual('2');
564
- const timeAfter = Date.now();
565
-
566
- expect(findFn).toHaveBeenCalledTimes(2);
567
- expect(findFn).toHaveBeenNthCalledWith(1, '1');
568
- expect(findFn).toHaveBeenNthCalledWith(2, '2');
569
-
570
- const timeTotal = timeAfter - timeBefore;
571
- expect(timeTotal > 100);
572
- expect(timeTotal < 150);
573
- });
574
- });
575
-
576
- describe('findFolderContainingFile', () => {
577
- test('find appropriate folder', async () => {
578
- await expect(
579
- findFolderContainingFile(
580
- ['/abcdef', '/gehij', __dirname, '/klmn'],
581
- 'index.test.ts',
582
- ),
583
- ).resolves.toEqual(__dirname);
584
- });
585
-
586
- test('return undefined if no folder contain such file', async () => {
587
- await expect(
588
- findFolderContainingFile(['/abcdef', '/gehij', '/klmn'], 'index.test.ts'),
589
- ).resolves.toBeUndefined();
590
- });
591
- });
592
-
593
- describe('getFolderContainingFile', () => {
594
- test('get appropriate folder', async () => {
595
- await expect(
596
- getFolderContainingFile(
597
- ['/abcdef', '/gehij', __dirname, '/klmn'],
598
- 'index.test.ts',
599
- ),
600
- ).resolves.toEqual(__dirname);
601
- });
602
-
603
- test('throw if no folder contain such file', async () => {
604
- await expect(
605
- getFolderContainingFile(['/abcdef', '/gehij', '/klmn'], 'index.test.ts'),
606
- ).rejects.toThrowErrorMatchingSnapshot();
607
- });
608
- });
609
-
610
- describe('updateTranslationFileMessages', () => {
611
- test('should update messages', () => {
612
- expect(
613
- updateTranslationFileMessages(
614
- {
615
- path: 'abc',
616
- content: {
617
- t1: {message: 't1 message', description: 't1 desc'},
618
- t2: {message: 't2 message', description: 't2 desc'},
619
- t3: {message: 't3 message', description: 't3 desc'},
620
- },
621
- },
622
- (message) => `prefix ${message} suffix`,
623
- ),
624
- ).toEqual({
625
- path: 'abc',
626
- content: {
627
- t1: {message: 'prefix t1 message suffix', description: 't1 desc'},
628
- t2: {message: 'prefix t2 message suffix', description: 't2 desc'},
629
- t3: {message: 'prefix t3 message suffix', description: 't3 desc'},
630
- },
631
- });
632
- });
633
- });
634
-
635
- describe('parseMarkdownHeadingId', () => {
636
- test('can parse simple heading without id', () => {
637
- expect(parseMarkdownHeadingId('## Some heading')).toEqual({
638
- text: '## Some heading',
639
- id: undefined,
640
- });
641
- });
642
-
643
- test('can parse simple heading with id', () => {
644
- expect(parseMarkdownHeadingId('## Some heading {#custom-_id}')).toEqual({
645
- text: '## Some heading',
646
- id: 'custom-_id',
647
- });
648
- });
649
-
650
- test('can parse heading not ending with the id', () => {
651
- expect(parseMarkdownHeadingId('## {#custom-_id} Some heading')).toEqual({
652
- text: '## {#custom-_id} Some heading',
653
- id: undefined,
654
- });
655
- });
656
-
657
- test('can parse heading with multiple id', () => {
658
- expect(parseMarkdownHeadingId('## Some heading {#id1} {#id2}')).toEqual({
659
- text: '## Some heading {#id1}',
660
- id: 'id2',
661
- });
662
- });
663
-
664
- test('can parse heading with link and id', () => {
665
- expect(
666
- parseMarkdownHeadingId(
667
- '## Some heading [facebook](https://facebook.com) {#id}',
668
- ),
669
- ).toEqual({
670
- text: '## Some heading [facebook](https://facebook.com)',
671
- id: 'id',
672
- });
673
- });
674
-
675
- test('can parse heading with only id', () => {
676
- expect(parseMarkdownHeadingId('## {#id}')).toEqual({
677
- text: '##',
678
- id: 'id',
679
- });
680
- });
681
- });