Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt exports to camel case #25

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,50 +28,50 @@
* [Label Database](https://github.com/adraffy/ens-labels/)

```Javascript
import {ens_normalize} from '@adraffy/ens-normalize'; // or require()
import {ensNormalize} from '@adraffy/ens-normalize'; // or require()
// npm i @adraffy/ens-normalize
// browser: https://cdn.jsdelivr.net/npm/@adraffy/ens-normalize@latest/dist/index.min.mjs (or .cjs)

// *** ALL errors thrown by this library are safe to print ***
// - characters are shown as {HEX} if should_escape()
// - characters are shown as {HEX} if shouldEscape()
// - potentially different bidi directions inside "quotes"
// - 200E is used near "quotes" to prevent spillover
// - an "error type" can be extracted by slicing up to the first (:)

// string -> string
// throws on invalid names
// output ready for namehash
let normalized = ens_normalize('RaFFY🚴‍♂️.eTh');
let normalized = ensNormalize('RaFFY🚴‍♂️.eTh');
// => "raffy🚴‍♂.eth"

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

Format names with fully-qualified emoji:
```Javascript
// works like ens_normalize()
// works like ensNormalize()
// output ready for display
let pretty = ens_beautify('1⃣2⃣.eth');
let pretty = ensBeautify('1⃣2⃣.eth');
// => "1️⃣2️⃣.eth"

// note: normalization is unchanged:
// ens_normalize(ens_beautify(x)) == ens_normalize(x)
// ensNormalize(ensBeautify(x)) == ensNormalize(x)
```

Normalize name fragments for [substring search](./test/fragment.js):
```Javascript
// these fragments fail ens_normalize()
// these fragments fail ensNormalize()
// but will normalize fine as fragments
let frag1 = ens_normalize_fragment('AB--'); // expected error: label ext
let frag2 = ens_normalize_fragment('\u{303}'); // expected error: leading cm
let frag3 = ens_normalize_fragment('οо'); // expected error: mixture
let frag1 = ensNormalizeFragment('AB--'); // expected error: label ext
let frag2 = ensNormalizeFragment('\u{303}'); // expected error: leading cm
let frag3 = ensNormalizeFragment('οо'); // expected error: mixture
```

Input-based tokenization:
```Javascript
// string -> Token[]
// never throws
let tokens = ens_tokenize('_R💩\u{FE0F}a\u{FE0F}\u{304}\u{AD}./');
let tokens = ensTokenize('_R💩\u{FE0F}a\u{FE0F}\u{304}\u{AD}./');
// [
// { type: 'valid', cp: [ 95 ] }, // valid (as-is)
// {
Expand Down Expand Up @@ -104,16 +104,16 @@ let tokens = ens_tokenize('_R💩\u{FE0F}a\u{FE0F}\u{304}\u{AD}./');
// ]

// note: if name is normalizable, then:
// ens_normalize(ens_tokenize(name).map(token => {
// ensNormalize(ensTokenize(name).map(token => {
// ** convert valid/mapped/nfc/stop to string **
// }).join('')) == ens_normalize(name)
// }).join('')) == ensNormalize(name)
```

Output-based tokenization:
```Javascript
// string -> Label[]
// never throws
let labels = ens_split('💩Raffy.eth_');
let labels = ensSplit('💩Raffy.eth_');
// [
// {
// input: [ 128169, 82, 97, 102, 102, 121 ],
Expand All @@ -140,7 +140,7 @@ let labels = ens_split('💩Raffy.eth_');
Generate a sorted array of supported emoji codepoints:
```Javascript
// () -> number[][]
let emojis = ens_emoji();
let emojis = ensEmoji();
// [
// [ 2764 ],
// [ 128169, 65039 ],
Expand All @@ -152,13 +152,13 @@ let emojis = ens_emoji();
Determine if a character shouldn't be printed directly:
```Javascript
// number -> bool
should_escape(0x202E); // eg. RIGHT-TO-LEFT OVERRIDE => true
shouldEscape(0x202E); // eg. RIGHT-TO-LEFT OVERRIDE => true
```

Determine if a character is a combining mark:
```Javascript
// number -> bool
is_combining_mark(0x20E3); // eg. COMBINING ENCLOSING KEYCAP => true
isCombiningMark(0x20E3); // eg. COMBINING ENCLOSING KEYCAP => true
```

## Build
Expand Down
4 changes: 2 additions & 2 deletions derive/dump-bidi-class.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {UNICODE, PRINTER} from './unicode-version.js';
import {explode_cp} from './utils.js';
import {explodeCp} from './utils.js';

// TODO: improve this input mechanism
for (let cp of new Set(explode_cp(process.argv.slice(2).join('')))) {
for (let cp of new Set(explodeCp(process.argv.slice(2).join('')))) {
let ch = UNICODE.char_map.get(cp);
console.log((ch ? ch.bidi_class : '?').padEnd(3), PRINTER.desc_for_cp(cp));
}
6 changes: 3 additions & 3 deletions derive/dump-confuse.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// suggestion: if unicode changed, compute this file, then diff against original

import {UNICODE, NF, IDNA, PRINTER} from './unicode-version.js';
import {compare_arrays, hex_cp} from './utils.js';
import {compareArrays, hex_cp} from './utils.js';

let confusables = UNICODE.read_confusables();
let valid_set = new Set(IDNA.valid);
Expand Down Expand Up @@ -30,8 +30,8 @@ console.log(`export default [`);
for (let [target, cps] of confusables) {
let nfc = NF.nfc(target);
let nfd = NF.nfd(target);
let same_nfc = !compare_arrays(target, nfc);
let same_nfd = !compare_arrays(target, nfd);
let same_nfc = !compareArrays(target, nfc);
let same_nfd = !compareArrays(target, nfd);
let sameness;
if (same_nfc !== same_nfd) {
sameness = `NFC(${same_nfc}) NFD(${same_nfd})`;
Expand Down
4 changes: 2 additions & 2 deletions derive/dump-short.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {UNICODE} from './unicode-version.js';
import {explode_cp, hex_cp} from './utils.js';
import {explodeCp, hex_cp} from './utils.js';

for (let info of UNICODE.char_map.values()) {
if (info.short) {
Expand All @@ -13,7 +13,7 @@ for (let info of UNICODE.char_map.values()) {
let singles = [];
let seqs = [];
for (let [form, name] of UNICODE.short_names()) {
let cps = explode_cp(form);
let cps = explodeCp(form);
if (cps.length == 1) {
let [cp] = cps;
console.log(`${hex_cp(cps[0])} ${UNICODE.get_name(cp)} ${name}`);
Expand Down
18 changes: 9 additions & 9 deletions derive/make.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {mkdirSync, writeFileSync, createWriteStream, readFileSync} from 'node:fs';
import {deepStrictEqual} from 'node:assert';
import {compare_arrays, explode_cp, parse_cp_sequence, print_section, print_checked, print_table} from './utils.js';
import {compareArrays, explodeCp, parse_cp_sequence, print_section, print_checked, print_table} from './utils.js';
import {UNICODE, NF, IDNA, PRINTER} from './unicode-version.js';
import CHARS_VALID from './rules/chars-valid.js';
import CHARS_DISALLOWED from './rules/chars-disallow.js';
Expand Down Expand Up @@ -43,7 +43,7 @@ console.log(`Unicode Version: ${version_str(UNICODE.unicode_version)}`);
console.log(`CLDR Version: ${version_str(UNICODE.cldr_version)}`);

// 20230214: moved from unicode-version.js
if (NF.run_tests().length) throw new Error('nf implementation wrong');
if (NF.runTests().length) throw new Error('nf implementation wrong');
NF.run_random_tests();

// these are our primary output structures
Expand Down Expand Up @@ -196,7 +196,7 @@ for (let {cps, group, subgroup} of UNICODE.read_emoji_test()) {
print_section('Add Mapped Characters');
for (let [x, ys] of CHARS_MAPPED) {
let old = mapped.get(x);
if (old && !compare_arrays(old, ys)) throw new Error(`Duplicate mapped: ${PRINTER.desc_for_mapped(x, ys)}`);
if (old && !compareArrays(old, ys)) throw new Error(`Duplicate mapped: ${PRINTER.desc_for_mapped(x, ys)}`);
disallow_char(x);
mapped.set(x, ys);
console.log(`Add Mapped: ${PRINTER.desc_for_mapped(x, ys)}`);
Expand Down Expand Up @@ -314,7 +314,7 @@ for (let config of SCRIPT_GROUPS) {
}
// there are no restrictions on extra
// we can enforce this later
let extra_set = new Set(extra.flat(Infinity).flatMap(x => typeof x === 'string' ? explode_cp(x) : x));
let extra_set = new Set(extra.flat(Infinity).flatMap(x => typeof x === 'string' ? explodeCp(x) : x));
let group = new ScriptGroup(name, test_set, rest_set, extra_set, cm);
script_groups.push(group);
} catch (err) {
Expand Down Expand Up @@ -364,7 +364,7 @@ print_table(['Valid', 'Name', 'Test', 'Rest', 'Extra'], script_groups.map(g => {
print_section(`Compute CM Whitelist`);
let cm_whitelist = new Set();
for (let form of CM_WHITELIST) {
let cps = NF.nfc(explode_cp(form));
let cps = NF.nfc(explodeCp(form));
try {
let key = String.fromCodePoint(...cps);
if (cm_whitelist.has(key)) {
Expand Down Expand Up @@ -703,10 +703,10 @@ print_checked('Emoji Boundaries');

for (let info of valid_emoji.values()) {
let {cps} = info;
if (compare_arrays(cps, NF.nfc(cps))) {
if (compareArrays(cps, NF.nfc(cps))) {
throw new Error(`Emoji doesn't survive NFC: ${PRINTER.desc_for_emoji(info)}`);
}
if (compare_arrays(cps, NF.nfd(cps))) {
if (compareArrays(cps, NF.nfd(cps))) {
throw new Error(`Emoji doesn't survive NFD: ${PRINTER.desc_for_emoji(info)}`);
}
}
Expand Down Expand Up @@ -823,7 +823,7 @@ for (let g of script_groups) {
cm = [];
for (let [cp, seqs] of g.cm_map) {
if (seqs.length == 1 && seqs[0].length == 0) continue; // just a valid char
seqs.sort(compare_arrays);
seqs.sort(compareArrays);
cm.push({cp, seqs});
}
cm.sort((a, b) => a.cp - b.cp);
Expand Down Expand Up @@ -908,7 +908,7 @@ write_json('spec.json', {
created,
unicode: version_str(UNICODE.unicode_version),
cldr: version_str(UNICODE.cldr_version),
emoji: [...valid_emoji.values()].map(x => x.cps).sort(compare_arrays),
emoji: [...valid_emoji.values()].map(x => x.cps).sort(compareArrays),
ignored: sorted(ignored),
mapped: [...mapped].sort((a, b) => a[0] - b[0]),
fenced: [...fenced_map].sort((a, b) => a[0] - b[0]),
Expand Down
14 changes: 7 additions & 7 deletions derive/nf.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {compare_arrays, explode_cp, permutations} from './utils.js';
import {compareArrays, explodeCp, permutations} from './utils.js';

// algorithmic hangul
// https://www.unicode.org/versions/Unicode10.0.0/ch03.pdf (page 149)
Expand Down Expand Up @@ -187,17 +187,17 @@ export function create_nf(unicode) {
is_composite(cp) {
return this.nfd([cp]).length > 1;
},
run_tests() {
runTests() {
let errors = [];
for (let [name, cases] of Object.entries(unicode.read_nf_tests())) {
for (let strs of cases) {
let [input, nfd0, nfc0] = strs.map(explode_cp);
let [input, nfd0, nfc0] = strs.map(explodeCp);
let nfd1 = this.nfd(input);
if (compare_arrays(nfd0, nfd1)) {
if (compareArrays(nfd0, nfd1)) {
errors.push({name, input, nfd0, nfd1});
}
let nfc1 = this.nfc(input);
if (compare_arrays(nfc0, nfc1)) {
if (compareArrays(nfc0, nfc1)) {
errors.push({name, input, nfc0, nfc1});
}
}
Expand All @@ -210,10 +210,10 @@ export function create_nf(unicode) {
for (let i = 0; i < len; i++) {
v[i] = Math.random() * 0x110000|0;
}
if (compare_arrays(this.nfc(v), this.nfc(this.nfd(v)))) {
if (compareArrays(this.nfc(v), this.nfc(this.nfd(v)))) {
throw new Error('random nfc');
}
if (compare_arrays(this.nfd(v), this.nfd(this.nfc(v)))) {
if (compareArrays(this.nfd(v), this.nfd(this.nfc(v)))) {
throw new Error('random nfd');
}
}
Expand Down
16 changes: 8 additions & 8 deletions derive/unicode-logic.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {MAX_CP, parse_cp, parse_cp_range, parse_cp_sequence, hex_cp, hex_seq, explode_cp, quote_cp, require_cp} from './utils.js';
import {MAX_CP, parse_cp, parse_cp_range, parse_cp_sequence, hex_cp, hex_seq, explodeCp, quote_cp, require_cp} from './utils.js';
import {readFileSync} from 'node:fs';
import UNPRINTABLES from './unprintables.js';

Expand Down Expand Up @@ -154,7 +154,7 @@ export class UnicodeSpec {
// since they are lost due to the stupid A..B syntax
// emoji sequences already have proper names
for (let [form, name] of Object.entries(this.read_short_names())) {
let cps = explode_cp(form);
let cps = explodeCp(form);
if (cps.length == 1) {
let info = this.char_map.get(cps[0]);
if (info) {
Expand Down Expand Up @@ -284,7 +284,7 @@ export class UnicodeSpec {
}
get_script_set(x) {
let ret = new Set();
for (let cp of explode_cp(x)) {
for (let cp of explodeCp(x)) {
let script = this.get_script(cp);
if (script) {
ret.add(script);
Expand All @@ -294,7 +294,7 @@ export class UnicodeSpec {
}
get_extended_script_set(x) {
let ret = new Set();
for (let cp of explode_cp(x)) {
for (let cp of explodeCp(x)) {
let info = this.char_map.get(cp);
if (!info) continue;
let {extended, script} = info;
Expand All @@ -313,7 +313,7 @@ export class UnicodeSpec {
}
get_resolved_script_set(x) {
// https://www.unicode.org/reports/tr39/#def-resolved-script-set
let cps = explode_cp(x);
let cps = explodeCp(x);
if (!cps.length) return new Set();
let resolved = this.get_augmented_script_set(cps[0]);
for (let i = 1; i < cps.length; i++) {
Expand Down Expand Up @@ -580,7 +580,7 @@ export class UnicodeSpec {
if (cps.length != 26) throw new Error('expected 26 regionals'); // meh
let dx = cps[0] - 0x41; // 'A'
return regions.map(region => {
let cps = explode_cp(region).map(x => x + dx);
let cps = explodeCp(region).map(x => x + dx);
let name = `Flag Sequence: ${region}`;
let type = 'DerivedFlagSequence';
return {cps, name, type};
Expand Down Expand Up @@ -702,7 +702,7 @@ export class UnicodeSpec {
let key = String.fromCodePoint(...parse_cp_sequence(target));
this.get_bucket(key).push(parse_cp(src));
}
})).map(([key, cps]) => [explode_cp(key), cps]);
})).map(([key, cps]) => [explodeCp(key), cps]);
}
read_intentional_confusables() {
// returns list of pairs
Expand Down Expand Up @@ -811,7 +811,7 @@ export class UnicodePrinter {
return `${hex_cp(cp)} (${this.spec.get_display(cp)}) ${this.spec.get_name(cp)}`;
}
desc_for_cps(x) {
let cps = explode_cp(x);
let cps = explodeCp(x);
return `${hex_seq(cps)} (${this.spec.safe_str(cps)}) ${this.names(cps)}`;
}
desc_for_mapped(x, ys) {
Expand Down
4 changes: 2 additions & 2 deletions derive/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export function hex_seq(cps) {
return `[${cps.map(hex_cp).join(' ')}]`;
}

export function explode_cp(x) {
export function explodeCp(x) {
if (typeof x === 'string') {
return [...x].map(c => c.codePointAt(0));
} else if (Number.isInteger(x)) {
Expand Down Expand Up @@ -153,7 +153,7 @@ export function parse_cps(spec) {
throw new TypeError(`unknown character spec: ${spec}`);
}

export function compare_arrays(a, b) {
export function compareArrays(a, b) {
let n = a.length;
let c = n - b.length;
for (let i = 0; c == 0 && i < n; i++) c = a[i] - b[i];
Expand Down
Loading