UNPKG

5.42 kBJavaScriptView Raw
1/**
2 * lodash 4.0.2 (Custom Build) <https://lodash.com/>
3 * Build: `lodash modularize exports="npm" -o ./`
4 * Copyright 2012-2016 The Dojo Foundation <http://dojofoundation.org/>
5 * Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE>
6 * Copyright 2009-2016 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
7 * Available under MIT license <https://lodash.com/license>
8 */
9var charsEndIndex = require('lodash._charsendindex');
10
11/** Used as references for various `Number` constants. */
12var INFINITY = 1 / 0;
13
14/** `Object#toString` result references. */
15var symbolTag = '[object Symbol]';
16
17/** Used to match leading and trailing whitespace. */
18var reTrimEnd = /\s+$/;
19
20/** Used to compose unicode character classes. */
21var rsAstralRange = '\\ud800-\\udfff',
22 rsComboMarksRange = '\\u0300-\\u036f\\ufe20-\\ufe23',
23 rsComboSymbolsRange = '\\u20d0-\\u20f0',
24 rsVarRange = '\\ufe0e\\ufe0f';
25
26/** Used to compose unicode capture groups. */
27var rsAstral = '[' + rsAstralRange + ']',
28 rsCombo = '[' + rsComboMarksRange + rsComboSymbolsRange + ']',
29 rsFitz = '\\ud83c[\\udffb-\\udfff]',
30 rsModifier = '(?:' + rsCombo + '|' + rsFitz + ')',
31 rsNonAstral = '[^' + rsAstralRange + ']',
32 rsRegional = '(?:\\ud83c[\\udde6-\\uddff]){2}',
33 rsSurrPair = '[\\ud800-\\udbff][\\udc00-\\udfff]',
34 rsZWJ = '\\u200d';
35
36/** Used to compose unicode regexes. */
37var reOptMod = rsModifier + '?',
38 rsOptVar = '[' + rsVarRange + ']?',
39 rsOptJoin = '(?:' + rsZWJ + '(?:' + [rsNonAstral, rsRegional, rsSurrPair].join('|') + ')' + rsOptVar + reOptMod + ')*',
40 rsSeq = rsOptVar + reOptMod + rsOptJoin,
41 rsSymbol = '(?:' + [rsNonAstral + rsCombo + '?', rsCombo, rsRegional, rsSurrPair, rsAstral].join('|') + ')';
42
43/** Used to match [string symbols](https://mathiasbynens.be/notes/javascript-unicode). */
44var reComplexSymbol = RegExp(rsFitz + '(?=' + rsFitz + ')|' + rsSymbol + rsSeq, 'g');
45
46/**
47 * Converts `string` to an array.
48 *
49 * @private
50 * @param {string} string The string to convert.
51 * @returns {Array} Returns the converted array.
52 */
53function stringToArray(string) {
54 return string.match(reComplexSymbol);
55}
56
57/** Used for built-in method references. */
58var objectProto = global.Object.prototype;
59
60/**
61 * Used to resolve the [`toStringTag`](http://ecma-international.org/ecma-262/6.0/#sec-object.prototype.tostring)
62 * of values.
63 */
64var objectToString = objectProto.toString;
65
66/** Built-in value references. */
67var Symbol = global.Symbol;
68
69/** Used to convert symbols to primitives and strings. */
70var symbolProto = Symbol ? Symbol.prototype : undefined,
71 symbolToString = Symbol ? symbolProto.toString : undefined;
72
73/**
74 * Checks if `value` is object-like. A value is object-like if it's not `null`
75 * and has a `typeof` result of "object".
76 *
77 * @static
78 * @memberOf _
79 * @category Lang
80 * @param {*} value The value to check.
81 * @returns {boolean} Returns `true` if `value` is object-like, else `false`.
82 * @example
83 *
84 * _.isObjectLike({});
85 * // => true
86 *
87 * _.isObjectLike([1, 2, 3]);
88 * // => true
89 *
90 * _.isObjectLike(_.noop);
91 * // => false
92 *
93 * _.isObjectLike(null);
94 * // => false
95 */
96function isObjectLike(value) {
97 return !!value && typeof value == 'object';
98}
99
100/**
101 * Checks if `value` is classified as a `Symbol` primitive or object.
102 *
103 * @static
104 * @memberOf _
105 * @category Lang
106 * @param {*} value The value to check.
107 * @returns {boolean} Returns `true` if `value` is correctly classified, else `false`.
108 * @example
109 *
110 * _.isSymbol(Symbol.iterator);
111 * // => true
112 *
113 * _.isSymbol('abc');
114 * // => false
115 */
116function isSymbol(value) {
117 return typeof value == 'symbol' ||
118 (isObjectLike(value) && objectToString.call(value) == symbolTag);
119}
120
121/**
122 * Converts `value` to a string if it's not one. An empty string is returned
123 * for `null` and `undefined` values. The sign of `-0` is preserved.
124 *
125 * @static
126 * @memberOf _
127 * @category Lang
128 * @param {*} value The value to process.
129 * @returns {string} Returns the string.
130 * @example
131 *
132 * _.toString(null);
133 * // => ''
134 *
135 * _.toString(-0);
136 * // => '-0'
137 *
138 * _.toString([1, 2, 3]);
139 * // => '1,2,3'
140 */
141function toString(value) {
142 // Exit early for strings to avoid a performance hit in some environments.
143 if (typeof value == 'string') {
144 return value;
145 }
146 if (value == null) {
147 return '';
148 }
149 if (isSymbol(value)) {
150 return Symbol ? symbolToString.call(value) : '';
151 }
152 var result = (value + '');
153 return (result == '0' && (1 / value) == -INFINITY) ? '-0' : result;
154}
155
156/**
157 * Removes trailing whitespace or specified characters from `string`.
158 *
159 * @static
160 * @memberOf _
161 * @category String
162 * @param {string} [string=''] The string to trim.
163 * @param {string} [chars=whitespace] The characters to trim.
164 * @param- {Object} [guard] Enables use as an iteratee for functions like `_.map`.
165 * @returns {string} Returns the trimmed string.
166 * @example
167 *
168 * _.trimEnd(' abc ');
169 * // => ' abc'
170 *
171 * _.trimEnd('-_-abc-_-', '_-');
172 * // => '-_-abc'
173 */
174function trimEnd(string, chars, guard) {
175 string = toString(string);
176 if (!string) {
177 return string;
178 }
179 if (guard || chars === undefined) {
180 return string.replace(reTrimEnd, '');
181 }
182 chars = (chars + '');
183 if (!chars) {
184 return string;
185 }
186 var strSymbols = stringToArray(string);
187 return strSymbols.slice(0, charsEndIndex(strSymbols, stringToArray(chars)) + 1).join('');
188}
189
190module.exports = trimEnd;