cspell-dictionary 8.18.0 → 8.19.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.
@@ -1,6 +1,6 @@
1
1
  import type { DictionaryInformation, ReplaceMap } from '@cspell/cspell-types';
2
2
  import type { SuggestionCollector, SuggestionResult, WeightMap } from 'cspell-trie-lib';
3
- import type { SuggestOptions } from './SuggestOptions.js';
3
+ import type { SuggestOptionsRO } from './SuggestOptions.js';
4
4
  export type { DictionaryDefinitionInline } from '@cspell/cspell-types';
5
5
  export interface SearchOptions {
6
6
  /**
@@ -14,7 +14,9 @@ export interface SearchOptions {
14
14
  */
15
15
  ignoreCase?: boolean | undefined;
16
16
  }
17
+ export type SearchOptionsRO = Readonly<SearchOptions>;
17
18
  export type FindOptions = SearchOptions;
19
+ export type FindOptionsRO = Readonly<FindOptions>;
18
20
  export interface Suggestion {
19
21
  word: string;
20
22
  isPreferred?: boolean | undefined;
@@ -31,6 +33,7 @@ export interface FindResult {
31
33
  noSuggest: boolean;
32
34
  }
33
35
  export type HasOptions = SearchOptions;
36
+ export type HasOptionsRO = Readonly<HasOptions>;
34
37
  export type IgnoreCaseOption = boolean;
35
38
  export interface SpellingDictionaryOptions {
36
39
  repMap?: ReplaceMap;
@@ -42,6 +45,13 @@ export interface SpellingDictionaryOptions {
42
45
  * This is a NO Suggest dictionary used for words to be ignored.
43
46
  */
44
47
  noSuggest?: boolean | undefined;
48
+ /**
49
+ * Some dictionaries may contain flagged words that are not valid in the language. These are often
50
+ * words that are used in other languages or might be generated through compounding.
51
+ * This setting allows flagged words to be ignored when checking the dictionary.
52
+ * The effect is similar to the word not being in the dictionary.
53
+ */
54
+ ignoreForbiddenWords?: boolean | undefined;
45
55
  /**
46
56
  * Extra dictionary information used in improving suggestions
47
57
  * based upon locale.
@@ -67,6 +77,7 @@ export interface SpellingDictionaryOptions {
67
77
  */
68
78
  weightMap?: WeightMap | undefined;
69
79
  }
80
+ export type SpellingDictionaryOptionsRO = Readonly<SpellingDictionaryOptions>;
70
81
  export interface DictionaryInfo {
71
82
  /** The name of the dictionary */
72
83
  readonly name: string;
@@ -78,9 +89,9 @@ export interface DictionaryInfo {
78
89
  export interface SpellingDictionary extends DictionaryInfo {
79
90
  readonly type: string;
80
91
  readonly containsNoSuggestWords: boolean;
81
- has(word: string, options?: HasOptions): boolean;
92
+ has(word: string, options?: HasOptionsRO): boolean;
82
93
  /** A more detailed search for a word, might take longer than `has` */
83
- find(word: string, options?: SearchOptions): FindResult | undefined;
94
+ find(word: string, options?: SearchOptionsRO): FindResult | undefined;
84
95
  /**
85
96
  * Checks if a word is forbidden.
86
97
  * @param word - word to check.
@@ -93,15 +104,15 @@ export interface SpellingDictionary extends DictionaryInfo {
93
104
  * @param word - word to check
94
105
  * @param options - options
95
106
  */
96
- isNoSuggestWord(word: string, options: HasOptions): boolean;
107
+ isNoSuggestWord(word: string, options: HasOptionsRO): boolean;
97
108
  /**
98
109
  * Generate suggestions for a word
99
110
  * @param word - word
100
111
  * @param suggestOptions - options
101
112
  */
102
- suggest(word: string, suggestOptions?: SuggestOptions): SuggestionResult[];
113
+ suggest(word: string, suggestOptions?: SuggestOptionsRO): SuggestionResult[];
103
114
  getPreferredSuggestions?: (word: string) => PreferredSuggestion[];
104
- genSuggestions(collector: SuggestionCollector, suggestOptions: SuggestOptions): void;
115
+ genSuggestions(collector: SuggestionCollector, suggestOptions: SuggestOptionsRO): void;
105
116
  mapWord(word: string): string;
106
117
  /**
107
118
  * Generates all possible word combinations by applying `repMap`.
@@ -1,11 +1,11 @@
1
- import type { SearchOptions, SpellingDictionary } from './SpellingDictionary.js';
1
+ import type { SearchOptionsRO, SpellingDictionary } from './SpellingDictionary.js';
2
2
  export interface SpellingDictionaryCollection extends SpellingDictionary {
3
3
  readonly type: 'SpellingDictionaryCollection';
4
4
  readonly dictionaries: SpellingDictionary[];
5
5
  getErrors(): Error[];
6
6
  }
7
7
  export declare function createCollection(dictionaries: SpellingDictionary[], name: string, source?: string): SpellingDictionaryCollection;
8
- declare function isWordInAnyDictionary(dicts: SpellingDictionary[], word: string, options: SearchOptions): SpellingDictionary | undefined;
8
+ declare function isWordInAnyDictionary(dicts: SpellingDictionary[], word: string, options: SearchOptionsRO): SpellingDictionary | undefined;
9
9
  declare function isWordForbiddenInAnyDictionary(dicts: SpellingDictionary[], word: string, ignoreCase: boolean | undefined): SpellingDictionary | undefined;
10
10
  export declare function isSpellingDictionaryCollection(dict: SpellingDictionary): dict is SpellingDictionaryCollection;
11
11
  export declare const __testing__: {
@@ -1,10 +1,11 @@
1
1
  import type { ITrie, SuggestionCollector, SuggestionResult } from 'cspell-trie-lib';
2
- import type { FindResult, HasOptions, SpellingDictionary, SpellingDictionaryOptions } from './SpellingDictionary.js';
2
+ import type { FindResult, HasOptionsRO, SpellingDictionary, SpellingDictionaryOptionsRO } from './SpellingDictionary.js';
3
3
  import type { SuggestOptions } from './SuggestOptions.js';
4
4
  export declare class SpellingDictionaryFromTrie implements SpellingDictionary {
5
+ #private;
5
6
  readonly trie: ITrie;
6
7
  readonly name: string;
7
- readonly options: SpellingDictionaryOptions;
8
+ readonly options: SpellingDictionaryOptionsRO;
8
9
  readonly source: string;
9
10
  private _size;
10
11
  readonly knownWords: Set<string>;
@@ -15,15 +16,15 @@ export declare class SpellingDictionaryFromTrie implements SpellingDictionary {
15
16
  readonly isDictionaryCaseSensitive: boolean;
16
17
  readonly containsNoSuggestWords: boolean;
17
18
  private weightMap;
18
- constructor(trie: ITrie, name: string, options: SpellingDictionaryOptions, source?: string, size?: number);
19
+ constructor(trie: ITrie, name: string, options: SpellingDictionaryOptionsRO, source?: string, size?: number);
19
20
  get size(): number;
20
- has(word: string, hasOptions?: HasOptions): boolean;
21
- find(word: string, hasOptions?: HasOptions): FindResult | undefined;
21
+ has(word: string, hasOptions?: HasOptionsRO): boolean;
22
+ find(word: string, hasOptions?: HasOptionsRO): FindResult | undefined;
22
23
  private resolveOptions;
23
24
  private _find;
24
25
  private findAnyForm;
25
26
  private _findAnyForm;
26
- isNoSuggestWord(word: string, options?: HasOptions): boolean;
27
+ isNoSuggestWord(word: string, options?: HasOptionsRO): boolean;
27
28
  isForbidden(word: string, _ignoreCaseAndAccents?: boolean): boolean;
28
29
  suggest(word: string, suggestOptions?: SuggestOptions): SuggestionResult[];
29
30
  private _suggest;
@@ -38,7 +39,7 @@ export declare class SpellingDictionaryFromTrie implements SpellingDictionary {
38
39
  * @param options - options.
39
40
  * @returns SpellingDictionary
40
41
  */
41
- export declare function createSpellingDictionaryFromTrieFile(data: string | Buffer, name: string, source: string, options: SpellingDictionaryOptions): SpellingDictionary;
42
+ export declare function createSpellingDictionaryFromTrieFile(data: string | Buffer, name: string, source: string, options: SpellingDictionaryOptionsRO): SpellingDictionary;
42
43
  declare function outerWordForms(word: string, mapWord: (word: string) => string[]): Iterable<string>;
43
44
  export declare const __testing__: {
44
45
  outerWordForms: typeof outerWordForms;
@@ -2,9 +2,7 @@ import { CompoundWordsMethod, decodeTrie, suggestionCollector } from 'cspell-tri
2
2
  import { clean } from '../util/clean.js';
3
3
  import { createMapper, createRepMapper } from '../util/repMap.js';
4
4
  import * as Defaults from './defaults.js';
5
- import { createWeightMapFromDictionaryInformation, defaultNumSuggestions, hasOptionToSearchOption, impersonateCollector, wordSearchForms, wordSuggestFormsArray, } from './SpellingDictionaryMethods.js';
6
- const findWordOptionsCaseSensitive = Object.freeze({ caseSensitive: true });
7
- const findWordOptionsNotCaseSensitive = Object.freeze({ caseSensitive: false });
5
+ import { createWeightMapFromDictionaryInformation, defaultNumSuggestions, hasOptionToSearchOption, impersonateCollector, wordSearchForms, wordSuggestForms, } from './SpellingDictionaryMethods.js';
8
6
  export class SpellingDictionaryFromTrie {
9
7
  trie;
10
8
  name;
@@ -18,6 +16,9 @@ export class SpellingDictionaryFromTrie {
18
16
  type = 'SpellingDictionaryFromTrie';
19
17
  isDictionaryCaseSensitive;
20
18
  containsNoSuggestWords;
19
+ #ignoreForbiddenWords = false;
20
+ #findWordOptionsCaseSensitive = { caseSensitive: true };
21
+ #findWordOptionsNotCaseSensitive = { caseSensitive: false };
21
22
  weightMap;
22
23
  constructor(trie, name, options, source = 'from trie', size) {
23
24
  this.trie = trie;
@@ -30,6 +31,11 @@ export class SpellingDictionaryFromTrie {
30
31
  this.containsNoSuggestWords = options.noSuggest || false;
31
32
  this._size = size || 0;
32
33
  this.weightMap = options.weightMap || createWeightMapFromDictionaryInformation(options.dictionaryInformation);
34
+ this.#ignoreForbiddenWords = !!options.ignoreForbiddenWords;
35
+ if (this.#ignoreForbiddenWords) {
36
+ this.#findWordOptionsCaseSensitive.checkForbidden = true;
37
+ this.#findWordOptionsNotCaseSensitive.checkForbidden = true;
38
+ }
33
39
  }
34
40
  get size() {
35
41
  if (!this._size) {
@@ -50,12 +56,15 @@ export class SpellingDictionaryFromTrie {
50
56
  has(word, hasOptions) {
51
57
  const { useCompounds, ignoreCase } = this.resolveOptions(hasOptions);
52
58
  const r = this._find(word, useCompounds, ignoreCase);
53
- return !!r && !r.forbidden && !!r.found;
59
+ return (r && !r.forbidden && !!r.found) || false;
54
60
  }
55
61
  find(word, hasOptions) {
56
62
  const { useCompounds, ignoreCase } = this.resolveOptions(hasOptions);
57
63
  const r = this._find(word, useCompounds, ignoreCase);
58
- const { forbidden = this.isForbidden(word) } = r || {};
64
+ const { forbidden = this.#isForbidden(word) } = r || {};
65
+ if (this.#ignoreForbiddenWords && forbidden) {
66
+ return undefined;
67
+ }
59
68
  if (!r && !forbidden)
60
69
  return undefined;
61
70
  const { found = forbidden ? word : false } = r || {};
@@ -77,7 +86,9 @@ export class SpellingDictionaryFromTrie {
77
86
  return undefined;
78
87
  }
79
88
  _findAnyForm(mWord, useCompounds, ignoreCase) {
80
- const opts = ignoreCase ? findWordOptionsNotCaseSensitive : findWordOptionsCaseSensitive;
89
+ const opts = ignoreCase
90
+ ? this.#findWordOptionsNotCaseSensitive
91
+ : this.#findWordOptionsCaseSensitive;
81
92
  const findResult = this.trie.findWord(mWord, opts);
82
93
  if (findResult.found !== false) {
83
94
  return findResult;
@@ -104,6 +115,9 @@ export class SpellingDictionaryFromTrie {
104
115
  return this.containsNoSuggestWords ? this.has(word, options) : false;
105
116
  }
106
117
  isForbidden(word, _ignoreCaseAndAccents) {
118
+ return this.#ignoreForbiddenWords ? false : this.#isForbidden(word, _ignoreCaseAndAccents);
119
+ }
120
+ #isForbidden(word, _ignoreCaseAndAccents) {
107
121
  return this.trie.isForbiddenWord(word);
108
122
  }
109
123
  suggest(word, suggestOptions = {}) {
@@ -131,7 +145,9 @@ export class SpellingDictionaryFromTrie {
131
145
  return;
132
146
  const _compoundMethod = suggestOptions.compoundMethod ??
133
147
  (this.options.useCompounds ? CompoundWordsMethod.JOIN_WORDS : CompoundWordsMethod.NONE);
134
- wordSuggestFormsArray(collector.word).forEach((w) => this.trie.genSuggestions(impersonateCollector(collector, w), _compoundMethod));
148
+ for (const w of wordSuggestForms(collector.word)) {
149
+ this.trie.genSuggestions(impersonateCollector(collector, w), _compoundMethod);
150
+ }
135
151
  }
136
152
  getErrors() {
137
153
  return [];
@@ -29,5 +29,6 @@ export interface SuggestOptions {
29
29
  */
30
30
  timeout?: number | undefined;
31
31
  }
32
+ export type SuggestOptionsRO = Readonly<SuggestOptions>;
32
33
  export declare function createSuggestOptions(numSuggestions?: number, compoundMethod?: CompoundWordsMethod, numChanges?: number, ignoreCase?: boolean): SuggestOptions;
33
34
  //# sourceMappingURL=SuggestOptions.d.ts.map
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "access": "public",
5
5
  "provenance": true
6
6
  },
7
- "version": "8.18.0",
7
+ "version": "8.19.0",
8
8
  "description": "A spelling dictionary library useful for checking words and getting suggestions.",
9
9
  "type": "module",
10
10
  "sideEffects": false,
@@ -54,14 +54,14 @@
54
54
  "node": ">=18"
55
55
  },
56
56
  "dependencies": {
57
- "@cspell/cspell-pipe": "8.18.0",
58
- "@cspell/cspell-types": "8.18.0",
59
- "cspell-trie-lib": "8.18.0",
57
+ "@cspell/cspell-pipe": "8.19.0",
58
+ "@cspell/cspell-types": "8.19.0",
59
+ "cspell-trie-lib": "8.19.0",
60
60
  "fast-equals": "^5.2.2"
61
61
  },
62
62
  "devDependencies": {
63
63
  "gensequence": "^7.0.0",
64
64
  "lorem-ipsum": "^2.0.8"
65
65
  },
66
- "gitHead": "6c7297918e9ff4f1c4fa1501d119d58bf4ce2237"
66
+ "gitHead": "ebfecabea342ba34ee368178f3864a7a194936d5"
67
67
  }