atriusmaps-node-sdk
Version:
This project provides an API to Atrius Personal Wayfinder maps within a Node environment. See the README.md for more information
80 lines (77 loc) • 1.82 kB
JavaScript
;
function pipeline(str, normalize2, split, _collapse) {
if (str) {
if (normalize2) {
str = replace(
str,
/** @type {Array<string|RegExp>} */
normalize2
);
}
if (this.matcher) {
str = replace(str, this.matcher);
}
if (this.stemmer && str.length > 1) {
str = replace(str, this.stemmer);
}
if (_collapse && str.length > 1) {
str = collapse(str);
}
if (split || split === "") {
const words = str.split(
/** @type {string|RegExp} */
split
);
return this.filter ? filter(words, this.filter) : words;
}
}
return str;
}
const regex_whitespace = /[\p{Z}\p{S}\p{P}\p{C}]+/u;
const regex_normalize = /[\u0300-\u036f'-]/gu;
function normalize(str) {
if (str.normalize) {
str = str.normalize("NFD").replace(regex_normalize, "");
}
return str;
}
function replace(str, regexp) {
for (let i = 0, len = regexp.length; i < len; i += 2) {
str = str.replace(regexp[i], regexp[i + 1]);
if (!str) {
break;
}
}
return str;
}
function regex(str) {
return new RegExp(str, "g");
}
function collapse(string) {
let final = "";
let prev = "";
for (let i = 0, len = string.length, char; i < len; i++) {
if ((char = string[i]) !== prev) {
final += prev = char;
}
}
return final;
}
function filter(words, map) {
const length = words.length;
const filtered = [];
for (let i = 0, count = 0; i < length; i++) {
const word = words[i];
if (word && !map[word]) {
filtered[count++] = word;
}
}
return filtered;
}
exports.collapse = collapse;
exports.filter = filter;
exports.normalize = normalize;
exports.pipeline = pipeline;
exports.regex = regex;
exports.regex_whitespace = regex_whitespace;
exports.replace = replace;