TrimString.js 804 B

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