IsWordChar.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var $TypeError = GetIntrinsic('%TypeError%');
  4. var callBound = require('call-bind/callBound');
  5. var $indexOf = callBound('String.prototype.indexOf');
  6. var IsArray = require('./IsArray');
  7. var Type = require('./Type');
  8. var WordCharacters = require('./WordCharacters');
  9. var assertRecord = require('../helpers/assertRecord');
  10. var every = require('../helpers/every');
  11. var isInteger = require('../helpers/isInteger');
  12. var isChar = function isChar(c) {
  13. return typeof c === 'string';
  14. };
  15. // https://262.ecma-international.org/14.0/#sec-runtime-semantics-iswordchar-abstract-operation
  16. // note: prior to ES2023, this AO erroneously omitted the latter of its arguments.
  17. module.exports = function IsWordChar(rer, Input, e) {
  18. assertRecord(Type, 'RegExp Record', 'rer', rer);
  19. if (!IsArray(Input) || !every(Input, isChar)) {
  20. throw new $TypeError('Assertion failed: `Input` must be a List of characters');
  21. }
  22. if (!isInteger(e)) {
  23. throw new $TypeError('Assertion failed: `e` must be an integer');
  24. }
  25. var InputLength = Input.length; // step 1
  26. if (e === -1 || e === InputLength) {
  27. return false; // step 2
  28. }
  29. var c = Input[e]; // step 3
  30. return $indexOf(WordCharacters(rer), c) > -1; // steps 4-5
  31. };