@0001-labs/idv1 3.1.0 → 3.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.
- package/README.md +1 -1
- package/package.json +1 -1
- package/src/identifier.ts +24 -0
package/README.md
CHANGED
|
@@ -51,7 +51,7 @@ npx idv1 resolve co12Ca # the shape, taxonomy and namespace a code reads as
|
|
|
51
51
|
npx idv1 hardcoded src # exits 1 with each quoted -NS-code written into source
|
|
52
52
|
```
|
|
53
53
|
|
|
54
|
-
The same operations are exported: `readDirectory`, `writeDirectory`, `directoryHealth`, `resolveCode`, `taxonomyOf`, `describeDirectory`, `runDirectoryCommand` and `hardcodedCodes`. `recordHealth(directory, records, released)` checks an app's coded records against its `.id`: each code parses, fits its taxonomy pattern, sits in a declared namespace and is unique, and a record's code matches the one it was released with. Run both before every release, so codes are read from records and never typed into code.
|
|
54
|
+
The same operations are exported: `readDirectory`, `writeDirectory`, `directoryHealth`, `resolveCode`, `taxonomyOf`, `describeDirectory`, `runDirectoryCommand` and `hardcodedCodes`. `recordHealth(directory, records, released)` checks an app's coded records against its `.id`: each code parses, fits its taxonomy pattern, sits in a declared namespace and is unique, and a record's code matches the one it was released with. Run both before every release, so codes are read from records and never typed into code. To give database rows codes, store each in a column beside the row's own key: `mintCode(pattern, { taken })` draws an unused code for a shape or taxonomy pattern (`XXerXX`, `doNNXX`; `at` fixes characters such as an operation pair), and `backfillCodes(rows, pattern, { taken })` codes the rows kept before the column existed.
|
|
55
55
|
|
|
56
56
|
The package ships TypeScript source. Bundlers and `tsx` read it directly. Plain Node 22.18 or later imports `@0001-labs/idv1/register` first, then dynamically imports the package.
|
|
57
57
|
|
package/package.json
CHANGED
package/src/identifier.ts
CHANGED
|
@@ -91,6 +91,30 @@ export function codeCandidate<K extends CodeShape>(shape: K, options: { letters?
|
|
|
91
91
|
}
|
|
92
92
|
|
|
93
93
|
|
|
94
|
+
const SLOTS: Record<string, string> = { L: LETTERS, N: DIGITS, X: ALPHABET, O: CAPITALS, P: CAPITALS }
|
|
95
|
+
export type MintOptions = { taken?: (code: string) => boolean; random?: Random; at?: Record<number, string> }
|
|
96
|
+
|
|
97
|
+
export function mintCode(pattern: string, options: MintOptions = {}): string {
|
|
98
|
+
const random = options.random ?? secureRandom
|
|
99
|
+
const slots = pattern.replace(/\s/g, '')
|
|
100
|
+
const shape = parseCode(slots.replace(/N/g, '1').replace(/[LXOP]/g, 'a'))?.shape
|
|
101
|
+
if (slots.length !== 6 || !shape) throw new Error(`${pattern} is not a six-character pattern.`)
|
|
102
|
+
for (let attempt = 0; attempt < 1000; attempt++) {
|
|
103
|
+
const candidate = [...slots].map((slot, index) => options.at?.[index] ?? (SLOTS[slot] ? pick(SLOTS[slot], random) : slot)).join('')
|
|
104
|
+
if (parseCode(candidate)?.shape === shape && !(shape === 'LLLLNN' ? blockedLLLLNNCode(candidate) : blockedCodeLetters(candidate)) && !options.taken?.(candidate)) return candidate
|
|
105
|
+
}
|
|
106
|
+
throw new Error(`No unused ${pattern} code could be drawn.`)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function backfillCodes<T>(rows: readonly T[], pattern: string, options: Omit<MintOptions, 'at'> & { at?: (row: T) => Record<number, string> | undefined } = {}): [T, string][] {
|
|
110
|
+
const minted = new Set<string>()
|
|
111
|
+
return rows.map(row => {
|
|
112
|
+
const code = mintCode(pattern, { random: options.random, at: options.at?.(row), taken: code => minted.has(code) || !!options.taken?.(code) })
|
|
113
|
+
minted.add(code)
|
|
114
|
+
return [row, code]
|
|
115
|
+
})
|
|
116
|
+
}
|
|
117
|
+
|
|
94
118
|
export function caseVariantsLLNNNN(code: string): string[] {
|
|
95
119
|
const [first, second] = [code[0], code[1]].map(letter => [...new Set([letter.toUpperCase(), letter.toLowerCase()])])
|
|
96
120
|
return first.flatMap(a => second.map(b => a + b + code.slice(2)))
|