2020-03-20 19:00:48 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const slugify = require('@sindresorhus/slugify');
|
|
|
|
|
2020-05-13 13:17:25 +02:00
|
|
|
const nameToSlug = (name, options = { separator: '-' }) => slugify(name, options);
|
2020-03-20 19:00:48 +01:00
|
|
|
|
|
|
|
const nameToCollectionName = name => slugify(name, { separator: '_' });
|
|
|
|
|
2020-05-08 13:50:00 +02:00
|
|
|
const getCommonBeginning = (str1 = '', str2 = '') => {
|
|
|
|
let common = '';
|
|
|
|
let index = 0;
|
|
|
|
while (index < str1.length && index < str2.length) {
|
|
|
|
if (str1[index] === str2[index]) {
|
|
|
|
common += str1[index];
|
|
|
|
index += 1;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return common;
|
|
|
|
};
|
|
|
|
|
2020-04-30 12:11:12 +02:00
|
|
|
const escapeQuery = (query, charsToEscape, escapeChar = '\\') => {
|
2020-04-24 12:06:35 +02:00
|
|
|
return query
|
|
|
|
.split('')
|
|
|
|
.reduce(
|
|
|
|
(escapedQuery, char) =>
|
|
|
|
charsToEscape.includes(char)
|
2020-04-30 12:11:12 +02:00
|
|
|
? `${escapedQuery}${escapeChar}${char}`
|
2020-04-24 12:06:35 +02:00
|
|
|
: `${escapedQuery}${char}`,
|
|
|
|
''
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-06-23 16:31:16 +02:00
|
|
|
const stringIncludes = (arr, val) => arr.map(String).includes(String(val));
|
|
|
|
const stringEquals = (a, b) => String(a) === String(b);
|
|
|
|
|
2020-03-20 19:00:48 +01:00
|
|
|
module.exports = {
|
|
|
|
nameToSlug,
|
|
|
|
nameToCollectionName,
|
2020-05-08 13:50:00 +02:00
|
|
|
getCommonBeginning,
|
2020-04-24 12:06:35 +02:00
|
|
|
escapeQuery,
|
2020-06-23 16:31:16 +02:00
|
|
|
stringIncludes,
|
|
|
|
stringEquals,
|
2020-03-20 19:00:48 +01:00
|
|
|
};
|