GithubHelp home page GithubHelp logo

adraffy / punycode.js Goto Github PK

View Code? Open in Web Editor NEW
2.0 4.0 4.0 87 KB

Low-level Punycode encoder/decoder without IDNA

Home Page: https://adraffy.github.io/punycode.js/test/demo.html

License: MIT License

JavaScript 56.74% HTML 43.26%
javascript punycode

punycode.js's Introduction

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';
// npm i @adraffy/punycode
// browser: https://cdn.jsdelivr.net/npm/@adraffy/punycode@latest/dist/index.min.js

// (string|number[]) -> string
// input unicode string or codepoints
// returns string, prepends "xn--" if puny
// throws on error
puny_encoded('💩'); // "xn--ls8h"
puny_encoded([0x1F4A9]); // "xn--ls8h"

// pure ascii does not need encoded:
puny_encoded('abc');  // "abc"
puny_encoded([0x61,0x62,0x63]); // "abc"

// (string|TypedArray|number[]) -> number[]
// input ascii string or bytes
// decodes if puny ("xn--" prefix)
// returns array of unicode codepoints
// throws on error
puny_decoded([0x61,0x62,0x63]); // <61 62 63>
puny_decoded('xn--ls8h'); // <1F4A9>

Lower-level functions:

import {puny_encoded_bytes, puny_decode, is_surrogate} from '@adraffy/punycode';

// (string|number[]) -> number[]
// input unicode string or codepoints
// returns array of bytes, prepends "xn--" if puny
// throws on error
puny_encoded_bytes("abc"); // <61 62 63>
puny_encoded_bytes([0x1F4A9]); // <78 6E 2D 2D 6C 73 38 68>
// note: always returns a copy

// (string|TypedArray|number[]) -> number[]
// input ascii string or bytes
// always decodes as puny
// throws on error
puny_decode([0x6C,0x73,0x38,0x68]); // <1F4A9>
puny_decode('ls8h'); // <1F4A9>
// note: always returns a copy

// number -> bool
// return true if codepoint is surrogate
is_surrogate(0x61); // "a" => false
is_surrogate(0xDFFF); // true

Use caution when converting decoded codepoints to a string:

let str0 = '💩'; 
// two different encodings
let enc0 = puny_encoded(str0); // "xn--ls8h"
let enc1 = puny_encoded([str0.charCodeAt(0), str0.charCodeAt(1)]); // "xn--8c9by4f"
// two different decodings
let dec0 = puny_decoded(enc0); // <1F4A9>
let dec1 = puny_decoded(enc1); // <D83D DCA9>
// however, equal strings
str0 === String.fromCodePoint(...dec0); // true
str0 === String.fromCodePoint(...dec1); // true

// check "roundtrip" (recommended)
// decoded(encoded(decoded(x))) == x
if (puny !== puny_decoded(puny_encoded(puny_decoded(puny)))) {
	throw new Error('roundtrip mismatch');
}
// or, check for surrogates:
if (decoded.some(is_surrogate)) {
	throw new Error('contains surrogates');
}

Build

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

punycode.js's People

Contributors

adraffy avatar

Stargazers

Pana avatar

Watchers

 avatar  avatar dartman avatar alex avatar

punycode.js's Issues

is_RFC1123 bug

This is not about the library itself, but the online demo.

function is_RFC1123(name) {
	let max = 253;
	if (name.length > max+1) return false; 
	if (!/^[a-zA-z0-9-.]+$/) return false;
	if (name.endsWith('.')) name = name.slice(0, -1);
	if (name.length > max) return false;
	return name.split('.').every(s => !s.startsWith('-') && !s.endsWith('-') && s.length < 64);
}

The regex always passes 😉

I believe it should be like this:

if (!/^[a-zA-Z0-9.-]+$/g.test(name)) return false;
  1. .test
  2. A-z -> A-Z

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.