IsWordChar.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. 'use strict';
  2. var $TypeError = require('es-errors/type');
  3. var callBound = require('call-bound');
  4. var isInteger = require('math-intrinsics/isInteger');
  5. var $indexOf = callBound('String.prototype.indexOf');
  6. var IsArray = require('./IsArray');
  7. var WordCharacters = require('./WordCharacters');
  8. var every = require('../helpers/every');
  9. var isRegExpRecord = require('../helpers/records/regexp-record');
  10. var isChar = function isChar(c) {
  11. return typeof c === 'string';
  12. };
  13. // https://262.ecma-international.org/14.0/#sec-runtime-semantics-iswordchar-abstract-operation
  14. module.exports = function IsWordChar(rer, Input, e) {
  15. if (!isRegExpRecord(rer)) {
  16. throw new $TypeError('Assertion failed: `rer` must be a RegExp Record');
  17. }
  18. if (!IsArray(Input) || !every(Input, isChar)) {
  19. throw new $TypeError('Assertion failed: `Input` must be a List of characters');
  20. }
  21. if (!isInteger(e)) {
  22. throw new $TypeError('Assertion failed: `e` must be an integer');
  23. }
  24. var InputLength = Input.length; // step 1
  25. if (e === -1 || e === InputLength) {
  26. return false; // step 2
  27. }
  28. var c = Input[e]; // step 3
  29. return $indexOf(WordCharacters(rer), c) > -1; // steps 4-5
  30. };