Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/FoxxMD/string-sameness. Compare the sameness of two strings foxxmd.github.io/string-sameness
compare cosine-similarity dice-coefficient levenshtein-distance sameness string text typescript
Something went wrong. Try again.
1.5 kB · 43 lines
TypeScript
at main
1234567891011121314151617181920212223242526272829303132333435363738394041424344import {StringTransformFunc} from "../atomic.js";
const PUNCTUATION_REGEX = new RegExp(/[`=(){}<>;',.~!@#$%^&*_+|:"?\-\\\[\]\/]/g);const NON_ALPHANUMERIC_REGEX = new RegExp(/[^\w\s]|_/g);const WHITESPACE_REGEX = new RegExp(/\s/g);const MULTI_WHITESPACE_REGEX = new RegExp(/\s{2,}/g);
const lowercase: StringTransformFunc = (str: string) => str.toLocaleLowerCase();const trim: StringTransformFunc = (str: string) => str.trim();const replaceUnicode: StringTransformFunc = (str: string) => str.normalize('NFD').replace(/[\u0300-\u036f]/g, "");const removePunctuation: StringTransformFunc = (str: string) => str.replace(PUNCTUATION_REGEX, '');const removeNonAlphanumeric: StringTransformFunc = (str: string) => str.replace(NON_ALPHANUMERIC_REGEX, '');const removeWhitespace: StringTransformFunc = (str: string) => str.replace(WHITESPACE_REGEX, '');const replaceMultiWhitespace: StringTransformFunc = (str: string) => str.replace(MULTI_WHITESPACE_REGEX, ' ');
const transforms = { lowercase, trim, replaceMultiWhitespace, replaceUnicode, removeWhitespace, removePunctuation}
const strDefaultTransforms: StringTransformFunc[] = [ replaceUnicode, removePunctuation, trim, replaceMultiWhitespace, lowercase];
export { lowercase, trim, replaceUnicode, removePunctuation, removeWhitespace, removeNonAlphanumeric, replaceMultiWhitespace, transforms, strDefaultTransforms}