cspell-trie-lib 6.10.1 → 6.11.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.
@@ -63,7 +63,7 @@ export declare function findWordExact(root: Root | TrieNode | undefined, word: s
63
63
  export declare function isEndOfWordNode(n: TrieNode | undefined): boolean;
64
64
  declare function findLegacyCompoundWord(roots: (TrieNode | undefined)[], word: string, minCompoundLength: number): FindResult;
65
65
  export declare function isForbiddenWord(root: Root | TrieNode | undefined, word: string, forbiddenPrefix: string): boolean;
66
- export declare function createFindOptions(options: PartialFindOptions | undefined): FindOptions;
66
+ export declare const createFindOptions: (p: PartialFindOptions) => FindOptions;
67
67
  export declare const __testing__: {
68
68
  findLegacyCompoundWord: typeof findLegacyCompoundWord;
69
69
  };
package/dist/lib/find.js CHANGED
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.__testing__ = exports.createFindOptions = exports.isForbiddenWord = exports.isEndOfWordNode = exports.findWordExact = exports.findCompoundNode = exports.findLegacyCompound = exports.findWordNode = exports.findWord = void 0;
4
4
  const constants_1 = require("./constants");
5
+ const memorizeLastCall_1 = require("./utils/memorizeLastCall");
5
6
  const trie_util_1 = require("./trie-util");
6
7
  const TrieNode_1 = require("./TrieNode");
7
8
  const defaultLegacyMinCompoundLength = 3;
@@ -22,7 +23,7 @@ const knownCompoundModes = new Map(arrayCompoundModes.map((a) => [a, a]));
22
23
  * @param options
23
24
  */
24
25
  function findWord(root, word, options) {
25
- return _findWord(root, word, createFindOptions(options));
26
+ return _findWord(root, word, (0, exports.createFindOptions)(options));
26
27
  }
27
28
  exports.findWord = findWord;
28
29
  /**
@@ -32,7 +33,7 @@ exports.findWord = findWord;
32
33
  * @param options
33
34
  */
34
35
  function findWordNode(root, word, options) {
35
- return _findWordNode(root, word, createFindOptions(options));
36
+ return _findWordNode(root, word, (0, exports.createFindOptions)(options));
36
37
  }
37
38
  exports.findWordNode = findWordNode;
38
39
  /**
@@ -282,10 +283,10 @@ function isForbiddenWord(root, word, forbiddenPrefix) {
282
283
  return findWordExact(root?.c?.get(forbiddenPrefix), word);
283
284
  }
284
285
  exports.isForbiddenWord = isForbiddenWord;
285
- function createFindOptions(options) {
286
+ exports.createFindOptions = (0, memorizeLastCall_1.memorizeLastCall)(_createFindOptions);
287
+ function _createFindOptions(options) {
286
288
  return (0, trie_util_1.mergeDefaults)(options, _defaultFindOptions);
287
289
  }
288
- exports.createFindOptions = createFindOptions;
289
290
  exports.__testing__ = {
290
291
  findLegacyCompoundWord,
291
292
  };
@@ -23,7 +23,7 @@
23
23
  */
24
24
  Object.defineProperty(exports, "__esModule", { value: true });
25
25
  exports.__testing__ = exports.importTrie = exports.serializeTrie = exports.DATA = void 0;
26
- const cspell_pipe_1 = require("@cspell/cspell-pipe");
26
+ const sync_1 = require("@cspell/cspell-pipe/sync");
27
27
  const gensequence_1 = require("gensequence");
28
28
  const trie_util_1 = require("../trie-util");
29
29
  const TrieNode_1 = require("../TrieNode");
@@ -194,7 +194,7 @@ function buildReferenceMap(root, base) {
194
194
  walk(root);
195
195
  // sorted highest to lowest
196
196
  const refCountAndNode = [
197
- ...(0, cspell_pipe_1.pipeSync)(refCount, (0, cspell_pipe_1.opFilter)(([_, ref]) => ref.c >= 2)),
197
+ ...(0, sync_1.pipe)(refCount, (0, sync_1.opFilter)(([_, ref]) => ref.c >= 2)),
198
198
  ].sort((a, b) => b[1].c - a[1].c || a[1].n - b[1].n);
199
199
  let adj = 0;
200
200
  const baseLogScale = 1 / Math.log(base);
@@ -215,7 +215,7 @@ function importTrie(linesX) {
215
215
  linesX = typeof linesX === 'string' ? linesX.split(/(?<=\n)/) : linesX;
216
216
  let radix = 10;
217
217
  const comment = /^\s*#/;
218
- const iter = tapIterable((0, cspell_pipe_1.pipeSync)(linesX, (0, cspell_pipe_1.opConcatMap)((a) => a.split(/(?<=\n)(?!$)/))));
218
+ const iter = tapIterable((0, sync_1.pipe)(linesX, (0, sync_1.opConcatMap)((a) => a.split(/(?<=\n)(?!$)/))));
219
219
  function parseHeaderRows(headerRows) {
220
220
  const header = headerRows.slice(0, 2).join('\n');
221
221
  const headerReg = /^TrieXv[34]\nbase=(\d+)$/;
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.mapDictionaryInformationToAdjustment = exports.mapDictionaryInformation = void 0;
4
- const cspell_pipe_1 = require("@cspell/cspell-pipe");
4
+ const sync_1 = require("@cspell/cspell-pipe/sync");
5
5
  const locale_1 = require("../models/locale");
6
6
  const util_1 = require("../utils/util");
7
7
  const mapCosts_1 = require("./mapCosts");
@@ -27,7 +27,7 @@ exports.mapDictionaryInformation = mapDictionaryInformation;
27
27
  function processAlphabet(alphabet, locale, editCost) {
28
28
  const csAlphabet = toCharSets(alphabet, 'a-zA-Z', editCost.baseCost);
29
29
  return [
30
- ...(0, cspell_pipe_1.pipeSync)(csAlphabet, (0, cspell_pipe_1.opMap)((cs) => (0, mapToSuggestionCostDef_1.parseAlphabet)(cs, locale, editCost)), (0, cspell_pipe_1.opFlatten)()),
30
+ ...(0, sync_1.pipeSync)(csAlphabet, (0, sync_1.opMap)((cs) => (0, mapToSuggestionCostDef_1.parseAlphabet)(cs, locale, editCost)), (0, sync_1.opFlatten)()),
31
31
  ...(0, mapToSuggestionCostDef_1.calcFirstCharacterReplaceDefs)(csAlphabet, editCost),
32
32
  ];
33
33
  }
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.__testing__ = exports.joinLetters = exports.hunspellInformationToSuggestionCostDef = void 0;
4
- const cspell_pipe_1 = require("@cspell/cspell-pipe");
4
+ const sync_1 = require("@cspell/cspell-pipe/sync");
5
5
  const text_1 = require("../utils/text");
6
6
  const util_1 = require("../utils/util");
7
7
  const mapCosts_1 = require("./mapCosts");
@@ -30,7 +30,7 @@ function hunspellInformationToSuggestionCostDef(hunInfo, locales) {
30
30
  .map((a) => a.trim())
31
31
  .filter((a) => regSupportedAff.test(a))
32
32
  .filter((a) => !rejectAff.test(a));
33
- const defs = (0, cspell_pipe_1.pipeSync)(lines, (0, cspell_pipe_1.opMap)((line) => (0, cspell_pipe_1.pipeSync)(operations, (0, cspell_pipe_1.opMap)((fn) => fn(line, costs)), (0, cspell_pipe_1.opMap)(asArrayOf), (0, cspell_pipe_1.opFlatten)())), (0, cspell_pipe_1.opFlatten)(), (0, cspell_pipe_1.opFilter)(util_1.isDefined));
33
+ const defs = (0, sync_1.pipe)(lines, (0, sync_1.opMap)((line) => (0, sync_1.pipe)(operations, (0, sync_1.opMap)((fn) => fn(line, costs)), (0, sync_1.opMap)(asArrayOf), (0, sync_1.opFlatten)())), (0, sync_1.opFlatten)(), (0, sync_1.opFilter)(util_1.isDefined));
34
34
  return [...defs];
35
35
  }
36
36
  return parseAff(hunInfo.aff, costs);
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.splitMap = exports.calcCostsForAccentedLetters = exports.parseAccents = exports.calcFirstCharacterReplace = exports.calcFirstCharacterReplaceDefs = exports.parseAlphabetCaps = exports.parseAlphabet = void 0;
4
- const cspell_pipe_1 = require("@cspell/cspell-pipe");
4
+ const sync_1 = require("@cspell/cspell-pipe/sync");
5
5
  const operators_1 = require("gensequence/dist/operators");
6
6
  const text_1 = require("../utils/text");
7
7
  const util_1 = require("../utils/util");
@@ -10,10 +10,10 @@ function parseAlphabet(cs, locale, editCost) {
10
10
  const { cost, penalty } = cs;
11
11
  const characters = (0, text_1.expandCharacterSet)(cs.characters);
12
12
  const charForms = [
13
- ...(0, cspell_pipe_1.pipeSync)(characters, (0, cspell_pipe_1.opMap)((c) => (0, text_1.caseForms)(c, locale).sort())),
13
+ ...(0, sync_1.pipe)(characters, (0, sync_1.opMap)((c) => (0, text_1.caseForms)(c, locale).sort())),
14
14
  ];
15
15
  const alphabet = (0, mapHunspellInformation_1.joinLetters)([
16
- ...(0, cspell_pipe_1.pipeSync)(charForms, (0, cspell_pipe_1.opFlatten)(), (0, cspell_pipe_1.opMap)((letter) => (0, text_1.accentForms)(letter)), (0, cspell_pipe_1.opFlatten)(), (0, cspell_pipe_1.opUnique)()),
16
+ ...(0, sync_1.pipe)(charForms, (0, sync_1.opFlatten)(), (0, sync_1.opMap)((letter) => (0, text_1.accentForms)(letter)), (0, sync_1.opFlatten)(), (0, sync_1.opUnique)()),
17
17
  ].sort());
18
18
  const sugAlpha = (0, util_1.clean)({
19
19
  map: alphabet,
@@ -32,7 +32,7 @@ exports.parseAlphabet = parseAlphabet;
32
32
  function parseAlphabetCaps(alphabet, locale, editCost) {
33
33
  const characters = (0, text_1.expandCharacterSet)(alphabet);
34
34
  const charForms = [
35
- ...(0, cspell_pipe_1.pipeSync)(characters, (0, cspell_pipe_1.opMap)((c) => (0, text_1.caseForms)(c, locale).sort())),
35
+ ...(0, sync_1.pipe)(characters, (0, sync_1.opMap)((c) => (0, text_1.caseForms)(c, locale).sort())),
36
36
  ];
37
37
  const caps = charForms.map((a) => (0, mapHunspellInformation_1.joinLetters)(a)).join('|');
38
38
  const sugCaps = {
@@ -48,7 +48,7 @@ function calcFirstCharacterReplaceDefs(alphabets, editCost) {
48
48
  exports.calcFirstCharacterReplaceDefs = calcFirstCharacterReplaceDefs;
49
49
  function calcFirstCharacterReplace(cs, editCost) {
50
50
  const mapOfFirstLetters = [
51
- ...(0, cspell_pipe_1.pipeSync)((0, text_1.expandCharacterSet)(cs.characters), (0, cspell_pipe_1.opUnique)(), (0, cspell_pipe_1.opMap)((letter) => `(^${letter})`)),
51
+ ...(0, sync_1.pipe)((0, text_1.expandCharacterSet)(cs.characters), (0, sync_1.opUnique)(), (0, sync_1.opMap)((letter) => `(^${letter})`)),
52
52
  ]
53
53
  .sort()
54
54
  .join('') + '(^)';
@@ -65,7 +65,7 @@ exports.calcFirstCharacterReplace = calcFirstCharacterReplace;
65
65
  function parseAccents(cs, _editCost) {
66
66
  const { cost, penalty } = cs;
67
67
  const accents = (0, mapHunspellInformation_1.joinLetters)([
68
- ...(0, cspell_pipe_1.pipeSync)((0, text_1.expandCharacterSet)(cs.characters), (0, operators_1.map)((char) => (0, text_1.stripNonAccents)(char))),
68
+ ...(0, sync_1.pipe)((0, text_1.expandCharacterSet)(cs.characters), (0, operators_1.map)((char) => (0, text_1.stripNonAccents)(char))),
69
69
  ]);
70
70
  if (!accents)
71
71
  return undefined;
@@ -79,9 +79,9 @@ function parseAccents(cs, _editCost) {
79
79
  exports.parseAccents = parseAccents;
80
80
  function calcCostsForAccentedLetters(simpleMap, locale, costs) {
81
81
  const charactersWithAccents = [
82
- ...(0, cspell_pipe_1.pipeSync)(splitMap(simpleMap), (0, cspell_pipe_1.opMap)((char) => (0, text_1.caseForms)(char, locale)), (0, cspell_pipe_1.opFlatten)(), (0, cspell_pipe_1.opMap)((char) => [...(0, text_1.accentForms)(char)]), (0, cspell_pipe_1.opFilter)((forms) => forms.length > 1)),
82
+ ...(0, sync_1.pipe)(splitMap(simpleMap), (0, sync_1.opMap)((char) => (0, text_1.caseForms)(char, locale)), (0, sync_1.opFlatten)(), (0, sync_1.opMap)((char) => [...(0, text_1.accentForms)(char)]), (0, sync_1.opFilter)((forms) => forms.length > 1)),
83
83
  ];
84
- const characters = (0, cspell_pipe_1.pipeSync)(charactersWithAccents, (0, cspell_pipe_1.opMap)((forms) => new Set([...forms, ...forms.map((char) => (0, text_1.stripAccents)(char))])), (0, cspell_pipe_1.opMap)((forms) => [...forms].sort()), (0, cspell_pipe_1.opFilter)((forms) => forms.length > 1), (0, cspell_pipe_1.opMap)(mapHunspellInformation_1.joinLetters), (0, cspell_pipe_1.opUnique)());
84
+ const characters = (0, sync_1.pipe)(charactersWithAccents, (0, sync_1.opMap)((forms) => new Set([...forms, ...forms.map((char) => (0, text_1.stripAccents)(char))])), (0, sync_1.opMap)((forms) => [...forms].sort()), (0, sync_1.opFilter)((forms) => forms.length > 1), (0, sync_1.opMap)(mapHunspellInformation_1.joinLetters), (0, sync_1.opUnique)());
85
85
  const replaceAccentMap = [...characters].join('|');
86
86
  const cost = costs.accentCosts;
87
87
  const costToReplaceAccent = !replaceAccentMap ? [] : [{ map: replaceAccentMap, replace: cost }];
@@ -18,6 +18,8 @@ export declare class Trie {
18
18
  readonly root: TrieRoot;
19
19
  private count?;
20
20
  private _options;
21
+ private _findOptionsDefaults;
22
+ private _findOptionsExact;
21
23
  readonly isLegacy: boolean;
22
24
  private hasForbidden;
23
25
  constructor(root: TrieRoot, count?: number | undefined);
@@ -91,6 +93,8 @@ export declare class Trie {
91
93
  private calcIsLegacy;
92
94
  static create(words: Iterable<string> | IterableIterator<string>, options?: PartialTrieOptions): Trie;
93
95
  private createFindOptions;
96
+ private lastCreateFindOptionsMatchCaseMap;
97
+ private createFindOptionsMatchCase;
94
98
  }
95
99
  export interface FindWordOptions {
96
100
  caseSensitive?: boolean;
package/dist/lib/trie.js CHANGED
@@ -27,9 +27,16 @@ class Trie {
27
27
  constructor(root, count) {
28
28
  this.root = root;
29
29
  this.count = count;
30
+ this.lastCreateFindOptionsMatchCaseMap = new Map();
30
31
  this._options = (0, trie_util_1.mergeOptionalWithDefaults)(root);
31
32
  this.isLegacy = this.calcIsLegacy();
32
33
  this.hasForbidden = !!root.c.get(root.forbiddenWordPrefix);
34
+ this._findOptionsDefaults = {
35
+ caseInsensitivePrefix: this._options.stripCaseAndAccentsPrefix,
36
+ compoundFix: this._options.compoundCharacter,
37
+ forbidPrefix: this._options.forbiddenWordPrefix,
38
+ };
39
+ this._findOptionsExact = this.createFindOptions({ compoundMode: 'none' });
33
40
  }
34
41
  /**
35
42
  * Number of words in the Trie
@@ -72,8 +79,7 @@ class Trie {
72
79
  return r.node;
73
80
  }
74
81
  findExact(text) {
75
- const options = this.createFindOptions({ compoundMode: 'none' });
76
- return (0, find_1.findWordNode)(this.root, text, options).node;
82
+ return (0, find_1.findWordNode)(this.root, text, this._findOptionsExact).node;
77
83
  }
78
84
  has(word, minLegacyCompoundLength) {
79
85
  if (this.hasWord(word, false))
@@ -105,7 +111,7 @@ class Trie {
105
111
  });
106
112
  return (0, find_1.findLegacyCompound)(this.root, word, findOptions);
107
113
  }
108
- const findOptions = this.createFindOptions({ matchCase: options?.caseSensitive });
114
+ const findOptions = this.createFindOptionsMatchCase(options?.caseSensitive);
109
115
  return (0, find_1.findWord)(this.root, word, findOptions);
110
116
  }
111
117
  /**
@@ -194,15 +200,20 @@ class Trie {
194
200
  return new Trie(root, undefined);
195
201
  }
196
202
  createFindOptions(options = {}) {
197
- const { caseInsensitivePrefix = this._options.stripCaseAndAccentsPrefix, compoundFix = this._options.compoundCharacter, forbidPrefix = this._options.forbiddenWordPrefix, } = (0, trie_util_1.clean)(options);
198
203
  const findOptions = (0, find_1.createFindOptions)({
204
+ ...this._findOptionsDefaults,
199
205
  ...options,
200
- caseInsensitivePrefix,
201
- compoundFix,
202
- forbidPrefix,
203
206
  });
204
207
  return findOptions;
205
208
  }
209
+ createFindOptionsMatchCase(matchCase) {
210
+ const f = this.lastCreateFindOptionsMatchCaseMap.get(matchCase);
211
+ if (f !== undefined)
212
+ return f;
213
+ const findOptions = this.createFindOptions({ matchCase });
214
+ this.lastCreateFindOptionsMatchCaseMap.set(matchCase, findOptions);
215
+ return findOptions;
216
+ }
206
217
  }
207
218
  exports.Trie = Trie;
208
219
  //# sourceMappingURL=trie.js.map
@@ -0,0 +1,2 @@
1
+ export declare function memorizeLastCall<P, R>(fn: (p: P) => R): (p: P) => R;
2
+ //# sourceMappingURL=memorizeLastCall.d.ts.map
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.memorizeLastCall = void 0;
4
+ const SymEmpty = Symbol('memorizeLastCall');
5
+ function memorizeLastCall(fn) {
6
+ let lastP = undefined;
7
+ let lastR = SymEmpty;
8
+ function calc(p) {
9
+ if (lastR !== SymEmpty && lastP === p)
10
+ return lastR;
11
+ lastP = p;
12
+ lastR = fn(p);
13
+ return lastR;
14
+ }
15
+ return calc;
16
+ }
17
+ exports.memorizeLastCall = memorizeLastCall;
18
+ //# sourceMappingURL=memorizeLastCall.js.map
@@ -28,4 +28,11 @@ export declare function regexQuote(text: string): string;
28
28
  * @param replaceWithText - the text to substitute.
29
29
  */
30
30
  export declare function replaceAllFactory(match: string, replaceWithText: string): (text: string) => string;
31
+ /**
32
+ * Shallow is Equal test.
33
+ * @param a - array of values
34
+ * @param b - array of values
35
+ * @returns true if the values of `a` are exactly equal to the values of `b`
36
+ */
37
+ export declare function isArrayEqual<K>(a: K[], b: K[]): boolean;
31
38
  //# sourceMappingURL=util.d.ts.map
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.replaceAllFactory = exports.regexQuote = exports.replaceAll = exports.flatten = exports.unique = exports.clean = exports.cleanCopy = exports.isDefined = void 0;
3
+ exports.isArrayEqual = exports.replaceAllFactory = exports.regexQuote = exports.replaceAll = exports.flatten = exports.unique = exports.clean = exports.cleanCopy = exports.isDefined = void 0;
4
4
  function isDefined(a) {
5
5
  return a !== undefined;
6
6
  }
@@ -64,4 +64,21 @@ function replaceAllFactory(match, replaceWithText) {
64
64
  return (text) => text.replace(r, replaceWithText);
65
65
  }
66
66
  exports.replaceAllFactory = replaceAllFactory;
67
+ /**
68
+ * Shallow is Equal test.
69
+ * @param a - array of values
70
+ * @param b - array of values
71
+ * @returns true if the values of `a` are exactly equal to the values of `b`
72
+ */
73
+ function isArrayEqual(a, b) {
74
+ if (a === b)
75
+ return true;
76
+ let isMatch = a.length === b.length;
77
+ const len = a.length;
78
+ for (let i = 0; isMatch && i < len; ++i) {
79
+ isMatch = a[i] === b[i];
80
+ }
81
+ return isMatch;
82
+ }
83
+ exports.isArrayEqual = isArrayEqual;
67
84
  //# sourceMappingURL=util.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cspell-trie-lib",
3
- "version": "6.10.1",
3
+ "version": "6.11.0",
4
4
  "description": "Trie Data Structure to support cspell.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -37,8 +37,8 @@
37
37
  },
38
38
  "homepage": "https://github.com/streetsidesoftware/cspell#readme",
39
39
  "dependencies": {
40
- "@cspell/cspell-pipe": "^6.10.1",
41
- "@cspell/cspell-types": "^6.10.1",
40
+ "@cspell/cspell-pipe": "^6.11.0",
41
+ "@cspell/cspell-types": "^6.11.0",
42
42
  "fs-extra": "^10.1.0",
43
43
  "gensequence": "^4.0.2"
44
44
  },
@@ -47,11 +47,11 @@
47
47
  },
48
48
  "devDependencies": {
49
49
  "@cspell/dict-en_us": "^2.3.3",
50
- "@cspell/dict-es-es": "^2.2.1",
50
+ "@cspell/dict-es-es": "^2.2.2",
51
51
  "@types/fs-extra": "^9.0.13",
52
- "@types/node": "^18.7.18",
53
- "jest": "^29.0.3",
52
+ "@types/node": "^18.7.23",
53
+ "jest": "^29.1.1",
54
54
  "rimraf": "^3.0.2"
55
55
  },
56
- "gitHead": "03625a3f7ca4ef85f3549ccae73d90d2cd3ac0f0"
56
+ "gitHead": "664676692898c3b6aa150fb2c6656b56f49a54b8"
57
57
  }