CharacterComplement.js 885 B

12345678910111213141516171819202122232425262728293031323334
  1. 'use strict';
  2. var $TypeError = require('es-errors/type');
  3. var AllCharacters = require('./AllCharacters');
  4. var CharSet = require('../helpers/CharSet').CharSet;
  5. var isRegExpRecord = require('../helpers/records/regexp-record');
  6. // https://262.ecma-international.org/15.0/#sec-charactercomplement
  7. module.exports = function CharacterComplement(rer, S) {
  8. if (!isRegExpRecord(rer)) {
  9. throw new $TypeError('Assertion failed: `rer` must be a RegExp Record');
  10. }
  11. if (!(S instanceof CharSet)) {
  12. throw new $TypeError('Assertion failed: S must be a CharSet');
  13. }
  14. var A = AllCharacters(rer); // step 1
  15. // 2. Return the CharSet containing the CharSetElements of A which are not also CharSetElements of S.
  16. return new CharSet(
  17. function (x) { return !S.test(x) && A.test(x); },
  18. function (emit) {
  19. A.yield(function (x) {
  20. if (!S.test(x)) {
  21. emit(x);
  22. }
  23. });
  24. }
  25. );
  26. };