didyoumean3
Advanced tools
Comparing version 1.1.2 to 1.1.3
# changelog | ||
#### v-1.1.3 | ||
1. delete opt compartor | ||
2. add opt filter | ||
#### v-1.1.2 | ||
@@ -4,0 +9,0 @@ |
@@ -50,6 +50,6 @@ 'use strict'; | ||
const dice = (a, b) => { | ||
if (a === b) return 1; | ||
if (a === b) return 0; | ||
let la = a.length; | ||
let lb = b.length; | ||
if (!la || !lb) return 0; | ||
if (!la || !lb) return 1; | ||
let l = bigrams(a); | ||
@@ -64,3 +64,3 @@ let r = bigrams(b); | ||
return 2 * c / (la + lb); | ||
return 1 - 2 * c / (la + lb); | ||
}; | ||
@@ -173,6 +173,2 @@ | ||
const resultFactory = res => r => isFunction(res) ? res(r) : r; | ||
const compareFactory = (c, s) => (a, b) => { | ||
if (isFunction(c)) return c(a, b); | ||
return s === 'leven' ? a < b : a > b; | ||
}; | ||
@@ -183,3 +179,4 @@ const defaultOpts = { | ||
trimAll: false, | ||
diacritics: false | ||
diacritics: false, | ||
filter: () => true | ||
}; | ||
@@ -193,5 +190,5 @@ | ||
result, | ||
compartor | ||
filter | ||
} = _defaultOpts$opts, | ||
cfg = _objectWithoutPropertiesLoose(_defaultOpts$opts, ["val", "similar", "result", "compartor"]); | ||
cfg = _objectWithoutPropertiesLoose(_defaultOpts$opts, ["val", "similar", "result", "filter"]); | ||
@@ -202,3 +199,2 @@ s = normalize(s, cfg); | ||
const calc = similarFactory(similar); | ||
const compare = compareFactory(compartor, similar); | ||
const matches = []; | ||
@@ -210,5 +206,4 @@ let winner = null; | ||
const target = t[i]; | ||
const score = calc(s, normalize(getVal(target, val), cfg)); // TODO: condition? | ||
matches.push({ | ||
const score = calc(s, normalize(getVal(target, val), cfg)); | ||
filter(score, target) && matches.push({ | ||
score, | ||
@@ -218,3 +213,3 @@ target | ||
if (temp === null || compare(score, temp)) { | ||
if (temp === null || score < temp) { | ||
temp = score; | ||
@@ -221,0 +216,0 @@ winner = target; |
@@ -1,2 +0,2 @@ | ||
"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; | ||
"use strict";function e(){return(e=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e}).apply(this,arguments)}Object.defineProperty(exports,"__esModule",{value:!0});const t=e=>{const t=[],r=~-e.length;if(!r)return[e];let n=-1;for(;++n<r;)t[n]=e.slice(n,n+2);return t},r=(e,r)=>{if(e===r)return 0;let n=e.length,l=r.length;if(!n||!l)return 1;let i=t(e),o=t(r);n=i.length,l=o.length;let c,s=0;for(c=0;c<n;c++)o.includes(i[c])&&s++;return 1-2*s/(n+l)},n=[],l=[],i=(e,t)=>{if(e===t)return 0;const r=e;e.length>t.length&&(e=t,t=r);let i=e.length,o=t.length;if(!i)return o;for(;i>0&&e.charCodeAt(~-i)===t.charCodeAt(~-o);)i--,o--;if(!i)return o;let c=0;for(;c<i&&e.charCodeAt(c)===t.charCodeAt(c);)c++;if(i-=c,o-=c,!i)return o;let s=0;for(;s<o;)l[s]=t.charCodeAt(c+s),n[s]=++s;let u,a,f,h,g=0;for(s=0;s<i;s++)for(a=s,g=s+1,f=e.charCodeAt(c+s),h=0;h<o;h++)u=g,g=a,a=n[h],f!==l[h]&&(a<g&&(g=a),u<g&&(g=u),g++),n[h]=g;return g};var o={dice:r,leven:i};const c=e=>!!(e&&e.constructor&&e.call&&e.apply),s=(e,{normalize:t,trim:r,ignore:n,trimAll:l,diacritics:i})=>c(t)?t(e):(r&&(e=e.trim()),l&&(e=e.replace(/\s+/g,"")),i&&(e=e.normalize()),n&&(e=e.toLowerCase()),e),u=(e,t)=>"string"==typeof e?e:c(t)?t(e):"",a={ignore:!1,trim:!0,trimAll:!1,diacritics:!1,filter:()=>!0},f=(t,r,n)=>{const l=e({},a,{},n),{val:i,similar:f="leven",result:h,filter:g}=l,d=function(e,t){if(null==e)return{};var r,n,l={},i=Object.keys(e);for(n=0;n<i.length;n++)t.indexOf(r=i[n])>=0||(l[r]=e[r]);return l}(l,["val","similar","result","filter"]);t=s(t,d);const p=(e=>t=>c(e)?e(t):t)(h);if(!t)return p(null);const m=(e=>c(e)?e:o[e])(f),v=[];let y=null,A=null;for(let e=0,n=r.length;e<n;e++){const n=r[e],l=m(t,s(u(n,i),d));g(l,n)&&v.push({score:l,target:n}),(null===A||l<A)&&(A=l,y=n)}return p({matches:v,winner:y})};f.dice=r,f.leven=i,exports.default=f,exports.didyoumean3=f; | ||
//# sourceMappingURL=didyoumean3.cjs.production.min.js.map |
@@ -46,6 +46,6 @@ function _extends() { | ||
const dice = (a, b) => { | ||
if (a === b) return 1; | ||
if (a === b) return 0; | ||
let la = a.length; | ||
let lb = b.length; | ||
if (!la || !lb) return 0; | ||
if (!la || !lb) return 1; | ||
let l = bigrams(a); | ||
@@ -60,3 +60,3 @@ let r = bigrams(b); | ||
return 2 * c / (la + lb); | ||
return 1 - 2 * c / (la + lb); | ||
}; | ||
@@ -169,6 +169,2 @@ | ||
const resultFactory = res => r => isFunction(res) ? res(r) : r; | ||
const compareFactory = (c, s) => (a, b) => { | ||
if (isFunction(c)) return c(a, b); | ||
return s === 'leven' ? a < b : a > b; | ||
}; | ||
@@ -179,3 +175,4 @@ const defaultOpts = { | ||
trimAll: false, | ||
diacritics: false | ||
diacritics: false, | ||
filter: () => true | ||
}; | ||
@@ -189,5 +186,5 @@ | ||
result, | ||
compartor | ||
filter | ||
} = _defaultOpts$opts, | ||
cfg = _objectWithoutPropertiesLoose(_defaultOpts$opts, ["val", "similar", "result", "compartor"]); | ||
cfg = _objectWithoutPropertiesLoose(_defaultOpts$opts, ["val", "similar", "result", "filter"]); | ||
@@ -198,3 +195,2 @@ s = normalize(s, cfg); | ||
const calc = similarFactory(similar); | ||
const compare = compareFactory(compartor, similar); | ||
const matches = []; | ||
@@ -206,5 +202,4 @@ let winner = null; | ||
const target = t[i]; | ||
const score = calc(s, normalize(getVal(target, val), cfg)); // TODO: condition? | ||
matches.push({ | ||
const score = calc(s, normalize(getVal(target, val), cfg)); | ||
filter(score, target) && matches.push({ | ||
score, | ||
@@ -214,3 +209,3 @@ target | ||
if (temp === null || compare(score, temp)) { | ||
if (temp === null || score < temp) { | ||
temp = score; | ||
@@ -217,0 +212,0 @@ winner = target; |
@@ -13,4 +13,4 @@ export interface Val { | ||
} | ||
export interface Compartor { | ||
(a: number, b: number): boolean; | ||
export interface Filter { | ||
(score: number, target: string | object): boolean; | ||
} | ||
@@ -26,3 +26,3 @@ export declare type BuiltInSimilar = 'dice' | 'leven'; | ||
* @type {Function} result: you can custom your return result | ||
* @type {Function} compartor: you can custom the compare rules, because will maybe use the highest score or the lowest score | ||
* @type {Function} filter: you can filter the data into the returned results | ||
*/ | ||
@@ -38,3 +38,3 @@ export declare type Options = { | ||
result?: Return; | ||
compartor?: Compartor; | ||
filter?: Filter; | ||
}; | ||
@@ -46,2 +46,1 @@ export declare const isFunction: (f: any) => boolean; | ||
export declare const resultFactory: (res?: Return | undefined) => (r: any) => any; | ||
export declare const compareFactory: (c?: Compartor | undefined, s?: "dice" | "leven" | undefined) => (a: number, b: number) => boolean; |
{ | ||
"version": "1.1.2", | ||
"version": "1.1.3", | ||
"license": "MIT", | ||
@@ -4,0 +4,0 @@ "main": "dist/index.js", |
@@ -24,2 +24,3 @@ # didyoumean3 | ||
- Super fast | ||
- More flexible configuration | ||
- Super small (production.min.js ~ 2kb) and tree shaking! | ||
@@ -78,7 +79,6 @@ - [ ] Support emoji or diacritics | ||
// or use your custom algorithm | ||
// notice: If you customize the algorithm, | ||
// you must also specify a comparator to indicate whether the output is the smallest or the largest | ||
const your_leven = require('some/leven'); | ||
// notice: If you customize the algorithm, the optimal route must take the minimum | ||
const your_leven = require('some/your_leven'); | ||
const your_comparator = (a: number, b: number) => a < b; | ||
didyoumean3(input, list, { similar: your_leven, comparator: your_comparator }) | ||
didyoumean3(input, list, { similar: your_leven }) | ||
@@ -126,18 +126,18 @@ | ||
* @type {boolean} diacritics: 'café' -> 'café'.normalize() | ||
* @type {Function} val: when passing in an array of objects, you can specify the return result through val | ||
* @type {Function} val: when you need find the best result in a object list, it's useful | ||
* @type {string | Function} similar: use builtin shortest edit-distance algorithm or yours | ||
* @type {Function} result: you can custom your return result | ||
* @type {Function} compartor: you can custom the compare rules, because will maybe use the highest score or the lowest score | ||
* @type {Function} filter: you can filter the data into the returned results | ||
*/ | ||
export type Options = { | ||
ignore?: boolean, // default false | ||
trim?: boolean, // default true | ||
trimAll?: boolean, // default false | ||
diacritics?: boolean, // default false | ||
normalize?: Normalize, // default undefined | ||
val?: Val, // default undefined | ||
similar?: BuiltInSimilar | Similar, // default leven | ||
result?: Return, // default undefined | ||
compartor?: Compartor // default undefined | ||
} | ||
ignore?: boolean; | ||
trim?: boolean; | ||
trimAll?: boolean; | ||
diacritics?: boolean; | ||
normalize?: Normalize; | ||
val?: Val; | ||
similar?: BuiltInSimilar | Similar; | ||
result?: Return; | ||
filter?: Filter; | ||
}; | ||
``` | ||
@@ -144,0 +144,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
52475
434