IsWordChar.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. 'use strict';
  2. var $TypeError = require('es-errors/type');
  3. var callBound = require('call-bind/callBound');
  4. var $indexOf = callBound('String.prototype.indexOf');
  5. var IsArray = require('./IsArray');
  6. var WordCharacters = require('./WordCharacters');
  7. var every = require('../helpers/every');
  8. var isInteger = require('../helpers/isInteger');
  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. // note: prior to ES2023, this AO erroneously omitted the latter of its arguments.
  15. module.exports = function IsWordChar(rer, Input, e) {
  16. if (!isRegExpRecord(rer)) {
  17. throw new $TypeError('Assertion failed: `rer` must be a RegExp Record');
  18. }
  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. };