IsWordChar.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var $TypeError = GetIntrinsic('%TypeError%');
  4. var IsArray = require('./IsArray');
  5. var IsInteger = require('./IsInteger');
  6. var every = require('../helpers/every');
  7. var regexTester = require('safe-regex-test');
  8. var isChar = function isChar(c) {
  9. return typeof c === 'string' && c.length === 1;
  10. };
  11. var isWordCharacter = regexTester(/^[a-zA-Z0-9_]$/);
  12. // https://262.ecma-international.org/6.0/#sec-runtime-semantics-iswordchar-abstract-operation
  13. // note: prior to ES2023, this AO erroneously omitted the latter of its arguments.
  14. module.exports = function IsWordChar(e, InputLength, Input) {
  15. if (!IsInteger(e)) {
  16. throw new $TypeError('Assertion failed: `e` must be an integer');
  17. }
  18. if (!IsInteger(InputLength)) {
  19. throw new $TypeError('Assertion failed: `InputLength` must be an integer');
  20. }
  21. if (!IsArray(Input) || !every(Input, isChar)) {
  22. throw new $TypeError('Assertion failed: `Input` must be a List of characters');
  23. }
  24. if (e === -1 || e === InputLength) {
  25. return false; // step 1
  26. }
  27. var c = Input[e]; // step 2
  28. return isWordCharacter(c); // steps 3-4
  29. };