-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
32 lines (21 loc) · 1.06 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const latinMap = require('./latinMap');
const SAFE_CHARACTERS = /[^0-9a-zA-Z! _\\.\\*'\\(\\)\\\-/]/g;
const isNumber = (value) => typeof value === 'number';
const isString = (value) => Object.prototype.toString.call(value) === '[object String]';
const replaceLatinCharacters = (value) =>
value.replace(/[^A-Za-z0-9[\] ]/g, (character) => latinMap[character] || character);
const removeIllegalCharacters = (value) => value.replace(SAFE_CHARACTERS, '');
const isValidSeparator = (separator) => separator && !separator.match(SAFE_CHARACTERS);
const sanitize = (objectKey, separator = '-') => {
if (!isValidSeparator(separator)) {
throw new Error(`${separator} is not a valid separator`);
}
if (!objectKey || !isString(objectKey) && !isNumber(objectKey)) {
throw new Error(`Expected non-empty string or number, got ${objectKey}`);
}
if (isNumber(objectKey)) {
return objectKey.toString();
}
return removeIllegalCharacters(replaceLatinCharacters(objectKey.trim())).replace(/ /g, separator);
};
module.exports = sanitize;