similar-string
v0.1.0
Published
string extrapolated relations based on string similarity range between 0 and 1
Downloads
5
Maintainers
Readme
similar-string
This library provides string extrapolated relations based on string similarity range between 0 and 1.
Install
npm install similar-string --save
Why?
There are several assertions with strings like:
const _ = require('lodash')
const x = 'foobar'
const y = 'foo'
x === y // Is string x the same as y?
x.includes(y) // Does x include the string y?
_.startsWith(x, y) // Does x starts with prefix y
_.endsWith(x, y) // Does x ends with the suffix y
These assertions could be true
or false
. This package provides the same relations based on fuzzy logic. Then, we don't have boolean results. Instead of that, we have a [0, 1] range to know the partial truth value:
const stringDistance = require('leven') // stringDistance('abc', 'edc') === 2
const stringSimilarity = (a, b) => {
const maxLength = Math.max(a.length, b.length)
return maxLength === 0 ? 1 : (maxLength - stringDistance(a, b)) / maxLength
} // stringSimilarity('abc', 'edc') === 0.3333333
const SimilarString = require('similar-string')(stringSimilarity)
const x = new SimilarString('foobar')
const y = 'poo'
x.equals(y) // 0.3333333333333333
x.includes(y) // 0.6666666666666666
x.startsWith(y) // 0.6666666666666666
x.endsWith(y) // 0
Support
- Node.js >=6
License
MIT