CharacterRange.js 782 B

12345678910111213141516171819202122232425262728293031
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var callBound = require('call-bind/callBound');
  4. var $fromCharCode = GetIntrinsic('%String.fromCharCode%');
  5. var $TypeError = GetIntrinsic('%TypeError%');
  6. var $charCodeAt = callBound('%String.prototype.charCodeAt%');
  7. var $push = callBound('%Array.prototype.push%');
  8. module.exports = function CharacterRange(A, B) {
  9. if (A.length !== 1 || B.length !== 1) {
  10. throw new $TypeError('Assertion failed: CharSets A and B contain exactly one character');
  11. }
  12. var a = A[0];
  13. var b = B[0];
  14. var i = $charCodeAt(a, 0);
  15. var j = $charCodeAt(b, 0);
  16. if (!(i <= j)) {
  17. throw new $TypeError('Assertion failed: i is not <= j');
  18. }
  19. var arr = [];
  20. for (var k = i; k <= j; k += 1) {
  21. $push(arr, $fromCharCode(k));
  22. }
  23. return arr;
  24. };