Nobody Pays (for) Magazines

    @meltwater/coerce

    0.3.1 • Public • Published

    @meltwater/coerce

    Build Status A simple javascript package for type checking an object

    Install

    npm i --save @meltwater/coerce

    API reference

    Please see full api documentation here

    Usage

    class ValidatedObject {
        constructor({ value }) {
            if(typeof value !== string) {
                throw new TypeError(`options.value must be a string. Provided value: ${value}`);
            }
    
            this.value = value;
            Object.freeze(this);
        }
    }
    
    const badValue = { value: 1234 };
    coerce(badValue, ValidatedObject, 'Booooooom!');
    // This will throw a TypeError with the message 'Booooooom!'
    
    const goodValue = { value: 'so good' };
    const typedValue = coerce(goodValue, ValidatedObject, 'Booooooom');
    // This will return a new object that is an instanceof ValidatedObject with typedValue.value === 'so good'

    Install

    npm i @meltwater/coerce

    DownloadsWeekly Downloads

    4,941

    Version

    0.3.1

    License

    MIT

    Unpacked Size

    13.1 kB

    Total Files

    11

    Last publish

    Collaborators

    • meltwater-legion