UNPKG

@onesy/utils

Version:
55 lines (46 loc) 1.58 kB
import is from './is'; import capitalize from './capitalize'; export const optionsDefault = { filters: [',', '.', '-', '_', '\s+'], replaceWith: ' ', trim: true }; const cleanValue = function (value_) { let options_ = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; try { const options = { ...optionsDefault, ...options_ }; // Few predefined options // for className cammel case to regular // css property names convert if (options.className) { options.replaceWith = '-'; options.cammelCaseTransform = true; options.lowercase = true; } if (is('string', value_)) { let value = value_; if (options.url) { const parts = value.split('?').filter(Boolean); let path = parts[0]; const query = parts[1]; if (path.slice(-1) === '/') path = path.slice(0, -1); value = query ? [path, query].join('?') : path; return value; } if (options.cammelCaseTransform) value = value.split(/(?=[A-Z])/g).join(options.replaceWith || ' '); options.filters.forEach(filter => { const expression = "\\".concat(filter); const regexp = new RegExp(expression, 'g'); value = value.replace(regexp, options.replaceWith || ' '); }); if (options.trim) value = value.trim(); if (options.capitalize) value = capitalize(value); if (options.lowercase) value = value.toLocaleLowerCase(); return value; } return value_; } catch (error) {} return value_; }; export default cleanValue;