IsWordChar.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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 IsInteger = require('./IsInteger');
  8. var Type = require('./Type');
  9. var WordCharacters = require('./WordCharacters');
  10. var every = require('../helpers/every');
  11. var isChar = function isChar(c) {
  12. return typeof c === 'string';
  13. };
  14. // https://262.ecma-international.org/8.0/#sec-runtime-semantics-iswordchar-abstract-operation
  15. // note: prior to ES2023, this AO erroneously omitted the latter of its arguments.
  16. module.exports = function IsWordChar(e, InputLength, Input, IgnoreCase, Unicode) {
  17. if (!IsInteger(e)) {
  18. throw new $TypeError('Assertion failed: `e` must be an integer');
  19. }
  20. if (!IsInteger(InputLength)) {
  21. throw new $TypeError('Assertion failed: `InputLength` must be an integer');
  22. }
  23. if (!IsArray(Input) || !every(Input, isChar)) {
  24. throw new $TypeError('Assertion failed: `Input` must be a List of characters');
  25. }
  26. if (Type(IgnoreCase) !== 'Boolean' || Type(Unicode) !== 'Boolean') {
  27. throw new $TypeError('Assertion failed: `IgnoreCase` and `Unicode` must be booleans');
  28. }
  29. if (e === -1 || e === InputLength) {
  30. return false; // step 1
  31. }
  32. var c = Input[e]; // step 2
  33. var wordChars = WordCharacters(IgnoreCase, Unicode);
  34. return $indexOf(wordChars, c) > -1; // steps 3-4
  35. };