@stonyx/orm 0.3.2-alpha.64 → 0.3.2-alpha.65

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/src/utils.ts CHANGED
@@ -20,3 +20,53 @@ export function pluralize(word: string): string {
20
20
 
21
21
  return basePluralize(word);
22
22
  }
23
+
24
+ /**
25
+ * The highest NUMERIC id held by a set of records, or `0` when there is none.
26
+ *
27
+ * ONE COPY, and the duplication it replaces is the reason it lives here. Three
28
+ * near-identical reduces existed at once: `assignRecordId` (server-assigned id
29
+ * selection), `StandaloneDB.create` (src/standalone-db.ts) and the #203 test
30
+ * helper. `docs/improvements.md`'s standing WET Code category prescribes
31
+ * exactly this remedy -- extract into the module that already acts as the
32
+ * shared utility -- and `assignRecordId` already imported `isOrmRecord` from
33
+ * here.
34
+ *
35
+ * NON-NUMBERS ARE SKIPPED RATHER THAN COERCED TO `0`, AND THAT IS STYLISTIC.
36
+ * `StandaloneDB`'s shape mapped them to `0`, which can never beat a seed of
37
+ * `0`. Measured over eleven input classes (`[]`, `1`, `NaN`, `'5'`, `'abc'`,
38
+ * `-3`, `0`, `null`, `undefined`, `Infinity`, and mixed arrays) the two shapes
39
+ * produce IDENTICAL output on every one. In particular `typeof NaN` is
40
+ * `'number'`, so NEITHER shape coerces `NaN` -- both reject it on `NaN > max`,
41
+ * which is `false`. An earlier revision of this code asserted that the skip was
42
+ * what made the `NaN` case work; it is not, the comparison is, and that claim
43
+ * has been removed rather than left standing.
44
+ *
45
+ * WHAT IS LOAD-BEARING is that this is not `Math.max(...ids)`. `Math.max`
46
+ * returns `NaN` if any operand is `NaN`, and a record CAN be held under the key
47
+ * `NaN` -- so the obvious fix assigns `NaN`, lands on that slot and overwrites
48
+ * it, which is abofs/stonyx-orm#203 in a new disguise. Pinned by
49
+ * test/unit/assign-record-id-test.ts AC2; before that file existed the whole
50
+ * suite scored 951/0 under exactly that fix.
51
+ */
52
+ export function maxNumericId(records: { id?: unknown }[]): number {
53
+ return records.reduce((max: number, record) => {
54
+ const { id } = record;
55
+
56
+ return typeof id === 'number' && id > max ? id : max;
57
+ }, 0);
58
+ }
59
+
60
+ /**
61
+ * The message prefix `assignRecordId` throws with when no free id can be
62
+ * derived for a model, and the ONE string `createHandler` matches on to answer
63
+ * `409` instead of letting the rejection reach express's default handler.
64
+ *
65
+ * It lives here rather than in either file because both need it and neither
66
+ * should own a copy: a literal in two places is how the two id coercions in
67
+ * orm-request.ts drifted apart (see `coerceId`). The repo has no error codes
68
+ * and no custom error classes -- 24 bare `throw new Error` sites across `src/`
69
+ * -- so a shared prefix is the narrowest way to make ONE failure distinguishable
70
+ * without inventing an error taxonomy this codebase does not use.
71
+ */
72
+ export const NO_FREE_ID_ERROR = 'Cannot assign record ID: no free id available';