index.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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. MAX_SAFE_INTEGER = 9007199254740991,
  12. MAX_INTEGER = 1.7976931348623157e+308,
  13. NAN = 0 / 0;
  14. /** `Object#toString` result references. */
  15. var symbolTag = '[object Symbol]';
  16. /** Used to match leading and trailing whitespace. */
  17. var reTrim = /^\s+|\s+$/g;
  18. /** Used to detect bad signed hexadecimal string values. */
  19. var reIsBadHex = /^[-+]0x[0-9a-f]+$/i;
  20. /** Used to detect binary string values. */
  21. var reIsBinary = /^0b[01]+$/i;
  22. /** Used to detect octal string values. */
  23. var reIsOctal = /^0o[0-7]+$/i;
  24. /** Used to compose unicode character classes. */
  25. var rsAstralRange = '\\ud800-\\udfff',
  26. rsComboMarksRange = '\\u0300-\\u036f\\ufe20-\\ufe23',
  27. rsComboSymbolsRange = '\\u20d0-\\u20f0',
  28. rsVarRange = '\\ufe0e\\ufe0f';
  29. /** Used to compose unicode capture groups. */
  30. var rsAstral = '[' + rsAstralRange + ']',
  31. rsCombo = '[' + rsComboMarksRange + rsComboSymbolsRange + ']',
  32. rsFitz = '\\ud83c[\\udffb-\\udfff]',
  33. rsModifier = '(?:' + rsCombo + '|' + rsFitz + ')',
  34. rsNonAstral = '[^' + rsAstralRange + ']',
  35. rsRegional = '(?:\\ud83c[\\udde6-\\uddff]){2}',
  36. rsSurrPair = '[\\ud800-\\udbff][\\udc00-\\udfff]',
  37. rsZWJ = '\\u200d';
  38. /** Used to compose unicode regexes. */
  39. var reOptMod = rsModifier + '?',
  40. rsOptVar = '[' + rsVarRange + ']?',
  41. rsOptJoin = '(?:' + rsZWJ + '(?:' + [rsNonAstral, rsRegional, rsSurrPair].join('|') + ')' + rsOptVar + reOptMod + ')*',
  42. rsSeq = rsOptVar + reOptMod + rsOptJoin,
  43. rsSymbol = '(?:' + [rsNonAstral + rsCombo + '?', rsCombo, rsRegional, rsSurrPair, rsAstral].join('|') + ')';
  44. /** Used to match [string symbols](https://mathiasbynens.be/notes/javascript-unicode). */
  45. var reUnicode = RegExp(rsFitz + '(?=' + rsFitz + ')|' + rsSymbol + rsSeq, 'g');
  46. /** Used to detect strings with [zero-width joiners or code points from the astral planes](http://eev.ee/blog/2015/09/12/dark-corners-of-unicode/). */
  47. var reHasUnicode = RegExp('[' + rsZWJ + rsAstralRange + rsComboMarksRange + rsComboSymbolsRange + rsVarRange + ']');
  48. /** Built-in method references without a dependency on `root`. */
  49. var freeParseInt = parseInt;
  50. /** Detect free variable `global` from Node.js. */
  51. var freeGlobal = typeof global == 'object' && global && global.Object === Object && global;
  52. /** Detect free variable `self`. */
  53. var freeSelf = typeof self == 'object' && self && self.Object === Object && self;
  54. /** Used as a reference to the global object. */
  55. var root = freeGlobal || freeSelf || Function('return this')();
  56. /**
  57. * Gets the size of an ASCII `string`.
  58. *
  59. * @private
  60. * @param {string} string The string inspect.
  61. * @returns {number} Returns the string size.
  62. */
  63. var asciiSize = baseProperty('length');
  64. /**
  65. * Converts an ASCII `string` to an array.
  66. *
  67. * @private
  68. * @param {string} string The string to convert.
  69. * @returns {Array} Returns the converted array.
  70. */
  71. function asciiToArray(string) {
  72. return string.split('');
  73. }
  74. /**
  75. * The base implementation of `_.property` without support for deep paths.
  76. *
  77. * @private
  78. * @param {string} key The key of the property to get.
  79. * @returns {Function} Returns the new accessor function.
  80. */
  81. function baseProperty(key) {
  82. return function(object) {
  83. return object == null ? undefined : object[key];
  84. };
  85. }
  86. /**
  87. * Checks if `string` contains Unicode symbols.
  88. *
  89. * @private
  90. * @param {string} string The string to inspect.
  91. * @returns {boolean} Returns `true` if a symbol is found, else `false`.
  92. */
  93. function hasUnicode(string) {
  94. return reHasUnicode.test(string);
  95. }
  96. /**
  97. * Gets the number of symbols in `string`.
  98. *
  99. * @private
  100. * @param {string} string The string to inspect.
  101. * @returns {number} Returns the string size.
  102. */
  103. function stringSize(string) {
  104. return hasUnicode(string)
  105. ? unicodeSize(string)
  106. : asciiSize(string);
  107. }
  108. /**
  109. * Converts `string` to an array.
  110. *
  111. * @private
  112. * @param {string} string The string to convert.
  113. * @returns {Array} Returns the converted array.
  114. */
  115. function stringToArray(string) {
  116. return hasUnicode(string)
  117. ? unicodeToArray(string)
  118. : asciiToArray(string);
  119. }
  120. /**
  121. * Gets the size of a Unicode `string`.
  122. *
  123. * @private
  124. * @param {string} string The string inspect.
  125. * @returns {number} Returns the string size.
  126. */
  127. function unicodeSize(string) {
  128. var result = reUnicode.lastIndex = 0;
  129. while (reUnicode.test(string)) {
  130. result++;
  131. }
  132. return result;
  133. }
  134. /**
  135. * Converts a Unicode `string` to an array.
  136. *
  137. * @private
  138. * @param {string} string The string to convert.
  139. * @returns {Array} Returns the converted array.
  140. */
  141. function unicodeToArray(string) {
  142. return string.match(reUnicode) || [];
  143. }
  144. /** Used for built-in method references. */
  145. var objectProto = Object.prototype;
  146. /**
  147. * Used to resolve the
  148. * [`toStringTag`](http://ecma-international.org/ecma-262/7.0/#sec-object.prototype.tostring)
  149. * of values.
  150. */
  151. var objectToString = objectProto.toString;
  152. /** Built-in value references. */
  153. var Symbol = root.Symbol;
  154. /* Built-in method references for those with the same name as other `lodash` methods. */
  155. var nativeCeil = Math.ceil,
  156. nativeFloor = Math.floor;
  157. /** Used to convert symbols to primitives and strings. */
  158. var symbolProto = Symbol ? Symbol.prototype : undefined,
  159. symbolToString = symbolProto ? symbolProto.toString : undefined;
  160. /**
  161. * The base implementation of `_.repeat` which doesn't coerce arguments.
  162. *
  163. * @private
  164. * @param {string} string The string to repeat.
  165. * @param {number} n The number of times to repeat the string.
  166. * @returns {string} Returns the repeated string.
  167. */
  168. function baseRepeat(string, n) {
  169. var result = '';
  170. if (!string || n < 1 || n > MAX_SAFE_INTEGER) {
  171. return result;
  172. }
  173. // Leverage the exponentiation by squaring algorithm for a faster repeat.
  174. // See https://en.wikipedia.org/wiki/Exponentiation_by_squaring for more details.
  175. do {
  176. if (n % 2) {
  177. result += string;
  178. }
  179. n = nativeFloor(n / 2);
  180. if (n) {
  181. string += string;
  182. }
  183. } while (n);
  184. return result;
  185. }
  186. /**
  187. * The base implementation of `_.slice` without an iteratee call guard.
  188. *
  189. * @private
  190. * @param {Array} array The array to slice.
  191. * @param {number} [start=0] The start position.
  192. * @param {number} [end=array.length] The end position.
  193. * @returns {Array} Returns the slice of `array`.
  194. */
  195. function baseSlice(array, start, end) {
  196. var index = -1,
  197. length = array.length;
  198. if (start < 0) {
  199. start = -start > length ? 0 : (length + start);
  200. }
  201. end = end > length ? length : end;
  202. if (end < 0) {
  203. end += length;
  204. }
  205. length = start > end ? 0 : ((end - start) >>> 0);
  206. start >>>= 0;
  207. var result = Array(length);
  208. while (++index < length) {
  209. result[index] = array[index + start];
  210. }
  211. return result;
  212. }
  213. /**
  214. * The base implementation of `_.toString` which doesn't convert nullish
  215. * values to empty strings.
  216. *
  217. * @private
  218. * @param {*} value The value to process.
  219. * @returns {string} Returns the string.
  220. */
  221. function baseToString(value) {
  222. // Exit early for strings to avoid a performance hit in some environments.
  223. if (typeof value == 'string') {
  224. return value;
  225. }
  226. if (isSymbol(value)) {
  227. return symbolToString ? symbolToString.call(value) : '';
  228. }
  229. var result = (value + '');
  230. return (result == '0' && (1 / value) == -INFINITY) ? '-0' : result;
  231. }
  232. /**
  233. * Casts `array` to a slice if it's needed.
  234. *
  235. * @private
  236. * @param {Array} array The array to inspect.
  237. * @param {number} start The start position.
  238. * @param {number} [end=array.length] The end position.
  239. * @returns {Array} Returns the cast slice.
  240. */
  241. function castSlice(array, start, end) {
  242. var length = array.length;
  243. end = end === undefined ? length : end;
  244. return (!start && end >= length) ? array : baseSlice(array, start, end);
  245. }
  246. /**
  247. * Creates the padding for `string` based on `length`. The `chars` string
  248. * is truncated if the number of characters exceeds `length`.
  249. *
  250. * @private
  251. * @param {number} length The padding length.
  252. * @param {string} [chars=' '] The string used as padding.
  253. * @returns {string} Returns the padding for `string`.
  254. */
  255. function createPadding(length, chars) {
  256. chars = chars === undefined ? ' ' : baseToString(chars);
  257. var charsLength = chars.length;
  258. if (charsLength < 2) {
  259. return charsLength ? baseRepeat(chars, length) : chars;
  260. }
  261. var result = baseRepeat(chars, nativeCeil(length / stringSize(chars)));
  262. return hasUnicode(chars)
  263. ? castSlice(stringToArray(result), 0, length).join('')
  264. : result.slice(0, length);
  265. }
  266. /**
  267. * Checks if `value` is the
  268. * [language type](http://www.ecma-international.org/ecma-262/7.0/#sec-ecmascript-language-types)
  269. * of `Object`. (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`)
  270. *
  271. * @static
  272. * @memberOf _
  273. * @since 0.1.0
  274. * @category Lang
  275. * @param {*} value The value to check.
  276. * @returns {boolean} Returns `true` if `value` is an object, else `false`.
  277. * @example
  278. *
  279. * _.isObject({});
  280. * // => true
  281. *
  282. * _.isObject([1, 2, 3]);
  283. * // => true
  284. *
  285. * _.isObject(_.noop);
  286. * // => true
  287. *
  288. * _.isObject(null);
  289. * // => false
  290. */
  291. function isObject(value) {
  292. var type = typeof value;
  293. return !!value && (type == 'object' || type == 'function');
  294. }
  295. /**
  296. * Checks if `value` is object-like. A value is object-like if it's not `null`
  297. * and has a `typeof` result of "object".
  298. *
  299. * @static
  300. * @memberOf _
  301. * @since 4.0.0
  302. * @category Lang
  303. * @param {*} value The value to check.
  304. * @returns {boolean} Returns `true` if `value` is object-like, else `false`.
  305. * @example
  306. *
  307. * _.isObjectLike({});
  308. * // => true
  309. *
  310. * _.isObjectLike([1, 2, 3]);
  311. * // => true
  312. *
  313. * _.isObjectLike(_.noop);
  314. * // => false
  315. *
  316. * _.isObjectLike(null);
  317. * // => false
  318. */
  319. function isObjectLike(value) {
  320. return !!value && typeof value == 'object';
  321. }
  322. /**
  323. * Checks if `value` is classified as a `Symbol` primitive or object.
  324. *
  325. * @static
  326. * @memberOf _
  327. * @since 4.0.0
  328. * @category Lang
  329. * @param {*} value The value to check.
  330. * @returns {boolean} Returns `true` if `value` is a symbol, else `false`.
  331. * @example
  332. *
  333. * _.isSymbol(Symbol.iterator);
  334. * // => true
  335. *
  336. * _.isSymbol('abc');
  337. * // => false
  338. */
  339. function isSymbol(value) {
  340. return typeof value == 'symbol' ||
  341. (isObjectLike(value) && objectToString.call(value) == symbolTag);
  342. }
  343. /**
  344. * Converts `value` to a finite number.
  345. *
  346. * @static
  347. * @memberOf _
  348. * @since 4.12.0
  349. * @category Lang
  350. * @param {*} value The value to convert.
  351. * @returns {number} Returns the converted number.
  352. * @example
  353. *
  354. * _.toFinite(3.2);
  355. * // => 3.2
  356. *
  357. * _.toFinite(Number.MIN_VALUE);
  358. * // => 5e-324
  359. *
  360. * _.toFinite(Infinity);
  361. * // => 1.7976931348623157e+308
  362. *
  363. * _.toFinite('3.2');
  364. * // => 3.2
  365. */
  366. function toFinite(value) {
  367. if (!value) {
  368. return value === 0 ? value : 0;
  369. }
  370. value = toNumber(value);
  371. if (value === INFINITY || value === -INFINITY) {
  372. var sign = (value < 0 ? -1 : 1);
  373. return sign * MAX_INTEGER;
  374. }
  375. return value === value ? value : 0;
  376. }
  377. /**
  378. * Converts `value` to an integer.
  379. *
  380. * **Note:** This method is loosely based on
  381. * [`ToInteger`](http://www.ecma-international.org/ecma-262/7.0/#sec-tointeger).
  382. *
  383. * @static
  384. * @memberOf _
  385. * @since 4.0.0
  386. * @category Lang
  387. * @param {*} value The value to convert.
  388. * @returns {number} Returns the converted integer.
  389. * @example
  390. *
  391. * _.toInteger(3.2);
  392. * // => 3
  393. *
  394. * _.toInteger(Number.MIN_VALUE);
  395. * // => 0
  396. *
  397. * _.toInteger(Infinity);
  398. * // => 1.7976931348623157e+308
  399. *
  400. * _.toInteger('3.2');
  401. * // => 3
  402. */
  403. function toInteger(value) {
  404. var result = toFinite(value),
  405. remainder = result % 1;
  406. return result === result ? (remainder ? result - remainder : result) : 0;
  407. }
  408. /**
  409. * Converts `value` to a number.
  410. *
  411. * @static
  412. * @memberOf _
  413. * @since 4.0.0
  414. * @category Lang
  415. * @param {*} value The value to process.
  416. * @returns {number} Returns the number.
  417. * @example
  418. *
  419. * _.toNumber(3.2);
  420. * // => 3.2
  421. *
  422. * _.toNumber(Number.MIN_VALUE);
  423. * // => 5e-324
  424. *
  425. * _.toNumber(Infinity);
  426. * // => Infinity
  427. *
  428. * _.toNumber('3.2');
  429. * // => 3.2
  430. */
  431. function toNumber(value) {
  432. if (typeof value == 'number') {
  433. return value;
  434. }
  435. if (isSymbol(value)) {
  436. return NAN;
  437. }
  438. if (isObject(value)) {
  439. var other = typeof value.valueOf == 'function' ? value.valueOf() : value;
  440. value = isObject(other) ? (other + '') : other;
  441. }
  442. if (typeof value != 'string') {
  443. return value === 0 ? value : +value;
  444. }
  445. value = value.replace(reTrim, '');
  446. var isBinary = reIsBinary.test(value);
  447. return (isBinary || reIsOctal.test(value))
  448. ? freeParseInt(value.slice(2), isBinary ? 2 : 8)
  449. : (reIsBadHex.test(value) ? NAN : +value);
  450. }
  451. /**
  452. * Converts `value` to a string. An empty string is returned for `null`
  453. * and `undefined` values. The sign of `-0` is preserved.
  454. *
  455. * @static
  456. * @memberOf _
  457. * @since 4.0.0
  458. * @category Lang
  459. * @param {*} value The value to process.
  460. * @returns {string} Returns the string.
  461. * @example
  462. *
  463. * _.toString(null);
  464. * // => ''
  465. *
  466. * _.toString(-0);
  467. * // => '-0'
  468. *
  469. * _.toString([1, 2, 3]);
  470. * // => '1,2,3'
  471. */
  472. function toString(value) {
  473. return value == null ? '' : baseToString(value);
  474. }
  475. /**
  476. * Pads `string` on the right side if it's shorter than `length`. Padding
  477. * characters are truncated if they exceed `length`.
  478. *
  479. * @static
  480. * @memberOf _
  481. * @since 4.0.0
  482. * @category String
  483. * @param {string} [string=''] The string to pad.
  484. * @param {number} [length=0] The padding length.
  485. * @param {string} [chars=' '] The string used as padding.
  486. * @returns {string} Returns the padded string.
  487. * @example
  488. *
  489. * _.padEnd('abc', 6);
  490. * // => 'abc '
  491. *
  492. * _.padEnd('abc', 6, '_-');
  493. * // => 'abc_-_'
  494. *
  495. * _.padEnd('abc', 3);
  496. * // => 'abc'
  497. */
  498. function padEnd(string, length, chars) {
  499. string = toString(string);
  500. length = toInteger(length);
  501. var strLength = length ? stringSize(string) : 0;
  502. return (length && strLength < length)
  503. ? (string + createPadding(length - strLength, chars))
  504. : string;
  505. }
  506. module.exports = padEnd;