IsWordChar.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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 IsInteger = require('./IsInteger');
  7. var WordCharacters = require('./WordCharacters');
  8. var every = require('../helpers/every');
  9. var isChar = function isChar(c) {
  10. return typeof c === 'string';
  11. };
  12. // https://262.ecma-international.org/8.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, IgnoreCase, Unicode) {
  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 (typeof IgnoreCase !== 'boolean' || typeof Unicode !== 'boolean') {
  25. throw new $TypeError('Assertion failed: `IgnoreCase` and `Unicode` must be booleans');
  26. }
  27. if (e === -1 || e === InputLength) {
  28. return false; // step 1
  29. }
  30. var c = Input[e]; // step 2
  31. var wordChars = WordCharacters(IgnoreCase, Unicode);
  32. return $indexOf(wordChars, c) > -1; // steps 3-4
  33. };