@0001-labs/idv1 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +24 -1
  2. package/package.json +1 -1
  3. package/src/shapes.ts +20 -0
package/README.md CHANGED
@@ -16,13 +16,36 @@ Short codes whose shape says what they name. Every code is six characters, and o
16
16
  - **An item never ends in two digits**, or it would be an account.
17
17
  - **A code does not change by itself.** A product may give an account a chosen code, as when it should match the code its owner has elsewhere.
18
18
 
19
+ ## Which shape for what you build
20
+
21
+ Pick the shape by what the thing is, not by what a product calls it, so the same kind of thing has the same shape in every product and a code is easy to place on sight:
22
+
23
+ | What it is | Shape | Called in products | Example |
24
+ |---|---|---|---|
25
+ | A person | account | user, member, owner | `Jo4712` |
26
+ | Something that holds things | list | list, category, board, collection, folder, project | `12ABCD` |
27
+ | One thing kept | item | item, clip, listing, note, a piece of inspiration | `Ab12Cd` |
28
+ | A thing in the world with a code printed on it | product | product, tag, sticker | `AbCd12` |
29
+
30
+ Two products agree without asking each other: a category in one is a list's shape like a list in another, and a piece of inspiration is an item's shape like an item anywhere.
31
+
32
+ ```ts
33
+ import { codeCandidate, codeKind, isCodeOf, nameLetters } from '@0001-labs/idv1'
34
+
35
+ codeCandidate('list') // a category: 47KQWE
36
+ codeCandidate('item') // a piece: Ab12Cd
37
+ codeCandidate('account', { letters: nameLetters('Leo Andersen') }) // a person: Le0125
38
+ isCodeOf('list', '47KQWE') // true
39
+ codeKind('Ab12Cd') // 'item'
40
+ ```
41
+
19
42
  ## The scannable code
20
43
 
21
44
  `scanCode.ts` turns any six-character code into a strip of 16 × 4 cells (64 bits: a format byte, the code in five bytes, a two-byte check) and reads it back from a camera frame. Any single damaged cell fails the check rather than reading wrong. `scanCodePath` draws the strip as SVG; `readRgbaScanFrame`, `locateRgbaScanFrame` and `StableScan` read it from camera pixels, also when it is written on paper beside other marks.
22
45
 
23
46
  ## What is in here
24
47
 
25
- - `src/shapes.ts` — the patterns (`ACCOUNT_CODE`, `PRODUCT_CODE`, `LIST_CODE`, `ITEM_CODE`), `codeKind`, the candidates each shape is drawn from, and `nameLetters`
48
+ - `src/shapes.ts` — the patterns (`ACCOUNT_CODE`, `PRODUCT_CODE`, `LIST_CODE`, `ITEM_CODE`, and `CODE_PATTERN` by kind), `codeKind`, `isCodeOf`, the candidates each shape is drawn from (`codeCandidate` by kind), and `nameLetters`
26
49
  - `src/space.ts` — `ID_SPACE`, how many codes of each kind there can be, and `idUsage`
27
50
  - `src/words.ts` — the words and digits a product's code must never spell
28
51
  - `src/scanCode.ts`, `src/scanCodeDrawing.ts` — the scannable code
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@0001-labs/idv1",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Short codes whose shape says what they name: accounts, products, lists and items, how many there can be, and the scannable code that carries them.",
5
5
  "license": "MIT",
6
6
  "repository": {
package/src/shapes.ts CHANGED
@@ -118,6 +118,26 @@ export function itemCodeCandidate(random: Random = Math.random): string {
118
118
  return start + (/\d/.test(first) && /\d/.test(second) ? pick(LETTERS, random) : first) + second
119
119
  }
120
120
 
121
+ /** The pattern each kind is read by. */
122
+ export const CODE_PATTERN: Record<CodeKind, RegExp> = { account: ACCOUNT_CODE, product: PRODUCT_CODE, list: LIST_CODE, item: ITEM_CODE }
123
+
124
+ /** Whether a code has the shape of the kind a product expects: `isCodeOf('list', '12ABCD')`. */
125
+ export const isCodeOf = (kind: CodeKind, value: string): boolean => CODE_PATTERN[kind].test(value)
126
+
127
+ /**
128
+ * A code to try for any kind, so a product picks the shape by what it names
129
+ * and never by its pattern: `codeCandidate('list')` for a category,
130
+ * `codeCandidate('item')` for one thing kept. An account takes the name's
131
+ * letters (`nameLetters`), or two at random.
132
+ */
133
+ export function codeCandidate(kind: CodeKind, options: { letters?: string | null; random?: Random } = {}): string {
134
+ const random = options.random ?? Math.random
135
+ if (kind === 'account') return accountCodeCandidate(options.letters ?? null, random)
136
+ if (kind === 'product') return productCodeCandidate(random)
137
+ if (kind === 'list') return listCodeCandidate(random)
138
+ return itemCodeCandidate(random)
139
+ }
140
+
121
141
  /**
122
142
  * Every way a name-shaped code's two letters can be written, for a product
123
143
  * that must keep a code free in all its capitals (Jo4712, jo4712, JO4712, jO4712).