cspell-dictionary 6.28.0 → 6.29.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 (127) hide show
  1. package/dist/{SpellingDictionary → cjs/SpellingDictionary}/CachingDictionary.d.ts +3 -3
  2. package/dist/{SpellingDictionary → cjs/SpellingDictionary}/CachingDictionary.js +9 -9
  3. package/dist/{SpellingDictionary → cjs/SpellingDictionary}/FlagWordsDictionary.d.ts +1 -1
  4. package/dist/{SpellingDictionary → cjs/SpellingDictionary}/FlagWordsDictionary.js +11 -11
  5. package/dist/{SpellingDictionary → cjs/SpellingDictionary}/IgnoreWordsDictionary.d.ts +1 -1
  6. package/dist/{SpellingDictionary → cjs/SpellingDictionary}/IgnoreWordsDictionary.js +5 -5
  7. package/dist/{SpellingDictionary → cjs/SpellingDictionary}/SpellingDictionaryCollection.d.ts +1 -1
  8. package/dist/{SpellingDictionary → cjs/SpellingDictionary}/SpellingDictionaryCollection.js +12 -12
  9. package/dist/{SpellingDictionary → cjs/SpellingDictionary}/SpellingDictionaryFromTrie.d.ts +1 -1
  10. package/dist/{SpellingDictionary → cjs/SpellingDictionary}/SpellingDictionaryFromTrie.js +16 -16
  11. package/dist/{SpellingDictionary → cjs/SpellingDictionary}/SpellingDictionaryMethods.d.ts +1 -1
  12. package/dist/{SpellingDictionary → cjs/SpellingDictionary}/SpellingDictionaryMethods.js +5 -5
  13. package/dist/{SpellingDictionary → cjs/SpellingDictionary}/SuggestDictionary.d.ts +2 -2
  14. package/dist/{SpellingDictionary → cjs/SpellingDictionary}/SuggestDictionary.js +9 -9
  15. package/dist/{SpellingDictionary → cjs/SpellingDictionary}/Terms/index.d.ts +1 -1
  16. package/dist/cjs/SpellingDictionary/Typos/index.d.ts +4 -0
  17. package/dist/{SpellingDictionary → cjs/SpellingDictionary}/Typos/index.js +7 -7
  18. package/dist/{SpellingDictionary → cjs/SpellingDictionary}/Typos/typos.d.ts +1 -1
  19. package/dist/{SpellingDictionary → cjs/SpellingDictionary}/Typos/typosParser.d.ts +1 -1
  20. package/dist/{SpellingDictionary → cjs/SpellingDictionary}/Typos/typosParser.js +6 -6
  21. package/dist/{SpellingDictionary → cjs/SpellingDictionary}/Typos/util.d.ts +1 -1
  22. package/dist/{SpellingDictionary → cjs/SpellingDictionary}/TyposDictionary.d.ts +2 -2
  23. package/dist/{SpellingDictionary → cjs/SpellingDictionary}/TyposDictionary.js +10 -10
  24. package/dist/{SpellingDictionary → cjs/SpellingDictionary}/createInlineSpellingDictionary.d.ts +1 -1
  25. package/dist/cjs/SpellingDictionary/createInlineSpellingDictionary.js +25 -0
  26. package/dist/{SpellingDictionary → cjs/SpellingDictionary}/createSpellingDictionary.d.ts +2 -2
  27. package/dist/{SpellingDictionary → cjs/SpellingDictionary}/createSpellingDictionary.js +9 -9
  28. package/dist/{SpellingDictionary → cjs/SpellingDictionary}/index.d.ts +10 -10
  29. package/dist/{SpellingDictionary → cjs/SpellingDictionary}/index.js +20 -20
  30. package/dist/{index.d.ts → cjs/index.d.ts} +2 -2
  31. package/dist/{index.js → cjs/index.js} +11 -11
  32. package/dist/{util → cjs/util}/clean.d.ts +1 -1
  33. package/dist/{util → cjs/util}/repMap.js +4 -4
  34. package/dist/{util → cjs/util}/textMappers.js +2 -2
  35. package/dist/esm/SpellingDictionary/CachingDictionary.d.mts +30 -0
  36. package/dist/esm/SpellingDictionary/CachingDictionary.mjs +45 -0
  37. package/dist/esm/SpellingDictionary/FlagWordsDictionary.d.mts +11 -0
  38. package/dist/esm/SpellingDictionary/FlagWordsDictionary.mjs +134 -0
  39. package/dist/esm/SpellingDictionary/IgnoreWordsDictionary.d.mts +11 -0
  40. package/dist/esm/SpellingDictionary/IgnoreWordsDictionary.mjs +96 -0
  41. package/dist/esm/SpellingDictionary/SpellingDictionary.d.mts +150 -0
  42. package/dist/esm/SpellingDictionary/SpellingDictionary.mjs +4 -0
  43. package/dist/esm/SpellingDictionary/SpellingDictionaryCollection.d.mts +16 -0
  44. package/dist/esm/SpellingDictionary/SpellingDictionaryCollection.mjs +111 -0
  45. package/dist/esm/SpellingDictionary/SpellingDictionaryFromTrie.d.mts +50 -0
  46. package/dist/esm/SpellingDictionary/SpellingDictionaryFromTrie.mjs +172 -0
  47. package/dist/esm/SpellingDictionary/SpellingDictionaryMethods.d.mts +26 -0
  48. package/dist/esm/SpellingDictionary/SpellingDictionaryMethods.mjs +96 -0
  49. package/dist/esm/SpellingDictionary/SuggestDictionary.d.mts +20 -0
  50. package/dist/esm/SpellingDictionary/SuggestDictionary.mjs +97 -0
  51. package/dist/esm/SpellingDictionary/Terms/index.d.mts +2 -0
  52. package/dist/esm/SpellingDictionary/Terms/index.mjs +1 -0
  53. package/dist/esm/SpellingDictionary/Terms/terms.d.mts +12 -0
  54. package/dist/esm/SpellingDictionary/Terms/terms.mjs +1 -0
  55. package/dist/esm/SpellingDictionary/Typos/index.d.mts +4 -0
  56. package/dist/esm/SpellingDictionary/Typos/index.mjs +2 -0
  57. package/dist/esm/SpellingDictionary/Typos/typos.d.mts +18 -0
  58. package/dist/esm/SpellingDictionary/Typos/typos.mjs +1 -0
  59. package/dist/esm/SpellingDictionary/Typos/typosParser.d.mts +34 -0
  60. package/dist/esm/SpellingDictionary/Typos/typosParser.mjs +131 -0
  61. package/dist/esm/SpellingDictionary/Typos/util.d.mts +31 -0
  62. package/dist/esm/SpellingDictionary/Typos/util.mjs +105 -0
  63. package/dist/esm/SpellingDictionary/TyposDictionary.d.mts +21 -0
  64. package/dist/esm/SpellingDictionary/TyposDictionary.mjs +129 -0
  65. package/dist/esm/SpellingDictionary/createInlineSpellingDictionary.d.mts +3 -0
  66. package/dist/esm/SpellingDictionary/createInlineSpellingDictionary.mjs +20 -0
  67. package/dist/esm/SpellingDictionary/createSpellingDictionary.d.mts +23 -0
  68. package/dist/esm/SpellingDictionary/createSpellingDictionary.mjs +68 -0
  69. package/dist/esm/SpellingDictionary/defaults.d.mts +3 -0
  70. package/dist/esm/SpellingDictionary/defaults.mjs +2 -0
  71. package/dist/esm/SpellingDictionary/index.d.mts +11 -0
  72. package/dist/esm/SpellingDictionary/index.mjs +9 -0
  73. package/dist/esm/index.d.mts +3 -0
  74. package/dist/esm/index.mjs +1 -0
  75. package/dist/esm/util/AutoCache.d.mts +25 -0
  76. package/dist/esm/util/AutoCache.mjs +67 -0
  77. package/dist/esm/util/AutoResolve.d.mts +21 -0
  78. package/dist/esm/util/AutoResolve.mjs +52 -0
  79. package/dist/esm/util/IterableLike.d.mts +4 -0
  80. package/dist/esm/util/IterableLike.mjs +1 -0
  81. package/dist/esm/util/clean.d.mts +7 -0
  82. package/dist/esm/util/clean.mjs +13 -0
  83. package/dist/esm/util/regexHelper.d.mts +7 -0
  84. package/dist/esm/util/regexHelper.mjs +8 -0
  85. package/dist/esm/util/repMap.d.mts +27 -0
  86. package/dist/esm/util/repMap.mjs +152 -0
  87. package/dist/esm/util/simpleCache.d.mts +46 -0
  88. package/dist/esm/util/simpleCache.mjs +135 -0
  89. package/dist/esm/util/text.d.mts +10 -0
  90. package/dist/esm/util/text.mjs +46 -0
  91. package/dist/esm/util/textMappers.d.mts +3 -0
  92. package/dist/esm/util/textMappers.mjs +15 -0
  93. package/dist/esm/util/types.d.mts +7 -0
  94. package/dist/esm/util/types.mjs +1 -0
  95. package/dist/esm/util/util.d.mts +2 -0
  96. package/dist/esm/util/util.mjs +3 -0
  97. package/package.json +27 -18
  98. package/dist/SpellingDictionary/Typos/index.d.ts +0 -4
  99. package/dist/SpellingDictionary/createInlineSpellingDictionary.js +0 -25
  100. /package/dist/{SpellingDictionary → cjs/SpellingDictionary}/SpellingDictionary.d.ts +0 -0
  101. /package/dist/{SpellingDictionary → cjs/SpellingDictionary}/SpellingDictionary.js +0 -0
  102. /package/dist/{SpellingDictionary → cjs/SpellingDictionary}/Terms/index.js +0 -0
  103. /package/dist/{SpellingDictionary → cjs/SpellingDictionary}/Terms/terms.d.ts +0 -0
  104. /package/dist/{SpellingDictionary → cjs/SpellingDictionary}/Terms/terms.js +0 -0
  105. /package/dist/{SpellingDictionary → cjs/SpellingDictionary}/Typos/typos.js +0 -0
  106. /package/dist/{SpellingDictionary → cjs/SpellingDictionary}/Typos/util.js +0 -0
  107. /package/dist/{SpellingDictionary → cjs/SpellingDictionary}/defaults.d.ts +0 -0
  108. /package/dist/{SpellingDictionary → cjs/SpellingDictionary}/defaults.js +0 -0
  109. /package/dist/{util → cjs/util}/AutoCache.d.ts +0 -0
  110. /package/dist/{util → cjs/util}/AutoCache.js +0 -0
  111. /package/dist/{util → cjs/util}/AutoResolve.d.ts +0 -0
  112. /package/dist/{util → cjs/util}/AutoResolve.js +0 -0
  113. /package/dist/{util → cjs/util}/IterableLike.d.ts +0 -0
  114. /package/dist/{util → cjs/util}/IterableLike.js +0 -0
  115. /package/dist/{util → cjs/util}/clean.js +0 -0
  116. /package/dist/{util → cjs/util}/regexHelper.d.ts +0 -0
  117. /package/dist/{util → cjs/util}/regexHelper.js +0 -0
  118. /package/dist/{util → cjs/util}/repMap.d.ts +0 -0
  119. /package/dist/{util → cjs/util}/simpleCache.d.ts +0 -0
  120. /package/dist/{util → cjs/util}/simpleCache.js +0 -0
  121. /package/dist/{util → cjs/util}/text.d.ts +0 -0
  122. /package/dist/{util → cjs/util}/text.js +0 -0
  123. /package/dist/{util → cjs/util}/textMappers.d.ts +0 -0
  124. /package/dist/{util → cjs/util}/types.d.ts +0 -0
  125. /package/dist/{util → cjs/util}/types.js +0 -0
  126. /package/dist/{util → cjs/util}/util.d.ts +0 -0
  127. /package/dist/{util → cjs/util}/util.js +0 -0
@@ -0,0 +1,68 @@
1
+ import { buildTrieFast, parseDictionaryLines } from 'cspell-trie-lib';
2
+ import { deepEqual } from 'fast-equals';
3
+ import { AutoWeakCache, SimpleCache } from '../util/simpleCache.mjs';
4
+ import { defaultOptions } from './SpellingDictionary.mjs';
5
+ import { SpellingDictionaryFromTrie } from './SpellingDictionaryFromTrie.mjs';
6
+ import { createWeightMapFromDictionaryInformation } from './SpellingDictionaryMethods.mjs';
7
+ const cachedDictionaries = new AutoWeakCache(_createSpellingDictionary, 64);
8
+ const maxSetSize = 3;
9
+ const cachedParamsByWordList = new SimpleCache(64);
10
+ /**
11
+ * Create a SpellingDictionary
12
+ * @param wordList - list of words
13
+ * @param name - name of dictionary
14
+ * @param source - filename or uri
15
+ * @param options - dictionary options
16
+ * @returns a Spelling Dictionary
17
+ */
18
+ export function createSpellingDictionary(wordList, name, source, options) {
19
+ const params = [wordList, name, source, options];
20
+ if (!Array.isArray(wordList)) {
21
+ return _createSpellingDictionary(params);
22
+ }
23
+ const cached = cachedParamsByWordList.get(name) || new Set();
24
+ for (const cachedParams of cached) {
25
+ if (deepEqual(params, cachedParams)) {
26
+ return cachedDictionaries.get(cachedParams);
27
+ }
28
+ }
29
+ if (cached.size > maxSetSize)
30
+ cached.clear();
31
+ cached.add(params);
32
+ cachedParamsByWordList.set(name, cached);
33
+ return cachedDictionaries.get(params);
34
+ }
35
+ function _createSpellingDictionary(params) {
36
+ const [wordList, name, source, options] = params;
37
+ // console.log(`createSpellingDictionary ${name} ${source}`);
38
+ const parseOptions = { stripCaseAndAccents: options?.supportNonStrictSearches ?? true };
39
+ const words = parseDictionaryLines(wordList, parseOptions);
40
+ const trie = buildTrieFast(words);
41
+ const opts = { ...(options || defaultOptions) };
42
+ if (opts.weightMap === undefined && opts.dictionaryInformation) {
43
+ opts.weightMap = createWeightMapFromDictionaryInformation(opts.dictionaryInformation);
44
+ }
45
+ return new SpellingDictionaryFromTrie(trie, name, opts, source);
46
+ }
47
+ export function createFailedToLoadDictionary(name, source, error, options) {
48
+ options = options || {};
49
+ return {
50
+ name,
51
+ source,
52
+ type: 'error',
53
+ containsNoSuggestWords: false,
54
+ has: () => false,
55
+ find: () => undefined,
56
+ isNoSuggestWord: () => false,
57
+ isForbidden: () => false,
58
+ suggest: () => [],
59
+ mapWord: (a) => a,
60
+ genSuggestions: () => {
61
+ return;
62
+ },
63
+ size: 0,
64
+ options,
65
+ isDictionaryCaseSensitive: false,
66
+ getErrors: () => [error],
67
+ };
68
+ }
@@ -0,0 +1,3 @@
1
+ export declare const ignoreCase: true;
2
+ export declare const isForbiddenIgnoreCaseAndAccents: false;
3
+ //# sourceMappingURL=defaults.d.mts.map
@@ -0,0 +1,2 @@
1
+ export const ignoreCase = true;
2
+ export const isForbiddenIgnoreCaseAndAccents = false;
@@ -0,0 +1,11 @@
1
+ export { CachingDictionary, createCachingDictionary } from './CachingDictionary.mjs';
2
+ export { createInlineSpellingDictionary } from './createInlineSpellingDictionary.mjs';
3
+ export { createFailedToLoadDictionary, createSpellingDictionary } from './createSpellingDictionary.mjs';
4
+ export { createFlagWordsDictionary, createFlagWordsDictionary as createForbiddenWordsDictionary, } from './FlagWordsDictionary.mjs';
5
+ export { createIgnoreWordsDictionary } from './IgnoreWordsDictionary.mjs';
6
+ export type { DictionaryDefinitionInline, FindOptions, FindResult, HasOptions, SearchOptions, SpellingDictionary, SpellingDictionaryOptions, SuggestionCollector, SuggestionResult, SuggestOptions, } from './SpellingDictionary.mjs';
7
+ export { createCollection, SpellingDictionaryCollection } from './SpellingDictionaryCollection.mjs';
8
+ export { createSpellingDictionaryFromTrieFile } from './SpellingDictionaryFromTrie.mjs';
9
+ export { createSuggestDictionary } from './SuggestDictionary.mjs';
10
+ export { createTyposDictionary } from './TyposDictionary.mjs';
11
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1,9 @@
1
+ export { createCachingDictionary } from './CachingDictionary.mjs';
2
+ export { createInlineSpellingDictionary } from './createInlineSpellingDictionary.mjs';
3
+ export { createFailedToLoadDictionary, createSpellingDictionary } from './createSpellingDictionary.mjs';
4
+ export { createFlagWordsDictionary, createFlagWordsDictionary as createForbiddenWordsDictionary, } from './FlagWordsDictionary.mjs';
5
+ export { createIgnoreWordsDictionary } from './IgnoreWordsDictionary.mjs';
6
+ export { createCollection } from './SpellingDictionaryCollection.mjs';
7
+ export { createSpellingDictionaryFromTrieFile } from './SpellingDictionaryFromTrie.mjs';
8
+ export { createSuggestDictionary } from './SuggestDictionary.mjs';
9
+ export { createTyposDictionary } from './TyposDictionary.mjs';
@@ -0,0 +1,3 @@
1
+ export type { CachingDictionary, FindOptions, FindResult, HasOptions, SearchOptions, SpellingDictionary, SpellingDictionaryCollection, SpellingDictionaryOptions, SuggestionCollector, SuggestionResult, SuggestOptions, } from './SpellingDictionary/index.mjs';
2
+ export { createCachingDictionary, createCollection, createFailedToLoadDictionary, createFlagWordsDictionary, createForbiddenWordsDictionary, createIgnoreWordsDictionary, createInlineSpellingDictionary, createSpellingDictionary, createSpellingDictionaryFromTrieFile, createSuggestDictionary, } from './SpellingDictionary/index.mjs';
3
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ export { createCachingDictionary, createCollection, createFailedToLoadDictionary, createFlagWordsDictionary, createForbiddenWordsDictionary, createIgnoreWordsDictionary, createInlineSpellingDictionary, createSpellingDictionary, createSpellingDictionaryFromTrieFile, createSuggestDictionary, } from './SpellingDictionary/index.mjs';
@@ -0,0 +1,25 @@
1
+ interface AutoCache<R> extends CacheStats {
2
+ (word: string): R;
3
+ }
4
+ export interface CacheStats {
5
+ hits: number;
6
+ misses: number;
7
+ swaps: number;
8
+ }
9
+ declare class Cache01<R> implements CacheStats {
10
+ readonly maxSize: number;
11
+ private count;
12
+ private cache0;
13
+ private cache1;
14
+ hits: number;
15
+ misses: number;
16
+ swaps: number;
17
+ constructor(maxSize: number);
18
+ get(key: string): R | undefined;
19
+ set(key: string, value: R): this;
20
+ }
21
+ export declare function createCache01<R>(size: number): Cache01<R>;
22
+ export declare function autoCache<R>(fn: (p: string) => R, size?: number): AutoCache<R>;
23
+ export declare function extractStats(ac: AutoCache<unknown> | CacheStats): CacheStats;
24
+ export {};
25
+ //# sourceMappingURL=AutoCache.d.mts.map
@@ -0,0 +1,67 @@
1
+ const CACHE_SIZE = 100;
2
+ class Cache01 {
3
+ constructor(maxSize) {
4
+ this.maxSize = maxSize;
5
+ this.count = 0;
6
+ this.cache0 = Object.create(null);
7
+ this.cache1 = Object.create(null);
8
+ this.hits = 0;
9
+ this.misses = 0;
10
+ this.swaps = 0;
11
+ }
12
+ get(key) {
13
+ const cache0 = this.cache0;
14
+ const cache1 = this.cache1;
15
+ if (key in cache0) {
16
+ ++this.hits;
17
+ return cache0[key];
18
+ }
19
+ if (key in cache1) {
20
+ ++this.hits;
21
+ ++this.count;
22
+ const r = cache1[key];
23
+ cache0[key] = r;
24
+ return r;
25
+ }
26
+ ++this.misses;
27
+ return undefined;
28
+ }
29
+ set(key, value) {
30
+ if (this.count >= this.maxSize) {
31
+ this.cache1 = this.cache0;
32
+ this.cache0 = Object.create(null);
33
+ this.swaps++;
34
+ this.count = 0;
35
+ }
36
+ ++this.count;
37
+ this.cache0[key] = value;
38
+ return this;
39
+ }
40
+ }
41
+ export function createCache01(size) {
42
+ return new Cache01(size);
43
+ }
44
+ export function autoCache(fn, size = CACHE_SIZE) {
45
+ const cache = createCache01(size);
46
+ const ac = get;
47
+ ac.hits = 0;
48
+ ac.misses = 0;
49
+ ac.swaps = 0;
50
+ function get(k) {
51
+ const f = cache.get(k);
52
+ if (f !== undefined) {
53
+ ++ac.hits;
54
+ return f;
55
+ }
56
+ const r = fn(k);
57
+ cache.set(k, r);
58
+ ac.swaps = cache.swaps;
59
+ ++ac.misses;
60
+ return r;
61
+ }
62
+ return ac;
63
+ }
64
+ export function extractStats(ac) {
65
+ const { hits, misses, swaps } = ac;
66
+ return { hits, misses, swaps };
67
+ }
@@ -0,0 +1,21 @@
1
+ export declare function autoResolve<K, V>(map: Map<K, V>, key: K, resolve: (k: K) => V): V;
2
+ export declare class AutoResolveCache<K, V> {
3
+ readonly map: Map<K, V>;
4
+ get(k: K): V | undefined;
5
+ get(k: K, resolve: (k: K) => V): V;
6
+ get(k: K, resolve?: (k: K) => V): V | undefined;
7
+ has(k: K): boolean;
8
+ set(k: K, v: V): this;
9
+ }
10
+ export declare function createAutoResolveCache<K, V>(): AutoResolveCache<K, V>;
11
+ export declare function autoResolveWeak<K extends object, V>(map: WeakMap<K, V>, key: K, resolve: (k: K) => V): V;
12
+ export declare class AutoResolveWeakCache<K extends object, V> {
13
+ readonly map: WeakMap<K, V>;
14
+ get(k: K): V | undefined;
15
+ get(k: K, resolve: (k: K) => V): V;
16
+ get(k: K, resolve?: (k: K) => V): V | undefined;
17
+ has(k: K): boolean;
18
+ set(k: K, v: V): this;
19
+ }
20
+ export declare function createAutoResolveWeakCache<K extends object, V>(): AutoResolveWeakCache<K, V>;
21
+ //# sourceMappingURL=AutoResolve.d.mts.map
@@ -0,0 +1,52 @@
1
+ export function autoResolve(map, key, resolve) {
2
+ const found = map.get(key);
3
+ if (found !== undefined || map.has(key))
4
+ return found;
5
+ const value = resolve(key);
6
+ map.set(key, value);
7
+ return value;
8
+ }
9
+ export class AutoResolveCache {
10
+ constructor() {
11
+ this.map = new Map();
12
+ }
13
+ get(k, resolve) {
14
+ return resolve ? autoResolve(this.map, k, resolve) : this.map.get(k);
15
+ }
16
+ has(k) {
17
+ return this.map.has(k);
18
+ }
19
+ set(k, v) {
20
+ this.map.set(k, v);
21
+ return this;
22
+ }
23
+ }
24
+ export function createAutoResolveCache() {
25
+ return new AutoResolveCache();
26
+ }
27
+ export function autoResolveWeak(map, key, resolve) {
28
+ const found = map.get(key);
29
+ if (found !== undefined || map.has(key))
30
+ return found;
31
+ const value = resolve(key);
32
+ map.set(key, value);
33
+ return value;
34
+ }
35
+ export class AutoResolveWeakCache {
36
+ constructor() {
37
+ this.map = new WeakMap();
38
+ }
39
+ get(k, resolve) {
40
+ return resolve ? autoResolveWeak(this.map, k, resolve) : this.map.get(k);
41
+ }
42
+ has(k) {
43
+ return this.map.has(k);
44
+ }
45
+ set(k, v) {
46
+ this.map.set(k, v);
47
+ return this;
48
+ }
49
+ }
50
+ export function createAutoResolveWeakCache() {
51
+ return new AutoResolveWeakCache();
52
+ }
@@ -0,0 +1,4 @@
1
+ export interface IterableLike<T> {
2
+ [Symbol.iterator]: () => Iterator<T> | IterableIterator<T>;
3
+ }
4
+ //# sourceMappingURL=IterableLike.d.mts.map
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,7 @@
1
+ import type { RemoveUndefined } from './types.mjs';
2
+ /**
3
+ * Delete all `undefined` fields from an object.
4
+ * @param src - object to be cleaned
5
+ */
6
+ export declare function clean<T extends object>(src: T): RemoveUndefined<T>;
7
+ //# sourceMappingURL=clean.d.mts.map
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Delete all `undefined` fields from an object.
3
+ * @param src - object to be cleaned
4
+ */
5
+ export function clean(src) {
6
+ const r = src;
7
+ for (const key of Object.keys(r)) {
8
+ if (r[key] === undefined) {
9
+ delete r[key];
10
+ }
11
+ }
12
+ return r;
13
+ }
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Escape a string so it can be used as an exact match within a RegExp.
3
+ * @param s - string to escape
4
+ * @returns - the escaped string.
5
+ */
6
+ export declare function escapeRegEx(s: string): string;
7
+ //# sourceMappingURL=regexHelper.d.mts.map
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Escape a string so it can be used as an exact match within a RegExp.
3
+ * @param s - string to escape
4
+ * @returns - the escaped string.
5
+ */
6
+ export function escapeRegEx(s) {
7
+ return s.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&').replace(/-/g, '\\x2d');
8
+ }
@@ -0,0 +1,27 @@
1
+ import type { CharacterSet, ReplaceMap } from '@cspell/cspell-types';
2
+ export type ReplaceMapper = (src: string) => string;
3
+ export declare function createMapper(repMap: ReplaceMap | undefined, ignoreCharset?: string): ReplaceMapper;
4
+ declare function charsetToRepMapRegEx(charset: CharacterSet | undefined, replaceWith?: string): ReplaceMap | undefined;
5
+ declare function createMapperRegExp(repMap: ReplaceMap): RegExp;
6
+ interface RepTrieNode {
7
+ rep?: string[];
8
+ children?: Record<string, RepTrieNode>;
9
+ }
10
+ interface Edit {
11
+ b: number;
12
+ e: number;
13
+ r: string;
14
+ }
15
+ export declare function createRepMapper(repMap: ReplaceMap | undefined, ignoreCharset?: string): (word: string) => string[];
16
+ declare function applyEdits(word: string, edits: Edit[]): string[];
17
+ declare function calcAllEdits(root: RepTrieNode, word: string): Edit[];
18
+ declare function createTrie(repMap: ReplaceMap | undefined, ignoreCharset?: string): RepTrieNode;
19
+ export declare const __testing__: {
20
+ charsetToRepMap: typeof charsetToRepMapRegEx;
21
+ createMapperRegExp: typeof createMapperRegExp;
22
+ createTrie: typeof createTrie;
23
+ calcAllEdits: typeof calcAllEdits;
24
+ applyEdits: typeof applyEdits;
25
+ };
26
+ export {};
27
+ //# sourceMappingURL=repMap.d.mts.map
@@ -0,0 +1,152 @@
1
+ import { expandCharacterSet } from 'cspell-trie-lib';
2
+ import { escapeRegEx } from './regexHelper.mjs';
3
+ import { isDefined } from './util.mjs';
4
+ export function createMapper(repMap, ignoreCharset) {
5
+ if (!repMap && !ignoreCharset)
6
+ return (a) => a;
7
+ repMap = repMap || [];
8
+ const charsetMap = charsetToRepMapRegEx(ignoreCharset);
9
+ if (charsetMap) {
10
+ repMap = repMap.concat(charsetMap);
11
+ }
12
+ const filteredMap = repMap.filter(([match, _]) => !!match);
13
+ if (!filteredMap.length) {
14
+ return (a) => a;
15
+ }
16
+ const regEx = createMapperRegExp(repMap);
17
+ const values = repMap.filter(([match, _]) => !!match).map(([_, into]) => into);
18
+ function resolve(m, ...matches) {
19
+ const index = matches.findIndex((a) => !!a);
20
+ return 0 <= index && index < values.length ? values[index] : m;
21
+ }
22
+ return function (s) {
23
+ return s.replace(regEx, resolve);
24
+ };
25
+ }
26
+ function charsetToRepMapRegEx(charset, replaceWith = '') {
27
+ if (!charset)
28
+ return undefined;
29
+ return charset
30
+ .split('|')
31
+ .map((chars) => `[${chars.replace(/[\][\\]/g, '\\$&')}]`)
32
+ .map((map) => [map, replaceWith]);
33
+ }
34
+ function charsetToRepMap(charset, replaceWith = '') {
35
+ if (!charset)
36
+ return undefined;
37
+ return charset
38
+ .split('|')
39
+ .flatMap((chars) => [...expandCharacterSet(chars)])
40
+ .map((char) => [char, replaceWith]);
41
+ }
42
+ function expandReplaceMap(repMap) {
43
+ return repMap.flatMap(([from, replaceWith]) => from.split('|').map((w) => [w, replaceWith]));
44
+ }
45
+ function createMapperRegExp(repMap) {
46
+ const filteredMap = repMap.filter(([match, _]) => !!match);
47
+ if (!filteredMap.length) {
48
+ return /$^/;
49
+ }
50
+ const regExStr = filteredMap
51
+ .map(([from, _]) => from)
52
+ // make sure it compiles into a regex
53
+ .map((s) => {
54
+ try {
55
+ // fix up any nested ()
56
+ const r = s.match(/\(/) ? s.replace(/\((?=.*\))/g, '(?:').replace(/\(\?:\?/g, '(?') : s;
57
+ new RegExp(r);
58
+ s = r;
59
+ }
60
+ catch (err) {
61
+ return escapeRegEx(s);
62
+ }
63
+ return s;
64
+ })
65
+ .map((s) => `(${s})`)
66
+ .join('|');
67
+ const regEx = new RegExp(regExStr, 'g');
68
+ return regEx;
69
+ }
70
+ export function createRepMapper(repMap, ignoreCharset) {
71
+ if (!repMap && !ignoreCharset)
72
+ return (word) => [word];
73
+ const trie = createTrie(repMap, ignoreCharset);
74
+ // const root = createTrie(repMap, ignoreCharset);
75
+ return (word) => {
76
+ const edits = calcAllEdits(trie, word);
77
+ return applyEdits(word, edits);
78
+ };
79
+ }
80
+ function applyEdits(word, edits) {
81
+ if (!edits.length)
82
+ return [word];
83
+ // Prepare
84
+ const letterEdits = [];
85
+ for (let i = 0; i < word.length; ++i) {
86
+ letterEdits[i] = { edits: [{ b: i, e: i + 1, r: word[i] }], suffixes: [] };
87
+ }
88
+ letterEdits[word.length] = { edits: [], suffixes: [''] };
89
+ // Add edits
90
+ for (const edit of edits) {
91
+ const le = letterEdits[edit.b];
92
+ le.edits.push(edit);
93
+ }
94
+ // Apply edits in reverse
95
+ for (let i = word.length - 1; i >= 0; --i) {
96
+ const le = letterEdits[i];
97
+ const sfx = le.suffixes;
98
+ for (const edit of le.edits) {
99
+ const pfx = edit.r;
100
+ const nSfx = letterEdits[edit.e].suffixes;
101
+ for (const s of nSfx) {
102
+ sfx.push(pfx + s);
103
+ }
104
+ }
105
+ }
106
+ const results = new Set(letterEdits[0].suffixes);
107
+ return [...results];
108
+ }
109
+ function calcAllEdits(root, word) {
110
+ const edits = [];
111
+ function walk(node, b, e) {
112
+ if (node.rep) {
113
+ node.rep.forEach((r) => edits.push({ b, e, r }));
114
+ }
115
+ if (e === word.length || !node.children)
116
+ return;
117
+ const n = node.children[word[e]];
118
+ if (!n)
119
+ return;
120
+ walk(n, b, e + 1);
121
+ }
122
+ for (let i = 0; i < word.length; ++i) {
123
+ walk(root, i, i);
124
+ }
125
+ return edits;
126
+ }
127
+ function createTrie(repMap, ignoreCharset) {
128
+ const combined = [repMap, charsetToRepMap(ignoreCharset)].filter(isDefined).flatMap((a) => a);
129
+ const expanded = expandReplaceMap(combined);
130
+ const trieRoot = Object.create(null);
131
+ expanded.forEach(([match, replaceWith]) => addToTrie(trieRoot, match, replaceWith));
132
+ return trieRoot;
133
+ }
134
+ function addToTrie(node, match, replaceWith) {
135
+ while (match) {
136
+ const children = node.children || (node.children = Object.create(null));
137
+ const k = match[0];
138
+ const childNode = children[k] || (children[k] = Object.create(null));
139
+ node = childNode;
140
+ match = match.slice(1);
141
+ }
142
+ const s = new Set(node.rep || []);
143
+ s.add(replaceWith);
144
+ node.rep = [...s];
145
+ }
146
+ export const __testing__ = {
147
+ charsetToRepMap: charsetToRepMapRegEx,
148
+ createMapperRegExp,
149
+ createTrie,
150
+ calcAllEdits,
151
+ applyEdits,
152
+ };
@@ -0,0 +1,46 @@
1
+ export declare class SimpleWeakCache<K extends object, T> {
2
+ readonly size: number;
3
+ private L0;
4
+ private L1;
5
+ private L2;
6
+ private sizeL0;
7
+ constructor(size: number);
8
+ has(key: K): boolean;
9
+ get(key: K): T | undefined;
10
+ set(key: K, value: T): void;
11
+ private _set;
12
+ private caches;
13
+ private rotate;
14
+ }
15
+ export declare class AutoWeakCache<K extends object, T> extends SimpleWeakCache<K, T> {
16
+ readonly factory: (key: K) => T;
17
+ constructor(factory: (key: K) => T, size: number);
18
+ get(key: K): T;
19
+ }
20
+ /**
21
+ * This will cache between `size` and 3 x `size` items.
22
+ * It has three stashes, L0, L1, and L2. Each can contain `size` items.
23
+ * When L0 is full, its items are given to L1 and L1's are given to L2, and L2 is empties.
24
+ *
25
+ * The stashes are searched in order, L0...L2. If an item is found in L1, or L2, it is
26
+ * promoted to L0.
27
+ */
28
+ export declare class SimpleCache<K, T> {
29
+ readonly size: number;
30
+ private L0;
31
+ private L1;
32
+ private L2;
33
+ constructor(size: number);
34
+ has(key: K): boolean;
35
+ get(key: K): T | undefined;
36
+ set(key: K, value: T): void;
37
+ private _set;
38
+ private caches;
39
+ private rotate;
40
+ }
41
+ export declare class AutoCache<K, T> extends SimpleCache<K, T> {
42
+ readonly factory: (key: K) => T;
43
+ constructor(factory: (key: K) => T, size: number);
44
+ get(key: K): T;
45
+ }
46
+ //# sourceMappingURL=simpleCache.d.mts.map