32 lines
1.2 KiB
JavaScript
32 lines
1.2 KiB
JavaScript
|
'use strict';
|
||
|
var uncurryThis = require('../internals/function-uncurry-this');
|
||
|
var requireObjectCoercible = require('../internals/require-object-coercible');
|
||
|
var toString = require('../internals/to-string');
|
||
|
var whitespaces = require('../internals/whitespaces');
|
||
|
|
||
|
var replace = uncurryThis(''.replace);
|
||
|
var ltrim = RegExp('^[' + whitespaces + ']+');
|
||
|
var rtrim = RegExp('(^|[^' + whitespaces + '])[' + whitespaces + ']+$');
|
||
|
|
||
|
// `String.prototype.{ trim, trimStart, trimEnd, trimLeft, trimRight }` methods implementation
|
||
|
var createMethod = function (TYPE) {
|
||
|
return function ($this) {
|
||
|
var string = toString(requireObjectCoercible($this));
|
||
|
if (TYPE & 1) string = replace(string, ltrim, '');
|
||
|
if (TYPE & 2) string = replace(string, rtrim, '$1');
|
||
|
return string;
|
||
|
};
|
||
|
};
|
||
|
|
||
|
module.exports = {
|
||
|
// `String.prototype.{ trimLeft, trimStart }` methods
|
||
|
// https://tc39.es/ecma262/#sec-string.prototype.trimstart
|
||
|
start: createMethod(1),
|
||
|
// `String.prototype.{ trimRight, trimEnd }` methods
|
||
|
// https://tc39.es/ecma262/#sec-string.prototype.trimend
|
||
|
end: createMethod(2),
|
||
|
// `String.prototype.trim` method
|
||
|
// https://tc39.es/ecma262/#sec-string.prototype.trim
|
||
|
trim: createMethod(3)
|
||
|
};
|