UNPKG

@onesy/utils

Version:
57 lines (56 loc) 2.13 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.optionsDefault = void 0; const is_1 = __importDefault(require("./is")); const capitalize_1 = __importDefault(require("./capitalize")); exports.optionsDefault = { filters: [',', '.', '-', '_', '\s+'], replaceWith: ' ', trim: true, }; const cleanValue = (value_, options_ = {}) => { try { const options = Object.assign(Object.assign({}, exports.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 ((0, is_1.default)('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 = `\\${filter}`; const regexp = new RegExp(expression, 'g'); value = value.replace(regexp, options.replaceWith || ' '); }); if (options.trim) value = value.trim(); if (options.capitalize) value = (0, capitalize_1.default)(value); if (options.lowercase) value = value.toLocaleLowerCase(); return value; } return value_; } catch (error) { } return value_; }; exports.default = cleanValue;