A restricted Struct type for JavaScript.
const struct = require('struct-type');
const t = struct.types;
const Point2d = struct('Point2d', {
x: t.Any,
y: t.Any
});
// Make new instances with `.make`
const p1 = Point2d.make({ x: 1, y: 2 });
const p2 = Point2d.make({ x: 2, y: 2 });
// Update fields with `set<Field>`.
// This constructs a new prototype-based shallow copy (with Object.create):
const p1_2 = p1.setX(2);
p1; // ==> { x: 1, y: 2 }
p1_2; // ==> { x: 2, __proto__: p1 }
// Flatten structs into regular objects to pass to APIs expecting regular JS objects:
p1_2.toObject();
// ==> { x: 2, y: 2, __proto__: null }
// Each struct gets its own type function:
const Line = struct('Line', {
start: Point2d.type,
end: Point2d.type
});
const line = Line.make({ start: p1, end: p2 });
The officially supported way of getting struct-type is through npm:
$ npm install struct-type
NOTE
If you don't have npm, you'll need to install Node.js in your system before installing struct-type.
A tool like Browserify or Webpack can be used to run struct-type in platforms that don't implement Node-style modules, like the Browser.
struct-type is supported in all platforms that support ECMAScript 5.
NOTE
For platforms that don't support ECMAScript 5, (like IE8 and 9) the es5-shim library can be used to provide the additional runtime support.
If you think you've found a bug in the project, or want to voice your frustration about using it (maybe the documentation isn't clear enough? Maybe it takes too much effort to use?), feel free to open a new issue in the Github issue tracker.
Pull Requests are welcome. By submitting a Pull Request you agree with releasing your code under the MIT licence.
You can join the Gitter Channel for quick support. You may also contact the author directly through email, or Twitter.
Note that all interactions in this project are subject to Origami Tower's Code of Conduct.
struct-type is copyright (c) Quildreen Motta 2016, and released under the MIT licence. See the LICENCE
file in this repository for detailed information.