2020-03-20 19:00:48 +01:00
|
|
|
'use strict';
|
2021-08-17 19:25:14 +08:00
|
|
|
const _ = require('lodash');
|
2020-03-20 19:00:48 +01:00
|
|
|
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: '_' });
|
|
|
|
|
2021-08-17 19:10:29 +02:00
|
|
|
const getCommonBeginning = (...strings) =>
|
|
|
|
_.takeWhile(strings[0], (char, index) => strings.every(string => string[index] === char)).join(
|
|
|
|
''
|
|
|
|
);
|
2021-08-17 19:25:14 +08:00
|
|
|
|
|
|
|
const getCommonPath = (...paths) => {
|
|
|
|
const [segments, ...otherSegments] = paths.map(it => _.split(it, '/'));
|
|
|
|
return _.join(
|
2021-08-17 19:10:29 +02:00
|
|
|
_.takeWhile(segments, (str, index) => otherSegments.every(it => it[index] === str)),
|
|
|
|
'/'
|
|
|
|
);
|
2020-05-08 13:50:00 +02:00
|
|
|
};
|
|
|
|
|
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);
|
2021-08-20 15:23:02 +02:00
|
|
|
const isCamelCase = value => /^[a-z][a-zA-Z0-9]+$/.test(value);
|
|
|
|
const isKebabCase = value => /^([a-z][a-z0-9]*)(-[a-z0-9]+)*$/.test(value);
|
2020-06-23 16:31:16 +02:00
|
|
|
|
2020-03-20 19:00:48 +01:00
|
|
|
module.exports = {
|
|
|
|
nameToSlug,
|
|
|
|
nameToCollectionName,
|
2020-05-08 13:50:00 +02:00
|
|
|
getCommonBeginning,
|
2021-08-17 19:25:14 +08:00
|
|
|
getCommonPath,
|
2020-04-24 12:06:35 +02:00
|
|
|
escapeQuery,
|
2020-06-23 16:31:16 +02:00
|
|
|
stringIncludes,
|
|
|
|
stringEquals,
|
2021-08-20 15:23:02 +02:00
|
|
|
isCamelCase,
|
|
|
|
isKebabCase,
|
2020-03-20 19:00:48 +01:00
|
|
|
};
|