UNPKG

1.08 kBJavaScriptView Raw
1var requireObjectCoercible = require('../internals/require-object-coercible');
2var whitespaces = require('../internals/whitespaces');
3
4var whitespace = '[' + whitespaces + ']';
5var ltrim = RegExp('^' + whitespace + whitespace + '*');
6var rtrim = RegExp(whitespace + whitespace + '*$');
7
8// `String.prototype.{ trim, trimStart, trimEnd, trimLeft, trimRight }` methods implementation
9var createMethod = function (TYPE) {
10 return function ($this) {
11 var string = String(requireObjectCoercible($this));
12 if (TYPE & 1) string = string.replace(ltrim, '');
13 if (TYPE & 2) string = string.replace(rtrim, '');
14 return string;
15 };
16};
17
18module.exports = {
19 // `String.prototype.{ trimLeft, trimStart }` methods
20 // https://tc39.github.io/ecma262/#sec-string.prototype.trimstart
21 start: createMethod(1),
22 // `String.prototype.{ trimRight, trimEnd }` methods
23 // https://tc39.github.io/ecma262/#sec-string.prototype.trimend
24 end: createMethod(2),
25 // `String.prototype.trim` method
26 // https://tc39.github.io/ecma262/#sec-string.prototype.trim
27 trim: createMethod(3)
28};