UNPKG

camelo

Version:

Convert a string into camel case style by providing the separators.

40 lines (31 loc) 1.02 kB
import ucFirstArray from "uc-first-array" import regexEscape from "regex-escape" // Constants const DEFAULT_SPLIT = /[^a-zA-Z0-9]/g; /** * camelo * Converts an input string into camel-case style. * * @name camelo * @function * @param {String} input The input string. * @param {Regex|String|Array} regex A regular expression, a string character or an array of strings used to split the input string. * @param {Boolean} uc If `true`, it will uppercase the first word as well. * @return {String} The camelized input value. */ function camelo(input, regex, uc) { regex = regex || DEFAULT_SPLIT; let splits = null; if (Array.isArray(regex)) { regex = new RegExp(regex.map(regexEscape).join("|"), "g"); } else if (typeof regex === "boolean") { uc = regex; regex = DEFAULT_SPLIT; } splits = input.split(regex); if (uc) { return ucFirstArray(splits).join(""); } return splits[0] + ucFirstArray(splits.slice(1)).join(""); } export default camelo;