CharacterRange.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var callBound = require('call-bound');
  4. var $fromCharCode = GetIntrinsic('%String.fromCharCode%');
  5. var $TypeError = require('es-errors/type');
  6. var $charCodeAt = callBound('String.prototype.charCodeAt');
  7. var CharSet = require('../helpers/CharSet').CharSet;
  8. module.exports = function CharacterRange(A, B) {
  9. var a;
  10. var b;
  11. if (A instanceof CharSet || B instanceof CharSet) {
  12. if (!(A instanceof CharSet) || !(B instanceof CharSet)) {
  13. throw new $TypeError('Assertion failed: CharSets A and B are not both CharSets');
  14. }
  15. A.yield(function (c) {
  16. if (typeof a !== 'undefined') {
  17. throw new $TypeError('Assertion failed: CharSet A has more than one character');
  18. }
  19. a = c;
  20. });
  21. B.yield(function (c) {
  22. if (typeof b !== 'undefined') {
  23. throw new $TypeError('Assertion failed: CharSet B has more than one character');
  24. }
  25. b = c;
  26. });
  27. } else {
  28. if (A.length !== 1 || B.length !== 1) {
  29. throw new $TypeError('Assertion failed: CharSets A and B contain exactly one character');
  30. }
  31. a = A[0];
  32. b = B[0];
  33. }
  34. var i = $charCodeAt(a, 0);
  35. var j = $charCodeAt(b, 0);
  36. if (!(i <= j)) {
  37. throw new $TypeError('Assertion failed: i is not <= j');
  38. }
  39. var arr = [];
  40. for (var k = i; k <= j; k += 1) {
  41. arr[arr.length] = $fromCharCode(k);
  42. }
  43. return arr;
  44. };