Stateful JavaScript models: share a view, save the full state.
Master-Class lets you define which fields and actions a JavaScript object has in each state. Export a filtered copy for a client, or serialize the full model to pick up where you left off. An approval workflow might show an applicant the decision without the reviewer's notes; a card game might show each player their hand without revealing anyone else's.
Latest tested on Node.js 24. The package remains CommonJS and includes TypeScript declarations.
$ npm install master-classconst M = require('master-class');
const Game = M({players: [{card: M.String({scope: 'owner'})}]});
const game = new Game({players: [{card: 'Ace'}, {card: 'King'}]});
const view = game.export((options, value, path, copy) =>
options.scope === 'owner' && path[1] !== 0 ? 'face down' : copy()
);
console.log(JSON.stringify(view)); // {"players":[{"card":"Ace"},{"card":"face down"}]}
const saved = game.serialize(); // Keep on the server, not in the player message.
console.log(Game.deserialize(saved).players[1].card); // KingThe mapper enforces the visibility rule; scope is your label, not a built-in
permission. Play through the reveal to see how M.State
changes the available moves and how to restore the turn before the reveal.
| Task | API | Guide |
|---|---|---|
| Define a model and validate its fields | M(schema), new Model(data) |
Strict typing |
| Make a move available only in one phase | M.State(), when, delegate |
The state pattern |
| Send a filtered view to a client | model.export(mapper) |
Export a copy |
| Save a checkpoint and resume it later | model.serialize(), Model.deserialize(data) |
Save and restore |
| Observe changes to a model or its children | model.control.on('change', listener) |
Change control |
The guides are published at cardforest.github.io/master-class.
The React site uses Fumadocs and Next.js static export; Markdown in docs/ is
the single source of truth. The old GitBook configuration has been removed.
Building the documentation website requires Node.js 24 or newer.
cd website
npm ci
npm run devOpen http://localhost:3000/master-class/. To validate and export the site:
npm run typecheck
npm run build
npm testThe build writes static HTML, assets, and browser-side search data to website/out/.
Navigation uses ordinary links and full-page loads, without requiring a Next.js
server or its client-side route prefetch transport.
The separate website package does not add dependencies to the published library.
The documentation workflow checks pull requests and deploys updates on master
to GitHub Pages. In repository Settings > Pages, the source must be GitHub Actions.
It can also be deployed with the workflow's Run workflow action.
For TypeScript consumers, install @types/node and include "types": ["node"]
in your tsconfig.json compiler options; the control API extends Node's EventEmitter.
import M = require('master-class');
const Person = M({
name: String,
age: Number,
birthday(years: number) {
this.age += years;
}
});
const person = new Person({name: 'Ada', age: 36});
person.birthday(1);
// person.age = 'wrong'; // TypeScript error, also rejected at runtimePrimitive, object, array, getter, and mutator types are inferred from schemas.
State properties are dynamic and exposed as unknown; narrow them before use.
Only root models expose control, export() and serialize(). Managed arrays
have writable elements but a fixed length per instance.
Use export(mapper?) for a plain copy (formerly snapshot()), or
Model.deserialize(model.serialize()) for a full current-state round-trip.
Exports return unknown: mapped exports can replace or omit fields, so
createInstance(data) returns unknown when the input isn't known to match
the schema. See the serialization guide.
Unsupported schemas (including empty array schemas) throw TypeError.
npm ci
npm run typecheck
npm test
npm run build
npm packTypeScript sources live in src/; the build emits CommonJS JavaScript and declarations
into dist/. npm test builds first and runs the regression suite using Node's
built-in test runner. Browser testing and the legacy Karma, Browserify, JSHint,
and Travis configuration have been removed. GitHub Actions validates Node 24 on
Linux and Windows. npm pack validates types and tests before packaging the build.
The dependency on key-path 0.5.0 uses its bundled TypeScript declarations.
That release must be available in your npm registry for npm ci to succeed.
The repository's index.js forwards to the build for existing relative imports.
Internal source paths have moved from lib/ to src/lib/; compiled internal
modules are under dist/lib/.