cspell-dictionary 6.9.0

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 (77) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +3 -0
  3. package/dist/SpellingDictionary/Dictionaries.d.ts +9 -0
  4. package/dist/SpellingDictionary/Dictionaries.js +61 -0
  5. package/dist/SpellingDictionary/SpellingDictionary.d.ts +93 -0
  6. package/dist/SpellingDictionary/SpellingDictionary.js +6 -0
  7. package/dist/SpellingDictionary/SpellingDictionaryCollection.d.ts +34 -0
  8. package/dist/SpellingDictionary/SpellingDictionaryCollection.js +111 -0
  9. package/dist/SpellingDictionary/SpellingDictionaryError.d.ts +10 -0
  10. package/dist/SpellingDictionary/SpellingDictionaryError.js +18 -0
  11. package/dist/SpellingDictionary/SpellingDictionaryFromTrie.d.ts +36 -0
  12. package/dist/SpellingDictionary/SpellingDictionaryFromTrie.js +148 -0
  13. package/dist/SpellingDictionary/SpellingDictionaryMethods.d.ts +29 -0
  14. package/dist/SpellingDictionary/SpellingDictionaryMethods.js +114 -0
  15. package/dist/SpellingDictionary/charset.d.ts +3 -0
  16. package/dist/SpellingDictionary/charset.js +16 -0
  17. package/dist/SpellingDictionary/createSpellingDictionary.d.ts +17 -0
  18. package/dist/SpellingDictionary/createSpellingDictionary.js +91 -0
  19. package/dist/SpellingDictionary/index.d.ts +4 -0
  20. package/dist/SpellingDictionary/index.js +9 -0
  21. package/dist/index.d.ts +3 -0
  22. package/dist/index.js +8 -0
  23. package/dist/util/Comparable.d.ts +20 -0
  24. package/dist/util/Comparable.js +55 -0
  25. package/dist/util/FreqCounter.d.ts +16 -0
  26. package/dist/util/FreqCounter.js +52 -0
  27. package/dist/util/IterableLike.d.ts +4 -0
  28. package/dist/util/IterableLike.js +3 -0
  29. package/dist/util/Memorizer.d.ts +65 -0
  30. package/dist/util/Memorizer.js +138 -0
  31. package/dist/util/MinHeapQueue.d.ts +23 -0
  32. package/dist/util/MinHeapQueue.js +97 -0
  33. package/dist/util/PairingHeap.d.ts +32 -0
  34. package/dist/util/PairingHeap.js +90 -0
  35. package/dist/util/TextMap.d.ts +15 -0
  36. package/dist/util/TextMap.js +62 -0
  37. package/dist/util/TextRange.d.ts +28 -0
  38. package/dist/util/TextRange.js +144 -0
  39. package/dist/util/clean.d.ts +7 -0
  40. package/dist/util/clean.js +18 -0
  41. package/dist/util/debugPerf.d.ts +9 -0
  42. package/dist/util/debugPerf.js +22 -0
  43. package/dist/util/errors.d.ts +17 -0
  44. package/dist/util/errors.js +52 -0
  45. package/dist/util/fileReader.d.ts +4 -0
  46. package/dist/util/fileReader.js +21 -0
  47. package/dist/util/iterableIteratorLib.d.ts +4 -0
  48. package/dist/util/iterableIteratorLib.js +14 -0
  49. package/dist/util/logger.d.ts +33 -0
  50. package/dist/util/logger.js +46 -0
  51. package/dist/util/memorizerWeak.d.ts +6 -0
  52. package/dist/util/memorizerWeak.js +42 -0
  53. package/dist/util/regexHelper.d.ts +7 -0
  54. package/dist/util/regexHelper.js +13 -0
  55. package/dist/util/repMap.d.ts +4 -0
  56. package/dist/util/repMap.js +38 -0
  57. package/dist/util/resolveFile.d.ts +13 -0
  58. package/dist/util/resolveFile.js +127 -0
  59. package/dist/util/search.d.ts +6 -0
  60. package/dist/util/search.js +23 -0
  61. package/dist/util/simpleCache.d.ts +46 -0
  62. package/dist/util/simpleCache.js +143 -0
  63. package/dist/util/text.d.ts +9 -0
  64. package/dist/util/text.js +55 -0
  65. package/dist/util/textRegex.d.ts +1 -0
  66. package/dist/util/textRegex.js +2 -0
  67. package/dist/util/timer.d.ts +26 -0
  68. package/dist/util/timer.js +58 -0
  69. package/dist/util/types.d.ts +7 -0
  70. package/dist/util/types.js +3 -0
  71. package/dist/util/util.d.ts +2 -0
  72. package/dist/util/util.js +8 -0
  73. package/dist/util/util.test copy.d.ts +2 -0
  74. package/dist/util/util.test copy.js +17 -0
  75. package/dist/util/wordSplitter.d.ts +46 -0
  76. package/dist/util/wordSplitter.js +326 -0
  77. package/package.json +47 -0
@@ -0,0 +1,114 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.__testMethods__ = exports.createWeightMapFromDictionaryInformation = exports.suggestArgsToSuggestOptions = exports.hasOptionToSearchOption = exports.wordDictionaryFormsCollector = exports.wordSuggestForms = exports.wordSuggestFormsArray = exports.wordSearchForms = exports.defaultNumSuggestions = exports.suggestionCollector = exports.impersonateCollector = void 0;
4
+ const cspell_trie_lib_1 = require("cspell-trie-lib");
5
+ const clean_1 = require("../util/clean");
6
+ const gensequence_1 = require("gensequence");
7
+ const text_1 = require("../util/text");
8
+ var cspell_trie_lib_2 = require("cspell-trie-lib");
9
+ Object.defineProperty(exports, "impersonateCollector", { enumerable: true, get: function () { return cspell_trie_lib_2.impersonateCollector; } });
10
+ Object.defineProperty(exports, "suggestionCollector", { enumerable: true, get: function () { return cspell_trie_lib_2.suggestionCollector; } });
11
+ exports.defaultNumSuggestions = 10;
12
+ function wordSearchFormsArray(word, isDictionaryCaseSensitive, ignoreCase) {
13
+ return [...wordSearchForms(word, isDictionaryCaseSensitive, ignoreCase)];
14
+ }
15
+ function wordSearchForms(word, isDictionaryCaseSensitive, ignoreCase) {
16
+ const forms = new Set();
17
+ word = word.normalize('NFC');
18
+ const wordLc = word.toLowerCase();
19
+ if (ignoreCase) {
20
+ if (isDictionaryCaseSensitive) {
21
+ forms.add(wordLc);
22
+ }
23
+ else {
24
+ forms.add(wordLc);
25
+ // Legacy remove any unbound accents
26
+ forms.add(wordLc.replace(/\p{M}/gu, ''));
27
+ }
28
+ }
29
+ else {
30
+ if (isDictionaryCaseSensitive) {
31
+ forms.add(word);
32
+ forms.add(wordLc);
33
+ // HOUSE -> House, house
34
+ if ((0, text_1.isUpperCase)(word)) {
35
+ forms.add((0, text_1.ucFirst)(wordLc));
36
+ }
37
+ }
38
+ else {
39
+ forms.add(wordLc);
40
+ // Legacy remove any unbound accents
41
+ forms.add(wordLc.replace(/\p{M}/gu, ''));
42
+ }
43
+ }
44
+ return forms;
45
+ }
46
+ exports.wordSearchForms = wordSearchForms;
47
+ function wordSuggestFormsArray(word) {
48
+ return [...wordSuggestForms(word)];
49
+ }
50
+ exports.wordSuggestFormsArray = wordSuggestFormsArray;
51
+ function wordSuggestForms(word) {
52
+ word = word.normalize('NFC');
53
+ const forms = new Set([word]);
54
+ const wordLc = word.toLowerCase();
55
+ forms.add(wordLc);
56
+ return forms;
57
+ }
58
+ exports.wordSuggestForms = wordSuggestForms;
59
+ function* wordDictionaryForms(word, prefixNoCase) {
60
+ word = word.normalize('NFC');
61
+ const wordLc = word.toLowerCase();
62
+ const wordNa = (0, text_1.removeAccents)(word);
63
+ const wordLcNa = (0, text_1.removeAccents)(wordLc);
64
+ function wf(w, p = '') {
65
+ return { w, p };
66
+ }
67
+ const prefix = prefixNoCase;
68
+ yield wf(word);
69
+ yield wf(wordNa, prefix);
70
+ yield wf(wordLc, prefix);
71
+ yield wf(wordLcNa, prefix);
72
+ }
73
+ function wordDictionaryFormsCollector(prefixNoCase) {
74
+ const knownWords = new Set();
75
+ return (word) => {
76
+ return (0, gensequence_1.genSequence)(wordDictionaryForms(word, prefixNoCase))
77
+ .filter((w) => !knownWords.has(w.w))
78
+ .map((w) => w.p + w.w)
79
+ .filter((w) => !knownWords.has(w))
80
+ .map((w) => (knownWords.add(w), w));
81
+ };
82
+ }
83
+ exports.wordDictionaryFormsCollector = wordDictionaryFormsCollector;
84
+ const DEFAULT_HAS_OPTIONS = Object.freeze({});
85
+ function hasOptionToSearchOption(opt) {
86
+ return !opt ? DEFAULT_HAS_OPTIONS : opt;
87
+ }
88
+ exports.hasOptionToSearchOption = hasOptionToSearchOption;
89
+ function suggestArgsToSuggestOptions(args) {
90
+ const [_word, options, compoundMethod, numChanges, ignoreCase] = args;
91
+ const suggestOptions = typeof options === 'object'
92
+ ? options
93
+ : (0, clean_1.clean)({
94
+ numSuggestions: options,
95
+ compoundMethod,
96
+ numChanges,
97
+ ignoreCase,
98
+ includeTies: undefined,
99
+ timeout: undefined,
100
+ });
101
+ return suggestOptions;
102
+ }
103
+ exports.suggestArgsToSuggestOptions = suggestArgsToSuggestOptions;
104
+ function createWeightMapFromDictionaryInformation(di) {
105
+ return di ? (0, cspell_trie_lib_1.mapDictionaryInformationToWeightMap)(di) : undefined;
106
+ }
107
+ exports.createWeightMapFromDictionaryInformation = createWeightMapFromDictionaryInformation;
108
+ exports.__testMethods__ = {
109
+ wordSearchForms,
110
+ wordSearchFormsArray,
111
+ wordDictionaryForms,
112
+ wordDictionaryFormsCollector,
113
+ };
114
+ //# sourceMappingURL=SpellingDictionaryMethods.js.map
@@ -0,0 +1,3 @@
1
+ import { CharacterSet } from '@cspell/cspell-types';
2
+ export declare function charsetToRegExp(charset: CharacterSet | undefined): RegExp | undefined;
3
+ //# sourceMappingURL=charset.d.ts.map
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.charsetToRegExp = void 0;
4
+ function charsetToRegExp(charset) {
5
+ if (!charset)
6
+ return undefined;
7
+ try {
8
+ const reg = `[${charset.replace(/[\][\\]/g, '\\$&')}]`;
9
+ return new RegExp(reg, 'g');
10
+ }
11
+ catch (e) {
12
+ return undefined;
13
+ }
14
+ }
15
+ exports.charsetToRegExp = charsetToRegExp;
16
+ //# sourceMappingURL=charset.js.map
@@ -0,0 +1,17 @@
1
+ import { IterableLike } from '../util/IterableLike';
2
+ import { SpellingDictionary, SpellingDictionaryOptions, DictionaryInfo } from './SpellingDictionary';
3
+ export declare function createSpellingDictionary(wordList: readonly string[] | IterableLike<string>, name: string, source: string, options: SpellingDictionaryOptions | undefined): SpellingDictionary;
4
+ export declare function createForbiddenWordsDictionary(wordList: readonly string[], name: string, source: string, options: SpellingDictionaryOptions | undefined): SpellingDictionary;
5
+ export interface SpellingDictionaryLoadError extends Error {
6
+ /** The Error Name */
7
+ readonly name: string;
8
+ /** Possible Cause */
9
+ readonly cause?: Error | undefined;
10
+ /** Message to Display */
11
+ readonly message: string;
12
+ /** Dictionary Information */
13
+ readonly info: DictionaryInfo;
14
+ }
15
+ export declare function createFailedToLoadDictionary(error: SpellingDictionaryLoadError): SpellingDictionary;
16
+ export declare function createSpellingDictionaryLoadError(errorName: string, message: string, info: DictionaryInfo, cause?: Error): SpellingDictionaryLoadError;
17
+ //# sourceMappingURL=createSpellingDictionary.d.ts.map
@@ -0,0 +1,91 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createSpellingDictionaryLoadError = exports.createFailedToLoadDictionary = exports.createForbiddenWordsDictionary = exports.createSpellingDictionary = void 0;
4
+ const cspell_trie_lib_1 = require("cspell-trie-lib");
5
+ const fast_equals_1 = require("fast-equals");
6
+ const gensequence_1 = require("gensequence");
7
+ const simpleCache_1 = require("../util/simpleCache");
8
+ const SpellingDictionaryFromTrie_1 = require("./SpellingDictionaryFromTrie");
9
+ const SpellingDictionaryMethods_1 = require("./SpellingDictionaryMethods");
10
+ const defaultOptions = Object.freeze({
11
+ weightMap: undefined,
12
+ });
13
+ const cachedDictionaries = new simpleCache_1.AutoWeakCache(_createSpellingDictionary, 64);
14
+ const maxSetSize = 3;
15
+ const cachedParamsByWordList = new simpleCache_1.SimpleCache(64);
16
+ function createSpellingDictionary(wordList, name, source, options) {
17
+ const params = [wordList, name, source, options];
18
+ if (!Array.isArray(wordList)) {
19
+ return _createSpellingDictionary(params);
20
+ }
21
+ const cached = cachedParamsByWordList.get(name) || new Set();
22
+ for (const cachedParams of cached) {
23
+ if ((0, fast_equals_1.deepEqual)(params, cachedParams)) {
24
+ return cachedDictionaries.get(cachedParams);
25
+ }
26
+ }
27
+ if (cached.size > maxSetSize)
28
+ cached.clear();
29
+ cached.add(params);
30
+ cachedParamsByWordList.set(name, cached);
31
+ return cachedDictionaries.get(params);
32
+ }
33
+ exports.createSpellingDictionary = createSpellingDictionary;
34
+ function _createSpellingDictionary(params) {
35
+ const [wordList, name, source, options] = params;
36
+ // console.log(`createSpellingDictionary ${name} ${source}`);
37
+ const parseOptions = { stripCaseAndAccents: options?.supportNonStrictSearches ?? true };
38
+ const words = (0, cspell_trie_lib_1.parseDictionaryLines)(wordList, parseOptions);
39
+ const trie = (0, cspell_trie_lib_1.buildTrieFast)(words);
40
+ const opts = { ...(options || defaultOptions) };
41
+ if (opts.weightMap === undefined && opts.dictionaryInformation) {
42
+ opts.weightMap = (0, SpellingDictionaryMethods_1.createWeightMapFromDictionaryInformation)(opts.dictionaryInformation);
43
+ }
44
+ return new SpellingDictionaryFromTrie_1.SpellingDictionaryFromTrie(trie, name, opts, source);
45
+ }
46
+ function createForbiddenWordsDictionary(wordList, name, source, options) {
47
+ // console.log(`createSpellingDictionary ${name} ${source}`);
48
+ const words = (0, cspell_trie_lib_1.parseDictionaryLines)(wordList.concat(wordList.map((a) => a.toLowerCase())), {
49
+ stripCaseAndAccents: !options?.noSuggest,
50
+ });
51
+ const forbidWords = gensequence_1.operators.map((w) => '!' + w)(words);
52
+ const trie = (0, cspell_trie_lib_1.buildTrieFast)(forbidWords);
53
+ return new SpellingDictionaryFromTrie_1.SpellingDictionaryFromTrie(trie, name, options || defaultOptions, source);
54
+ }
55
+ exports.createForbiddenWordsDictionary = createForbiddenWordsDictionary;
56
+ function createFailedToLoadDictionary(error) {
57
+ const { name, source, options } = error.info;
58
+ return {
59
+ name,
60
+ source,
61
+ type: 'error',
62
+ containsNoSuggestWords: false,
63
+ has: () => false,
64
+ find: () => undefined,
65
+ isNoSuggestWord: () => false,
66
+ isForbidden: () => false,
67
+ suggest: () => [],
68
+ mapWord: (a) => a,
69
+ genSuggestions: () => {
70
+ return;
71
+ },
72
+ size: 0,
73
+ options,
74
+ isDictionaryCaseSensitive: false,
75
+ getErrors: () => [error],
76
+ };
77
+ }
78
+ exports.createFailedToLoadDictionary = createFailedToLoadDictionary;
79
+ function createSpellingDictionaryLoadError(errorName, message, info, cause) {
80
+ const err = {
81
+ name: errorName,
82
+ message,
83
+ info,
84
+ cause,
85
+ };
86
+ const stack = cause?.stack;
87
+ stack && (err.stack = stack);
88
+ return err;
89
+ }
90
+ exports.createSpellingDictionaryLoadError = createSpellingDictionaryLoadError;
91
+ //# sourceMappingURL=createSpellingDictionary.js.map
@@ -0,0 +1,4 @@
1
+ export { createForbiddenWordsDictionary, createSpellingDictionary } from './createSpellingDictionary';
2
+ export type { FindOptions, FindResult, HasOptions, SearchOptions, SpellingDictionary, SpellingDictionaryOptions, SuggestionCollector, SuggestionResult, SuggestOptions, } from './SpellingDictionary';
3
+ export { createCollection } from './SpellingDictionaryCollection';
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createCollection = exports.createSpellingDictionary = exports.createForbiddenWordsDictionary = void 0;
4
+ var createSpellingDictionary_1 = require("./createSpellingDictionary");
5
+ Object.defineProperty(exports, "createForbiddenWordsDictionary", { enumerable: true, get: function () { return createSpellingDictionary_1.createForbiddenWordsDictionary; } });
6
+ Object.defineProperty(exports, "createSpellingDictionary", { enumerable: true, get: function () { return createSpellingDictionary_1.createSpellingDictionary; } });
7
+ var SpellingDictionaryCollection_1 = require("./SpellingDictionaryCollection");
8
+ Object.defineProperty(exports, "createCollection", { enumerable: true, get: function () { return SpellingDictionaryCollection_1.createCollection; } });
9
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,3 @@
1
+ export { createCollection, createForbiddenWordsDictionary, createSpellingDictionary } from './SpellingDictionary';
2
+ export type { FindOptions, FindResult, HasOptions, SearchOptions, SpellingDictionary, SpellingDictionaryOptions, SuggestionCollector, SuggestionResult, SuggestOptions, } from './SpellingDictionary';
3
+ //# sourceMappingURL=index.d.ts.map
package/dist/index.js ADDED
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createSpellingDictionary = exports.createForbiddenWordsDictionary = exports.createCollection = void 0;
4
+ var SpellingDictionary_1 = require("./SpellingDictionary");
5
+ Object.defineProperty(exports, "createCollection", { enumerable: true, get: function () { return SpellingDictionary_1.createCollection; } });
6
+ Object.defineProperty(exports, "createForbiddenWordsDictionary", { enumerable: true, get: function () { return SpellingDictionary_1.createForbiddenWordsDictionary; } });
7
+ Object.defineProperty(exports, "createSpellingDictionary", { enumerable: true, get: function () { return SpellingDictionary_1.createSpellingDictionary; } });
8
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,20 @@
1
+ export declare type Comparable = number | string | boolean | undefined | null | Date;
2
+ export declare type ComparableFilter<T> = T extends Comparable ? T : never;
3
+ export declare type ComparablePropertyNames<T> = {
4
+ [K in keyof T]: T[K] extends Comparable ? K : never;
5
+ }[keyof T];
6
+ export declare type ComparableProperties<T> = Pick<T, ComparablePropertyNames<T>>;
7
+ export declare type CompareArg<T> = ComparablePropertyNames<T> | ((t: T) => Comparable);
8
+ export declare type CompareFn<T> = (a: T, b: T) => number;
9
+ export declare function compareBy<T>(extract: CompareArg<T>, ...extractors: CompareArg<T>[]): CompareFn<T>;
10
+ export declare function compareBy<T>(extract: CompareArg<T>): CompareFn<T>;
11
+ export declare function compareBy<T>(extract1: CompareArg<T>, extract2: CompareArg<T>): CompareFn<T>;
12
+ export declare function compareBy<T>(extract1: CompareArg<T>, extract2: CompareArg<T>, extract3: CompareArg<T>): CompareFn<T>;
13
+ export declare function compareByRev<T>(extract: CompareArg<T>, ...extractors: CompareArg<T>[]): CompareFn<T>;
14
+ export declare function compareByRev<T>(extract: CompareArg<T>): CompareFn<T>;
15
+ export declare function compareByRev<T>(extract1: CompareArg<T>, extract2: CompareArg<T>): CompareFn<T>;
16
+ export declare function compareByRev<T>(extract1: CompareArg<T>, extract2: CompareArg<T>, extract3: CompareArg<T>): CompareFn<T>;
17
+ export declare function compareEach<T>(...compareFn: CompareFn<T>[]): CompareFn<T>;
18
+ export declare function compare<T>(a: ComparableFilter<T>, b: ComparableFilter<T>): number;
19
+ export declare function reverse<T>(fn: CompareFn<T>): CompareFn<T>;
20
+ //# sourceMappingURL=Comparable.d.ts.map
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.reverse = exports.compare = exports.compareEach = exports.compareByRev = exports.compareBy = void 0;
4
+ function compareBy(extract, ...extractors) {
5
+ const compareFns = [extract, ...extractors]
6
+ .map((ex) => (typeof ex === 'function' ? ex : (t) => t[ex]))
7
+ .map((ex) => (a, b) => _compare(ex(a), ex(b)));
8
+ return compareEach(...compareFns);
9
+ }
10
+ exports.compareBy = compareBy;
11
+ function compareByRev(extract, ...extractors) {
12
+ return reverse(compareBy(extract, ...extractors));
13
+ }
14
+ exports.compareByRev = compareByRev;
15
+ function compareEach(...compareFn) {
16
+ return (a, b) => {
17
+ for (const fn of compareFn) {
18
+ const r = fn(a, b);
19
+ if (r) {
20
+ return r;
21
+ }
22
+ }
23
+ return 0;
24
+ };
25
+ }
26
+ exports.compareEach = compareEach;
27
+ function _compare(a, b) {
28
+ if (a === b)
29
+ return 0;
30
+ if (a === undefined)
31
+ return 1;
32
+ if (b === undefined)
33
+ return -1;
34
+ if (a === null)
35
+ return 1;
36
+ if (b === null)
37
+ return -1;
38
+ if (a < b)
39
+ return -1;
40
+ if (a > b)
41
+ return 1;
42
+ return 0;
43
+ }
44
+ function compare(a, b) {
45
+ return _compare(a, b);
46
+ }
47
+ exports.compare = compare;
48
+ function reverse(fn) {
49
+ return (a, b) => {
50
+ const r = fn(a, b);
51
+ return r ? -r : 0;
52
+ };
53
+ }
54
+ exports.reverse = reverse;
55
+ //# sourceMappingURL=Comparable.js.map
@@ -0,0 +1,16 @@
1
+ import { IterableLike } from './IterableLike';
2
+ export declare class FreqCounter<T> {
3
+ private _total;
4
+ readonly _counters: Map<T, number>;
5
+ get total(): number;
6
+ get counters(): Map<T, number>;
7
+ getCount(key: T): number | undefined;
8
+ getFreq(key: T): number;
9
+ addKeyCount(key: T, count: number): this;
10
+ addKey(key: T): this;
11
+ addKeys(keys: IterableLike<T>): void;
12
+ addKeyCounts(values: IterableLike<[T, number]>): void;
13
+ merge(...freqCounters: FreqCounter<T>[]): FreqCounter<T>;
14
+ static create<T>(values?: IterableLike<T>): FreqCounter<T>;
15
+ }
16
+ //# sourceMappingURL=FreqCounter.d.ts.map
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FreqCounter = void 0;
4
+ class FreqCounter {
5
+ constructor() {
6
+ this._total = 0;
7
+ this._counters = new Map();
8
+ }
9
+ get total() {
10
+ return this._total;
11
+ }
12
+ get counters() {
13
+ return this._counters;
14
+ }
15
+ getCount(key) {
16
+ return this._counters.get(key);
17
+ }
18
+ getFreq(key) {
19
+ return (this.getCount(key) || 0) / (this._total || 1);
20
+ }
21
+ addKeyCount(key, count) {
22
+ this._total += count;
23
+ this._counters.set(key, (this._counters.get(key) || 0) + count);
24
+ return this;
25
+ }
26
+ addKey(key) {
27
+ return this.addKeyCount(key, 1);
28
+ }
29
+ addKeys(keys) {
30
+ for (const key of keys) {
31
+ this.addKey(key);
32
+ }
33
+ }
34
+ addKeyCounts(values) {
35
+ for (const pair of values) {
36
+ this.addKeyCount(pair[0], pair[1]);
37
+ }
38
+ }
39
+ merge(...freqCounters) {
40
+ for (const fc of freqCounters) {
41
+ this.addKeyCounts(fc._counters);
42
+ }
43
+ return this;
44
+ }
45
+ static create(values) {
46
+ const fc = new FreqCounter();
47
+ fc.addKeys(values || []);
48
+ return fc;
49
+ }
50
+ }
51
+ exports.FreqCounter = FreqCounter;
52
+ //# sourceMappingURL=FreqCounter.js.map
@@ -0,0 +1,4 @@
1
+ export interface IterableLike<T> {
2
+ [Symbol.iterator]: () => Iterator<T> | IterableIterator<T>;
3
+ }
4
+ //# sourceMappingURL=IterableLike.d.ts.map
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=IterableLike.js.map
@@ -0,0 +1,65 @@
1
+ /** Only types that can be easily turned into strings */
2
+ declare type P0 = string | number | boolean | RegExp | undefined;
3
+ declare type Primitive = P0 | P0[];
4
+ /**
5
+ * Memorize the result of a function call to be returned on later calls with the same parameters.
6
+ *
7
+ * Note: The parameters are converted into a string: `key = args.join('>!@[')`
8
+ *
9
+ * For speed, it keeps two caches, L0 and L1. Each cache can contain up to `size` values. But that actual number
10
+ * of cached values is between `size + 1` and `size * 2`.
11
+ *
12
+ * Caches are NOT sorted. Items are added to L0 until it is full. Once it is full, L1 takes over L0's values and L0 is cleared.
13
+ *
14
+ * If an item is not found in L0, L1 is checked before calling the `fn` and the resulting value store in L0.
15
+ *
16
+ * @param fn - function to be called.
17
+ * @param size - size of cache
18
+ */
19
+ export declare function memorizer<F extends (...args: Primitive[]) => any, Args extends Parameters<F> = Parameters<F>, R extends ReturnType<F> = ReturnType<F>>(fn: F, size?: number): (...args: Args) => R;
20
+ /**
21
+ * Memorize the result of a function call to be returned on later calls with the same parameters.
22
+ *
23
+ * Note: `keyFn` is use to convert the function parameters into a string to look up in the cache.
24
+ *
25
+ * For speed, it keeps two caches, L0 and L1. Each cache can contain up to `size` values. But that actual number
26
+ * of cached values is between `size + 1` and `size * 2`.
27
+ *
28
+ * Caches are NOT sorted. Items are added to L0 until it is full. Once it is full, L1 takes over L0's values and L0 is cleared.
29
+ *
30
+ * If an item is not found in L0, L1 is checked before calling the `fn` and the resulting value store in L0.
31
+ *
32
+ * @param fn - function to be memorized
33
+ * @param keyFn - extracts a `key` value from the arguments to `fn` to be used as the key to the cache
34
+ * @param size - size of the cache.
35
+ * @returns A function
36
+ */
37
+ export declare function memorizerKeyBy<F extends (...args: any[]) => any, Args extends Parameters<F> = Parameters<F>, R extends ReturnType<F> = ReturnType<F>>(fn: F, keyFn: (...args: Args) => string, size?: number): (...args: Args) => R;
38
+ /**
39
+ * Create a function that memorizes the last call. If the next call is called with the same arguments, the
40
+ * the last value is returned.
41
+ * @param fn - function to memorize
42
+ * @returns a new function.
43
+ */
44
+ export declare function memorizeLastCall<T>(fn: (...p: []) => T): (...p: []) => T;
45
+ export declare function memorizeLastCall<T, K0>(fn: (...p: [K0]) => T): (...p: [K0]) => T;
46
+ export declare function memorizeLastCall<T, K0, K1>(fn: (...p: [K0, K1]) => T): (...p: [K0, K1]) => T;
47
+ export declare function memorizeLastCall<T, K0, K1, K2>(fn: (...p: [K0, K1, K2]) => T): (...p: [K0, K1, K2]) => T;
48
+ export declare function memorizeLastCall<T, K0, K1, K2, K3>(fn: (...p: [K0, K1, K2, K3]) => T): (...p: [K0, K1, K2, K3]) => T;
49
+ export declare function memorizeLastCall<T, K>(fn: (...p: [...K[]]) => T): (...p: [...K[]]) => T;
50
+ /**
51
+ * Creates a function that will call `fn` exactly once when invoked and remember the value returned.
52
+ * All subsequent calls will return exactly same value.
53
+ * @param fn - function to call
54
+ * @returns a new function
55
+ */
56
+ export declare function callOnce<T>(fn: () => T): () => T;
57
+ /**
58
+ * Create a function that will memorize all calls to `fn` to ensure that `fn` is
59
+ * called exactly once for each unique set of arguments.
60
+ * @param fn - function to memorize
61
+ * @returns a function
62
+ */
63
+ export declare function memorizerAll<K extends any[], T>(fn: (...p: K) => T): (...p: K) => T;
64
+ export {};
65
+ //# sourceMappingURL=Memorizer.d.ts.map
@@ -0,0 +1,138 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.memorizerAll = exports.callOnce = exports.memorizeLastCall = exports.memorizerKeyBy = exports.memorizer = void 0;
4
+ const util_1 = require("./util");
5
+ /* eslint-disable @typescript-eslint/no-explicit-any */
6
+ const defaultSize = 50000;
7
+ /**
8
+ * Memorize the result of a function call to be returned on later calls with the same parameters.
9
+ *
10
+ * Note: The parameters are converted into a string: `key = args.join('>!@[')`
11
+ *
12
+ * For speed, it keeps two caches, L0 and L1. Each cache can contain up to `size` values. But that actual number
13
+ * of cached values is between `size + 1` and `size * 2`.
14
+ *
15
+ * Caches are NOT sorted. Items are added to L0 until it is full. Once it is full, L1 takes over L0's values and L0 is cleared.
16
+ *
17
+ * If an item is not found in L0, L1 is checked before calling the `fn` and the resulting value store in L0.
18
+ *
19
+ * @param fn - function to be called.
20
+ * @param size - size of cache
21
+ */
22
+ function memorizer(fn, size) {
23
+ return memorizerKeyBy(fn, (...args) => args.join('>!@['), size);
24
+ }
25
+ exports.memorizer = memorizer;
26
+ /**
27
+ * Memorize the result of a function call to be returned on later calls with the same parameters.
28
+ *
29
+ * Note: `keyFn` is use to convert the function parameters into a string to look up in the cache.
30
+ *
31
+ * For speed, it keeps two caches, L0 and L1. Each cache can contain up to `size` values. But that actual number
32
+ * of cached values is between `size + 1` and `size * 2`.
33
+ *
34
+ * Caches are NOT sorted. Items are added to L0 until it is full. Once it is full, L1 takes over L0's values and L0 is cleared.
35
+ *
36
+ * If an item is not found in L0, L1 is checked before calling the `fn` and the resulting value store in L0.
37
+ *
38
+ * @param fn - function to be memorized
39
+ * @param keyFn - extracts a `key` value from the arguments to `fn` to be used as the key to the cache
40
+ * @param size - size of the cache.
41
+ * @returns A function
42
+ */
43
+ function memorizerKeyBy(fn, keyFn, size = defaultSize) {
44
+ let count = 0;
45
+ let cacheL0 = Object.create(null);
46
+ let cacheL1 = Object.create(null);
47
+ return (...args) => {
48
+ const key = keyFn(...args);
49
+ if (key in cacheL0)
50
+ return cacheL0[key];
51
+ const v = key in cacheL1 ? cacheL1[key] : fn(...args);
52
+ if (count >= size) {
53
+ cacheL1 = cacheL0;
54
+ cacheL0 = Object.create(null);
55
+ count = 0;
56
+ }
57
+ cacheL0[key] = v;
58
+ ++count;
59
+ return v;
60
+ };
61
+ }
62
+ exports.memorizerKeyBy = memorizerKeyBy;
63
+ function memorizeLastCall(fn) {
64
+ let last;
65
+ return (...p) => {
66
+ if (last && (0, util_1.isArrayEqual)(last.args, p)) {
67
+ return last.value;
68
+ }
69
+ const args = p;
70
+ const value = fn(...args);
71
+ last = { args, value };
72
+ return value;
73
+ };
74
+ }
75
+ exports.memorizeLastCall = memorizeLastCall;
76
+ /**
77
+ * Creates a function that will call `fn` exactly once when invoked and remember the value returned.
78
+ * All subsequent calls will return exactly same value.
79
+ * @param fn - function to call
80
+ * @returns a new function
81
+ */
82
+ function callOnce(fn) {
83
+ let last;
84
+ return () => {
85
+ if (last) {
86
+ return last.value;
87
+ }
88
+ last = {
89
+ value: fn(),
90
+ };
91
+ return last.value;
92
+ };
93
+ }
94
+ exports.callOnce = callOnce;
95
+ /**
96
+ * Create a function that will memorize all calls to `fn` to ensure that `fn` is
97
+ * called exactly once for each unique set of arguments.
98
+ * @param fn - function to memorize
99
+ * @returns a function
100
+ */
101
+ function memorizerAll(fn) {
102
+ const r = {};
103
+ function find(p) {
104
+ let n = r;
105
+ for (const k of p) {
106
+ if (!n)
107
+ break;
108
+ n = n.c?.get(k);
109
+ }
110
+ return n;
111
+ }
112
+ function set(p, v) {
113
+ let n = r;
114
+ for (const k of p) {
115
+ const c = n.c?.get(k);
116
+ if (c) {
117
+ n = c;
118
+ continue;
119
+ }
120
+ const r = {};
121
+ n.c = n.c || new Map();
122
+ n.c.set(k, r);
123
+ n = r;
124
+ }
125
+ n.v = v;
126
+ }
127
+ return (...p) => {
128
+ const f = find(p);
129
+ if (f && 'v' in f) {
130
+ return f.v;
131
+ }
132
+ const v = fn(...p);
133
+ set(p, v);
134
+ return v;
135
+ };
136
+ }
137
+ exports.memorizerAll = memorizerAll;
138
+ //# sourceMappingURL=Memorizer.js.map
@@ -0,0 +1,23 @@
1
+ declare function addToHeap<T>(t: T[], c: T, compare: (a: T, b: T) => number): void;
2
+ declare function takeFromHeap<T>(t: T[], compare: (a: T, b: T) => number): T | undefined;
3
+ /**
4
+ * MinHeapQueue - based upon a minHeap array.
5
+ */
6
+ export declare class MinHeapQueue<T> implements IterableIterator<T> {
7
+ readonly compare: (a: T, b: T) => number;
8
+ private values;
9
+ constructor(compare: (a: T, b: T) => number);
10
+ add(t: T): MinHeapQueue<T>;
11
+ get length(): number;
12
+ dequeue(): T | undefined;
13
+ concat(i: Iterable<T>): MinHeapQueue<T>;
14
+ next(): IteratorResult<T>;
15
+ [Symbol.iterator](): IterableIterator<T>;
16
+ clone(): MinHeapQueue<T>;
17
+ }
18
+ export declare const __testing__: {
19
+ addToHeap: typeof addToHeap;
20
+ takeFromHeap: typeof takeFromHeap;
21
+ };
22
+ export {};
23
+ //# sourceMappingURL=MinHeapQueue.d.ts.map