Skip to content

armgit / bento

Public Imported from GitHub
0

Bento, the office suite that fits in a file

slides: pin the count-up number rules from the #584 ruling (rig) (#703)

nyblnetcd14687ced02
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 6013eb52..9a68d495 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -105,6 +105,12 @@ jobs:
# ⌘S, auto-save and Update this file all write through the queue.
run: node scripts/test-slides-save-revisions.ts
+ - name: Slides count-up number rig
+ # #584 ruling: a single dot is a decimal point (12.500), only a
+ # well-formed group with two or more separators is thousands
+ # (1.250.000 settles as authored and counts up grouped).
+ run: node scripts/test-slides-countup-number.ts
+
- name: Typecheck kernel (standalone)
# Apps also typecheck kernel transitively through their imports; this
# catches kernel-only errors under the kernel's own flags. It ALSO
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 653c672b..7959263a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -11,6 +11,13 @@ pre-1.0.
## [Unreleased]
+- **Count-up leaves versions and addresses alone.** On a text box that counts
+ up, a run of digits that isn't a number — a version like `1.2.3`, an address
+ like `192.168.1.1` — used to be animated too and settle rewritten (`1.2.3`
+ became `12.3`). It now stays exactly as typed while the real numbers beside
+ it count up. As before, `12.500` counts as twelve point five and
+ `1.250.000` as one million two hundred and fifty thousand (#584).
+
- **Indent and outdent bullets.** While editing text, Tab moves the bullet
under the caret one level deeper, beneath the one above it, and Shift+Tab
moves it back up; select several lines to move them together. This works on
diff --git a/scripts/test-slides-countup-number.ts b/scripts/test-slides-countup-number.ts
new file mode 100644
index 00000000..0adf7814
--- /dev/null
+++ b/scripts/test-slides-countup-number.ts
@@ -0,0 +1,80 @@
+#!/usr/bin/env node
+// SPDX-License-Identifier: MIT
+// Copyright (c) 2026 The Bento authors
+// How the count-up reads and re-writes an authored number (#584).
+//
+// node scripts/test-slides-countup-number.ts
+//
+// WHAT THIS PROVES — the maintainer's ruling of 2026-10-15 on #584, against
+// slides/src/countnumber.ts, the code present.ts's runCountUp uses:
+// - a SINGLE dot is a decimal point: `12.500` is twelve point five, and the
+// count-up ticks through decimals (`7.811`) on the way to it;
+// - only a well-formed group pattern with TWO or more separators is
+// thousands: `1.250.000` settles as `1.250.000` (it used to settle as
+// `1250.000`), and its frames stay grouped (`625.000`);
+// - the comma conventions #171 fixed keep working, and mixed separators read
+// the last one as the decimal point.
+// Frames are composed exactly as runCountUp composes them: value × progress,
+// written back in the author's own shape.
+
+import { register } from 'node:module'
+register('./lib/ts-resolve-hooks.mjs', import.meta.url)
+const { readNumber, writeNumber } = await import('../slides/src/countnumber.ts')
+
+let failures = 0, checks = 0
+const ok = (cond: boolean, msg: string) => { checks++; if (!cond) { failures++; console.log(` FAIL ${msg}`) } else console.log(` ok ${msg}`) }
+const frame = (raw: string, p: number) => { const s = readNumber(raw); return writeNumber(s.value * p, s) }
+
+console.log('\nthe ruling: one dot is a decimal point\n')
+{
+ const s = readNumber('12.500')
+ ok(s.value === 12.5 && s.decimals === 3 && s.point === '.' && s.group === '', `12.500 reads as 12.5 to three places (${JSON.stringify(s)})`)
+ ok(frame('12.500', 1) === '12.500', `and settles as authored (${frame('12.500', 1)})`)
+ ok(/^\d+\.\d{3}$/.test(frame('12.500', 0.62)), `its frames are decimals, as the ruling says they should be (${frame('12.500', 0.62)})`)
+ ok(readNumber('1.234').value === 1.234, '1.234 is one-point-two-three-four')
+}
+
+console.log('\nthe ruling: two or more well-formed separators are thousands\n')
+{
+ const s = readNumber('1.250.000')
+ ok(s.value === 1250000 && s.group === '.' && s.point === '', `1.250.000 reads as 1,250,000 grouped by dots (${JSON.stringify(s)})`)
+ ok(frame('1.250.000', 1) === '1.250.000', `and settles as authored, not 1250.000 (${frame('1.250.000', 1)})`)
+ ok(frame('1.250.000', 0.5) === '625.000', `its frames stay grouped (${frame('1.250.000', 0.5)})`)
+ ok(frame('12.345.678', 1) === '12.345.678' && readNumber('12.345.678').value === 12345678, 'a two-digit leading group works too')
+ ok(readNumber('1.250.00').point === '.', 'a malformed group (1.250.00) is not read as thousands')
+}
+
+console.log('\nthe comma conventions (#171) and mixed separators\n')
+{
+ ok(readNumber('1,234,567').value === 1234567 && frame('1,234,567', 1) === '1,234,567', '1,234,567 groups by commas')
+ ok(readNumber('1,234').value === 1234, 'a single comma before exactly three digits groups')
+ ok(readNumber('1,5').value === 1.5 && frame('1,5', 1) === '1,5', 'otherwise a comma is the decimal point')
+ ok(readNumber('1.234,5').value === 1234.5 && frame('1.234,5', 1) === '1.234,5', '1.234,5: the last separator is the decimal point')
+ ok(readNumber('1,234.5').value === 1234.5 && frame('1,234.5', 1) === '1,234.5', '1,234.5: likewise')
+}
+
+console.log('\nwhat is not a number stays as authored\n')
+{
+ const { countPlan, isCountable } = await import('../slides/src/countnumber.ts')
+ for (const raw of ['1.2.3', '192.168.1.1', '1,2,3', '1.2,3.4']) ok(!isCountable(raw), `"${raw}" is not a countable number`)
+ for (const raw of ['42', '12.500', '1.250.000', '1,234,567', '1,5', '1.234,5', '1,234.5']) ok(isCountable(raw), `"${raw}" is`)
+ const v = countPlan('Release 1.2.3 shipped to 1.250.000 users')!
+ ok(v(0.5) === 'Release 1.2.3 shipped to 625.000 users' && v(1) === 'Release 1.2.3 shipped to 1.250.000 users',
+ `a version beside a number: only the number counts (${v(0.5)})`)
+ const ip = countPlan('Server 192.168.1.1 handled 12.500 requests')!
+ ok(ip(1) === 'Server 192.168.1.1 handled 12.500 requests' && ip(0.5).startsWith('Server 192.168.1.1 handled '),
+ `an address stays exactly as typed (${ip(0.5)})`)
+ ok(countPlan('Version 1.2.3') === null && countPlan('No digits here') === null, 'text with nothing countable is not animated at all')
+ ok(countPlan('grew 25.')!(1) === 'grew 25.', 'a sentence-ending full stop is kept')
+}
+
+console.log('\nthe count-up uses this code\n')
+{
+ const { readFileSync } = await import('node:fs')
+ const src = readFileSync(new URL('../slides/src/present.ts', import.meta.url), 'utf8')
+ ok(/import \{ countPlan \} from '\.\/countnumber'/.test(src) && /const plan = countPlan\(/.test(src) && !/function (readNumber|writeNumber|countPlan)\(/.test(src),
+ 'present.ts runs its count-up through countnumber.ts countPlan and keeps no copy of its own')
+}
+
+console.log(`\n${checks - failures}/${checks} checks passed`)
+process.exit(failures ? 1 : 0)
diff --git a/slides/src/countnumber.ts b/slides/src/countnumber.ts
new file mode 100644
index 00000000..847018ca
--- /dev/null
+++ b/slides/src/countnumber.ts
@@ -0,0 +1,126 @@
+// SPDX-License-Identifier: MIT
+// Copyright (c) 2026 The Bento authors
+//
+// Reading and writing an AUTHORED number for the count-up (present.ts
+// runCountUp). Pure, so scripts/test-slides-countup-number.ts pins the rules
+// — the maintainer's 2026-10-15 ruling on #584: a single dot is a decimal
+// point (`12.500` is twelve point five); only a well-formed group pattern
+// with two or more separators (`1.250.000`) is thousands.
+
+/**
+ * How a number was WRITTEN, so the count-up can put it back the same way.
+ *
+ * The number must settle exactly as the author typed it. Routing through
+ * `Intl.NumberFormat(navigator.language)` is the tempting fix and the wrong
+ * one: slide content is authored, so the same deck would read `1,234.5` for
+ * one viewer and `1.234,5` for another. Locale follows the viewer for CHROME
+ * only (`PLATFORM.md` §3).
+ */
+export interface NumberShape {
+ value: number
+ decimals: number
+ group: string // separator between thousands, '' if the author used none
+ point: string // decimal separator, '' if the number is an integer
+}
+
+/**
+ * Read an authored number. Separators are genuinely ambiguous, so the rules
+ * are stated rather than guessed:
+ *
+ * - BOTH `.` and `,` present → the LAST one is the decimal point, the other
+ * groups. `1,234.5` → 1234.5, `1.234,5` → 1234.5.
+ * - Only `,` → grouping if there are several (`1,234,567`), or if a single one
+ * is followed by exactly three digits (`1,234`). Otherwise a decimal comma
+ * (`1,23`, `1,2345`).
+ * - Only `.` → a decimal point, with one exception. A single dot reads as a
+ * decimal point, always: a deck writing `1.234` means
+ * one-point-two-three-four, and reading it as grouping would break every
+ * three-decimal number to fix a rarer case. The exception is a well-formed
+ * group pattern, `-?\d{1,3}(\.\d{3}){2,}`, which needs at least two
+ * separators and three digits in every group after the first, with an
+ * optional leading minus: `1.250.000` and `-1.250.000` cannot be a decimal
+ * point under any convention, so they group and settle as the author typed
+ * them rather than as `1250.000`. Anything else with two or more dots,
+ * like `1.2.3` or `192.168.1.1`, stays on the decimal path, because reading
+ * those as groups would rewrite version strings and addresses into numbers
+ * that look legitimate.
+ */
+export function readNumber(raw: string): NumberShape {
+ const dots = (raw.match(/\./g) ?? []).length
+ const commas = (raw.match(/,/g) ?? []).length
+ let point = ''
+ if (dots && commas) point = raw.lastIndexOf('.') > raw.lastIndexOf(',') ? '.' : ','
+ else if (commas) point = commas > 1 || /,\d{3}$/.test(raw) ? '' : ','
+ // Two or more dots group, but only when the token is a well-formed group pattern.
+ // `dots > 1` alone is too broad: it would read `1.2.3` as 123 and
+ // `192.168.1.1` as 19.216.811, which is a version string and an address
+ // being rewritten into plausible-looking numbers. Requiring three-digit
+ // groups after the first keeps those on the documented decimal path.
+ else if (dots) point = /^-?\d{1,3}(\.\d{3}){2,}$/.test(raw) ? '' : '.'
+ const group = point === '.' ? (commas ? ',' : '')
+ : point === ',' ? (dots ? '.' : '')
+ : (commas ? ',' : dots ? '.' : '')
+ const cut = point ? raw.lastIndexOf(point) : -1
+ const whole = (cut >= 0 ? raw.slice(0, cut) : raw).replace(/[.,]/g, '')
+ const frac = cut >= 0 ? raw.slice(cut + 1) : ''
+ return { value: Number(frac ? `${whole}.${frac}` : whole), decimals: frac.length, group, point }
+}
+
+/** Put a number back in the author's own convention. */
+export function writeNumber(value: number, shape: NumberShape): string {
+ const fixed = value.toFixed(shape.decimals)
+ const dot = fixed.indexOf('.')
+ let whole = dot >= 0 ? fixed.slice(0, dot) : fixed
+ const frac = dot >= 0 ? fixed.slice(dot + 1) : ''
+ if (shape.group) whole = whole.replace(/\B(?=(\d{3})+(?!\d))/g, shape.group)
+ return frac ? whole + shape.point + frac : whole
+}
+
+/**
+ * Is this digit run a NUMBER the count-up may animate? Only the shapes
+ * readNumber defines: plain digits, a single separator (a decimal point, or a
+ * comma read either way), a well-formed group pattern (`1,234,567`,
+ * `1.250.000`), or both separators with everything before the last one a
+ * well-formed group (`1.234,5`, `1,234.5`). Anything else — a version
+ * `1.2.3`, an address `192.168.1.1`, `1,2,3` — is not a number, and is left
+ * exactly as the author typed it: animating it would rewrite it (`1.2.3`
+ * settled as `12.3`).
+ */
+export function isCountable(raw: string): boolean {
+ const dots = (raw.match(/\./g) ?? []).length
+ const commas = (raw.match(/,/g) ?? []).length
+ if (dots && commas) {
+ const point = raw.lastIndexOf('.') > raw.lastIndexOf(',') ? '.' : ','
+ const group = point === '.' ? ',' : '.'
+ const cut = raw.lastIndexOf(point)
+ return raw.indexOf(point) === cut && new RegExp(`^\\d{1,3}(\\${group}\\d{3})*$`).test(raw.slice(0, cut))
+ }
+ const n = dots || commas
+ if (n <= 1) return true
+ return new RegExp(`^\\d{1,3}(\\${dots ? '.' : ','}\\d{3})+$`).test(raw)
+}
+
+/**
+ * The count-up for one element's text: a function from progress (0…1) to
+ * the text at that moment, or null when there is nothing to count. Every
+ * countable number runs from 0 to its value in the author's own convention;
+ * every other digit run, and every character between, stays as authored.
+ */
+export function countPlan(final: string): ((p: number) => string) | null {
+ // Separators only count BETWEEN digits, so a sentence ending in a number
+ // ("grew 25.") keeps its full stop instead of having it swallowed and
+ // re-emitted as part of the value.
+ const tokens = [...final.matchAll(/\d+(?:[.,]\d+)*/g)]
+ const shapes = tokens.map((m) => (isCountable(m[0]) ? readNumber(m[0]) : null))
+ if (!shapes.some(Boolean)) return null
+ return (p) => {
+ let out = ''
+ let last = 0
+ tokens.forEach((m, i) => {
+ const s = shapes[i]
+ out += final.slice(last, m.index) + (s ? writeNumber(s.value * p, s) : m[0])
+ last = m.index! + m[0].length
+ })
+ return out + final.slice(last)
+ }
+}
diff --git a/slides/src/present.ts b/slides/src/present.ts
index 388e44c2..e1f9474f 100644
--- a/slides/src/present.ts
+++ b/slides/src/present.ts
@@ -20,6 +20,7 @@ import type { ShowEvent, ShowVerbs } from '../../kernel/src/sync/session.ts'
import { FollowState, laserDue } from './follow'
import { StepState, stepOf, shownAt } from './steps'
import { offlineEnabled } from './update'
+import { countPlan } from './countnumber'
const MORPH_DURATION_DEFAULT = 0.65
/** Set per-deck by doc.present.morphSeconds; clamped to something sane. */
@@ -1724,100 +1725,18 @@ function settleGuarantee(pairs: Array<[HTMLElement, SlideElement]>) {
}
/** Animate every number in the element's text from 0 to its final value. */
-/**
- * How a number was WRITTEN, so the count-up can put it back the same way.
- *
- * The number must settle exactly as the author typed it. Routing through
- * `Intl.NumberFormat(navigator.language)` is the tempting fix and the wrong
- * one: slide content is authored, so the same deck would read `1,234.5` for
- * one viewer and `1.234,5` for another. Locale follows the viewer for CHROME
- * only (`PLATFORM.md` §3).
- */
-interface NumberShape {
- value: number
- decimals: number
- group: string // separator between thousands, '' if the author used none
- point: string // decimal separator, '' if the number is an integer
-}
-
-/**
- * Read an authored number. Separators are genuinely ambiguous, so the rules
- * are stated rather than guessed:
- *
- * - BOTH `.` and `,` present → the LAST one is the decimal point, the other
- * groups. `1,234.5` → 1234.5, `1.234,5` → 1234.5.
- * - Only `,` → grouping if there are several (`1,234,567`), or if a single one
- * is followed by exactly three digits (`1,234`). Otherwise a decimal comma
- * (`1,23`, `1,2345`).
- * - Only `.` → a decimal point, with one exception. A single dot reads as a
- * decimal point, always: a deck writing `1.234` means
- * one-point-two-three-four, and reading it as grouping would break every
- * three-decimal number to fix a rarer case. The exception is a well-formed
- * group pattern, `-?\d{1,3}(\.\d{3}){2,}`, which needs at least two
- * separators and three digits in every group after the first, with an
- * optional leading minus: `1.250.000` and `-1.250.000` cannot be a decimal
- * point under any convention, so they group and settle as the author typed
- * them rather than as `1250.000`. Anything else with two or more dots,
- * like `1.2.3` or `192.168.1.1`, stays on the decimal path, because reading
- * those as groups would rewrite version strings and addresses into numbers
- * that look legitimate.
- */
-function readNumber(raw: string): NumberShape {
- const dots = (raw.match(/\./g) ?? []).length
- const commas = (raw.match(/,/g) ?? []).length
- let point = ''
- if (dots && commas) point = raw.lastIndexOf('.') > raw.lastIndexOf(',') ? '.' : ','
- else if (commas) point = commas > 1 || /,\d{3}$/.test(raw) ? '' : ','
- // Two or more dots group, but only when the token is a well-formed group pattern.
- // `dots > 1` alone is too broad: it would read `1.2.3` as 123 and
- // `192.168.1.1` as 19.216.811, which is a version string and an address
- // being rewritten into plausible-looking numbers. Requiring three-digit
- // groups after the first keeps those on the documented decimal path.
- else if (dots) point = /^-?\d{1,3}(\.\d{3}){2,}$/.test(raw) ? '' : '.'
- const group = point === '.' ? (commas ? ',' : '')
- : point === ',' ? (dots ? '.' : '')
- : (commas ? ',' : dots ? '.' : '')
- const cut = point ? raw.lastIndexOf(point) : -1
- const whole = (cut >= 0 ? raw.slice(0, cut) : raw).replace(/[.,]/g, '')
- const frac = cut >= 0 ? raw.slice(cut + 1) : ''
- return { value: Number(frac ? `${whole}.${frac}` : whole), decimals: frac.length, group, point }
-}
-
-/** Put a number back in the author's own convention. */
-function writeNumber(value: number, shape: NumberShape): string {
- const fixed = value.toFixed(shape.decimals)
- const dot = fixed.indexOf('.')
- let whole = dot >= 0 ? fixed.slice(0, dot) : fixed
- const frac = dot >= 0 ? fixed.slice(dot + 1) : ''
- if (shape.group) whole = whole.replace(/\B(?=(\d{3})+(?!\d))/g, shape.group)
- return frac ? whole + shape.point + frac : whole
-}
-
function runCountUp(node: HTMLElement) {
const inner = node.querySelector<HTMLElement>('.bento-text-inner') ?? node
- const final = inner.textContent ?? ''
- // Separators only count BETWEEN digits, so a sentence ending in a number
- // ("grew 25.") keeps its full stop instead of having it swallowed and
- // re-emitted as part of the value.
- const tokens = [...final.matchAll(/\d+(?:[.,]\d+)*/g)]
- if (!tokens.length) return
- const shapes = tokens.map((m) => readNumber(m[0]))
+ // what animates and what stays as authored: countnumber.ts countPlan
+ const plan = countPlan(inner.textContent ?? '')
+ if (!plan) return
const state = { p: 0 }
anim.to(state, {
p: 1,
duration: 1.15,
delay: 0.15,
ease: 'power2.out',
- onUpdate() {
- let out = ''
- let last = 0
- tokens.forEach((m, i) => {
- out += final.slice(last, m.index)
- out += writeNumber(shapes[i].value * state.p, shapes[i])
- last = m.index! + m[0].length
- })
- inner.textContent = out + final.slice(last)
- },
+ onUpdate() { inner.textContent = plan(state.p) },
})
}