TypeScript implementation #4

Merged
sezieru merged 4 commits from feat/kb20-ts into main 2025-10-31 14:03:30 +00:00
Owner

Initial implementation of base-20 in TypeScript.

  • Supports UTF-8.
  • Supports custom code point mapping.
  • Heavily documented.
Initial implementation of base-20 in TypeScript. - [x] Supports UTF-8. - [x] Supports custom code point mapping. - [x] Heavily documented.
sezieru self-assigned this 2025-10-30 10:42:02 +00:00
- Add strict digit mappers (error on unknown chars) for paritty with C90/C++20/Py3
- Validate ZERO (single scalar) and custom alphabet (20 DISTINCT characters)
- Introduce kb20EncodeBinaryDigits/kb20DecodeBinaryDigits (0xFF separator)
- JSDoc across helpers; standardized MSB-first/big-endian terminology
- No format change for text mode; Unicode digits supported via code points
- Reject alphabets where '-' would appear among digit symbols in text mode
- Guard applies to both contiguous ZERO..ZERO+19 and explicit 20-char alphabets
- Binary-digits mode unaffected (0xFF separator; digits are raw 0..19)
Author
Owner

TypeScript strict parsing, alphabet hygiene, binary-digits mode, and docs

Summary

Brings the TypeScript reference to parity with the C90/C++20/Python3 implementations. Text mode supports
Unicode digit alphabets (e.g. Kaktovik), parsing is strict, custom alphabets must be 20 distinct characters, and
a binary-digits mode (0xFF separator) is included. All byte folding is MSB-first (big-endian) and doc'd.

What changed

  • Text mode: strict digit mappers; options validated (single-scalar ZERO, 20-distinct alphabet).
  • Binary-digits: kb20EncodeBinaryDigits/kb20DecodeBinaryDigits added.
  • JSDoc: clarified MSB-first, Horner folding, and wire formats.
  • Text-mode hyphen guard to prevent '-' as a digit symbol.

Verification

You can test without a framework:

# Build once:
npm i -D typescript @types/node
npx tsc kb20.ts --outDir dist

# Node smoke tests (ASCII, Kaktovik, explicit alphabet, binary-digits)
node - <<'NODE'
const kb = require('./dist/kb20.js');
const crypto = require('crypto');

function encDecText(s, opts) {
  const enc = kb.kb20EncodeAscii(Buffer.from(s, 'utf8'), opts);
  const dec = kb.kb20DecodeAscii(enc, opts);
  console.log(s === Buffer.from(dec).toString('utf8') ? 'ok' : 'FAIL', opts || {});
}

function encDecBin(n) {
  const src = crypto.randomBytes(n);
  const enc = kb.kb20EncodeBinaryDigits(src);
  const dec = kb.kb20DecodeBinaryDigits(enc);
  console.log(src.equals(Buffer.from(dec)) ? 'ok' : 'FAIL', 'bin', n);
}

encDecText('this is test text', {ZERO:'A'});
encDecText('abcXYZ', {ZERO:String.fromCodePoint(0x1D2C0)});
encDecText('EdgeCase42!', {useMap:true, alphabet:'0123456789ABCDEFGHIJ'});
encDecBin(257); [4,5,6,9,10,11].forEach(encDecBin);

// Negative tests (should throw)
try { kb.kb20DecodeAscii('NO-DASH-HERE', {ZERO:'A'}); } catch { console.log('ok bad-sep'); }
try { kb.kb20DecodeAscii('1-?@', {useMap:true, alphabet:'0123456789ABCDEFGHIJ'}); } catch { console.log('ok bad-digit'); }
NODE

Compatibility

No format changes vs other languages. Text separator remains '-'; binary-digits uses 0xFF.

## TypeScript strict parsing, alphabet hygiene, binary-digits mode, and docs ### Summary Brings the TypeScript reference to parity with the C90/C++20/Python3 implementations. Text mode supports Unicode digit alphabets (e.g. Kaktovik), parsing is strict, custom alphabets must be 20 distinct characters, and a binary-digits mode (0xFF separator) is included. All byte folding is **MSB-first (big-endian)** and doc'd. ### What changed - Text mode: strict digit mappers; options validated (single-scalar `ZERO`, 20-distinct `alphabet`). - Binary-digits: `kb20EncodeBinaryDigits`/`kb20DecodeBinaryDigits` added. - JSDoc: clarified MSB-first, Horner folding, and wire formats. - Text-mode hyphen guard to prevent `'-'` as a digit symbol. ### Verification You can test without a framework: ```shell # Build once: npm i -D typescript @types/node npx tsc kb20.ts --outDir dist # Node smoke tests (ASCII, Kaktovik, explicit alphabet, binary-digits) node - <<'NODE' const kb = require('./dist/kb20.js'); const crypto = require('crypto'); function encDecText(s, opts) { const enc = kb.kb20EncodeAscii(Buffer.from(s, 'utf8'), opts); const dec = kb.kb20DecodeAscii(enc, opts); console.log(s === Buffer.from(dec).toString('utf8') ? 'ok' : 'FAIL', opts || {}); } function encDecBin(n) { const src = crypto.randomBytes(n); const enc = kb.kb20EncodeBinaryDigits(src); const dec = kb.kb20DecodeBinaryDigits(enc); console.log(src.equals(Buffer.from(dec)) ? 'ok' : 'FAIL', 'bin', n); } encDecText('this is test text', {ZERO:'A'}); encDecText('abcXYZ', {ZERO:String.fromCodePoint(0x1D2C0)}); encDecText('EdgeCase42!', {useMap:true, alphabet:'0123456789ABCDEFGHIJ'}); encDecBin(257); [4,5,6,9,10,11].forEach(encDecBin); // Negative tests (should throw) try { kb.kb20DecodeAscii('NO-DASH-HERE', {ZERO:'A'}); } catch { console.log('ok bad-sep'); } try { kb.kb20DecodeAscii('1-?@', {useMap:true, alphabet:'0123456789ABCDEFGHIJ'}); } catch { console.log('ok bad-digit'); } NODE ``` ### Compatibility No format changes vs other languages. Text separator remains `'-'`; binary-digits uses `0xFF`.
sezieru changed title from WIP: TypeScript implementation to TypeScript implementation 2025-10-31 14:02:55 +00:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: fosster/libb20#4
No description provided.