Skip to content

A simple javascript package for type checking an object

License

Notifications You must be signed in to change notification settings

meltwater/coerce

Folders and files

NameName
Last commit message
Last commit date

Latest commit

author
Joe Theriault
Jul 8, 2021
5578a5c · Jul 8, 2021

History

28 Commits
Jul 8, 2021
Oct 7, 2019
Jul 8, 2021
Jul 8, 2021
Jul 8, 2021
Jul 11, 2019
Jul 8, 2021
Jan 17, 2019
Jan 17, 2019
Jul 8, 2021
Sep 23, 2020
Jul 8, 2021
Jan 17, 2019
Jan 17, 2019
Jul 8, 2021
Jul 8, 2021
Jul 8, 2021

Repository files navigation

@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'