index.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /**
  2. * lodash (Custom Build) <https://lodash.com/>
  3. * Build: `lodash modularize exports="npm" -o ./`
  4. * Copyright jQuery Foundation and other contributors <https://jquery.org/>
  5. * Released under MIT license <https://lodash.com/license>
  6. * Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE>
  7. * Copyright Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
  8. */
  9. /** Used as references for various `Number` constants. */
  10. var INFINITY = 1 / 0;
  11. /** `Object#toString` result references. */
  12. var symbolTag = '[object Symbol]';
  13. /** Used to match words composed of alphanumeric characters. */
  14. var reAsciiWord = /[^\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f]+/g;
  15. /** Used to match Latin Unicode letters (excluding mathematical operators). */
  16. var reLatin = /[\xc0-\xd6\xd8-\xf6\xf8-\xff\u0100-\u017f]/g;
  17. /** Used to compose unicode character classes. */
  18. var rsAstralRange = '\\ud800-\\udfff',
  19. rsComboMarksRange = '\\u0300-\\u036f\\ufe20-\\ufe23',
  20. rsComboSymbolsRange = '\\u20d0-\\u20f0',
  21. rsDingbatRange = '\\u2700-\\u27bf',
  22. rsLowerRange = 'a-z\\xdf-\\xf6\\xf8-\\xff',
  23. rsMathOpRange = '\\xac\\xb1\\xd7\\xf7',
  24. rsNonCharRange = '\\x00-\\x2f\\x3a-\\x40\\x5b-\\x60\\x7b-\\xbf',
  25. rsPunctuationRange = '\\u2000-\\u206f',
  26. rsSpaceRange = ' \\t\\x0b\\f\\xa0\\ufeff\\n\\r\\u2028\\u2029\\u1680\\u180e\\u2000\\u2001\\u2002\\u2003\\u2004\\u2005\\u2006\\u2007\\u2008\\u2009\\u200a\\u202f\\u205f\\u3000',
  27. rsUpperRange = 'A-Z\\xc0-\\xd6\\xd8-\\xde',
  28. rsVarRange = '\\ufe0e\\ufe0f',
  29. rsBreakRange = rsMathOpRange + rsNonCharRange + rsPunctuationRange + rsSpaceRange;
  30. /** Used to compose unicode capture groups. */
  31. var rsApos = "['\u2019]",
  32. rsBreak = '[' + rsBreakRange + ']',
  33. rsCombo = '[' + rsComboMarksRange + rsComboSymbolsRange + ']',
  34. rsDigits = '\\d+',
  35. rsDingbat = '[' + rsDingbatRange + ']',
  36. rsLower = '[' + rsLowerRange + ']',
  37. rsMisc = '[^' + rsAstralRange + rsBreakRange + rsDigits + rsDingbatRange + rsLowerRange + rsUpperRange + ']',
  38. rsFitz = '\\ud83c[\\udffb-\\udfff]',
  39. rsModifier = '(?:' + rsCombo + '|' + rsFitz + ')',
  40. rsNonAstral = '[^' + rsAstralRange + ']',
  41. rsRegional = '(?:\\ud83c[\\udde6-\\uddff]){2}',
  42. rsSurrPair = '[\\ud800-\\udbff][\\udc00-\\udfff]',
  43. rsUpper = '[' + rsUpperRange + ']',
  44. rsZWJ = '\\u200d';
  45. /** Used to compose unicode regexes. */
  46. var rsLowerMisc = '(?:' + rsLower + '|' + rsMisc + ')',
  47. rsUpperMisc = '(?:' + rsUpper + '|' + rsMisc + ')',
  48. rsOptLowerContr = '(?:' + rsApos + '(?:d|ll|m|re|s|t|ve))?',
  49. rsOptUpperContr = '(?:' + rsApos + '(?:D|LL|M|RE|S|T|VE))?',
  50. reOptMod = rsModifier + '?',
  51. rsOptVar = '[' + rsVarRange + ']?',
  52. rsOptJoin = '(?:' + rsZWJ + '(?:' + [rsNonAstral, rsRegional, rsSurrPair].join('|') + ')' + rsOptVar + reOptMod + ')*',
  53. rsSeq = rsOptVar + reOptMod + rsOptJoin,
  54. rsEmoji = '(?:' + [rsDingbat, rsRegional, rsSurrPair].join('|') + ')' + rsSeq;
  55. /** Used to match apostrophes. */
  56. var reApos = RegExp(rsApos, 'g');
  57. /**
  58. * Used to match [combining diacritical marks](https://en.wikipedia.org/wiki/Combining_Diacritical_Marks) and
  59. * [combining diacritical marks for symbols](https://en.wikipedia.org/wiki/Combining_Diacritical_Marks_for_Symbols).
  60. */
  61. var reComboMark = RegExp(rsCombo, 'g');
  62. /** Used to match complex or compound words. */
  63. var reUnicodeWord = RegExp([
  64. rsUpper + '?' + rsLower + '+' + rsOptLowerContr + '(?=' + [rsBreak, rsUpper, '$'].join('|') + ')',
  65. rsUpperMisc + '+' + rsOptUpperContr + '(?=' + [rsBreak, rsUpper + rsLowerMisc, '$'].join('|') + ')',
  66. rsUpper + '?' + rsLowerMisc + '+' + rsOptLowerContr,
  67. rsUpper + '+' + rsOptUpperContr,
  68. rsDigits,
  69. rsEmoji
  70. ].join('|'), 'g');
  71. /** Used to detect strings that need a more robust regexp to match words. */
  72. var reHasUnicodeWord = /[a-z][A-Z]|[A-Z]{2,}[a-z]|[0-9][a-zA-Z]|[a-zA-Z][0-9]|[^a-zA-Z0-9 ]/;
  73. /** Used to map Latin Unicode letters to basic Latin letters. */
  74. var deburredLetters = {
  75. // Latin-1 Supplement block.
  76. '\xc0': 'A', '\xc1': 'A', '\xc2': 'A', '\xc3': 'A', '\xc4': 'A', '\xc5': 'A',
  77. '\xe0': 'a', '\xe1': 'a', '\xe2': 'a', '\xe3': 'a', '\xe4': 'a', '\xe5': 'a',
  78. '\xc7': 'C', '\xe7': 'c',
  79. '\xd0': 'D', '\xf0': 'd',
  80. '\xc8': 'E', '\xc9': 'E', '\xca': 'E', '\xcb': 'E',
  81. '\xe8': 'e', '\xe9': 'e', '\xea': 'e', '\xeb': 'e',
  82. '\xcc': 'I', '\xcd': 'I', '\xce': 'I', '\xcf': 'I',
  83. '\xec': 'i', '\xed': 'i', '\xee': 'i', '\xef': 'i',
  84. '\xd1': 'N', '\xf1': 'n',
  85. '\xd2': 'O', '\xd3': 'O', '\xd4': 'O', '\xd5': 'O', '\xd6': 'O', '\xd8': 'O',
  86. '\xf2': 'o', '\xf3': 'o', '\xf4': 'o', '\xf5': 'o', '\xf6': 'o', '\xf8': 'o',
  87. '\xd9': 'U', '\xda': 'U', '\xdb': 'U', '\xdc': 'U',
  88. '\xf9': 'u', '\xfa': 'u', '\xfb': 'u', '\xfc': 'u',
  89. '\xdd': 'Y', '\xfd': 'y', '\xff': 'y',
  90. '\xc6': 'Ae', '\xe6': 'ae',
  91. '\xde': 'Th', '\xfe': 'th',
  92. '\xdf': 'ss',
  93. // Latin Extended-A block.
  94. '\u0100': 'A', '\u0102': 'A', '\u0104': 'A',
  95. '\u0101': 'a', '\u0103': 'a', '\u0105': 'a',
  96. '\u0106': 'C', '\u0108': 'C', '\u010a': 'C', '\u010c': 'C',
  97. '\u0107': 'c', '\u0109': 'c', '\u010b': 'c', '\u010d': 'c',
  98. '\u010e': 'D', '\u0110': 'D', '\u010f': 'd', '\u0111': 'd',
  99. '\u0112': 'E', '\u0114': 'E', '\u0116': 'E', '\u0118': 'E', '\u011a': 'E',
  100. '\u0113': 'e', '\u0115': 'e', '\u0117': 'e', '\u0119': 'e', '\u011b': 'e',
  101. '\u011c': 'G', '\u011e': 'G', '\u0120': 'G', '\u0122': 'G',
  102. '\u011d': 'g', '\u011f': 'g', '\u0121': 'g', '\u0123': 'g',
  103. '\u0124': 'H', '\u0126': 'H', '\u0125': 'h', '\u0127': 'h',
  104. '\u0128': 'I', '\u012a': 'I', '\u012c': 'I', '\u012e': 'I', '\u0130': 'I',
  105. '\u0129': 'i', '\u012b': 'i', '\u012d': 'i', '\u012f': 'i', '\u0131': 'i',
  106. '\u0134': 'J', '\u0135': 'j',
  107. '\u0136': 'K', '\u0137': 'k', '\u0138': 'k',
  108. '\u0139': 'L', '\u013b': 'L', '\u013d': 'L', '\u013f': 'L', '\u0141': 'L',
  109. '\u013a': 'l', '\u013c': 'l', '\u013e': 'l', '\u0140': 'l', '\u0142': 'l',
  110. '\u0143': 'N', '\u0145': 'N', '\u0147': 'N', '\u014a': 'N',
  111. '\u0144': 'n', '\u0146': 'n', '\u0148': 'n', '\u014b': 'n',
  112. '\u014c': 'O', '\u014e': 'O', '\u0150': 'O',
  113. '\u014d': 'o', '\u014f': 'o', '\u0151': 'o',
  114. '\u0154': 'R', '\u0156': 'R', '\u0158': 'R',
  115. '\u0155': 'r', '\u0157': 'r', '\u0159': 'r',
  116. '\u015a': 'S', '\u015c': 'S', '\u015e': 'S', '\u0160': 'S',
  117. '\u015b': 's', '\u015d': 's', '\u015f': 's', '\u0161': 's',
  118. '\u0162': 'T', '\u0164': 'T', '\u0166': 'T',
  119. '\u0163': 't', '\u0165': 't', '\u0167': 't',
  120. '\u0168': 'U', '\u016a': 'U', '\u016c': 'U', '\u016e': 'U', '\u0170': 'U', '\u0172': 'U',
  121. '\u0169': 'u', '\u016b': 'u', '\u016d': 'u', '\u016f': 'u', '\u0171': 'u', '\u0173': 'u',
  122. '\u0174': 'W', '\u0175': 'w',
  123. '\u0176': 'Y', '\u0177': 'y', '\u0178': 'Y',
  124. '\u0179': 'Z', '\u017b': 'Z', '\u017d': 'Z',
  125. '\u017a': 'z', '\u017c': 'z', '\u017e': 'z',
  126. '\u0132': 'IJ', '\u0133': 'ij',
  127. '\u0152': 'Oe', '\u0153': 'oe',
  128. '\u0149': "'n", '\u017f': 'ss'
  129. };
  130. /** Detect free variable `global` from Node.js. */
  131. var freeGlobal = typeof global == 'object' && global && global.Object === Object && global;
  132. /** Detect free variable `self`. */
  133. var freeSelf = typeof self == 'object' && self && self.Object === Object && self;
  134. /** Used as a reference to the global object. */
  135. var root = freeGlobal || freeSelf || Function('return this')();
  136. /**
  137. * A specialized version of `_.reduce` for arrays without support for
  138. * iteratee shorthands.
  139. *
  140. * @private
  141. * @param {Array} [array] The array to iterate over.
  142. * @param {Function} iteratee The function invoked per iteration.
  143. * @param {*} [accumulator] The initial value.
  144. * @param {boolean} [initAccum] Specify using the first element of `array` as
  145. * the initial value.
  146. * @returns {*} Returns the accumulated value.
  147. */
  148. function arrayReduce(array, iteratee, accumulator, initAccum) {
  149. var index = -1,
  150. length = array ? array.length : 0;
  151. if (initAccum && length) {
  152. accumulator = array[++index];
  153. }
  154. while (++index < length) {
  155. accumulator = iteratee(accumulator, array[index], index, array);
  156. }
  157. return accumulator;
  158. }
  159. /**
  160. * Splits an ASCII `string` into an array of its words.
  161. *
  162. * @private
  163. * @param {string} The string to inspect.
  164. * @returns {Array} Returns the words of `string`.
  165. */
  166. function asciiWords(string) {
  167. return string.match(reAsciiWord) || [];
  168. }
  169. /**
  170. * The base implementation of `_.propertyOf` without support for deep paths.
  171. *
  172. * @private
  173. * @param {Object} object The object to query.
  174. * @returns {Function} Returns the new accessor function.
  175. */
  176. function basePropertyOf(object) {
  177. return function(key) {
  178. return object == null ? undefined : object[key];
  179. };
  180. }
  181. /**
  182. * Used by `_.deburr` to convert Latin-1 Supplement and Latin Extended-A
  183. * letters to basic Latin letters.
  184. *
  185. * @private
  186. * @param {string} letter The matched letter to deburr.
  187. * @returns {string} Returns the deburred letter.
  188. */
  189. var deburrLetter = basePropertyOf(deburredLetters);
  190. /**
  191. * Checks if `string` contains a word composed of Unicode symbols.
  192. *
  193. * @private
  194. * @param {string} string The string to inspect.
  195. * @returns {boolean} Returns `true` if a word is found, else `false`.
  196. */
  197. function hasUnicodeWord(string) {
  198. return reHasUnicodeWord.test(string);
  199. }
  200. /**
  201. * Splits a Unicode `string` into an array of its words.
  202. *
  203. * @private
  204. * @param {string} The string to inspect.
  205. * @returns {Array} Returns the words of `string`.
  206. */
  207. function unicodeWords(string) {
  208. return string.match(reUnicodeWord) || [];
  209. }
  210. /** Used for built-in method references. */
  211. var objectProto = Object.prototype;
  212. /**
  213. * Used to resolve the
  214. * [`toStringTag`](http://ecma-international.org/ecma-262/7.0/#sec-object.prototype.tostring)
  215. * of values.
  216. */
  217. var objectToString = objectProto.toString;
  218. /** Built-in value references. */
  219. var Symbol = root.Symbol;
  220. /** Used to convert symbols to primitives and strings. */
  221. var symbolProto = Symbol ? Symbol.prototype : undefined,
  222. symbolToString = symbolProto ? symbolProto.toString : undefined;
  223. /**
  224. * The base implementation of `_.toString` which doesn't convert nullish
  225. * values to empty strings.
  226. *
  227. * @private
  228. * @param {*} value The value to process.
  229. * @returns {string} Returns the string.
  230. */
  231. function baseToString(value) {
  232. // Exit early for strings to avoid a performance hit in some environments.
  233. if (typeof value == 'string') {
  234. return value;
  235. }
  236. if (isSymbol(value)) {
  237. return symbolToString ? symbolToString.call(value) : '';
  238. }
  239. var result = (value + '');
  240. return (result == '0' && (1 / value) == -INFINITY) ? '-0' : result;
  241. }
  242. /**
  243. * Creates a function like `_.camelCase`.
  244. *
  245. * @private
  246. * @param {Function} callback The function to combine each word.
  247. * @returns {Function} Returns the new compounder function.
  248. */
  249. function createCompounder(callback) {
  250. return function(string) {
  251. return arrayReduce(words(deburr(string).replace(reApos, '')), callback, '');
  252. };
  253. }
  254. /**
  255. * Checks if `value` is object-like. A value is object-like if it's not `null`
  256. * and has a `typeof` result of "object".
  257. *
  258. * @static
  259. * @memberOf _
  260. * @since 4.0.0
  261. * @category Lang
  262. * @param {*} value The value to check.
  263. * @returns {boolean} Returns `true` if `value` is object-like, else `false`.
  264. * @example
  265. *
  266. * _.isObjectLike({});
  267. * // => true
  268. *
  269. * _.isObjectLike([1, 2, 3]);
  270. * // => true
  271. *
  272. * _.isObjectLike(_.noop);
  273. * // => false
  274. *
  275. * _.isObjectLike(null);
  276. * // => false
  277. */
  278. function isObjectLike(value) {
  279. return !!value && typeof value == 'object';
  280. }
  281. /**
  282. * Checks if `value` is classified as a `Symbol` primitive or object.
  283. *
  284. * @static
  285. * @memberOf _
  286. * @since 4.0.0
  287. * @category Lang
  288. * @param {*} value The value to check.
  289. * @returns {boolean} Returns `true` if `value` is a symbol, else `false`.
  290. * @example
  291. *
  292. * _.isSymbol(Symbol.iterator);
  293. * // => true
  294. *
  295. * _.isSymbol('abc');
  296. * // => false
  297. */
  298. function isSymbol(value) {
  299. return typeof value == 'symbol' ||
  300. (isObjectLike(value) && objectToString.call(value) == symbolTag);
  301. }
  302. /**
  303. * Converts `value` to a string. An empty string is returned for `null`
  304. * and `undefined` values. The sign of `-0` is preserved.
  305. *
  306. * @static
  307. * @memberOf _
  308. * @since 4.0.0
  309. * @category Lang
  310. * @param {*} value The value to process.
  311. * @returns {string} Returns the string.
  312. * @example
  313. *
  314. * _.toString(null);
  315. * // => ''
  316. *
  317. * _.toString(-0);
  318. * // => '-0'
  319. *
  320. * _.toString([1, 2, 3]);
  321. * // => '1,2,3'
  322. */
  323. function toString(value) {
  324. return value == null ? '' : baseToString(value);
  325. }
  326. /**
  327. * Deburrs `string` by converting
  328. * [Latin-1 Supplement](https://en.wikipedia.org/wiki/Latin-1_Supplement_(Unicode_block)#Character_table)
  329. * and [Latin Extended-A](https://en.wikipedia.org/wiki/Latin_Extended-A)
  330. * letters to basic Latin letters and removing
  331. * [combining diacritical marks](https://en.wikipedia.org/wiki/Combining_Diacritical_Marks).
  332. *
  333. * @static
  334. * @memberOf _
  335. * @since 3.0.0
  336. * @category String
  337. * @param {string} [string=''] The string to deburr.
  338. * @returns {string} Returns the deburred string.
  339. * @example
  340. *
  341. * _.deburr('déjà vu');
  342. * // => 'deja vu'
  343. */
  344. function deburr(string) {
  345. string = toString(string);
  346. return string && string.replace(reLatin, deburrLetter).replace(reComboMark, '');
  347. }
  348. /**
  349. * Converts `string` to
  350. * [kebab case](https://en.wikipedia.org/wiki/Letter_case#Special_case_styles).
  351. *
  352. * @static
  353. * @memberOf _
  354. * @since 3.0.0
  355. * @category String
  356. * @param {string} [string=''] The string to convert.
  357. * @returns {string} Returns the kebab cased string.
  358. * @example
  359. *
  360. * _.kebabCase('Foo Bar');
  361. * // => 'foo-bar'
  362. *
  363. * _.kebabCase('fooBar');
  364. * // => 'foo-bar'
  365. *
  366. * _.kebabCase('__FOO_BAR__');
  367. * // => 'foo-bar'
  368. */
  369. var kebabCase = createCompounder(function(result, word, index) {
  370. return result + (index ? '-' : '') + word.toLowerCase();
  371. });
  372. /**
  373. * Splits `string` into an array of its words.
  374. *
  375. * @static
  376. * @memberOf _
  377. * @since 3.0.0
  378. * @category String
  379. * @param {string} [string=''] The string to inspect.
  380. * @param {RegExp|string} [pattern] The pattern to match words.
  381. * @param- {Object} [guard] Enables use as an iteratee for methods like `_.map`.
  382. * @returns {Array} Returns the words of `string`.
  383. * @example
  384. *
  385. * _.words('fred, barney, & pebbles');
  386. * // => ['fred', 'barney', 'pebbles']
  387. *
  388. * _.words('fred, barney, & pebbles', /[^, ]+/g);
  389. * // => ['fred', 'barney', '&', 'pebbles']
  390. */
  391. function words(string, pattern, guard) {
  392. string = toString(string);
  393. pattern = guard ? undefined : pattern;
  394. if (pattern === undefined) {
  395. return hasUnicodeWord(string) ? unicodeWords(string) : asciiWords(string);
  396. }
  397. return string.match(pattern) || [];
  398. }
  399. module.exports = kebabCase;