IsWordChar.js 1.1 KB

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