Named Tuple
Named tuples are small immutable data structures which are easy to define and use. Inspiration comes from the Python collections module.
Installation
npm install --save named-tuple
Usage
const createNamedTuple = const Point = const p = 12 16 // Property accesspx // 12py // 16 // Iterator ...p // [ 12, 16 ] // Keys & valuesObject // [ 'x', 'y' ]Object // [ 12, 16 ] // Immutablepx = 48 // TypeError: Cannot assign to read only property 'x' of #<Point>pz = 64 // TypeError: Can't add property z, object is not extensible // Beautiful generated sourcePoint// class Point {// constructor (x, y) {// this.x = x// this.y = y// Object.freeze(this)// }// }PointprototypeSymboliterator// function* () {// yield this.x// yield this.y// }
API
createNamedTuple(name, ...props)
Create a new named tuple with the provided class name (name
) and instance properties (props
). Returns the newly defined class.
Immutability
When instantiating a new instance of a named tuple, the object immediately gets frozen using Object.freeze
.
Nothing can be added to or removed from the properties set of a frozen object. Any attempt to do so will fail, either silently or by throwing a TypeError exception (most commonly, but not exclusively, when in strict mode).
Valid class- and property names
This module should be considered live source scaffolding and it won't do anything to try and validate your class- and property names. An invalid name could result in an error, or worse. Never use user supplied data to create the classes.