substring.js 740 B

1234567891011121314151617181920
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var $TypeError = GetIntrinsic('%TypeError%');
  4. var IsIntegralNumber = require('./IsIntegralNumber');
  5. var Type = require('./Type');
  6. var callBound = require('call-bind/callBound');
  7. var $slice = callBound('String.prototype.slice');
  8. // https://262.ecma-international.org/12.0/#substring
  9. module.exports = function substring(S, inclusiveStart, exclusiveEnd) {
  10. if (Type(S) !== 'String' || !IsIntegralNumber(inclusiveStart) || (arguments.length > 2 && !IsIntegralNumber(exclusiveEnd))) {
  11. throw new $TypeError('`S` must be a String, and `inclusiveStart` and `exclusiveEnd` must be integers');
  12. }
  13. return $slice(S, inclusiveStart, arguments.length > 2 ? exclusiveEnd : S.length);
  14. };