TrimString.js 756 B

123456789101112131415161718192021222324252627
  1. 'use strict';
  2. var trimStart = require('string.prototype.trimstart');
  3. var trimEnd = require('string.prototype.trimend');
  4. var $TypeError = require('es-errors/type');
  5. var RequireObjectCoercible = require('./RequireObjectCoercible');
  6. var ToString = require('./ToString');
  7. // https://262.ecma-international.org/10.0/#sec-trimstring
  8. module.exports = function TrimString(string, where) {
  9. var str = RequireObjectCoercible(string);
  10. var S = ToString(str);
  11. var T;
  12. if (where === 'start') {
  13. T = trimStart(S);
  14. } else if (where === 'end') {
  15. T = trimEnd(S);
  16. } else if (where === 'start+end') {
  17. T = trimStart(trimEnd(S));
  18. } else {
  19. throw new $TypeError('Assertion failed: invalid `where` value; must be "start", "end", or "start+end"');
  20. }
  21. return T;
  22. };