@justai/core 0.43.0 → 0.43.1

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/dist/index.d.ts CHANGED
@@ -33,6 +33,12 @@
33
33
  * ./scroll landing on the kernel, and the spy
34
34
  * ./mark the persona's mark, and adopting a host's
35
35
  * ./face a host persona's face, for a child mounted on it
36
+ * ./random a fair random integer, pick, sample and shuffle — crypto.getRandomValues only
37
+ * ./list one item per line: trim, NFC, limits, duplicates reported (never removed)
38
+ *
39
+ * The last two have NO line in the barrel below, on purpose: they are not shell behaviour, and their
40
+ * generic names (`parse`, `pick`, `sample`) would crowd the namespace every persona imports. They are
41
+ * reached only through their own doors — `@justai/core/random`, `@justai/core/list`.
36
42
  *
37
43
  * The test that decided the boundaries, borrowed from `@justai/screen` — the framework's first
38
44
  * package written modular from its first line: **none of them knows what the others are for.** When
package/dist/index.js CHANGED
@@ -33,6 +33,12 @@
33
33
  * ./scroll landing on the kernel, and the spy
34
34
  * ./mark the persona's mark, and adopting a host's
35
35
  * ./face a host persona's face, for a child mounted on it
36
+ * ./random a fair random integer, pick, sample and shuffle — crypto.getRandomValues only
37
+ * ./list one item per line: trim, NFC, limits, duplicates reported (never removed)
38
+ *
39
+ * The last two have NO line in the barrel below, on purpose: they are not shell behaviour, and their
40
+ * generic names (`parse`, `pick`, `sample`) would crowd the namespace every persona imports. They are
41
+ * reached only through their own doors — `@justai/core/random`, `@justai/core/list`.
36
42
  *
37
43
  * The test that decided the boundaries, borrowed from `@justai/screen` — the framework's first
38
44
  * package written modular from its first line: **none of them knows what the others are for.** When
package/dist/list.d.ts ADDED
@@ -0,0 +1,36 @@
1
+ export declare const MAX_ITEMS = 1000;
2
+ export declare const MAX_GRAPHEMES = 120;
3
+ export interface Entry {
4
+ /** What is shown: trimmed, NFC, at most `maxGraphemes` graphemes. */
5
+ text: string;
6
+ /** The comparison key for duplicates: NFC and lower-cased. */
7
+ key: string;
8
+ }
9
+ export interface Duplicate {
10
+ text: string;
11
+ count: number;
12
+ }
13
+ export interface Parsed {
14
+ entries: Entry[];
15
+ /** Non-empty lines past `maxItems`, left out. */
16
+ overflow: number;
17
+ /** Lines cut down to `maxGraphemes`. */
18
+ shortened: number;
19
+ /** Every value that appears more than once, in first-seen order. Nothing is removed. */
20
+ duplicates: Duplicate[];
21
+ /** A single line holding two or more commas: offer to split it. */
22
+ commaHint: boolean;
23
+ }
24
+ export interface ParseOptions {
25
+ /** How many entries are kept (default 1000). */
26
+ maxItems?: number;
27
+ /** How many graphemes one entry may hold (default 120). */
28
+ maxGraphemes?: number;
29
+ }
30
+ /** How many characters a person sees. */
31
+ export declare const graphemeLength: (s: string) => number;
32
+ export declare function parse(raw: string, options?: ParseOptions): Parsed;
33
+ /** The same text with every duplicate after its first appearance removed. Blank lines go too. */
34
+ export declare function keepOneEach(raw: string): string;
35
+ /** One entry per comma-separated part, for when the person said yes to "Split by commas?". */
36
+ export declare function splitByCommas(raw: string): string;
package/dist/list.js ADDED
@@ -0,0 +1,97 @@
1
+ // THE LIST — what a person typed, turned into entries. No DOM, no product meaning. `@justai/core/list`.
2
+ //
3
+ // queries: core list · one item per line · parse a textarea · trim · NFC · blank lines · duplicates ·
4
+ // keep one each · split by commas · Smith, John · 1000 items · 120 characters · grapheme ·
5
+ // emoji length · maxItems · maxGraphemes
6
+ //
7
+ // Lifted from Name Picker (name-picker/src/lib/list.ts, production since 2026-10-22) when Team Generator
8
+ // became its second consumer. The parsing is the same; two things changed, both on purpose:
9
+ //
10
+ // · the limits are OPTIONS (`maxItems`, `maxGraphemes`) with Name Picker's values as defaults, and
11
+ // · the module no longer says what a duplicate MEANS.
12
+ //
13
+ // ── A DUPLICATE IS A FACT HERE, AND A MEANING ONLY IN A PRODUCT ─────────────────────────────────
14
+ //
15
+ // This file reports that the same normalised line appears more than once — trim, NFC, case-insensitive
16
+ // — and how many times, in first-seen order. It never removes one. What that means is the product's
17
+ // sentence: in a raffle (Name Picker) two lines are two tickets; in Team Generator they are two people
18
+ // who share a name. Neither sentence belongs at this layer, and `keepOneEach` runs only when the person
19
+ // asks for it.
20
+ //
21
+ // ── EACH LINE IS ONE ENTRY ─────────────────────────────────────────────────────────────────────
22
+ //
23
+ // A line is trimmed, normalised to NFC (so "é" typed two ways is one value) and kept if anything is
24
+ // left. Commas are never a separator by themselves — "Smith, John" is one person — so a single line
25
+ // holding two or more commas only raises `commaHint`, and `splitByCommas` breaks it up when asked.
26
+ //
27
+ // ── LIMITS ARE SAID OUT LOUD ─────────────────────────────────────────────────────────────────────
28
+ //
29
+ // Past `maxItems` entries the rest are counted in `overflow`, not silently dropped; a line longer than
30
+ // `maxGraphemes` is cut and counted in `shortened`. Length is in GRAPHEMES — what a person sees as one
31
+ // character — so a family emoji is one, not seven.
32
+ export const MAX_ITEMS = 1000;
33
+ export const MAX_GRAPHEMES = 120;
34
+ /** Commas a person might type between items: Latin, Arabic, fullwidth and ideographic. */
35
+ const COMMAS = /[,،,、]/;
36
+ let segmenter = null;
37
+ const graphemes = (s) => {
38
+ segmenter ??= new Intl.Segmenter(undefined, { granularity: "grapheme" });
39
+ return Array.from(segmenter.segment(s), (g) => g.segment);
40
+ };
41
+ /** How many characters a person sees. */
42
+ export const graphemeLength = (s) => graphemes(s).length;
43
+ const clean = (line) => line.trim().normalize("NFC");
44
+ const keyOf = (text) => text.toLowerCase().normalize("NFC");
45
+ export function parse(raw, options = {}) {
46
+ const maxItems = options.maxItems ?? MAX_ITEMS;
47
+ const maxGraphemes = options.maxGraphemes ?? MAX_GRAPHEMES;
48
+ const entries = [];
49
+ let overflow = 0;
50
+ let shortened = 0;
51
+ for (const line of raw.split(/\r\n|\r|\n/)) {
52
+ let text = clean(line);
53
+ if (!text)
54
+ continue;
55
+ if (entries.length >= maxItems) {
56
+ overflow++;
57
+ continue;
58
+ }
59
+ const g = graphemes(text);
60
+ if (g.length > maxGraphemes) {
61
+ text = g.slice(0, maxGraphemes).join("").trim();
62
+ shortened++;
63
+ }
64
+ entries.push({ text, key: keyOf(text) });
65
+ }
66
+ const counts = new Map();
67
+ for (const e of entries) {
68
+ const d = counts.get(e.key);
69
+ if (d)
70
+ d.count++;
71
+ else
72
+ counts.set(e.key, { text: e.text, count: 1 });
73
+ }
74
+ const duplicates = [...counts.values()].filter((d) => d.count > 1);
75
+ const commaHint = entries.length === 1 && entries[0].text.split(COMMAS).length >= 3;
76
+ return { entries, overflow, shortened, duplicates, commaHint };
77
+ }
78
+ /** The same text with every duplicate after its first appearance removed. Blank lines go too. */
79
+ export function keepOneEach(raw) {
80
+ const seen = new Set();
81
+ const out = [];
82
+ for (const line of raw.split(/\r\n|\r|\n/)) {
83
+ const text = clean(line);
84
+ if (!text)
85
+ continue;
86
+ const k = keyOf(text);
87
+ if (seen.has(k))
88
+ continue;
89
+ seen.add(k);
90
+ out.push(text);
91
+ }
92
+ return out.join("\n");
93
+ }
94
+ /** One entry per comma-separated part, for when the person said yes to "Split by commas?". */
95
+ export function splitByCommas(raw) {
96
+ return raw.split(/\r\n|\r|\n/).flatMap((l) => l.split(COMMAS)).map(clean).filter(Boolean).join("\n");
97
+ }
@@ -0,0 +1,14 @@
1
+ /** Fills the array with uniformly random 32-bit values. The default is `crypto.getRandomValues`. */
2
+ export type RandomSource = (buf: Uint32Array<ArrayBuffer>) => Uint32Array<ArrayBuffer>;
3
+ export declare const cryptoSource: RandomSource;
4
+ /** A uniformly random integer in [0, max). Throws for a max that is not a whole number in [1, 2³²]. */
5
+ export declare function randInt(max: number, source?: RandomSource): number;
6
+ /** One element, every element equally likely. Throws on an empty list rather than returning undefined. */
7
+ export declare function pick<T>(items: readonly T[], source?: RandomSource): T;
8
+ /**
9
+ * `k` distinct elements, in the order they were drawn — a partial Fisher–Yates on a copy.
10
+ * Every k-subset is equally likely, and so is every order of it. The input is never touched.
11
+ */
12
+ export declare function sample<T>(items: readonly T[], k: number, source?: RandomSource): T[];
13
+ /** A new array holding the same elements in a uniformly random order (full Fisher–Yates). */
14
+ export declare function shuffle<T>(items: readonly T[], source?: RandomSource): T[];
package/dist/random.js ADDED
@@ -0,0 +1,72 @@
1
+ // THE RANDOMNESS — one source, four verbs, no DOM. `@justai/core/random`.
2
+ //
3
+ // queries: core random · randInt · pick · sample · shuffle · Fisher–Yates · rejection sampling ·
4
+ // modulo bias · Math.random · crypto.getRandomValues · fair pick · injected source for tests
5
+ //
6
+ // Lifted UNCHANGED from Name Picker (name-picker/src/lib/random.ts, production since 2026-10-22) when
7
+ // Team Generator became its second new consumer — the doctrine's "a layer is proven by its second
8
+ // consumer; move it before it is copied". The bodies below are byte-for-byte the ones that shipped.
9
+ //
10
+ // ── ONE SOURCE, AND IT IS THE BROWSER'S CRYPTOGRAPHIC GENERATOR ──────────────────────────────────
11
+ //
12
+ // Every random choice goes through `randInt`. There is no Math.random here, and a persona that says
13
+ // "picked with your browser's cryptographic random generator" must route ALL of its randomness —
14
+ // decoration included — through this module, or the sentence is only true of some of the page.
15
+ //
16
+ // ── NO MODULO BIAS ────────────────────────────────────────────────────────────────────────────
17
+ //
18
+ // A 32-bit value `% n` favours the low remainders whenever 2³² is not a multiple of n. So the values
19
+ // in the incomplete last block are thrown away and drawn again: `limit` is the largest multiple of
20
+ // n that fits, and anything at or above it is rejected. At most one draw in two is ever rejected
21
+ // (n just over 2³¹); for a list of 1,000 items it is about one in four million.
22
+ //
23
+ // ── NO DOM, NO FRAMEWORK, NO NODE ─────────────────────────────────────────────────────────────
24
+ //
25
+ // `crypto.getRandomValues` exists in every browser and in Node/Bun as a global; nothing else is
26
+ // touched. The source is injectable (`RandomSource`), so a test can drive any function with a fixed
27
+ // sequence and check the rejection path and every boundary without statistics.
28
+ export const cryptoSource = (buf) => crypto.getRandomValues(buf);
29
+ const RANGE = 0x1_0000_0000; // 2³²
30
+ /** A uniformly random integer in [0, max). Throws for a max that is not a whole number in [1, 2³²]. */
31
+ export function randInt(max, source = cryptoSource) {
32
+ if (!Number.isInteger(max) || max < 1 || max > RANGE) {
33
+ throw new RangeError(`randInt: max must be an integer in [1, 2^32], got ${max}`);
34
+ }
35
+ if (max === 1)
36
+ return 0;
37
+ const limit = RANGE - (RANGE % max); // the largest multiple of max that fits in 32 bits
38
+ const buf = new Uint32Array(1);
39
+ for (;;) {
40
+ const x = source(buf)[0];
41
+ if (x < limit)
42
+ return x % max;
43
+ }
44
+ }
45
+ /** One element, every element equally likely. Throws on an empty list rather than returning undefined. */
46
+ export function pick(items, source = cryptoSource) {
47
+ if (items.length === 0)
48
+ throw new RangeError("pick: empty list");
49
+ return items[randInt(items.length, source)];
50
+ }
51
+ /**
52
+ * `k` distinct elements, in the order they were drawn — a partial Fisher–Yates on a copy.
53
+ * Every k-subset is equally likely, and so is every order of it. The input is never touched.
54
+ */
55
+ export function sample(items, k, source = cryptoSource) {
56
+ const n = items.length;
57
+ if (!Number.isInteger(k) || k < 0 || k > n) {
58
+ throw new RangeError(`sample: k must be an integer in [0, ${n}], got ${k}`);
59
+ }
60
+ const a = items.slice();
61
+ for (let i = 0; i < k; i++) {
62
+ const j = i + randInt(n - i, source);
63
+ const tmp = a[i];
64
+ a[i] = a[j];
65
+ a[j] = tmp;
66
+ }
67
+ return a.slice(0, k);
68
+ }
69
+ /** A new array holding the same elements in a uniformly random order (full Fisher–Yates). */
70
+ export function shuffle(items, source = cryptoSource) {
71
+ return sample(items, items.length, source);
72
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@justai/core",
3
- "version": "0.43.0",
3
+ "version": "0.43.1",
4
4
  "description": "The framework-free heart of the persona shell — the load-bearing behaviour (auth capture, reveal, ready) as plain strings, so Astro and Next inject the SAME logic instead of each re-writing it.",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {
@@ -52,6 +52,14 @@
52
52
  "./face": {
53
53
  "types": "./dist/face.d.ts",
54
54
  "default": "./dist/face.js"
55
+ },
56
+ "./random": {
57
+ "types": "./dist/random.d.ts",
58
+ "default": "./dist/random.js"
59
+ },
60
+ "./list": {
61
+ "types": "./dist/list.d.ts",
62
+ "default": "./dist/list.js"
55
63
  }
56
64
  },
57
65
  "publishConfig": {