-
Notifications
You must be signed in to change notification settings - Fork 0
/
CompareStrings.ts
37 lines (31 loc) · 1.54 KB
/
CompareStrings.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { removeArrayDuplicates } from './RemoveArrayDuplicates';
/**
* Use this function to see how much one key resembles another.
*
* @param {string} [key] Element you want to search for in an Array.
* @param {string} [target] Text you want to compare.
* @returns {number}
*
* @example
* ```js
* import { compareStrings } from 'bucky.js';
*
* console.log(compareStrings('foo', 'bar'));
* ```
*/
export function compareStrings(key: string, target: string): number {
key = key.replace(/\s+/g, '').toLowerCase();
target = target.replace(/\s+/g, '').toLowerCase();
if (typeof key !== 'string') throw new TypeError(`The first parameter has to be of type string, received: ${typeof key}`);
if (typeof target !== 'string') throw new TypeError(`The second parameter has to be of type string, received: ${typeof key}`);
if ((!key.length && !target.length) || (key == target)) return 1;
if (!key.length || !target.length) return 0;
let stringsArray: string[] = removeArrayDuplicates([...key, ...target]),
resolveStrings = (str: string): number[] => stringsArray.map(i => str.split('').includes(i) ? 1 : 0),
resolvedStringKey = resolveStrings(key),
resolvedStringTarget = resolveStrings(target),
junction = [resolvedStringKey, resolvedStringTarget].map(i => i.filter(Boolean).length);
let [size, keySize, targetSize]: number[] = [0, ...junction];
for (let index in resolvedStringKey) size += resolvedStringKey[index] * resolvedStringTarget[index];
return (1.0 * size) / (Math.sqrt(keySize) * Math.sqrt(targetSize));
}