cspell-dictionary 7.3.9 → 8.0.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.
@@ -3,17 +3,20 @@ import { canonicalSearchOptions } from './SpellingDictionaryMethods.js';
3
3
  let dictionaryCounter = 0;
4
4
  const DefaultAutoCacheSize = 1000;
5
5
  class CachedDict {
6
+ dict;
7
+ options;
8
+ name;
9
+ id = ++dictionaryCounter;
6
10
  constructor(dict, options) {
7
11
  this.dict = dict;
8
12
  this.options = options;
9
- this.id = ++dictionaryCounter;
10
- this.has = autoCache((word) => this.dict.has(word, this.options), DefaultAutoCacheSize);
11
- this.isNoSuggestWord = autoCache((word) => this.dict.isNoSuggestWord(word, this.options), DefaultAutoCacheSize);
12
- this.isForbidden = autoCache((word) => this.dict.isForbidden(word), DefaultAutoCacheSize);
13
- this.getPreferredSuggestions = autoCache((word) => this.dict.getPreferredSuggestions?.(word), DefaultAutoCacheSize);
14
13
  this.name = dict.name;
15
14
  // console.log(`CachedDict for ${this.name}`);
16
15
  }
16
+ has = autoCache((word) => this.dict.has(word, this.options), DefaultAutoCacheSize);
17
+ isNoSuggestWord = autoCache((word) => this.dict.isNoSuggestWord(word, this.options), DefaultAutoCacheSize);
18
+ isForbidden = autoCache((word) => this.dict.isForbidden(word), DefaultAutoCacheSize);
19
+ getPreferredSuggestions = autoCache((word) => this.dict.getPreferredSuggestions?.(word), DefaultAutoCacheSize);
17
20
  stats() {
18
21
  return {
19
22
  name: this.name,
@@ -6,13 +6,14 @@ import { defaultOptions } from './SpellingDictionary.js';
6
6
  import { SpellingDictionaryFromTrie } from './SpellingDictionaryFromTrie.js';
7
7
  import { createTyposDictionary } from './TyposDictionary.js';
8
8
  class FlagWordsDictionaryTrie extends SpellingDictionaryFromTrie {
9
+ name;
10
+ source;
11
+ containsNoSuggestWords = false;
12
+ options = {};
9
13
  constructor(trie, name, source) {
10
14
  super(trie, name, defaultOptions, source);
11
15
  this.name = name;
12
16
  this.source = source;
13
- this.containsNoSuggestWords = false;
14
- this.options = {};
15
- this.isDictionaryCaseSensitive = true;
16
17
  }
17
18
  /**
18
19
  * A Forbidden word list does not "have" valid words.
@@ -36,17 +37,21 @@ class FlagWordsDictionaryTrie extends SpellingDictionaryFromTrie {
36
37
  genSuggestions() {
37
38
  return;
38
39
  }
40
+ isDictionaryCaseSensitive = true;
39
41
  }
40
42
  class FlagWordsDictionary {
43
+ name;
44
+ source;
45
+ dictTypos;
46
+ dictTrie;
47
+ containsNoSuggestWords = false;
48
+ options = {};
49
+ type = 'flag-words';
41
50
  constructor(name, source, dictTypos, dictTrie) {
42
51
  this.name = name;
43
52
  this.source = source;
44
53
  this.dictTypos = dictTypos;
45
54
  this.dictTrie = dictTrie;
46
- this.containsNoSuggestWords = false;
47
- this.options = {};
48
- this.type = 'flag-words';
49
- this.isDictionaryCaseSensitive = true;
50
55
  }
51
56
  /**
52
57
  * A Forbidden word list does not "have" valid words.
@@ -90,6 +95,7 @@ class FlagWordsDictionary {
90
95
  get size() {
91
96
  return this.dictTypos.size + (this.dictTrie?.size || 0);
92
97
  }
98
+ isDictionaryCaseSensitive = true;
93
99
  getErrors() {
94
100
  return [];
95
101
  }
@@ -5,13 +5,16 @@ import { createSpellingDictionary } from './createSpellingDictionary.js';
5
5
  import * as Defaults from './defaults.js';
6
6
  const NormalizeForm = 'NFC';
7
7
  class IgnoreWordsDictionary {
8
+ name;
9
+ source;
10
+ dict;
11
+ dictNonStrict;
12
+ containsNoSuggestWords = true;
13
+ options = {};
14
+ type = 'ignore';
8
15
  constructor(name, source, words) {
9
16
  this.name = name;
10
17
  this.source = source;
11
- this.containsNoSuggestWords = true;
12
- this.options = {};
13
- this.type = 'ignore';
14
- this.isDictionaryCaseSensitive = true;
15
18
  this.dict = new Set(words);
16
19
  this.dictNonStrict = new Set(pipe(this.dict, opFilter((w) => w.startsWith('~')), opMap((w) => w.slice(1))));
17
20
  }
@@ -65,6 +68,7 @@ class IgnoreWordsDictionary {
65
68
  get size() {
66
69
  return this.dict.size;
67
70
  }
71
+ isDictionaryCaseSensitive = true;
68
72
  getErrors() {
69
73
  return [];
70
74
  }
@@ -7,17 +7,17 @@ function identityString(w) {
7
7
  return w;
8
8
  }
9
9
  class SpellingDictionaryCollectionImpl {
10
+ dictionaries;
11
+ name;
12
+ options = { weightMap: undefined };
13
+ mapWord = identityString;
14
+ type = 'SpellingDictionaryCollection';
15
+ source;
16
+ isDictionaryCaseSensitive;
17
+ containsNoSuggestWords;
10
18
  constructor(dictionaries, name, source) {
11
19
  this.dictionaries = dictionaries;
12
20
  this.name = name;
13
- this.options = { weightMap: undefined };
14
- this.mapWord = identityString;
15
- this.type = 'SpellingDictionaryCollection';
16
- this._isNoSuggestWord = (word, options) => {
17
- if (!this.containsNoSuggestWords)
18
- return false;
19
- return !!isNoSuggestWordInAnyDictionary(this.dictionaries, word, options || {});
20
- };
21
21
  this.dictionaries = this.dictionaries.sort((a, b) => b.size - a.size);
22
22
  this.source = source || dictionaries.map((d) => d.name).join(', ');
23
23
  this.isDictionaryCaseSensitive = this.dictionaries.reduce((a, b) => a || b.isDictionaryCaseSensitive, false);
@@ -88,6 +88,11 @@ class SpellingDictionaryCollectionImpl {
88
88
  _isForbiddenInDict(word, ignoreCase) {
89
89
  return isWordForbiddenInAnyDictionary(this.dictionaries, word, ignoreCase);
90
90
  }
91
+ _isNoSuggestWord = (word, options) => {
92
+ if (!this.containsNoSuggestWords)
93
+ return false;
94
+ return !!isNoSuggestWordInAnyDictionary(this.dictionaries, word, options || {});
95
+ };
91
96
  }
92
97
  export function createCollection(dictionaries, name, source) {
93
98
  return new SpellingDictionaryCollectionImpl(dictionaries, name, source);
@@ -8,19 +8,25 @@ import { createWeightMapFromDictionaryInformation, defaultNumSuggestions, hasOpt
8
8
  const findWordOptionsCaseSensitive = Object.freeze({ caseSensitive: true });
9
9
  const findWordOptionsNotCaseSensitive = Object.freeze({ caseSensitive: false });
10
10
  export class SpellingDictionaryFromTrie {
11
+ trie;
12
+ name;
13
+ options;
14
+ source;
15
+ static cachedWordsLimit = 50000;
16
+ _size = 0;
17
+ knownWords = new Set();
18
+ unknownWords = new Set();
19
+ mapWord;
20
+ remapWord;
21
+ type = 'SpellingDictionaryFromTrie';
22
+ isDictionaryCaseSensitive;
23
+ containsNoSuggestWords;
24
+ weightMap;
11
25
  constructor(trie, name, options, source = 'from trie', size) {
12
26
  this.trie = trie;
13
27
  this.name = name;
14
28
  this.options = options;
15
29
  this.source = source;
16
- this._size = 0;
17
- this.knownWords = new Set();
18
- this.unknownWords = new Set();
19
- this.type = 'SpellingDictionaryFromTrie';
20
- this._find = findCache((word, useCompounds, ignoreCase) => this.findAnyForm(word, useCompounds, ignoreCase));
21
- this._isForbidden = autoCache((word) => {
22
- return this.trie.isForbiddenWord(word);
23
- });
24
30
  this.mapWord = createMapper(options.repMap, options.dictionaryInformation?.ignore);
25
31
  this.remapWord = createRepMapper(options.repMap, options.dictionaryInformation?.ignore);
26
32
  this.isDictionaryCaseSensitive = options.caseSensitive ?? trie.isCaseAware;
@@ -63,6 +69,7 @@ export class SpellingDictionaryFromTrie {
63
69
  const { useCompounds = this.options.useCompounds, ignoreCase = Defaults.ignoreCase } = hasOptionToSearchOption(hasOptions);
64
70
  return { useCompounds, ignoreCase };
65
71
  }
72
+ _find = findCache((word, useCompounds, ignoreCase) => this.findAnyForm(word, useCompounds, ignoreCase));
66
73
  findAnyForm(word, useCompounds, ignoreCase) {
67
74
  const outerForms = outerWordForms(word, this.remapWord ? this.remapWord : (word) => [this.mapWord(word)]);
68
75
  for (const form of outerForms) {
@@ -102,6 +109,9 @@ export class SpellingDictionaryFromTrie {
102
109
  isForbidden(word, _ignoreCaseAndAccents) {
103
110
  return this._isForbidden(word);
104
111
  }
112
+ _isForbidden = autoCache((word) => {
113
+ return this.trie.isForbiddenWord(word);
114
+ });
105
115
  suggest(word, suggestOptions = {}) {
106
116
  return this._suggest(word, suggestOptions);
107
117
  }
@@ -133,7 +143,6 @@ export class SpellingDictionaryFromTrie {
133
143
  return [];
134
144
  }
135
145
  }
136
- SpellingDictionaryFromTrie.cachedWordsLimit = 50000;
137
146
  /**
138
147
  * Create a dictionary from a trie file.
139
148
  * @param data - contents of a trie file.
@@ -5,14 +5,24 @@ import * as defaults from './defaults.js';
5
5
  import { processEntriesToTyposDef } from './Typos/index.js';
6
6
  import { extractAllSuggestions } from './Typos/util.js';
7
7
  class SuggestDictionaryImpl {
8
+ name;
9
+ source;
10
+ typosDef;
11
+ containsNoSuggestWords = false;
12
+ options = {};
13
+ type = 'suggest';
14
+ size;
15
+ /**
16
+ * Note: ignoreWordsLower is only suggestions with the case and accents removed.
17
+ * The logic is that if someone explicity ignored an upper case version, it does not
18
+ * mean that the lower case version is ok.
19
+ */
20
+ suggestions;
21
+ suggestionsLower;
8
22
  constructor(name, source, typosDef) {
9
23
  this.name = name;
10
24
  this.source = source;
11
25
  this.typosDef = typosDef;
12
- this.containsNoSuggestWords = false;
13
- this.options = {};
14
- this.type = 'suggest';
15
- this.isDictionaryCaseSensitive = true;
16
26
  this.size = Object.keys(typosDef).length;
17
27
  this.suggestions = extractAllSuggestions(typosDef);
18
28
  this.suggestionsLower = new Set(pipe(this.suggestions, mapperRemoveCaseAndAccents));
@@ -80,6 +90,7 @@ class SuggestDictionaryImpl {
80
90
  mapWord(word) {
81
91
  return word;
82
92
  }
93
+ isDictionaryCaseSensitive = true;
83
94
  getErrors() {
84
95
  return [];
85
96
  }
@@ -5,13 +5,26 @@ import * as defaults from './defaults.js';
5
5
  import { processEntriesToTyposDef } from './Typos/index.js';
6
6
  import { extractAllSuggestions, extractIgnoreValues } from './Typos/util.js';
7
7
  class TyposDictionaryImpl {
8
+ name;
9
+ source;
10
+ typosDef;
11
+ containsNoSuggestWords;
12
+ options = {};
13
+ type = 'typos';
14
+ size;
15
+ ignoreWords;
16
+ /**
17
+ * Note: ignoreWordsLower is only suggestions with the case and accents removed.
18
+ * The logic is that if someone explicity ignored an upper case version, it does not
19
+ * mean that the lower case version is ok.
20
+ */
21
+ suggestions;
22
+ suggestionsLower;
23
+ explicitIgnoreWords;
8
24
  constructor(name, source, typosDef, ignoreList) {
9
25
  this.name = name;
10
26
  this.source = source;
11
27
  this.typosDef = typosDef;
12
- this.options = {};
13
- this.type = 'typos';
14
- this.isDictionaryCaseSensitive = true;
15
28
  this.size = Object.keys(typosDef).length;
16
29
  this.explicitIgnoreWords = extractIgnoreValues(typosDef, '!');
17
30
  this.suggestions = extractAllSuggestions(typosDef);
@@ -112,6 +125,7 @@ class TyposDictionaryImpl {
112
125
  mapWord(word) {
113
126
  return word;
114
127
  }
128
+ isDictionaryCaseSensitive = true;
115
129
  getErrors() {
116
130
  return [];
117
131
  }
@@ -1,13 +1,14 @@
1
1
  const CACHE_SIZE = 100;
2
2
  class Cache01 {
3
+ maxSize;
4
+ count = 0;
5
+ cache0 = Object.create(null);
6
+ cache1 = Object.create(null);
7
+ hits = 0;
8
+ misses = 0;
9
+ swaps = 0;
3
10
  constructor(maxSize) {
4
11
  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
  }
12
13
  get(key) {
13
14
  const cache0 = this.cache0;
@@ -7,9 +7,7 @@ export function autoResolve(map, key, resolve) {
7
7
  return value;
8
8
  }
9
9
  export class AutoResolveCache {
10
- constructor() {
11
- this.map = new Map();
12
- }
10
+ map = new Map();
13
11
  get(k, resolve) {
14
12
  return resolve ? autoResolve(this.map, k, resolve) : this.map.get(k);
15
13
  }
@@ -33,9 +31,7 @@ export function autoResolveWeak(map, key, resolve) {
33
31
  return value;
34
32
  }
35
33
  export class AutoResolveWeakCache {
36
- constructor() {
37
- this.map = new WeakMap();
38
- }
34
+ map = new WeakMap();
39
35
  get(k, resolve) {
40
36
  return resolve ? autoResolveWeak(this.map, k, resolve) : this.map.get(k);
41
37
  }
@@ -1,10 +1,11 @@
1
1
  export class SimpleWeakCache {
2
+ size;
3
+ L0 = new WeakMap();
4
+ L1 = new WeakMap();
5
+ L2 = new WeakMap();
6
+ sizeL0 = 0;
2
7
  constructor(size) {
3
8
  this.size = size;
4
- this.L0 = new WeakMap();
5
- this.L1 = new WeakMap();
6
- this.L2 = new WeakMap();
7
- this.sizeL0 = 0;
8
9
  }
9
10
  has(key) {
10
11
  for (const c of this.caches()) {
@@ -50,6 +51,7 @@ export class SimpleWeakCache {
50
51
  }
51
52
  }
52
53
  export class AutoWeakCache extends SimpleWeakCache {
54
+ factory;
53
55
  constructor(factory, size) {
54
56
  super(size);
55
57
  this.factory = factory;
@@ -72,11 +74,12 @@ export class AutoWeakCache extends SimpleWeakCache {
72
74
  * promoted to L0.
73
75
  */
74
76
  export class SimpleCache {
77
+ size;
78
+ L0 = new Map();
79
+ L1 = new Map();
80
+ L2 = new Map();
75
81
  constructor(size) {
76
82
  this.size = size;
77
- this.L0 = new Map();
78
- this.L1 = new Map();
79
- this.L2 = new Map();
80
83
  }
81
84
  has(key) {
82
85
  for (const c of this.caches()) {
@@ -120,6 +123,7 @@ export class SimpleCache {
120
123
  }
121
124
  }
122
125
  export class AutoCache extends SimpleCache {
126
+ factory;
123
127
  constructor(factory, size) {
124
128
  super(size);
125
129
  this.factory = factory;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cspell-dictionary",
3
- "version": "7.3.9",
3
+ "version": "8.0.0",
4
4
  "description": "A spelling dictionary library useful for checking words and getting suggestions.",
5
5
  "type": "module",
6
6
  "types": "dist/index.d.ts",
@@ -42,14 +42,14 @@
42
42
  },
43
43
  "homepage": "https://github.com/streetsidesoftware/cspell#readme",
44
44
  "engines": {
45
- "node": ">=16"
45
+ "node": ">=18"
46
46
  },
47
47
  "dependencies": {
48
- "@cspell/cspell-pipe": "7.3.9",
49
- "@cspell/cspell-types": "7.3.9",
50
- "cspell-trie-lib": "7.3.9",
48
+ "@cspell/cspell-pipe": "8.0.0",
49
+ "@cspell/cspell-types": "8.0.0",
50
+ "cspell-trie-lib": "8.0.0",
51
51
  "fast-equals": "^4.0.3",
52
52
  "gensequence": "^6.0.0"
53
53
  },
54
- "gitHead": "77c7fb3ffd80a626ec07d704cefcaa21d62fd460"
54
+ "gitHead": "67c22bf98baed1c17bbc658fba8656262d17e370"
55
55
  }