oxedyne/daimond/dev/verify_starters.mjs
2.1 KiB, 1 run
created by r2519314175:693, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | // The starter tags, in every language. |
| 2 | // |
| 3 | // They are the one place the interface puts a word INTO the user's data. A tag |
| 4 | // is lowercased and space-collapsed by the store, so a starter that is not |
| 5 | // already in that form would draw a chip reading one thing and file another -- |
| 6 | // and the chip would then not match the tag it just made, so clicking it again |
| 7 | // would offer to add it a second time. The app normalises them defensively; |
| 8 | // this makes sure no locale is relying on that. |
| 9 | import fs from 'node:fs'; |
| 10 | import path from 'node:path'; |
| 11 | import { fileURLToPath } from 'node:url'; |
| 12 | |
| 13 | const HERE = path.dirname(fileURLToPath(import.meta.url)); |
| 14 | const I18N = path.join(HERE, '..', 'www', 'i18n'); |
| 15 | const LOCALES = ['en', 'de', 'es', 'fr', 'ja', 'ko', 'pt-BR', 'zh-Hans']; |
| 16 | |
| 17 | let bad = 0; |
| 18 | const check = (ok, what) => { console.log(`${ok ? 'PASS' : 'FAIL'} ${what}`); if (!ok) bad++; }; |
| 19 | |
| 20 | /// What the store would make of a tag: trimmed, inner whitespace collapsed, |
| 21 | /// lowercased. Mirrors normalise_tags in src/diamond_meta.rs. |
| 22 | const norm = (s) => s.split(/\s+/).filter(Boolean).join(' ').toLowerCase(); |
| 23 | |
| 24 | const MAX_TAG_LEN = 32; // src/diamond_meta.rs |
| 25 | |
| 26 | for (const loc of LOCALES) { |
| 27 | const src = fs.readFileSync(path.join(I18N, `${loc}.js`), 'utf8'); |
| 28 | const m = src.match(/'tag\.starters':\s*'((?:[^'\\]|\\.)*)'/); |
| 29 | check(!!m, `${loc}: declares tag.starters`); |
| 30 | if (!m) continue; |
| 31 | const raw = m[1].replace(/\\'/g, "'").split(',').map(s => s.trim()).filter(Boolean); |
| 32 | check(raw.length === 4, `${loc}: offers four starters (${raw.length}): ${raw.join(' / ')}`); |
| 33 | for (const tag of raw) { |
| 34 | check(norm(tag) === tag, |
| 35 | `${loc}: ${JSON.stringify(tag)} is already as the store would file it${norm(tag) === tag ? '' : ` (would become ${JSON.stringify(norm(tag))})`}`); |
| 36 | check([...tag].length <= MAX_TAG_LEN, |
| 37 | `${loc}: ${JSON.stringify(tag)} fits the ${MAX_TAG_LEN}-character cap (${[...tag].length})`); |
| 38 | } |
| 39 | // Distinct after normalisation, or the pool would offer the same tag twice. |
| 40 | const set = new Set(raw.map(norm)); |
| 41 | check(set.size === raw.length, `${loc}: the four are distinct`); |
| 42 | } |
| 43 | |
| 44 | console.log(bad ? `\n${bad} FAILED` : `\nALL PASS`); |
| 45 | process.exit(bad ? 1 : 0); |