close-enough
v1.1.0
Published
String comparison allowing for arbitrary differences
Downloads
24
Readme
close-enough
String comparison allowing for arbitrary differences.
Install
$ npm install --save close-enoughUsage
const closeEnough = require('close-enough');
// Create a comparator instance
const ce = closeEnough();
// Basic string comparison
ce.compare('Hello World', 'hello world'); // true - case insensitive
ce.compare('abc def', 'def abc'); // true - word order doesn't matter
ce.compare('café', 'cafe'); // true - diacritical marks are ignored
// Different strings
ce.compare('abc def', 'abc xyz'); // false
// Separators and spacing are normalized
ce.compare('hello-world', 'hello world'); // true
ce.compare('hello/world', 'hello_world'); // true
ce.compare('hello world', 'hello world'); // true
// Extra words are ignored (shorter string must be subset of longer)
ce.compare('abc def', 'abc zzz def'); // true
ce.compare('abc def ghi', 'abc def'); // true
// Get similarity score (0 = identical, higher = more different)
ce.score('hello world', 'hello world'); // 0
ce.score('hello world', 'hello earth'); // 0.5Advanced Usage
Filtering Generic Words
Remove common words that should be ignored during comparison:
const ce = closeEnough().generics(['hotel', 'inn', 'suites']);
ce.compare('Holiday Inn Hotel', 'Holiday Suites'); // trueUsing Synonyms
Define words that should be treated as equivalent:
const ce = closeEnough().synonyms({
'street': 'st',
'avenue': 'ave',
'road': 'rd'
});
ce.compare('Main Street', 'Main St'); // true
ce.compare('Park Avenue', 'Park Ave'); // trueChaining Configuration
You can chain multiple configuration methods:
const ce = closeEnough()
.generics(['hotel', 'inn', 'resort'])
.synonyms({
'street': 'st',
'avenue': 'ave'
});
ce.compare('Grand Hotel on Main Street', 'Grand Inn on Main St'); // trueAPI
compare(a, b)- Returnstrueif strings are considered close enough,falseotherwisescore(a, b)- Returns a numeric score indicating difference (0 = identical)generics(array)- Configure generic words to ignore during comparisonsynonyms(object)- Configure word synonyms for comparison
License
MIT © Damian Krzeminski
