@cjser/leven 4.1.0-cjser.2

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.
@@ -0,0 +1,145 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // packages/@cjser/leven.tmp-26-1778153260403/index.js
20
+ var index_exports = {};
21
+ __export(index_exports, {
22
+ closestMatch: () => closestMatch,
23
+ default: () => leven
24
+ });
25
+ module.exports = __toCommonJS(index_exports);
26
+ var array = [];
27
+ var characterCodeCache = [];
28
+ function leven(first, second, options) {
29
+ if (first === second) {
30
+ return 0;
31
+ }
32
+ const maxDistance = options == null ? void 0 : options.maxDistance;
33
+ const swap = first;
34
+ if (first.length > second.length) {
35
+ first = second;
36
+ second = swap;
37
+ }
38
+ let firstLength = first.length;
39
+ let secondLength = second.length;
40
+ while (firstLength > 0 && first.charCodeAt(~-firstLength) === second.charCodeAt(~-secondLength)) {
41
+ firstLength--;
42
+ secondLength--;
43
+ }
44
+ let start = 0;
45
+ while (start < firstLength && first.charCodeAt(start) === second.charCodeAt(start)) {
46
+ start++;
47
+ }
48
+ firstLength -= start;
49
+ secondLength -= start;
50
+ if (maxDistance !== void 0 && secondLength - firstLength > maxDistance) {
51
+ return maxDistance;
52
+ }
53
+ if (firstLength === 0) {
54
+ return maxDistance !== void 0 && secondLength > maxDistance ? maxDistance : secondLength;
55
+ }
56
+ let bCharacterCode;
57
+ let result;
58
+ let temporary;
59
+ let temporary2;
60
+ let index = 0;
61
+ let index2 = 0;
62
+ while (index < firstLength) {
63
+ characterCodeCache[index] = first.charCodeAt(start + index);
64
+ array[index] = ++index;
65
+ }
66
+ while (index2 < secondLength) {
67
+ bCharacterCode = second.charCodeAt(start + index2);
68
+ temporary = index2++;
69
+ result = index2;
70
+ for (index = 0; index < firstLength; index++) {
71
+ temporary2 = bCharacterCode === characterCodeCache[index] ? temporary : temporary + 1;
72
+ temporary = array[index];
73
+ result = array[index] = temporary > result ? temporary2 > result ? result + 1 : temporary2 : temporary2 > temporary ? temporary + 1 : temporary2;
74
+ }
75
+ if (maxDistance !== void 0) {
76
+ let rowMinimum = result;
77
+ for (index = 0; index < firstLength; index++) {
78
+ if (array[index] < rowMinimum) {
79
+ rowMinimum = array[index];
80
+ }
81
+ }
82
+ if (rowMinimum > maxDistance) {
83
+ return maxDistance;
84
+ }
85
+ }
86
+ }
87
+ array.length = firstLength;
88
+ characterCodeCache.length = firstLength;
89
+ return maxDistance !== void 0 && result > maxDistance ? maxDistance : result;
90
+ }
91
+ function closestMatch(target, candidates, options) {
92
+ if (!Array.isArray(candidates) || candidates.length === 0) {
93
+ return void 0;
94
+ }
95
+ const userMax = options == null ? void 0 : options.maxDistance;
96
+ const targetLength = target.length;
97
+ for (const candidate of candidates) {
98
+ if (candidate === target) {
99
+ return candidate;
100
+ }
101
+ }
102
+ if (userMax === 0) {
103
+ return void 0;
104
+ }
105
+ let best;
106
+ let bestDist = Number.POSITIVE_INFINITY;
107
+ const seen = /* @__PURE__ */ new Set();
108
+ for (const candidate of candidates) {
109
+ if (seen.has(candidate)) {
110
+ continue;
111
+ }
112
+ seen.add(candidate);
113
+ const lengthDiff = Math.abs(candidate.length - targetLength);
114
+ if (lengthDiff >= bestDist) {
115
+ continue;
116
+ }
117
+ if (userMax !== void 0 && lengthDiff > userMax) {
118
+ continue;
119
+ }
120
+ const cap = Number.isFinite(bestDist) ? userMax === void 0 ? bestDist : Math.min(bestDist, userMax) : userMax;
121
+ const distance = cap === void 0 ? leven(target, candidate) : leven(target, candidate, { maxDistance: cap });
122
+ if (userMax !== void 0 && distance > userMax) {
123
+ continue;
124
+ }
125
+ let actualD = distance;
126
+ if (cap !== void 0 && distance === cap && cap === userMax) {
127
+ actualD = leven(target, candidate);
128
+ }
129
+ if (actualD < bestDist) {
130
+ bestDist = actualD;
131
+ best = candidate;
132
+ if (bestDist === 0) {
133
+ break;
134
+ }
135
+ }
136
+ }
137
+ if (userMax !== void 0 && bestDist > userMax) {
138
+ return void 0;
139
+ }
140
+ return best;
141
+ }
142
+ // Annotate the CommonJS export names for ESM import in node:
143
+ 0 && (module.exports = {
144
+ closestMatch
145
+ });
package/index.d.ts ADDED
@@ -0,0 +1,58 @@
1
+ export interface Options {
2
+ /**
3
+ Maximum Levenshtein distance to calculate.
4
+
5
+ If the actual distance exceeds this value, the function will return the maximum distance instead of the actual distance. This can significantly improve performance when you only care about matches within a certain threshold.
6
+
7
+ @example
8
+ ```
9
+ import leven from '@cjser/leven';
10
+
11
+ leven('abcdef', '123456', {maxDistance: 3});
12
+ //=> 3
13
+
14
+ leven('cat', 'cow', {maxDistance: 5});
15
+ //=> 2
16
+ ```
17
+ */
18
+ readonly maxDistance?: number;
19
+ }
20
+
21
+ /**
22
+ Measure the difference between two strings using the Levenshtein distance algorithm.
23
+
24
+ @param first - First string.
25
+ @param second - Second string.
26
+ @param options - Options.
27
+ @returns Distance between `first` and `second`. If `maxDistance` is provided and the actual distance exceeds it, returns `maxDistance`.
28
+
29
+ @example
30
+ ```
31
+ import leven from '@cjser/leven';
32
+
33
+ leven('cat', 'cow');
34
+ //=> 2
35
+ ```
36
+ */
37
+ export default function leven(first: string, second: string, options?: Options): number;
38
+
39
+ /**
40
+ Find the closest matching string from an array of candidates.
41
+
42
+ @param target - The string to find matches for.
43
+ @param candidates - Array of candidate strings to search through.
44
+ @param options - Options.
45
+ @returns The closest matching string from candidates, or `undefined` if no candidates are provided or if no match is found within `maxDistance`.
46
+
47
+ @example
48
+ ```
49
+ import {closestMatch} from '@cjser/leven';
50
+
51
+ closestMatch('kitten', ['sitting', 'kitchen', 'mittens']);
52
+ //=> 'kitchen'
53
+
54
+ closestMatch('hello', ['jello', 'yellow', 'bellow'], {maxDistance: 2});
55
+ //=> 'jello'
56
+ ```
57
+ */
58
+ export function closestMatch(target: string, candidates: readonly string[], options?: Options): string | undefined;
package/index.js ADDED
@@ -0,0 +1,175 @@
1
+ const array = [];
2
+ const characterCodeCache = [];
3
+
4
+ export default function leven(first, second, options) {
5
+ if (first === second) {
6
+ return 0;
7
+ }
8
+
9
+ const maxDistance = options?.maxDistance;
10
+ const swap = first;
11
+
12
+ // Swapping the strings if `a` is longer than `b` so we know which one is the
13
+ // shortest & which one is the longest
14
+ if (first.length > second.length) {
15
+ first = second;
16
+ second = swap;
17
+ }
18
+
19
+ let firstLength = first.length;
20
+ let secondLength = second.length;
21
+
22
+ // Performing suffix trimming:
23
+ // We can linearly drop suffix common to both strings since they
24
+ // don't increase distance at all
25
+ // Note: `~-` is the bitwise way to perform a `- 1` operation
26
+ while (firstLength > 0 && (first.charCodeAt(~-firstLength) === second.charCodeAt(~-secondLength))) {
27
+ firstLength--;
28
+ secondLength--;
29
+ }
30
+
31
+ // Performing prefix trimming
32
+ // We can linearly drop prefix common to both strings since they
33
+ // don't increase distance at all
34
+ let start = 0;
35
+
36
+ while (start < firstLength && (first.charCodeAt(start) === second.charCodeAt(start))) {
37
+ start++;
38
+ }
39
+
40
+ firstLength -= start;
41
+ secondLength -= start;
42
+
43
+ // Early termination after trimming: if difference in length exceeds max distance
44
+ if (maxDistance !== undefined && secondLength - firstLength > maxDistance) {
45
+ return maxDistance;
46
+ }
47
+
48
+ if (firstLength === 0) {
49
+ return maxDistance !== undefined && secondLength > maxDistance
50
+ ? maxDistance
51
+ : secondLength;
52
+ }
53
+
54
+ let bCharacterCode;
55
+ let result;
56
+ let temporary;
57
+ let temporary2;
58
+ let index = 0;
59
+ let index2 = 0;
60
+
61
+ while (index < firstLength) {
62
+ characterCodeCache[index] = first.charCodeAt(start + index);
63
+ array[index] = ++index;
64
+ }
65
+
66
+ while (index2 < secondLength) {
67
+ bCharacterCode = second.charCodeAt(start + index2);
68
+ temporary = index2++;
69
+ result = index2;
70
+
71
+ for (index = 0; index < firstLength; index++) {
72
+ temporary2 = bCharacterCode === characterCodeCache[index] ? temporary : temporary + 1;
73
+ temporary = array[index];
74
+ // eslint-disable-next-line no-multi-assign
75
+ result = array[index] = temporary > result
76
+ ? (temporary2 > result ? result + 1 : temporary2)
77
+ : (temporary2 > temporary ? temporary + 1 : temporary2);
78
+ }
79
+
80
+ // Early termination: if all values in current row exceed maxDistance
81
+ if (maxDistance !== undefined) {
82
+ let rowMinimum = result;
83
+ for (index = 0; index < firstLength; index++) {
84
+ if (array[index] < rowMinimum) {
85
+ rowMinimum = array[index];
86
+ }
87
+ }
88
+
89
+ if (rowMinimum > maxDistance) {
90
+ return maxDistance;
91
+ }
92
+ }
93
+ }
94
+
95
+ // Bound arrays to avoid retaining large previous sizes
96
+ array.length = firstLength;
97
+ characterCodeCache.length = firstLength;
98
+
99
+ return maxDistance !== undefined && result > maxDistance ? maxDistance : result;
100
+ }
101
+
102
+ export function closestMatch(target, candidates, options) {
103
+ if (!Array.isArray(candidates) || candidates.length === 0) {
104
+ return undefined;
105
+ }
106
+
107
+ const userMax = options?.maxDistance;
108
+ const targetLength = target.length;
109
+
110
+ // Exact match fast-path
111
+ for (const candidate of candidates) {
112
+ if (candidate === target) {
113
+ return candidate;
114
+ }
115
+ }
116
+
117
+ if (userMax === 0) {
118
+ return undefined;
119
+ }
120
+
121
+ let best;
122
+ let bestDist = Number.POSITIVE_INFINITY;
123
+ const seen = new Set();
124
+
125
+ for (const candidate of candidates) {
126
+ if (seen.has(candidate)) {
127
+ continue;
128
+ }
129
+
130
+ seen.add(candidate);
131
+
132
+ const lengthDiff = Math.abs(candidate.length - targetLength);
133
+ if (lengthDiff >= bestDist) {
134
+ continue;
135
+ }
136
+
137
+ if (userMax !== undefined && lengthDiff > userMax) {
138
+ continue;
139
+ }
140
+
141
+ const cap = Number.isFinite(bestDist)
142
+ ? (userMax === undefined ? bestDist : Math.min(bestDist, userMax))
143
+ : userMax;
144
+
145
+ const distance = cap === undefined
146
+ ? leven(target, candidate)
147
+ : leven(target, candidate, {maxDistance: cap});
148
+
149
+ // Skip candidates that exceed the user's maximum distance
150
+ if (userMax !== undefined && distance > userMax) {
151
+ continue;
152
+ }
153
+
154
+ // If we got a capped result that equals the cap, we need the actual distance
155
+ // for accurate comparison, but only if the cap was due to userMax
156
+ let actualD = distance;
157
+ if (cap !== undefined && distance === cap && cap === userMax) {
158
+ actualD = leven(target, candidate);
159
+ }
160
+
161
+ if (actualD < bestDist) {
162
+ bestDist = actualD;
163
+ best = candidate;
164
+ if (bestDist === 0) {
165
+ break;
166
+ }
167
+ }
168
+ }
169
+
170
+ if (userMax !== undefined && bestDist > userMax) {
171
+ return undefined;
172
+ }
173
+
174
+ return best;
175
+ }
package/license ADDED
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/package.json ADDED
@@ -0,0 +1,86 @@
1
+ {
2
+ "name": "@cjser/leven",
3
+ "version": "4.1.0-cjser.2",
4
+ "description": "Measure the difference between two strings using the Levenshtein distance algorithm",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://code.moenext.com/3rdeye/cjser.git"
9
+ },
10
+ "funding": "https://github.com/sponsors/sindresorhus",
11
+ "author": {
12
+ "name": "Sindre Sorhus",
13
+ "email": "sindresorhus@gmail.com",
14
+ "url": "https://sindresorhus.com"
15
+ },
16
+ "type": "module",
17
+ "exports": {
18
+ "require": "./dist-cjser/index.cjs",
19
+ "default": "./index.js"
20
+ },
21
+ "types": "./index.d.ts",
22
+ "sideEffects": false,
23
+ "engines": {
24
+ "node": "^12.20.0 || ^14.13.1 || >=16.0.0"
25
+ },
26
+ "scripts": {
27
+ "test": "xo && ava",
28
+ "bench": "node bench.js"
29
+ },
30
+ "files": [
31
+ "index.js",
32
+ "index.d.ts",
33
+ "dist-cjser"
34
+ ],
35
+ "keywords": [
36
+ "leven",
37
+ "levenshtein",
38
+ "distance",
39
+ "algorithm",
40
+ "string",
41
+ "difference",
42
+ "diff",
43
+ "fast",
44
+ "fuzzy",
45
+ "similar",
46
+ "similarity",
47
+ "compare",
48
+ "comparison",
49
+ "edit",
50
+ "text",
51
+ "match",
52
+ "matching"
53
+ ],
54
+ "devDependencies": {
55
+ "ava": "^3.15.0",
56
+ "fast-levenshtein": "^3.0.0",
57
+ "ld": "^0.1.0",
58
+ "levdist": "^2.2.10",
59
+ "levenshtein": "^1.0.5",
60
+ "levenshtein-component": "^0.0.1",
61
+ "levenshtein-edit-distance": "^3.0.0",
62
+ "natural": "^5.0.4",
63
+ "talisman": "^1.1.4",
64
+ "tinybench": "^4.0.1",
65
+ "xo": "^0.44.0"
66
+ },
67
+ "main": "./dist-cjser/index.cjs",
68
+ "cjser": {
69
+ "sourceVersion": "4.1.0",
70
+ "cjserVersion": 2,
71
+ "original": {
72
+ "name": "leven",
73
+ "version": "4.1.0",
74
+ "exports": "./index.js",
75
+ "repository": "sindresorhus/leven",
76
+ "files": [
77
+ "index.js",
78
+ "index.d.ts"
79
+ ],
80
+ "scripts": {
81
+ "test": "xo && ava",
82
+ "bench": "node bench.js"
83
+ }
84
+ }
85
+ }
86
+ }
package/readme.md ADDED
@@ -0,0 +1,109 @@
1
+ # leven
2
+
3
+ > Measure the difference between two strings using the [Levenshtein distance](https://en.wikipedia.org/wiki/Levenshtein_distance) algorithm
4
+
5
+ ## Install
6
+
7
+ ```sh
8
+ npm install leven
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```js
14
+ import leven from 'leven';
15
+
16
+ leven('cat', 'cow');
17
+ //=> 2
18
+ ```
19
+
20
+ ## API
21
+
22
+ ### leven(first, second, options?)
23
+
24
+ #### first
25
+
26
+ Type: `string`
27
+
28
+ First string.
29
+
30
+ #### second
31
+
32
+ Type: `string`
33
+
34
+ Second string.
35
+
36
+ #### options
37
+
38
+ Type: `object`
39
+
40
+ ##### maxDistance
41
+
42
+ Type: `number`
43
+
44
+ Maximum distance to calculate.
45
+
46
+ If the actual distance exceeds this value, the function will return `maxDistance` instead of the actual distance. This can significantly improve performance when you only care about matches within a certain threshold.
47
+
48
+ ```js
49
+ import leven from 'leven';
50
+
51
+ leven('abcdef', '123456', {maxDistance: 3});
52
+ //=> 3
53
+
54
+ leven('cat', 'cow', {maxDistance: 5});
55
+ //=> 2
56
+ ```
57
+
58
+ ### closestMatch(target, candidates, options?)
59
+
60
+ Find the closest matching string from an array of candidates.
61
+
62
+ #### target
63
+
64
+ Type: `string`
65
+
66
+ The string to find matches for.
67
+
68
+ #### candidates
69
+
70
+ Type: `string[]`
71
+
72
+ Array of candidate strings to search through.
73
+
74
+ #### options
75
+
76
+ Type: `object`
77
+
78
+ Same options as `leven()`.
79
+
80
+ ##### maxDistance
81
+
82
+ Type: `number`
83
+
84
+ Maximum distance to consider. Candidates with a distance greater than this value will be ignored.
85
+
86
+ Returns the closest matching string from candidates, or `undefined` if no candidates are provided or if no match is found within `maxDistance`.
87
+
88
+ ```js
89
+ import {closestMatch} from 'leven';
90
+
91
+ closestMatch('kitten', ['sitting', 'kitchen', 'mittens']);
92
+ //=> 'kitchen'
93
+
94
+ closestMatch('hello', ['jello', 'yellow', 'bellow'], {maxDistance: 2});
95
+ //=> 'jello'
96
+
97
+ // No match within distance threshold
98
+ closestMatch('abcdef', ['123456', '1234567890'], {maxDistance: 2});
99
+ //=> undefined
100
+ ```
101
+
102
+ ## Related
103
+
104
+ - [leven-cli](https://github.com/sindresorhus/leven-cli) - CLI for this module
105
+
106
+ ## cjser
107
+
108
+ This package is a CommonJS-compatible build generated by cjser for projects that still need `require()` support. The source version matches the original npm package version, with a cjser prerelease suffix for this generated build.
109
+ Original repository: https://github.com/sindresorhus/leven