feat(js): m01 util and m02 model with tests, fixtures, purity audit
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
5be3cebe71
commit
22c221a042
@@ -0,0 +1,29 @@
|
||||
// m01 util: small helpers used by every later module.
|
||||
|
||||
/** `range(n)` is `[0, 1, ..., n-1]`; `range(0)` is `[]`. */
|
||||
export const range = (n) => Array.from({ length: n }, (_, i) => i)
|
||||
|
||||
/** `xs` without its last element; an empty list stays empty. Never mutates `xs`. */
|
||||
export const dropLast = (xs) => xs.slice(0, -1)
|
||||
|
||||
/** True if some element `x` of `xs` satisfies `eq(x, p)`. `eq` lets callers compare structurally (e.g. `pointEquals`). */
|
||||
export const contains = (xs, p, eq) => xs.some((x) => eq(x, p))
|
||||
|
||||
/** `pipe(x, f, g)` is `g(f(x))`: thread a value through functions left to right. */
|
||||
export const pipe = (x, ...fns) => fns.reduce((acc, f) => f(acc), x)
|
||||
|
||||
/** `compose(f, g)(x)` is `f(g(x))`: functions applied right to left. */
|
||||
export const compose = (...fns) => (x) => fns.reduceRight((acc, f) => f(acc), x)
|
||||
|
||||
/**
|
||||
* Freeze `obj` and everything reachable from it, then return it.
|
||||
* Freezing is the one deliberate "mutation" here: it makes later accidental mutation throw.
|
||||
*/
|
||||
export const deepFreeze = (obj) => {
|
||||
Object.freeze(obj)
|
||||
// Freezing before recursing means cyclic structures stop at the already-frozen node.
|
||||
Object.values(obj).forEach((v) => {
|
||||
if (v !== null && typeof v === 'object' && !Object.isFrozen(v)) deepFreeze(v)
|
||||
})
|
||||
return obj
|
||||
}
|
||||
Reference in New Issue
Block a user