Skip to content

Low-level Punycode encoder/decoder without IDNA

License

Notifications You must be signed in to change notification settings

namebasehq/punycode.js

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

punycode.js

0-dependancy low-level Punycode encoder/decoder without IDNA that works in the browser.

  • 2KB Default — full library

Demo

import {puny_encode, puny_decode} from '@adraffy/punycode';
// npm i @adraffy/punycode
// browser: https://unpkg.com/@adraffy/punycode@latest/dist/index.min.js

// (number[], prefixed: bool) -> number[]
// input any array of codepoints
// returns same array (punycode not needed) or encoded codepoints 
// if prefixed, prepends "xn--"
// throws on error
puny_encode([65, 66, 67]);   // [65, 66, 67] <== same!
puny_encode([128169]);       // [108, 115, 56, 104]
puny_encode([128169], true); // [120, 110, 45, 45, 108, 115, 56, 104]

// number[] -> number[]
// input any known puny_encoded array of codepoints
// eg. xn--<this part>
// returns decoded codepoints 
// throws on error
puny_decode([108, 115, 56, 104]); // [128169]

Prefixed Strings

import {puny_encoded, puny_decoded} from '@adraffy/punycode';

// string|number[] -> string
puny_encoded("abc") == puny_encoded([65, 66, 67]) == "abc";
puny_encoded("💩") == puny_encoded([128169]) == "xn--ls8h";

// string|number[]|ArrayBufferView -> string
puny_decoded("abc") == puny_decoded([65, 66, 67]) == "abc";

// the following return "💩"
puny_decoded("xn--ls8h");
puny_decoded([120, 110, 45, 45, 108, 115, 56, 104]);
puny_decoded(new Uint8Array([120, 110, 45, 45, 108, 115, 56, 104]));

Build

  • npm run test — run tests
  • npm run build — create /dist/

About

Low-level Punycode encoder/decoder without IDNA

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 64.1%
  • HTML 35.9%