estools / escodegen

ECMAScript code generator
BSD 2-Clause "Simplified" License
2.65k stars 335 forks source link

Online demo doesn't return the emoji back #451

Closed jd-solanki closed 2 years ago

jd-solanki commented 2 years ago

Assume I have the below code:

const satisfactionEmojis = ['😭', 'đŸ˜ĸ', '☚ī¸', '🙁', '😐', '🙂', '😊', '😁', '😄', '😍']

When I paste them in your online tool it is generating string like below:

const satisfactionEmojis = [
        '\ud83d\ude2d',
        '\ud83d\ude22',
        '\u2639\ufe0f',
        '\ud83d\ude41',
        '\ud83d\ude10',
        '\ud83d\ude42',
        '\ud83d\ude0a',
        '\ud83d\ude01',
        '\ud83d\ude04',
        '\ud83d\ude0d'
    ];

not the emoji.

How can I get back the emojies?

jd-solanki commented 2 years ago

I am using escodegen for my internal tooling and stuck with emoji being converted in this string. It will be great if you let me know how can I get back the emoji from those strings.

Thanks.

papandreou commented 2 years ago

Take a look at the escapeless option: https://github.com/estools/escodegen/wiki/API#optionformatescapeless

without escapeless

> console.log(require('escodegen').generate(require('esprima').parse(`const satisfactionEmojis = ['😭', 'đŸ˜ĸ', '☚ī¸', '🙁', '😐', '🙂', '😊', '😁', '😄', '😍']`)));
const satisfactionEmojis = [
    '\uD83D\uDE2D',
    '\uD83D\uDE22',
    '\u2639ī¸',
    '\uD83D\uDE41',
    '\uD83D\uDE10',
    '\uD83D\uDE42',
    '\uD83D\uDE0A',
    '\uD83D\uDE01',
    '\uD83D\uDE04',
    '\uD83D\uDE0D'
];

with escapeless

> console.log(require('escodegen').generate(require('esprima').parse(`const satisfactionEmojis = ['😭', 'đŸ˜ĸ', '☚ī¸', '🙁', '😐', '🙂', '😊', '😁', '😄', '😍']`), {format: {escapeless: true}}));
const satisfactionEmojis = [
    '😭',
    'đŸ˜ĸ',
    '☚ī¸',
    '🙁',
    '😐',
    '🙂',
    '😊',
    '😁',
    '😄',
    '😍'
];
jd-solanki commented 2 years ago

That made my day ❤ī¸

Thanks