char-surrogate-pair-to-single-unicode-transform.js 611 B

123456789101112131415161718192021222324252627
  1. /**
  2. * The MIT License (MIT)
  3. * Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
  4. */
  5. 'use strict';
  6. /**
  7. * A regexp-tree plugin to transform surrogate pairs into single unicode code point
  8. *
  9. * \ud83d\ude80 -> \u{1f680}
  10. */
  11. module.exports = {
  12. shouldRun: function shouldRun(ast) {
  13. return ast.flags.includes('u');
  14. },
  15. Char: function Char(path) {
  16. var node = path.node;
  17. if (node.kind !== 'unicode' || !node.isSurrogatePair || isNaN(node.codePoint)) {
  18. return;
  19. }
  20. node.value = '\\u{' + node.codePoint.toString(16) + '}';
  21. delete node.isSurrogatePair;
  22. }
  23. };