@adraffy/ens-normalize

Compact ES6 Ethereum Name Service (ENS) Name Normalizer

Usage no npm install needed!

<script type="module">
  import adraffyEnsNormalize from 'https://cdn.skypack.dev/@adraffy/ens-normalize';
</script>

README

ens-normalize.js

1-file, 1-function, 1-argument, 0-dependancy Compact ES6 Ethereum Name Service (ENS) Name Normalizer.


import {ens_normalize} from '@adraffy/ens-normalize';
// browser: 
// 'https://unpkg.com/@adraffy/ens-normalize@latest/dist/ens-normalize.min.js'
// alternatives:
// - ens-normalize-xbidi.min.js (no CheckBidi)
// - ens-normalize-xnfc.min.js (use default String.normalize)
// see: /dist/ for more

// Primary API: string -> string
let normalized = ens_normalize('šŸš“ā€ā™‚ļø.eth'); // throws 
// ready for namehash

// errors:
// - not a string
// - contains disallowed character
// - punycode error
// - label has double-hyphen
// - label starts/ends with hyphen
// - label starts with combining mark
// - character out of context
// - bidi error

// note: does not enforce .eth TLD 3-character minimum

Instead of exposing an IDNA-like API (is_valid(), get_mapped(), etc.), this library converts names to tokens for use in providing a better UX for end-users. Also, see: parts.js submodule below.

// Secondary API: string -> [{tokens,...}]
// turn a name into a list of tokens
let tokens = ens_tokenize('RšŸ’©\uFE0Fa\xAD./'); // never throws
// [
//   {m: [0x72], u:[0x52]},              // mapped u:"R" -> m:"r"
//   {e: [0x1F4A9], u:[0x1F4A9,0xFE0F]}, // emoji: u:"šŸ’©" -> e:"šŸ’©"
//   {v: [0x61]},                        // valid: "a"
//   {i: 0xAD},                          // ignored: \xAD
//   {},                                 // stop: "."
//   {d: 0x2F}                           // disallowed: "/"
// ]

Independent submodules:

// Unicode Normalized Forms
// see: build/nf.js (algo)
// see: build/lib-nf.js (api)
// see: https://adraffy.github.io/ens-normalize.js/test/report-nf.html
import {nfc, nfd} from 'dist/nf.min.js';
// {nfc,nfd}(string): string

// CheckBidi 
// see: build/bidi.js (algo)
// see: build/lib-bidi.js (api)
// see: https://www.rfc-editor.org/rfc/rfc5893.html#section-2
import {check_bidi, is_bidi_domain_name} from 'dist/bidi.min.js';
// is_bidi_domain_name(string): bool
// check_bidi(string) throws

// Parts -- generate HTML from parsed tokens
// see: build/lib-parts.js (api)
// see: https://adraffy.github.io/ens-normalize.js/test/report-emoji.html
import {dom_from_tokens, use_default_style} from 'dist/parts.min.js';
// use_default_style(); installs a stylesheet
// DOMNode.append(dom_from_tokens(ens_tokenize('raffy.eth')));

Building

  • Clone to access build/. The actual source is in build/lib-normalize.js. You can run this file directly.
  • Run node build/unicode.js download to download data from unicode.org.
  • Run node build/unicode.js parse to parse those files into JSON files.
  • Run node build/build-tables.js all to build compressed rule payloads.
  • Run npm run test-source to test build/lib-normalize.js.
  • Run npm run build or node build/build.js all to inject the compressed tables into the source template and create the readable and minified dist/ files.
  • Run npm run test-build to test dist/ens-normalize.js.