Budibase is an open-source low-code platform for creating internal apps in minutes. Supports PostgreSQL, MySQL, MSSQL, MongoDB, Rest API, Docker, K8s 🚀
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

32 lines
1.1 KiB

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;