punycode.js
0-dependancy low-level Punycode encoder/decoder without IDNA that works in the browser.
2KB Default — full library
Demo
import {puny_encoded, puny_decoded} from '@adraffy/punycode';
puny_encoded('đź’©');
puny_encoded([0x1F4A9]);
puny_encoded('abc');
puny_encoded([0x61,0x62,0x63]);
puny_decoded([0x61,0x62,0x63]);
puny_decoded('xn--ls8h');
Lower-level functions:
import {puny_encoded_bytes, puny_decode, is_surrogate} from '@adraffy/punycode';
puny_encoded_bytes("abc");
puny_encoded_bytes([0x1F4A9]);
puny_decode([0x6C,0x73,0x38,0x68]);
puny_decode('ls8h');
is_surrogate(0x61);
is_surrogate(0xDFFF);
Use caution when converting decoded codepoints to a string:
let str0 = 'đź’©';
let enc0 = puny_encoded(str0);
let enc1 = puny_encoded([str0.charCodeAt(0), str0.charCodeAt(1)]);
let dec0 = puny_decoded(enc0));
let dec1 = puny_decoded(enc1));
str0 === String.fromCodePoint(...dec0);
str0 === String.fromCodePoint(...dec1);
if (puny !== puny_decoded(puny_encoded(puny_decoded(puny)))) {
throw new Error('roundtrip mismatch');
}
if (decoded.some(is_surrogate)) {
throw new Error('contains surrogates');
}
Build
npm run test — run tests
npm run build — create /dist/