How to use punycode - 10 common examples

To help you get started, we’ve selected a few punycode examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github graalvm / graaljs / test / simple / test-punycode.js View on Github external
// (R) 
  'd9juau41awczczp':
      '\u305D\u306E\u30B9\u30D4\u30FC\u30C9\u3067',

  // (S) -> $1.00 <-
  '-> $1.00 <--':
      '\u002D\u003E\u0020\u0024\u0031\u002E\u0030\u0030\u0020\u003C\u002D'
};

var errors = 0;

for (var encoded in tests) {
  var decoded = tests[encoded];
  try {
    assert.equal(punycode.encode(decoded), encoded);
  } catch (e) {
    console.error('FAIL: expected %j, got %j', e.expected, e.actual);
    errors++;
  }
  try {
    assert.equal(punycode.decode(encoded), decoded);
  } catch (e) {
    console.error('FAIL: expected %j, got %j', e.expected, e.actual);
    errors++;
  }
}

assert.equal(errors, 0);
github mpangrazzi / pepper / test / pepper.js View on Github external
} else {
      // hostnames are always lower case.
      this.hostname = this.hostname.toLowerCase();
    }

    if (!ipv6Hostname) {
      // IDNA Support: Returns a puny coded representation of "domain".
      // It only converts the part of the domain name that
      // has non ASCII characters. I.e. it dosent matter if
      // you call it with a domain that already is in ASCII.
      var domainArray = this.hostname.split('.');
      var newOut = [];
      for (var i = 0; i < domainArray.length; ++i) {
        var s = domainArray[i];
        newOut.push(s.match(/[^A-Za-z0-9_-]/) ?
            'xn--' + punycode.encode(s) : s);
      }
      this.hostname = newOut.join('.');
    }

    var p = this.port ? ':' + this.port : '';
    var h = this.hostname || '';
    this.host = h + p;
    this.href += this.host;

    // strip [ and ] from the hostname
    // the host field still retains them, though
    if (ipv6Hostname) {
      this.hostname = this.hostname.substr(1, this.hostname.length - 2);
      if (rest[0] !== '/') {
        rest = '/' + rest;
      }
github graalvm / graaljs / test / simple / test-punycode.js View on Github external
var common = require('../common');
var punycode = require('punycode');
var 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.encode('日本語'), 'wgv71a119e');

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');
assert.equal(punycode.decode('wgv71a119e'), '日本語');

// http://tools.ietf.org/html/rfc3492#section-7.1
var tests = {
  // (A) Arabic (Egyptian)
  'egbpdaj6bu4bxfgehfvwxn':
      '\u0644\u064A\u0647\u0645\u0627\u0628\u062A\u0643\u0644\u0645\u0648' +
      '\u0634\u0639\u0631\u0628\u064A\u061F',

  // (B) Chinese (simplified)
  'ihqwcrb4cv8a8dqg056pqjye':
      '\u4ED6\u4EEC\u4E3A\u4EC0\u4E48\u4E0D\u8BF4\u4E2D\u6587',
github webpack / node-libs-browser / test / suite / node / simple / test-punycode.js View on Github external
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

var common = require('../common');
var punycode = require('punycode');
var 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.encode('日本語'), 'wgv71a119e');

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');
assert.equal(punycode.decode('wgv71a119e'), '日本語');

// http://tools.ietf.org/html/rfc3492#section-7.1
var tests = {
  // (A) Arabic (Egyptian)
  'egbpdaj6bu4bxfgehfvwxn':
      '\u0644\u064A\u0647\u0645\u0627\u0628\u062A\u0643\u0644\u0645\u0648' +
      '\u0634\u0639\u0631\u0628\u064A\u061F',

  // (B) Chinese (simplified)
  'ihqwcrb4cv8a8dqg056pqjye':
github webpack / node-libs-browser / test / suite / node / simple / test-punycode.js View on Github external
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.encode('日本語'), 'wgv71a119e');

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');
assert.equal(punycode.decode('wgv71a119e'), '日本語');

// http://tools.ietf.org/html/rfc3492#section-7.1
var tests = {
  // (A) Arabic (Egyptian)
  'egbpdaj6bu4bxfgehfvwxn':
      '\u0644\u064A\u0647\u0645\u0627\u0628\u062A\u0643\u0644\u0645\u0648' +
      '\u0634\u0639\u0631\u0628\u064A\u061F',

  // (B) Chinese (simplified)
  'ihqwcrb4cv8a8dqg056pqjye':
      '\u4ED6\u4EEC\u4E3A\u4EC0\u4E48\u4E0D\u8BF4\u4E2D\u6587',

  // (C) Chinese (traditional)
  'ihqwctvzc91f659drss3x8bo0yb':
      '\u4ED6\u5011\u7232\u4EC0\u9EBD\u4E0D\u8AAA\u4E2D\u6587',
github insin / newforms / lib / validators.js View on Github external
, __call__ : function(value) {
    try {
      RegexValidator.prototype.__call__.call(this, value)
    }
    catch (e) {
      if (!(e instanceof ValidationError) ||
          !value ||
          value.indexOf('@') == -1) {
        throw e
      }

      // Trivial case failed - try for possible IDN domain-part
      var parts = value.split('@')
      try {
        parts[parts.length - 1] = punycode.toASCII(parts[parts.length - 1])
      }
      catch (ue) {
        throw e
      }
      RegexValidator.prototype.__call__.call(this, parts.join('@'))
    }
  }
})
github salesforce / tough-cookie / lib / cookie.js View on Github external
function canonicalDomain(str) {
  if (str == null) {
    return null;
  }
  str = str.trim().replace(/^\./, ""); // S4.1.2.3 & S5.2.3: ignore leading .

  // convert to IDN if any non-ASCII characters
  if (punycode && /[^\u0001-\u007f]/.test(str)) {
    str = punycode.toASCII(str);
  }

  return str.toLowerCase();
}
github rockstat / web-sdk / src / functions / pageSource.js View on Github external
source.refHash = simpleHash(page.ref);

  // Direct with marks -> campaign
  // Direct with fbclid -> facebooj social
  if (ref === '') {
    if (has_utm || has_os || has_gclid || has_yclid) {
      source.type = SESSION_CAMPAIGN;
    }
    if(has_fbclid){
      source.type = SESSION_SOCIAL
      source.engine = ENGINE_FACEBOOK
    }
    return source;
  }

  source.refhost = punycode.toUnicode(removeWww(ref.hostname));

  // Internal
  if (ref && source.refhost === removeWww(page.domain)) {
    log('internal detect', ref, source.refhost, removeWww(page.hostname))
    source.type = source.hasMarks ? SESSION_CAMPAIGN : SESSION_INTERNAL;
    return source;
  }

  // Other types: campaigns, organic, social

  const refDomainParts = source.refhost.split('.').reverse();

  for (let i = 0; i < RULES.length; i++) {
    const rule = RULES[i];
    const ruleDomainParts = rule.domain.split('.').reverse();
    const refDomainPartsClone = refDomainParts.slice();
github macbre / phantomas / phantomjs-nodify / modules / url.js View on Github external
}

    // hostnames are always lower case.
    out.hostname = out.hostname.toLowerCase();

    if (!ipv6Hostname) {
      // IDNA Support: Returns a puny coded representation of "domain".
      // It only converts the part of the domain name that
      // has non ASCII characters. I.e. it dosent matter if
      // you call it with a domain that already is in ASCII.
      var domainArray = out.hostname.split('.');
      var newOut = [];
      for (var i = 0; i < domainArray.length; ++i) {
        var s = domainArray[i];
        newOut.push(s.match(/[^A-Za-z0-9_-]/) ?
            'xn--' + punycode.encode(s) : s);
      }
      out.hostname = newOut.join('.');
    }

    out.host = (out.hostname || '') +
        ((out.port) ? ':' + out.port : '');
    out.href += out.host;

    // strip [ and ] from the hostname
    if (ipv6Hostname) {
      out.hostname = out.hostname.substr(1, out.hostname.length - 2);
      if (rest[0] !== '/') {
        rest = '/' + rest;
      }
    }
  }
github kieran / barista / dist / barista.js View on Github external
} else {
      // hostnames are always lower case.
      this.hostname = this.hostname.toLowerCase();
    }

    if (!ipv6Hostname) {
      // IDNA Support: Returns a puny coded representation of "domain".
      // It only converts the part of the domain name that
      // has non ASCII characters. I.e. it dosent matter if
      // you call it with a domain that already is in ASCII.
      var domainArray = this.hostname.split('.');
      var newOut = [];
      for (var i = 0; i < domainArray.length; ++i) {
        var s = domainArray[i];
        newOut.push(s.match(/[^A-Za-z0-9_-]/) ?
            'xn--' + punycode.encode(s) : s);
      }
      this.hostname = newOut.join('.');
    }

    var p = this.port ? ':' + this.port : '';
    var h = this.hostname || '';
    this.host = h + p;
    this.href += this.host;

    // strip [ and ] from the hostname
    // the host field still retains them, though
    if (ipv6Hostname) {
      this.hostname = this.hostname.substr(1, this.hostname.length - 2);
      if (rest[0] !== '/') {
        rest = '/' + rest;
      }

punycode

A robust Punycode converter that fully complies to RFC 3492 and RFC 5891, and works on nearly all JavaScript platforms.

MIT
Latest version published 1 year ago

Package Health Score

77 / 100
Full package analysis