Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

javascript punycode encoder and decoder

View punycode.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
/**
* Copyright (C) 2011 by Ben Noordhuis <info@bnoordhuis.nl>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
 
TMIN = 1;
TMAX = 26;
BASE = 36;
SKEW = 38;
DAMP = 700; // initial bias scaler
INITIAL_N = 128;
INITIAL_BIAS = 72;
 
function adapt_bias(delta, n_points, is_first) {
// scale back, then increase delta
delta /= is_first ? DAMP : 2;
delta += ~~(delta / n_points);
 
var s = (BASE - TMIN)
var t = ~~((s * TMAX) / 2) // threshold=455
 
for (var k = 0; delta > t; k += BASE) {
delta = ~~(delta / s);
}
 
var a = (BASE - TMIN + 1) * delta
var b = (delta + SKEW)
 
return k + ~~(a / b)
}
 
function next_smallest_codepoint(codepoints, n) {
var m = 0x110000; // unicode upper bound + 1
 
for (var i = 0, len = codepoints.length; i < len; ++i) {
var c = codepoints[i];
if (c >= n && c < m) {
m = c;
}
}
 
// sanity check - should not happen
if (m >= 0x110000) {
throw new Error('Next smallest code point not found.');
}
 
return m;
}
 
function encode_digit(d) {
return d + (d < 26 ? 97 : 22);
}
 
function decode_digit(d) {
if (d >= 48 && d <= 57) {
return d - 22 // 0..9
}
if (d >= 65 && d <= 90) {
return d - 65 // A..Z
}
if (d >= 97 && d <= 122) {
return d - 97 // a..z
}
throw new Error('Illegal digit #' + d)
}
 
function threshold(k, bias) {
if (k <= bias + TMIN) {
return TMIN;
}
if (k >= bias + TMAX) {
return TMAX;
}
return k - bias;
}
 
function encode_int(bias, delta) {
var result = [];
 
for (var k = BASE, q = delta; ; k += BASE) {
var t = threshold(k, bias);
if (q < t) {
result.push(encode_digit(q));
break;
}
else {
result.push(encode_digit(t + ((q - t) % (BASE - t))));
q = ~~((q - t) / (BASE - t));
}
}
 
return result;
}
 
function encode(input) {
if (typeof input != 'string') {
throw new Error('Argument must be a string.');
}
 
input = input.split('').map(function(c) {
return c.charCodeAt(0);
});
 
var output = [];
var non_basic = [];
 
for (var i = 0, len = input.length; i < len; ++i) {
var c = input[i];
if (c < 128) {
output.push(c);
}
else {
non_basic.push(c);
}
}
 
var b, h;
b = h = output.length;
 
if (b) {
output.push(45); // delimiter '-'
}
 
var n = INITIAL_N;
var bias = INITIAL_BIAS;
var delta = 0;
 
for (var len = input.length; h < len; ++n, ++delta) {
var m = next_smallest_codepoint(non_basic, n);
delta += (m - n) * (h + 1);
n = m;
 
for (var i = 0; i < len; ++i) {
var c = input[i];
if (c < n) {
if (++delta == 0) {
throw new Error('Delta overflow.');
}
}
else if (c == n) {
// TODO append in-place? i.e. -> output.push.apply(output, encode_int(bias, delta));
output = output.concat(encode_int(bias, delta));
bias = adapt_bias(delta, h + 1, b == h);
delta = 0;
h++;
}
}
}
 
return String.fromCharCode.apply(String, output);
}
 
function decode(input) {
if (typeof input != 'string') {
throw new Error('Argument must be a string.');
}
 
// find basic code points/delta separator
var b = 1 + input.lastIndexOf('-');
 
input = input.split('').map(function(c) {
return c.charCodeAt(0);
});
 
// start with a copy of the basic code points
var output = input.slice(0, b ? (b - 1) : 0);
 
var n = INITIAL_N;
var bias = INITIAL_BIAS;
 
for (var i = 0, len = input.length; b < len; ++i) {
var org_i = i;
 
for (var k = BASE, w = 1; ; k += BASE) {
var d = decode_digit(input[b++]);
 
// TODO overflow check
i += d * w;
 
var t = threshold(k, bias);
if (d < t) {
break;
}
 
// TODO overflow check
w *= BASE - t;
}
 
var x = 1 + output.length;
bias = adapt_bias(i - org_i, x, org_i == 0);
// TODO overflow check
n += ~~(i / x);
i %= x;
 
output.splice(i, 0, n);
}
 
return String.fromCharCode.apply(String, output);
}
 
if (typeof exports == 'object') {
exports.encode = encode;
exports.decode = decode;
}
View punycode.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
/**
* Copyright (C) 2011 by Ben Noordhuis <info@bnoordhuis.nl>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
 
punycode = require('./punycode');
assert = require('assert');
 
assert.equal(punycode.encode('ü'), 'tda');
assert.equal(punycode.encode('Goethe'), 'Goethe-');
assert.equal(punycode.encode('Bücher'), 'Bcher-kva');
assert.equal(punycode.encode(
'Willst du die Blüthe des frühen, die Früchte des späteren Jahres'),
'Willst du die Blthe des frhen, die Frchte des spteren Jahres-x9e96lkal');
 
assert.equal(punycode.decode('tda'), 'ü');
assert.equal(punycode.decode('Goethe-'), 'Goethe');
assert.equal(punycode.decode('Bcher-kva'), 'Bücher');
assert.equal(punycode.decode(
'Willst du die Blthe des frhen, die Frchte des spteren Jahres-x9e96lkal'),
'Willst du die Blüthe des frühen, die Früchte des späteren Jahres');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.