didyoumean3
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -38,41 +38,28 @@ 'use strict'; | ||
const dice = (a, b) => { | ||
let lena = a.length; | ||
let lenb = b.length; // boundary conditions | ||
if (!lena && !lenb) return 1; | ||
if (!lena || !lenb) return 0; | ||
if (a === b) return 1; // means when the words like 'ab' with 'a', it's work not well | ||
if (lena < 2 || lenb < 2) return 0; | ||
let map = new Map(); | ||
let e = 0; // intersections | ||
let r; // bigrams | ||
let c; // counter | ||
const bigrams = s => { | ||
const grams = []; | ||
const l = ~-s.length; | ||
if (!l) return [s]; | ||
let i = -1; | ||
let t = lena - 1; | ||
while (++i < t) { | ||
r = a.substring(i, i + 2); | ||
c = map.has(r) ? map.get(r) + 1 : 1; | ||
map.set(r, c); | ||
} | ||
while (++i < l) grams[i] = s.slice(i, i + 2); | ||
i = -1; | ||
t = lenb - 1; | ||
return grams; | ||
}; | ||
while (++i < t) { | ||
r = b.substring(i, i + 2); | ||
c = map.has(r) ? map.get(r) : 0; | ||
const dice = (a, b) => { | ||
if (a === b) return 1; | ||
let la = a.length; | ||
let lb = b.length; | ||
if (!la || !lb) return 0; | ||
let l = bigrams(a); | ||
let r = bigrams(b); | ||
la = l.length; | ||
lb = r.length; | ||
let i, | ||
c = 0; | ||
if (c > 0) { | ||
e++; | ||
map.set(r, ~-c); | ||
} | ||
} | ||
for (i = 0; i < la; i++) r.includes(l[i]) && c++; | ||
return 2 * e / (lena + lenb - 2); | ||
return 2 * c / (la + lb); | ||
}; | ||
@@ -95,4 +82,3 @@ | ||
let lb = b.length; | ||
if (!la) return lb; | ||
if (!lb) return la; // trim common suffix, that's the reson why we need swith the params according to it's length | ||
if (!la) return lb; // trim common suffix, that's the reson why we need swith the params according to it's length | ||
@@ -99,0 +85,0 @@ while (la > 0 && a.charCodeAt(~-la) === b.charCodeAt(~-lb)) { |
@@ -1,2 +0,2 @@ | ||
"use strict";function r(){return(r=Object.assign||function(r){for(var e=1;e<arguments.length;e++){var t=arguments[e];for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(r[n]=t[n])}return r}).apply(this,arguments)}Object.defineProperty(exports,"__esModule",{value:!0});const e=(r,e)=>{let t=r.length,n=e.length;if(!t&&!n)return 1;if(!t||!n)return 0;if(r===e)return 1;if(t<2||n<2)return 0;let l,o,i=new Map,s=0,a=-1,c=t-1;for(;++a<c;)l=r.substring(a,a+2),o=i.has(l)?i.get(l)+1:1,i.set(l,o);for(a=-1,c=n-1;++a<c;)l=e.substring(a,a+2),o=i.has(l)?i.get(l):0,o>0&&(s++,i.set(l,~-o));return 2*s/(t+n-2)},t=[],n=[],l=(r,e)=>{if(r===e)return 0;const l=r;r.length>e.length&&(r=e,e=l);let o=r.length,i=e.length;if(!o)return i;if(!i)return o;for(;o>0&&r.charCodeAt(~-o)===e.charCodeAt(~-i);)o--,i--;if(!o)return i;let s=0;for(;s<o&&r.charCodeAt(s)===e.charCodeAt(s);)s++;if(o-=s,i-=s,!o)return i;let a=0;for(;a<i;)n[a]=e.charCodeAt(s+a),t[a]=++a;let c,u,f,h,g=0;for(a=0;a<o;a++)for(u=a,g=a+1,f=r.charCodeAt(s+a),h=0;h<i;h++)c=g,g=u,u=t[h],f!==n[h]&&(u<g&&(g=u),c<g&&(g=c),g++),t[h]=g;return g};var o={dice:e,leven:l};const i=r=>!!(r&&r.constructor&&r.call&&r.apply),s=(r,{normalize:e,trim:t,ignore:n,trimAll:l,diacritics:o})=>i(e)?e(r):(t&&(r=r.trim()),l&&(r=r.replace(/\s+/g,"")),o&&(r=r.normalize()),n&&(r=r.toLowerCase()),r),a=(r,e)=>"string"==typeof r?r:i(e)?e(r):"",c={ignore:!1,trim:!0,trimAll:!1,diacritics:!1},u=(e,t,n)=>{const l=r({},c,{},n),{val:u,similar:f="leven",result:h,compartor:g}=l,p=function(r,e){if(null==r)return{};var t,n,l={},o=Object.keys(r);for(n=0;n<o.length;n++)e.indexOf(t=o[n])>=0||(l[t]=r[t]);return l}(l,["val","similar","result","compartor"]);e=s(e,p);const d=(r=>e=>i(r)?r(e):e)(h);if(!e)return d(null);const m=(r=>i(r)?r:o[r])(f),v=((r,e)=>(t,n)=>i(r)?r(t,n):"leven"===e?t<n:t>n)(g,f),y=[];let A=null,C=null;for(let r=0,n=t.length;r<n;r++){const n=t[r],l=m(e,s(a(n,u),p));y.push({score:l,target:n}),(null===C||v(l,C))&&(C=l,A=n)}return d({matches:y,winner:A})};u.dice=e,u.leven=l,exports.default=u,exports.didyoumean3=u; | ||
"use strict";function e(){return(e=Object.assign||function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])}return e}).apply(this,arguments)}Object.defineProperty(exports,"__esModule",{value:!0});const r=e=>{const r=[],t=~-e.length;if(!t)return[e];let n=-1;for(;++n<t;)r[n]=e.slice(n,n+2);return r},t=(e,t)=>{if(e===t)return 1;let n=e.length,l=t.length;if(!n||!l)return 0;let o=r(e),i=r(t);n=o.length,l=i.length;let c,a=0;for(c=0;c<n;c++)i.includes(o[c])&&a++;return 2*a/(n+l)},n=[],l=[],o=(e,r)=>{if(e===r)return 0;const t=e;e.length>r.length&&(e=r,r=t);let o=e.length,i=r.length;if(!o)return i;for(;o>0&&e.charCodeAt(~-o)===r.charCodeAt(~-i);)o--,i--;if(!o)return i;let c=0;for(;c<o&&e.charCodeAt(c)===r.charCodeAt(c);)c++;if(o-=c,i-=c,!o)return i;let a=0;for(;a<i;)l[a]=r.charCodeAt(c+a),n[a]=++a;let s,u,f,h,g=0;for(a=0;a<o;a++)for(u=a,g=a+1,f=e.charCodeAt(c+a),h=0;h<i;h++)s=g,g=u,u=n[h],f!==l[h]&&(u<g&&(g=u),s<g&&(g=s),g++),n[h]=g;return g};var i={dice:t,leven:o};const c=e=>!!(e&&e.constructor&&e.call&&e.apply),a=(e,{normalize:r,trim:t,ignore:n,trimAll:l,diacritics:o})=>c(r)?r(e):(t&&(e=e.trim()),l&&(e=e.replace(/\s+/g,"")),o&&(e=e.normalize()),n&&(e=e.toLowerCase()),e),s=(e,r)=>"string"==typeof e?e:c(r)?r(e):"",u={ignore:!1,trim:!0,trimAll:!1,diacritics:!1},f=(r,t,n)=>{const l=e({},u,{},n),{val:o,similar:f="leven",result:h,compartor:g}=l,d=function(e,r){if(null==e)return{};var t,n,l={},o=Object.keys(e);for(n=0;n<o.length;n++)r.indexOf(t=o[n])>=0||(l[t]=e[t]);return l}(l,["val","similar","result","compartor"]);r=a(r,d);const p=(e=>r=>c(e)?e(r):r)(h);if(!r)return p(null);const m=(e=>c(e)?e:i[e])(f),v=((e,r)=>(t,n)=>c(e)?e(t,n):"leven"===r?t<n:t>n)(g,f),y=[];let A=null,C=null;for(let e=0,n=t.length;e<n;e++){const n=t[e],l=m(r,a(s(n,o),d));y.push({score:l,target:n}),(null===C||v(l,C))&&(C=l,A=n)}return p({matches:y,winner:A})};f.dice=t,f.leven=o,exports.default=f,exports.didyoumean3=f; | ||
//# sourceMappingURL=didyoumean3.cjs.production.min.js.map |
@@ -34,41 +34,28 @@ function _extends() { | ||
const dice = (a, b) => { | ||
let lena = a.length; | ||
let lenb = b.length; // boundary conditions | ||
if (!lena && !lenb) return 1; | ||
if (!lena || !lenb) return 0; | ||
if (a === b) return 1; // means when the words like 'ab' with 'a', it's work not well | ||
if (lena < 2 || lenb < 2) return 0; | ||
let map = new Map(); | ||
let e = 0; // intersections | ||
let r; // bigrams | ||
let c; // counter | ||
const bigrams = s => { | ||
const grams = []; | ||
const l = ~-s.length; | ||
if (!l) return [s]; | ||
let i = -1; | ||
let t = lena - 1; | ||
while (++i < t) { | ||
r = a.substring(i, i + 2); | ||
c = map.has(r) ? map.get(r) + 1 : 1; | ||
map.set(r, c); | ||
} | ||
while (++i < l) grams[i] = s.slice(i, i + 2); | ||
i = -1; | ||
t = lenb - 1; | ||
return grams; | ||
}; | ||
while (++i < t) { | ||
r = b.substring(i, i + 2); | ||
c = map.has(r) ? map.get(r) : 0; | ||
const dice = (a, b) => { | ||
if (a === b) return 1; | ||
let la = a.length; | ||
let lb = b.length; | ||
if (!la || !lb) return 0; | ||
let l = bigrams(a); | ||
let r = bigrams(b); | ||
la = l.length; | ||
lb = r.length; | ||
let i, | ||
c = 0; | ||
if (c > 0) { | ||
e++; | ||
map.set(r, ~-c); | ||
} | ||
} | ||
for (i = 0; i < la; i++) r.includes(l[i]) && c++; | ||
return 2 * e / (lena + lenb - 2); | ||
return 2 * c / (la + lb); | ||
}; | ||
@@ -91,4 +78,3 @@ | ||
let lb = b.length; | ||
if (!la) return lb; | ||
if (!lb) return la; // trim common suffix, that's the reson why we need swith the params according to it's length | ||
if (!la) return lb; // trim common suffix, that's the reson why we need swith the params according to it's length | ||
@@ -95,0 +81,0 @@ while (la > 0 && a.charCodeAt(~-la) === b.charCodeAt(~-lb)) { |
{ | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"license": "MIT", | ||
@@ -25,9 +25,10 @@ "main": "dist/index.js", | ||
"build": "tsdx build --target node", | ||
"test": "tsdx test", | ||
"test": "tsdx test --coverage", | ||
"lint": "tsdx lint --fix", | ||
"prepare": "tsdx build --target node" | ||
"prepare": "tsdx build --target node", | ||
"bench": "npm run build && node bench.js" | ||
}, | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "tsdx lint" | ||
"pre-commit": "tsdx lint --fix" | ||
} | ||
@@ -34,0 +35,0 @@ }, |
@@ -36,2 +36,4 @@ # didyoumean3 | ||
``` | ||
**notice: If you customize the algorithm, you must also specify a comparator to indicate whether the output is the smallest or the largest** | ||
read more config info 👇 | ||
@@ -89,6 +91,6 @@ | ||
```js | ||
didyoumean x 193,411 ops/sec ±1.39% (87 runs sampled) | ||
didyoumean2 x 303,996 ops/sec ±1.72% (82 runs sampled) | ||
didyoumean3-leven x 489,616 ops/sec ±0.76% (89 runs sampled) | ||
didyoumean3-dice x 130,456 ops/sec ±0.57% (91 runs sampled) | ||
didyoumean x 194,593 ops/sec ±1.07% (84 runs sampled) | ||
didyoumean2 x 311,318 ops/sec ±0.63% (90 runs sampled) | ||
didyoumean3-leven x 510,067 ops/sec ±0.48% (84 runs sampled) | ||
didyoumean3-dice x 294,427 ops/sec ±0.46% (85 runs sampled) | ||
Fastest is didyoumean3-leven | ||
@@ -99,2 +101,8 @@ ``` | ||
#### v-1.0.1 | ||
1. improve the performance of the dice-coefficient algorithm | ||
2. add CI/CD | ||
3. coverage test | ||
#### v-1.0.0 | ||
@@ -101,0 +109,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
121
0
53136
444