substring.js 644 B

1234567891011121314151617
  1. 'use strict';
  2. var $TypeError = require('es-errors/type');
  3. var isInteger = require('../helpers/isInteger');
  4. var callBound = require('call-bind/callBound');
  5. var $slice = callBound('String.prototype.slice');
  6. // https://262.ecma-international.org/12.0/#substring
  7. module.exports = function substring(S, inclusiveStart, exclusiveEnd) {
  8. if (typeof S !== 'string' || !isInteger(inclusiveStart) || (arguments.length > 2 && !isInteger(exclusiveEnd))) {
  9. throw new $TypeError('`S` must be a String, and `inclusiveStart` and `exclusiveEnd` must be integers');
  10. }
  11. return $slice(S, inclusiveStart, arguments.length > 2 ? exclusiveEnd : S.length);
  12. };