1.0.2 • Published 7 years ago

@mkrause/constructor v1.0.2

Weekly downloads
1
License
MIT
Repository
github
Last release
7 years ago

constructor.js

Utility library to create JavaScript constructors based on a declarative schema.

Usage:

Example:

    import constructor from '@mkrause/constructor';
    
    const MyConstructor = constructor({ x: String, y: Number });
    const myInstance = MyConstructor({ x: "foo", y: 42 });
    myInstance instanceof MyConstructor; // true
    
    MyConstructor({ x: "foo", y: "42" }); // Throws `InvalidInstanceException`

Each instance stores the validated value in a property accessed through a special symbol constructor.internal:

    const Person = constructor({ name: String, score: Number });
    const john = Person({ name: "John", score: 101 });
    
    john[constructor.internal]; // { name: "John", score: 101 }

This internal property is not supposed to be accessed by the consumer directly. Instead, a constructor should specify a public API through extend():

    const Person = constructor({ name: String, score: Number })
        .extend({
            get name() { return this[constructor.internal].name; },
            get score() { return this[constructor.internal].score; },
        });
    
    const john = Person({ name: "John", score: 101 });
    john.name; // "John"

Any JS constructor can be used as type in a schema, including other constructors defined using constructor():

    const Post = constructor({ author: Person, submitted: Date });
    const post = Post({ author: john, submitted: new Date("2017-01-01") });

Similar libraries

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago