转换字符串为中滑线连接格式

it2025-05-01  8

/** * 转换字符串为中滑线连接格式 * Converts `string` to [kebab case](https://en.wikipedia.org/wiki/Letter_case#Special_case_styles) * @param {string} [string=''] The string to convert * @returns {string} Returns the kebab cased string * @example * kebabCase('Foo Bar') * // => 'foo-bar' * kebabCase('fooBar') * // => 'foo-bar' * kebabCase('__FOO_BAR__') * // => 'foo-bar' */ import capitalize from "./capitalize" // 我的博客搜索【转换字符串string的首字母为大写】 import words from "./words" //我的博客搜索【拆分字符串string中的词变为数组】 import deburr from "./deburr" //我的博客搜索【转换字符串string中拉丁语-1补充字母和拉丁语扩张字母-A为基本的拉丁字母,并且去除组合变音标记】 /** * A specialized version of `reduce` for arrays * @param {Array} [array] The array to iteratee over. * @param {Function} iteratee The function invoked per iteration * @param {*} [accumulator] The initial value * @param {boolean} [initAccum] Specify using the first element of `array` as the initial value * @returns {*} Returns the accumulated value */ function arrayReduce(array, iteratee, accumulator, initAccum) { var index = -1, length = array == null ? 0 : array.length if (initAccum && length) { accumulator = array[++index] } while (++index < length) { accumulator = iteratee(accumulator, array[index], index, array) } return accumulator } // Used to match apostrophes. var rsApos = "['\u2019]" var reApos = new RegExp(rsApos, "g") function createCompounder(callback) { return function(string) { return arrayReduce(words(deburr(string).replace(reApos, "")), callback, "") } } var kebabCase = createCompounder(function(result, word, index) { return result + (index ? "-" : "") + word.toLowerCase() }) export default kebabCase

 

最新回复(0)