cmpstr 2.0.2 → 3.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.
- package/LICENSE +21 -21
- package/README.md +75 -499
- package/dist/CmpStr.esm.js +4863 -0
- package/dist/CmpStr.esm.js.map +1 -0
- package/dist/CmpStr.esm.min.js +8 -0
- package/dist/CmpStr.esm.min.js.map +1 -0
- package/dist/CmpStr.umd.js +4875 -0
- package/dist/CmpStr.umd.js.map +1 -0
- package/dist/CmpStr.umd.min.js +8 -0
- package/dist/CmpStr.umd.min.js.map +1 -0
- package/dist/cjs/CmpStr.js +663 -0
- package/dist/cjs/CmpStr.js.map +1 -0
- package/dist/cjs/CmpStrAsync.js +336 -0
- package/dist/cjs/CmpStrAsync.js.map +1 -0
- package/dist/cjs/index.js +15 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/metric/Cosine.js +101 -0
- package/dist/cjs/metric/Cosine.js.map +1 -0
- package/dist/cjs/metric/DamerauLevenshtein.js +110 -0
- package/dist/cjs/metric/DamerauLevenshtein.js.map +1 -0
- package/dist/cjs/metric/DiceSorensen.js +91 -0
- package/dist/cjs/metric/DiceSorensen.js.map +1 -0
- package/dist/cjs/metric/Hamming.js +82 -0
- package/dist/cjs/metric/Hamming.js.map +1 -0
- package/dist/cjs/metric/Jaccard.js +76 -0
- package/dist/cjs/metric/Jaccard.js.map +1 -0
- package/dist/cjs/metric/JaroWinkler.js +114 -0
- package/dist/cjs/metric/JaroWinkler.js.map +1 -0
- package/dist/cjs/metric/LCS.js +89 -0
- package/dist/cjs/metric/LCS.js.map +1 -0
- package/dist/cjs/metric/Levenshtein.js +94 -0
- package/dist/cjs/metric/Levenshtein.js.map +1 -0
- package/dist/cjs/metric/Metric.js +445 -0
- package/dist/cjs/metric/Metric.js.map +1 -0
- package/dist/cjs/metric/NeedlemanWunsch.js +95 -0
- package/dist/cjs/metric/NeedlemanWunsch.js.map +1 -0
- package/dist/cjs/metric/SmithWaterman.js +98 -0
- package/dist/cjs/metric/SmithWaterman.js.map +1 -0
- package/dist/cjs/metric/qGram.js +91 -0
- package/dist/cjs/metric/qGram.js.map +1 -0
- package/dist/cjs/phonetic/Cologne.js +112 -0
- package/dist/cjs/phonetic/Cologne.js.map +1 -0
- package/dist/cjs/phonetic/Metaphone.js +172 -0
- package/dist/cjs/phonetic/Metaphone.js.map +1 -0
- package/dist/cjs/phonetic/Phonetic.js +413 -0
- package/dist/cjs/phonetic/Phonetic.js.map +1 -0
- package/dist/cjs/phonetic/Soundex.js +135 -0
- package/dist/cjs/phonetic/Soundex.js.map +1 -0
- package/dist/cjs/utils/DeepMerge.js +144 -0
- package/dist/cjs/utils/DeepMerge.js.map +1 -0
- package/dist/cjs/utils/DiffChecker.js +500 -0
- package/dist/cjs/utils/DiffChecker.js.map +1 -0
- package/dist/cjs/utils/Filter.js +189 -0
- package/dist/cjs/utils/Filter.js.map +1 -0
- package/dist/cjs/utils/HashTable.js +175 -0
- package/dist/cjs/utils/HashTable.js.map +1 -0
- package/dist/cjs/utils/Normalizer.js +144 -0
- package/dist/cjs/utils/Normalizer.js.map +1 -0
- package/dist/cjs/utils/Pool.js +196 -0
- package/dist/cjs/utils/Pool.js.map +1 -0
- package/dist/cjs/utils/Profiler.js +229 -0
- package/dist/cjs/utils/Profiler.js.map +1 -0
- package/dist/cjs/utils/Registry.js +148 -0
- package/dist/cjs/utils/Registry.js.map +1 -0
- package/dist/cjs/utils/TextAnalyzer.js +358 -0
- package/dist/cjs/utils/TextAnalyzer.js.map +1 -0
- package/dist/esm/CmpStr.js +662 -0
- package/dist/esm/CmpStr.js.map +1 -0
- package/dist/esm/CmpStrAsync.js +331 -0
- package/dist/esm/CmpStrAsync.js.map +1 -0
- package/dist/esm/index.js +7 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/metric/Cosine.js +99 -0
- package/dist/esm/metric/Cosine.js.map +1 -0
- package/dist/esm/metric/DamerauLevenshtein.js +108 -0
- package/dist/esm/metric/DamerauLevenshtein.js.map +1 -0
- package/dist/esm/metric/DiceSorensen.js +89 -0
- package/dist/esm/metric/DiceSorensen.js.map +1 -0
- package/dist/esm/metric/Hamming.js +77 -0
- package/dist/esm/metric/Hamming.js.map +1 -0
- package/dist/esm/metric/Jaccard.js +74 -0
- package/dist/esm/metric/Jaccard.js.map +1 -0
- package/dist/esm/metric/JaroWinkler.js +112 -0
- package/dist/esm/metric/JaroWinkler.js.map +1 -0
- package/dist/esm/metric/LCS.js +87 -0
- package/dist/esm/metric/LCS.js.map +1 -0
- package/dist/esm/metric/Levenshtein.js +92 -0
- package/dist/esm/metric/Levenshtein.js.map +1 -0
- package/dist/esm/metric/Metric.js +442 -0
- package/dist/esm/metric/Metric.js.map +1 -0
- package/dist/esm/metric/NeedlemanWunsch.js +93 -0
- package/dist/esm/metric/NeedlemanWunsch.js.map +1 -0
- package/dist/esm/metric/SmithWaterman.js +96 -0
- package/dist/esm/metric/SmithWaterman.js.map +1 -0
- package/dist/esm/metric/qGram.js +89 -0
- package/dist/esm/metric/qGram.js.map +1 -0
- package/dist/esm/phonetic/Cologne.js +114 -0
- package/dist/esm/phonetic/Cologne.js.map +1 -0
- package/dist/esm/phonetic/Metaphone.js +174 -0
- package/dist/esm/phonetic/Metaphone.js.map +1 -0
- package/dist/esm/phonetic/Phonetic.js +409 -0
- package/dist/esm/phonetic/Phonetic.js.map +1 -0
- package/dist/esm/phonetic/Soundex.js +137 -0
- package/dist/esm/phonetic/Soundex.js.map +1 -0
- package/dist/esm/utils/DeepMerge.js +139 -0
- package/dist/esm/utils/DeepMerge.js.map +1 -0
- package/dist/esm/utils/DiffChecker.js +498 -0
- package/dist/esm/utils/DiffChecker.js.map +1 -0
- package/dist/esm/utils/Filter.js +187 -0
- package/dist/esm/utils/Filter.js.map +1 -0
- package/dist/esm/utils/HashTable.js +173 -0
- package/dist/esm/utils/HashTable.js.map +1 -0
- package/dist/esm/utils/Normalizer.js +142 -0
- package/dist/esm/utils/Normalizer.js.map +1 -0
- package/dist/esm/utils/Pool.js +194 -0
- package/dist/esm/utils/Pool.js.map +1 -0
- package/dist/esm/utils/Profiler.js +227 -0
- package/dist/esm/utils/Profiler.js.map +1 -0
- package/dist/esm/utils/Registry.js +142 -0
- package/dist/esm/utils/Registry.js.map +1 -0
- package/dist/esm/utils/TextAnalyzer.js +356 -0
- package/dist/esm/utils/TextAnalyzer.js.map +1 -0
- package/dist/types/CmpStr.d.ts +472 -0
- package/dist/types/CmpStrAsync.d.ts +233 -0
- package/dist/types/index.d.ts +51 -0
- package/dist/types/metric/Cosine.d.ts +57 -0
- package/dist/types/metric/DamerauLevenshtein.d.ts +50 -0
- package/dist/types/metric/DiceSorensen.d.ts +57 -0
- package/dist/types/metric/Hamming.d.ts +49 -0
- package/dist/types/metric/Jaccard.d.ts +48 -0
- package/dist/types/metric/JaroWinkler.d.ts +50 -0
- package/dist/types/metric/LCS.d.ts +50 -0
- package/dist/types/metric/Levenshtein.d.ts +50 -0
- package/dist/types/metric/Metric.d.ts +261 -0
- package/dist/types/metric/NeedlemanWunsch.d.ts +47 -0
- package/dist/types/metric/SmithWaterman.d.ts +48 -0
- package/dist/types/metric/index.d.ts +41 -0
- package/dist/types/metric/qGram.d.ts +56 -0
- package/dist/types/phonetic/Cologne.d.ts +46 -0
- package/dist/types/phonetic/Metaphone.d.ts +50 -0
- package/dist/types/phonetic/Phonetic.d.ts +189 -0
- package/dist/types/phonetic/Soundex.d.ts +49 -0
- package/dist/types/phonetic/index.d.ts +30 -0
- package/dist/types/utils/DeepMerge.d.ts +70 -0
- package/dist/types/utils/DiffChecker.d.ts +137 -0
- package/dist/types/utils/Filter.d.ts +97 -0
- package/dist/types/utils/HashTable.d.ts +86 -0
- package/dist/types/utils/Normalizer.d.ts +76 -0
- package/dist/types/utils/Pool.d.ts +63 -0
- package/dist/types/utils/Profiler.d.ts +129 -0
- package/dist/types/utils/Registry.d.ts +57 -0
- package/dist/types/utils/TextAnalyzer.d.ts +199 -0
- package/dist/types/utils/Types.d.ts +313 -0
- package/package.json +62 -49
- package/src/CmpStr.d.ts +0 -70
- package/src/CmpStr.js +0 -912
- package/src/CmpStrAsync.d.ts +0 -19
- package/src/CmpStrAsync.js +0 -204
- package/src/algorithms/cosine.js +0 -86
- package/src/algorithms/damerau.js +0 -78
- package/src/algorithms/dice.js +0 -65
- package/src/algorithms/hamming.js +0 -44
- package/src/algorithms/jaccard.js +0 -34
- package/src/algorithms/jaroWinkler.js +0 -106
- package/src/algorithms/lcs.js +0 -58
- package/src/algorithms/levenshtein.js +0 -70
- package/src/algorithms/needlemanWunsch.js +0 -72
- package/src/algorithms/qGram.js +0 -63
- package/src/algorithms/smithWaterman.js +0 -78
- package/src/algorithms/soundex.js +0 -152
- package/src/index.d.ts +0 -3
- package/src/index.js +0 -47
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Abstract Phonetic
|
|
3
|
+
* src/phonetic/Phonetic.ts
|
|
4
|
+
*
|
|
5
|
+
* @see https://en.wikipedia.org/wiki/Phonetic_algorithm
|
|
6
|
+
*
|
|
7
|
+
* A phonetic algorithm refers to a method for indexing words according to their
|
|
8
|
+
* pronunciation. When the algorithm relies on orthography, it is significantly
|
|
9
|
+
* influenced by the spelling conventions of the language for which it is intended:
|
|
10
|
+
* since the majority of phonetic algorithms were created for English, they tend
|
|
11
|
+
* to be less effective for indexing words in other languages.
|
|
12
|
+
*
|
|
13
|
+
* Phonetic search has numerous applications, and one of the initial use cases has
|
|
14
|
+
* been in trademark searches to verify that newly registered trademarks do not
|
|
15
|
+
* pose a risk of infringing upon existing trademarks due to their pronunciation.
|
|
16
|
+
*
|
|
17
|
+
* This module provides an abstract class for generating phonetic indices based
|
|
18
|
+
* on mappings and rules. It allows for the implementation of various phonetic
|
|
19
|
+
* algorithms by extending the abstract class.
|
|
20
|
+
*
|
|
21
|
+
* @module Phonetic
|
|
22
|
+
* @author Paul Köhler (komed3)
|
|
23
|
+
* @license MIT
|
|
24
|
+
*/
|
|
25
|
+
import type { PhoneticMap, PhoneticOptions, RegistryService, PhoneticMappingService } from '../utils/Types';
|
|
26
|
+
/**
|
|
27
|
+
* Abstract class representing a phonetic algorithm.
|
|
28
|
+
*
|
|
29
|
+
* The protected methods `applyRules`, `encode`, `mapChar`, `equalLen`, `word2Chars`,
|
|
30
|
+
* `exitEarly`, `adjustCode`, `loop` and `loopAsync` can be overridden in subclasses
|
|
31
|
+
* to implement specific phonetic algorithms.
|
|
32
|
+
*
|
|
33
|
+
* @abstract
|
|
34
|
+
*/
|
|
35
|
+
export declare abstract class Phonetic {
|
|
36
|
+
private static cache;
|
|
37
|
+
/**
|
|
38
|
+
* Default phonetic options.
|
|
39
|
+
*
|
|
40
|
+
* This object contains default settings for phonetic algorithms,
|
|
41
|
+
* implemented in the subclass.
|
|
42
|
+
*/
|
|
43
|
+
protected static default: PhoneticOptions;
|
|
44
|
+
private readonly algo;
|
|
45
|
+
protected readonly options: PhoneticOptions;
|
|
46
|
+
protected readonly map: PhoneticMap;
|
|
47
|
+
/**
|
|
48
|
+
* Static method to clear the cache of indexed words.
|
|
49
|
+
*/
|
|
50
|
+
static clear(): void;
|
|
51
|
+
/**
|
|
52
|
+
* Constructor for the Phonetic class.
|
|
53
|
+
*
|
|
54
|
+
* Initializes the phonetic algorithm with the specified options and mapping.
|
|
55
|
+
*
|
|
56
|
+
* @param {string} algo - The name of the algorithm (e.g. 'soundex')
|
|
57
|
+
* @param {PhoneticOptions} [opt] - Options for the phonetic algorithm
|
|
58
|
+
* @throws {Error} - If the requested mapping is not declared
|
|
59
|
+
*/
|
|
60
|
+
constructor(algo: string, opt?: PhoneticOptions);
|
|
61
|
+
/**
|
|
62
|
+
* Applies phonetic rules to a character in a word context.
|
|
63
|
+
*
|
|
64
|
+
* This method is designed to be generic and efficient for all phonetic algorithms.
|
|
65
|
+
* It checks all rule types (prev, next, prevNot, nextNot, position, etc.) and
|
|
66
|
+
* returns either the appropriate code (string) or undefined.
|
|
67
|
+
*
|
|
68
|
+
* @param {string} char - The current character
|
|
69
|
+
* @param {number} i - The current position within the word
|
|
70
|
+
* @param {string[]} chars - The word as an array of characters
|
|
71
|
+
* @param {number} charLen - The total length of the word
|
|
72
|
+
* @returns {string|undefined} - The rule code or undefined if no rule applies
|
|
73
|
+
*/
|
|
74
|
+
protected applyRules(char: string, i: number, chars: string[], charLen: number): string | undefined;
|
|
75
|
+
/**
|
|
76
|
+
* Generates the phonetic code for a given word.
|
|
77
|
+
*
|
|
78
|
+
* This method processes the word character by character, applying phonetic rules
|
|
79
|
+
* and mappings to generate a phonetic code.
|
|
80
|
+
*
|
|
81
|
+
* @param {string} word - The input word to be converted into a phonetic code
|
|
82
|
+
* @returns {string} - The generated phonetic code
|
|
83
|
+
*/
|
|
84
|
+
protected encode(word: string): string;
|
|
85
|
+
/**
|
|
86
|
+
* Converts a character to its phonetic code based on the mapping and rules.
|
|
87
|
+
*
|
|
88
|
+
* @param {string} char - The current character
|
|
89
|
+
* @param {number} i - The current position within the word
|
|
90
|
+
* @param {string[]} chars - The word as an array of characters
|
|
91
|
+
* @param {number} charLen - The total length of the word
|
|
92
|
+
* @param {string|null} lastCode - The last code generated (to avoid duplicates)
|
|
93
|
+
* @param {Record<string, string>} map - The phonetic mapping
|
|
94
|
+
* @returns {string|undefined} - The phonetic code or undefined if no code applies
|
|
95
|
+
*/
|
|
96
|
+
protected mapChar(char: string, i: number, chars: string[], charLen: number, lastCode: string | null, map: Record<string, string>): string | undefined;
|
|
97
|
+
/**
|
|
98
|
+
* Ensures the phonetic code has a fixed length by padding or truncating.
|
|
99
|
+
*
|
|
100
|
+
* @param {string} input - The input string to be adjusted
|
|
101
|
+
* @returns {string} - The adjusted string with fixed length
|
|
102
|
+
*/
|
|
103
|
+
protected equalLen(input: string): string;
|
|
104
|
+
/**
|
|
105
|
+
* Converts a word into an array of characters.
|
|
106
|
+
*
|
|
107
|
+
* @param {string} word - The input word to be converted
|
|
108
|
+
* @returns {string[]} - An array of characters from the input word
|
|
109
|
+
*/
|
|
110
|
+
protected word2Chars(word: string): string[];
|
|
111
|
+
/**
|
|
112
|
+
* Determines whether to exit early based on the current phonetic code length.
|
|
113
|
+
*
|
|
114
|
+
* @param {string} code - The current phonetic code
|
|
115
|
+
* @param {number} i - The current index in the word
|
|
116
|
+
* @returns {boolean} - True if the code length exceeds the specified limit, false otherwise
|
|
117
|
+
*/
|
|
118
|
+
protected exitEarly(code: string, i: number): boolean;
|
|
119
|
+
/**
|
|
120
|
+
* Adjusts the phonetic code.
|
|
121
|
+
*
|
|
122
|
+
* @param {string} code - The phonetic code to be adjusted
|
|
123
|
+
* @param {string[]} chars - Characters to be removed from the code
|
|
124
|
+
* @returns {string} - The adjusted phonetic code
|
|
125
|
+
*/
|
|
126
|
+
protected adjustCode(code: string, chars: string[]): string;
|
|
127
|
+
/**
|
|
128
|
+
* Processes an array of words to generate their phonetic indices.
|
|
129
|
+
*
|
|
130
|
+
* This method iterates over each word, generates its phonetic code,
|
|
131
|
+
* and ensures that the resulting codes are of equal length.
|
|
132
|
+
*
|
|
133
|
+
* @param {string[]} words - An array of words to be processed
|
|
134
|
+
* @returns {string[]} - An array of phonetic indices for the input words
|
|
135
|
+
*/
|
|
136
|
+
protected loop(words: string[]): string[];
|
|
137
|
+
/**
|
|
138
|
+
* Asynchronously processes an array of words to generate their phonetic indices.
|
|
139
|
+
*
|
|
140
|
+
* This method iterates over each word, generates its phonetic code asynchronously,
|
|
141
|
+
* and ensures that the resulting codes are of equal length.
|
|
142
|
+
*
|
|
143
|
+
* @param {string[]} words - An array of words to be processed
|
|
144
|
+
* @returns {Promise<string[]>} - A promise that resolves to an array of phonetic indices for the input words
|
|
145
|
+
*/
|
|
146
|
+
protected loopAsync(words: string[]): Promise<string[]>;
|
|
147
|
+
/**
|
|
148
|
+
* Get the name of the phonetic algorithm.
|
|
149
|
+
*
|
|
150
|
+
* @returns {string} - The name of the algorithm
|
|
151
|
+
*/
|
|
152
|
+
getAlgoName(): string;
|
|
153
|
+
/**
|
|
154
|
+
* Generates a phonetic index for the given input string.
|
|
155
|
+
*
|
|
156
|
+
* @param {string} input - The input string to be indexed
|
|
157
|
+
* @returns {string[]} - An array of phonetic indices for the input words
|
|
158
|
+
*/
|
|
159
|
+
getIndex(input: string): string[];
|
|
160
|
+
/**
|
|
161
|
+
* Asynchronously generates a phonetic index for the given input string.
|
|
162
|
+
*
|
|
163
|
+
* @param {string} input - The input string to be indexed
|
|
164
|
+
* @returns {Promise<string[]>} - A promise that resolves to an array of phonetic indices for the input words
|
|
165
|
+
*/
|
|
166
|
+
getIndexAsync(input: string): Promise<string[]>;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Phonetic registry service for managing phonetic implementations.
|
|
170
|
+
*
|
|
171
|
+
* This registry allows for dynamic registration and retrieval of phonetic classes,
|
|
172
|
+
* enabling the use of various phonetic algorithms in a consistent manner.
|
|
173
|
+
*/
|
|
174
|
+
export declare const PhoneticRegistry: RegistryService<Phonetic>;
|
|
175
|
+
/**
|
|
176
|
+
* Type definition for the Phonetic class constructor.
|
|
177
|
+
*
|
|
178
|
+
* This type is used to create instances of the Phonetic class, allowing for
|
|
179
|
+
* dynamic instantiation of phonetic algorithms.
|
|
180
|
+
*/
|
|
181
|
+
export type PhoneticCls = new (...args: any[]) => Phonetic;
|
|
182
|
+
/**
|
|
183
|
+
* Phonetic Mapping Service
|
|
184
|
+
*
|
|
185
|
+
* This service provides a simple interface to manage phonetic mappings across
|
|
186
|
+
* different phonetic algorithms. It allows adding, removing, checking existence,
|
|
187
|
+
* retrieving, and listing phonetic mappings for specified algorithms.
|
|
188
|
+
*/
|
|
189
|
+
export declare const PhoneticMappingRegistry: PhoneticMappingService;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Soundex Phonetic Algorithm
|
|
3
|
+
* src/phonetic/Soudex.ts
|
|
4
|
+
*
|
|
5
|
+
* @see https://en.wikipedia.org/wiki/Soundex
|
|
6
|
+
*
|
|
7
|
+
* Soundex is a phonetic algorithm for indexing names by sound. It is used to
|
|
8
|
+
* encode words into a phonetic representation, allowing for the comparison of
|
|
9
|
+
* words based on their pronunciation rather than their spelling. This works
|
|
10
|
+
* by mapping letters to digits, ignoring certain letters, and applying specific
|
|
11
|
+
* rules to handle character combinations.
|
|
12
|
+
*
|
|
13
|
+
* It is particularly useful for matching names that may be spelled differently
|
|
14
|
+
* but sound similar and commonly used in genealogical research and databases
|
|
15
|
+
* to find similar-sounding names.
|
|
16
|
+
*
|
|
17
|
+
* The Soundex algorithm is not case-sensitive and ignores vowels and certain
|
|
18
|
+
* consonants. It outputs an array of strings that represents the phonetic code
|
|
19
|
+
* of the input, typically limited to the length of four characters.
|
|
20
|
+
*
|
|
21
|
+
* @module Phonetic/Soundex
|
|
22
|
+
* @author Paul Köhler (komed3)
|
|
23
|
+
* @license MIT
|
|
24
|
+
*/
|
|
25
|
+
import type { PhoneticOptions } from '../utils/Types';
|
|
26
|
+
import { Phonetic } from './Phonetic';
|
|
27
|
+
/**
|
|
28
|
+
* Soundex class extends the Phonetic class to implement the Soundex phonetic algorithm.
|
|
29
|
+
*/
|
|
30
|
+
export declare class Soundex extends Phonetic {
|
|
31
|
+
protected static default: PhoneticOptions;
|
|
32
|
+
/**
|
|
33
|
+
* Constructor for the Soundex class.
|
|
34
|
+
*
|
|
35
|
+
* Initializes the Soundex phonetic algorithm with the mapping and options.
|
|
36
|
+
*
|
|
37
|
+
* @param {PhoneticOptions} [opt] - Options for the Soundex phonetic algorithm
|
|
38
|
+
*/
|
|
39
|
+
constructor(opt?: PhoneticOptions);
|
|
40
|
+
/**
|
|
41
|
+
* Adjusts the phonetic code by removing leading zeros and ensuring the
|
|
42
|
+
* first character is uppercase.
|
|
43
|
+
*
|
|
44
|
+
* @param {string} code - The phonetic code to adjust
|
|
45
|
+
* @param {string[]} chars - The characters used in the phonetic code
|
|
46
|
+
* @returns {string} - The adjusted phonetic code
|
|
47
|
+
*/
|
|
48
|
+
protected adjustCode(code: string, chars: string[]): string;
|
|
49
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Phonetic Registry Loader
|
|
3
|
+
* src/phonetic/index.ts
|
|
4
|
+
*
|
|
5
|
+
* This module serves as the central loader and registry for all phonetic algorithms
|
|
6
|
+
* available in the CmpStr library. It ensures that all phonetic implementations are
|
|
7
|
+
* registered with the PhoneticRegistry and available for use throughout the
|
|
8
|
+
* application.
|
|
9
|
+
*
|
|
10
|
+
* Each phonetic algorithm (such as Soundex, Cologne, Metaphone, etc.) is defined
|
|
11
|
+
* in its own module and is automatically registered with the PhoneticRegistry upon
|
|
12
|
+
* import. This design allows for easy extensibility: new phonetic algorithms can be
|
|
13
|
+
* added simply by creating a new module and importing it here. The registry pattern
|
|
14
|
+
* enables dynamic lookup, instantiation, and management of all available phonetic
|
|
15
|
+
* algorithms at runtime.
|
|
16
|
+
*
|
|
17
|
+
* Features:
|
|
18
|
+
* - Centralized registration of all built-in phonetic algorithms
|
|
19
|
+
* - Automatic registration via side-effect imports
|
|
20
|
+
* - Extensible: custom phonetic algorithms can be registered at runtime
|
|
21
|
+
* - Consistent interface for accessing, listing, and managing phonetic algorithms
|
|
22
|
+
* - Ensures that all phonetic algorithms are available for use in the CmpStr API
|
|
23
|
+
*
|
|
24
|
+
* @author Paul Köhler (komed3)
|
|
25
|
+
* @license MIT
|
|
26
|
+
*/
|
|
27
|
+
import './Cologne';
|
|
28
|
+
import './Metaphone';
|
|
29
|
+
import './Soundex';
|
|
30
|
+
export { PhoneticRegistry, PhoneticMappingRegistry, Phonetic, PhoneticCls } from './Phonetic';
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deep Merge Utility
|
|
3
|
+
* src/utils/DeepMerge.ts
|
|
4
|
+
*
|
|
5
|
+
* This module provides utility functions for deep merging objects, getting values by path,
|
|
6
|
+
* and setting values by path in a deeply nested object structure.
|
|
7
|
+
*
|
|
8
|
+
* It supports dot and bracket notation (e.g. `a.b[0].c`) as well as escaped keys.
|
|
9
|
+
*
|
|
10
|
+
* Included functions:
|
|
11
|
+
* - `get`: Retrieve a deeply nested value by path
|
|
12
|
+
* - `set`: Assign a value to a nested path
|
|
13
|
+
* - `merge`: Deeply merge two objects
|
|
14
|
+
* - `has`: Check whether a path exists
|
|
15
|
+
* - `rmv`: Delete a value at a path
|
|
16
|
+
*
|
|
17
|
+
* @module Utils/DeepMerge
|
|
18
|
+
* @author Paul Köhler
|
|
19
|
+
* @license MIT
|
|
20
|
+
*/
|
|
21
|
+
/**
|
|
22
|
+
* Deeply get a value from an object by a path string.
|
|
23
|
+
*
|
|
24
|
+
* @template T - The type of the object to get the value from
|
|
25
|
+
* @param {T} t - The object to get the value from
|
|
26
|
+
* @param {string} path - The path string, e.g. `a.b.c`
|
|
27
|
+
* @param {any} fallback - The default value to return if the path does not exist
|
|
28
|
+
* @returns {T|R|undefined} - The value at the specified path, otherwise the default value
|
|
29
|
+
*/
|
|
30
|
+
export declare function get<T extends Record<string, any>, R = any>(t: T, path: string, fallback?: any): T | R | undefined;
|
|
31
|
+
/**
|
|
32
|
+
* Check if a path exists in an object.
|
|
33
|
+
*
|
|
34
|
+
* @template T - The type of the object to get the value from
|
|
35
|
+
* @param {T} t - The object to check
|
|
36
|
+
* @param {string} path - The path string, e.g. `a.b.c`
|
|
37
|
+
* @returns {boolean} - True if the path exists, otherwise false
|
|
38
|
+
*/
|
|
39
|
+
export declare function has<T extends Record<string, any>>(t: T, path: string): boolean;
|
|
40
|
+
/**
|
|
41
|
+
* Deeply set a value in an object by a path string.
|
|
42
|
+
*
|
|
43
|
+
* @template T - The type of the object to get the value from
|
|
44
|
+
* @param {T} t - The object to set the value in
|
|
45
|
+
* @param {string} path - The path string, e.g. `a.b.c`
|
|
46
|
+
* @param {any} value - The value to set at the specified path
|
|
47
|
+
* @returns {T} - The modified object with the value set at the specified path
|
|
48
|
+
* @throws {Error} - Throws an error if the key is not a valid identifier
|
|
49
|
+
*/
|
|
50
|
+
export declare function set<T extends Record<string, any>>(t: T, path: string, value: any): T;
|
|
51
|
+
/**
|
|
52
|
+
* Deeply merge two objects, where the second object overrides the first.
|
|
53
|
+
*
|
|
54
|
+
* @template T - The type of the object to get the value from
|
|
55
|
+
* @param {T} t - The target object to merge into
|
|
56
|
+
* @param {T} o - The source object to merge from
|
|
57
|
+
* @param {boolean} [mergeUndefined=false] - Whether to merge undefined values
|
|
58
|
+
* @returns {T} - The merged object
|
|
59
|
+
*/
|
|
60
|
+
export declare function merge<T extends Record<string, any>>(t?: T | undefined, o?: T | undefined, mergeUndefined?: boolean): T;
|
|
61
|
+
/**
|
|
62
|
+
* Delete a value at a specified path in an object.
|
|
63
|
+
*
|
|
64
|
+
* @template T - The type of the object to get the value from
|
|
65
|
+
* @param {T} t - The object to delete the value from
|
|
66
|
+
* @param {string} path - The path string, e.g. `a.b.c`
|
|
67
|
+
* @param {boolean} [preserveEmpty=false] - Whether to preserve empty objects/arrays
|
|
68
|
+
* @returns {T} - The modified object with the value deleted at the specified path
|
|
69
|
+
*/
|
|
70
|
+
export declare function rmv<T extends Record<string, any>>(t: T, path: string, preserveEmpty?: boolean): T;
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DiffChecker Utility
|
|
3
|
+
* src/utils/DiffChecker.ts
|
|
4
|
+
*
|
|
5
|
+
* The DiffChecker class provides a robust and efficient utility for comparing two
|
|
6
|
+
* texts and extracting their differences (full lines or word mode). It supports
|
|
7
|
+
* context-aware grouping of changes, unified diff output (with CLI color or ASCII
|
|
8
|
+
* markup), and detailed change magnitude metrics. The class is highly configurable,
|
|
9
|
+
* allowing users to choose the diff granularity, case sensitivity, context lines,
|
|
10
|
+
* grouping, and output style. It is suitable for text comparison, code review
|
|
11
|
+
* tools, document versioning, and any application requiring precise and human-
|
|
12
|
+
* readable difference reporting.
|
|
13
|
+
*
|
|
14
|
+
* Features:
|
|
15
|
+
* - Line and word-based diffing
|
|
16
|
+
* - Case-insensitive comparison option
|
|
17
|
+
* - Context lines and grouping of adjacent changes
|
|
18
|
+
* - Unified diff output (ASCII or colored CLI)
|
|
19
|
+
* - Highlighting of changed segments within lines
|
|
20
|
+
* - Change magnitude calculation (relative to group or line)
|
|
21
|
+
* - Expand-all mode for full file context
|
|
22
|
+
*
|
|
23
|
+
* @module Utils/DiffChecker
|
|
24
|
+
* @author Paul Köhler (komed3)
|
|
25
|
+
* @license MIT
|
|
26
|
+
*/
|
|
27
|
+
import type { DiffOptions, DiffLine, DiffGroup } from './Types';
|
|
28
|
+
/**
|
|
29
|
+
* The DiffChecker class provides methods to compare two texts and generate
|
|
30
|
+
* structured diffs, grouped diffs, and unified diff outputs.
|
|
31
|
+
*/
|
|
32
|
+
export declare class DiffChecker {
|
|
33
|
+
private readonly a;
|
|
34
|
+
private readonly b;
|
|
35
|
+
private readonly options;
|
|
36
|
+
private entries;
|
|
37
|
+
private grouped;
|
|
38
|
+
private diffRun;
|
|
39
|
+
/**
|
|
40
|
+
* Constructs a new DiffChecker instance for comparing two texts.
|
|
41
|
+
*
|
|
42
|
+
* @param {string} a - The first (original) text
|
|
43
|
+
* @param {string} b - The second (modified) text
|
|
44
|
+
* @param {DiffOptions} [opt] - Optional diff configuration
|
|
45
|
+
*/
|
|
46
|
+
constructor(a: string, b: string, opt?: DiffOptions);
|
|
47
|
+
/**
|
|
48
|
+
* Splits both input texts into arrays of lines and returns them
|
|
49
|
+
* with the maximum line count.
|
|
50
|
+
*
|
|
51
|
+
* @returns { linesA: string[], linesB: string[], maxLen: number }
|
|
52
|
+
*/
|
|
53
|
+
private text2lines;
|
|
54
|
+
/**
|
|
55
|
+
* Tokenizes a string according to the current diff mode (line or word).
|
|
56
|
+
*
|
|
57
|
+
* @param {string} input - The string to tokenize
|
|
58
|
+
* @returns {string[]} - Array of tokens
|
|
59
|
+
*/
|
|
60
|
+
private tokenize;
|
|
61
|
+
/**
|
|
62
|
+
* Concatenates an array of tokens back into a string, respecting the diff mode.
|
|
63
|
+
*
|
|
64
|
+
* @param {string[]} input - Array of tokens
|
|
65
|
+
* @returns {string} - Concatenated string
|
|
66
|
+
*/
|
|
67
|
+
private concat;
|
|
68
|
+
/**
|
|
69
|
+
* Computes the diff between the two input texts and populates the
|
|
70
|
+
* entries and grouped arrays.
|
|
71
|
+
*/
|
|
72
|
+
private computeDiff;
|
|
73
|
+
/**
|
|
74
|
+
* Compares two lines and records their differences at the configured granularity.
|
|
75
|
+
*
|
|
76
|
+
* @param {string} a - Line from the first text
|
|
77
|
+
* @param {string} b - Line from the second text
|
|
78
|
+
* @param {number} line - Line number
|
|
79
|
+
*/
|
|
80
|
+
private lineDiff;
|
|
81
|
+
/**
|
|
82
|
+
* Finds all minimal diff blocks between two tokenized strings,
|
|
83
|
+
* returning original text and positions.
|
|
84
|
+
*
|
|
85
|
+
* @param {string} a - Original line (case preserved)
|
|
86
|
+
* @param {string} A - Original line (possibly lowercased)
|
|
87
|
+
* @param {string} b - Modified line (case preserved)
|
|
88
|
+
* @param {string} B - Modified line (possibly lowercased)
|
|
89
|
+
* @returns {DiffEntry[]} - Array of diff entries for this line
|
|
90
|
+
*/
|
|
91
|
+
private preciseDiff;
|
|
92
|
+
/**
|
|
93
|
+
* Groups adjacent changed lines together, including context lines,
|
|
94
|
+
* and calculates group metrics.
|
|
95
|
+
*/
|
|
96
|
+
private findGroups;
|
|
97
|
+
/**
|
|
98
|
+
* Calculates the change magnitude string for a group or line.
|
|
99
|
+
*
|
|
100
|
+
* @param {number} del - Number of deleted characters
|
|
101
|
+
* @param {number} ins - Number of inserted characters
|
|
102
|
+
* @param {number} baseLen - Base length for normalization
|
|
103
|
+
* @returns {string} - Magnitude string (e.g. "++-")
|
|
104
|
+
*/
|
|
105
|
+
private magnitude;
|
|
106
|
+
/**
|
|
107
|
+
* Generates a unified diff output as a string, with optional CLI coloring.
|
|
108
|
+
*
|
|
109
|
+
* @param {boolean} cli - If true, use CLI colors; otherwise, ASCII markup
|
|
110
|
+
* @returns {string} - Unified diff output
|
|
111
|
+
*/
|
|
112
|
+
private output;
|
|
113
|
+
/**
|
|
114
|
+
* Returns the structured diff as an array of DiffLine objects.
|
|
115
|
+
*
|
|
116
|
+
* @returns {DiffLine[]} - Array of line-level diffs
|
|
117
|
+
*/
|
|
118
|
+
getStructuredDiff(): DiffLine[];
|
|
119
|
+
/**
|
|
120
|
+
* Returns the grouped diff as an array of DiffGroup objects.
|
|
121
|
+
*
|
|
122
|
+
* @returns {DiffGroup[]} - Array of grouped diffs
|
|
123
|
+
*/
|
|
124
|
+
getGroupedDiff(): DiffGroup[];
|
|
125
|
+
/**
|
|
126
|
+
* Returns the unified diff as a plain ASCII string.
|
|
127
|
+
*
|
|
128
|
+
* @returns {string} - Unified diff (ASCII)
|
|
129
|
+
*/
|
|
130
|
+
getASCIIDiff(): string;
|
|
131
|
+
/**
|
|
132
|
+
* Returns the unified diff as a CLI-colored string.
|
|
133
|
+
*
|
|
134
|
+
* @returns {string} - Unified diff (CLI colors)
|
|
135
|
+
*/
|
|
136
|
+
getCLIDiff(): string;
|
|
137
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Filter Utility
|
|
3
|
+
* src/utils/Filter.ts
|
|
4
|
+
*
|
|
5
|
+
* This module provides a Filter class that allows for the management and application of
|
|
6
|
+
* filters to strings based on hooks. Filters can be added, removed, paused, resumed, and
|
|
7
|
+
* applied to input strings. Each filter has an id, a function, a priority, and options
|
|
8
|
+
* for activation and overrideability.
|
|
9
|
+
*
|
|
10
|
+
* @module Utils/Filter
|
|
11
|
+
* @author Paul Köhler (komed3)
|
|
12
|
+
* @license MIT
|
|
13
|
+
*/
|
|
14
|
+
import type { FilterFn, FilterOptions } from './Types';
|
|
15
|
+
/**
|
|
16
|
+
* The Filter class provides a way to manage and apply filters to strings based on hooks.
|
|
17
|
+
*/
|
|
18
|
+
export declare class Filter {
|
|
19
|
+
/**
|
|
20
|
+
* A static map to hold all filters.
|
|
21
|
+
* The key is the hook name, and the value is an array of FilterEntry objects.
|
|
22
|
+
*/
|
|
23
|
+
private static filters;
|
|
24
|
+
/**
|
|
25
|
+
* Finds a filter by its hook and id.
|
|
26
|
+
*
|
|
27
|
+
* @param {string} hook - The name of the hook
|
|
28
|
+
* @param {string} id - The id of the filter
|
|
29
|
+
* @returns {FilterEntry|undefined} - The FilterEntry if found, otherwise undefined
|
|
30
|
+
*/
|
|
31
|
+
private static find;
|
|
32
|
+
/**
|
|
33
|
+
* Adds a filter to the specified hook.
|
|
34
|
+
*
|
|
35
|
+
* @param {string} hook - The name of the hook
|
|
36
|
+
* @param {string} id - The id of the filter
|
|
37
|
+
* @param {FilterFn} fn - The filter function
|
|
38
|
+
* @param {FilterOptions} [opt] - Additional options for the filter
|
|
39
|
+
* @returns {boolean} - Returns true if the filter was added, false if it was not added due to override restrictions
|
|
40
|
+
*/
|
|
41
|
+
static add(hook: string, id: string, fn: FilterFn, opt?: FilterOptions): boolean;
|
|
42
|
+
/**
|
|
43
|
+
* Removes a filter by its hook and id.
|
|
44
|
+
*
|
|
45
|
+
* @param {string} hook - The name of the hook
|
|
46
|
+
* @param {string} id - The id of the filter
|
|
47
|
+
* @returns {boolean} - Returns true if the filter was removed, false if it was not found
|
|
48
|
+
*/
|
|
49
|
+
static remove(hook: string, id: string): boolean;
|
|
50
|
+
/**
|
|
51
|
+
* Pauses a filter by its hook and id.
|
|
52
|
+
*
|
|
53
|
+
* @param {string} hook - The name of the hook
|
|
54
|
+
* @param {string} id - The id of the filter
|
|
55
|
+
* @returns {boolean} - Returns true if the filter was paused, false if it was not found
|
|
56
|
+
*/
|
|
57
|
+
static pause(hook: string, id: string): boolean;
|
|
58
|
+
/**
|
|
59
|
+
* Resumes a filter by its hook and id.
|
|
60
|
+
*
|
|
61
|
+
* @param {string} hook - The name of the hook
|
|
62
|
+
* @param {string} id - The id of the filter
|
|
63
|
+
* @returns {boolean} - Returns true if the filter was resumed, false if it was not found
|
|
64
|
+
*/
|
|
65
|
+
static resume(hook: string, id: string): boolean;
|
|
66
|
+
/**
|
|
67
|
+
* Lists all filters for a given hook.
|
|
68
|
+
*
|
|
69
|
+
* @param {string} hook - The name of the hook
|
|
70
|
+
* @param {boolean} active - If true, only list active filters
|
|
71
|
+
* @returns {string[]} - An array of filter ids
|
|
72
|
+
*/
|
|
73
|
+
static list(hook: string, active?: boolean): string[];
|
|
74
|
+
/**
|
|
75
|
+
* Applies all active filters for a given hook to the input string(s).
|
|
76
|
+
*
|
|
77
|
+
* @param {string} hook - The name of the hook
|
|
78
|
+
* @param {string|string[]} input - The input string(s) to be filtered
|
|
79
|
+
* @returns {string|string[]} - The filtered string(s)
|
|
80
|
+
*/
|
|
81
|
+
static apply(hook: string, input: string | string[]): string | string[];
|
|
82
|
+
/**
|
|
83
|
+
* Applies all active filters for a given hook to the input string(s) asynchronously.
|
|
84
|
+
* Each filter function may return a Promise or a plain string; all are awaited in order.
|
|
85
|
+
*
|
|
86
|
+
* @param {string} hook - The name of the hook
|
|
87
|
+
* @param {string|string[]} input - The input string(s) to be filtered
|
|
88
|
+
* @returns {Promise<string|string[]>} - The filtered string(s)
|
|
89
|
+
*/
|
|
90
|
+
static applyAsync(hook: string, input: string | string[]): Promise<string | string[]>;
|
|
91
|
+
/**
|
|
92
|
+
* Clears all filters or filters for a specific hook.
|
|
93
|
+
*
|
|
94
|
+
* @param {string} [hook] - Optional name of the hook to clear filters for
|
|
95
|
+
*/
|
|
96
|
+
static clear(hook?: string): void;
|
|
97
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hash Table Utility
|
|
3
|
+
* src/utils/HashTable.ts
|
|
4
|
+
*
|
|
5
|
+
* @see https://en.wikipedia.org/wiki/Fowler–Noll–Vo_hash_function
|
|
6
|
+
* @see https://en.wikipedia.org/wiki/Hash_table
|
|
7
|
+
*
|
|
8
|
+
* This module implements an instantiable hash table/cache using the FNV-1a hash algorithm.
|
|
9
|
+
* It allows for multiple independent caches (e.g. for metrics, normalization, etc.) with
|
|
10
|
+
* type safety and high performance. The FNV-1a algorithm is factored out into its own
|
|
11
|
+
* static utility class to avoid code duplication and memory overhead.
|
|
12
|
+
*
|
|
13
|
+
* The key() method supports any number of string arguments, enabling flexible cache keys
|
|
14
|
+
* for different use cases (e.g. normalization, metrics, etc.).
|
|
15
|
+
*
|
|
16
|
+
* @module Utils/HashTable
|
|
17
|
+
* @author Paul Köhler (komed3)
|
|
18
|
+
* @license MIT
|
|
19
|
+
*/
|
|
20
|
+
/**
|
|
21
|
+
* HashTable class implements an instantiable hash table/cache.
|
|
22
|
+
* Allows for multiple independent caches with type safety and high performance.
|
|
23
|
+
*
|
|
24
|
+
* @template K - The type of the label for the key (e.g. string, MetricName, …)
|
|
25
|
+
* @template T - The type of value to be stored in the hash table (e.g. MetricCompute, string, …)
|
|
26
|
+
*/
|
|
27
|
+
export declare class HashTable<K extends string, T> {
|
|
28
|
+
private static readonly MAX_LEN;
|
|
29
|
+
private static readonly TABLE_SIZE;
|
|
30
|
+
/**
|
|
31
|
+
* The internal map to store entries.
|
|
32
|
+
* The key is a string generated from the label and any number of hashed strings.
|
|
33
|
+
* The value is of type T.
|
|
34
|
+
*/
|
|
35
|
+
private table;
|
|
36
|
+
/**
|
|
37
|
+
* Generates a unique hash key for any number of string arguments.
|
|
38
|
+
* The key is in the format "label-H1-H2-H3-..."
|
|
39
|
+
*
|
|
40
|
+
* @param {K} label - Label for this key (e.g. metric name, normalization flags, …)
|
|
41
|
+
* @param {string[]} strs - Array of strings to hash (e.g. input, params, …)
|
|
42
|
+
* @param {boolean} [sorted=false] - Whether to sort the hashes before creating the key
|
|
43
|
+
* @returns {string|false} - A unique hash key or false if any string is too long
|
|
44
|
+
*/
|
|
45
|
+
key(label: K, strs: string[], sorted?: boolean): string | false;
|
|
46
|
+
/**
|
|
47
|
+
* Checks if a key exists in the hash table.
|
|
48
|
+
*
|
|
49
|
+
* @param {string} key - The key to check
|
|
50
|
+
* @returns {boolean} - True if the key exists, false otherwise
|
|
51
|
+
*/
|
|
52
|
+
has(key: string): boolean;
|
|
53
|
+
/**
|
|
54
|
+
* Retrieves the entry from the hash table by its key.
|
|
55
|
+
*
|
|
56
|
+
* @param {string} key - The key to look up
|
|
57
|
+
* @returns {T|undefined} - The entry if found, undefined otherwise
|
|
58
|
+
*/
|
|
59
|
+
get(key: string): T | undefined;
|
|
60
|
+
/**
|
|
61
|
+
* Adds an entry to the hash table.
|
|
62
|
+
*
|
|
63
|
+
* @param {string} key - The hashed key for the entry
|
|
64
|
+
* @param {T} entry - The entry itself to add
|
|
65
|
+
* @param {boolean} [update=true] - Whether to update the entry if it already exists
|
|
66
|
+
* @returns {boolean} - True if added successfully, false if the table is full
|
|
67
|
+
*/
|
|
68
|
+
set(key: string, entry: T, update?: boolean): boolean;
|
|
69
|
+
/**
|
|
70
|
+
* Deletes an entry from the hash table by its key.
|
|
71
|
+
*
|
|
72
|
+
* @param {string} key - The key of the entry to delete
|
|
73
|
+
*/
|
|
74
|
+
delete(key: string): void;
|
|
75
|
+
/**
|
|
76
|
+
* Clears the hash table.
|
|
77
|
+
* This method removes all entries from the hash table.
|
|
78
|
+
*/
|
|
79
|
+
clear(): void;
|
|
80
|
+
/**
|
|
81
|
+
* Returns the current size of the hash table.
|
|
82
|
+
*
|
|
83
|
+
* @returns {number} - The number of entries in the hash table
|
|
84
|
+
*/
|
|
85
|
+
size(): number;
|
|
86
|
+
}
|