Hyponym brings powerful subtypes to JavaScript. Inspired by Haskell's type system, Hyponym is capable of expressing monads and other such constructs along with the more simple literal data types and primitives of JavaScript.
npm install hyponymHyponym is designed to have a very simple API and even provides a prelude of common types that you might need everyday.
var Hyponym = require('hyponym');
var number = 1890,
string = "kevin spacey",
object = {};
Hyponym.Number.validate(number); // true
Hyponym.Number.validate(string); // false
Hyponym.String.validate(string); // true
Hyponym.String.validate(object); // false
Hyponym.Object.validate(object); // true
Hyponym.Object.validate(number); // falseHyponym(options) -> Hyponym instance
Hyponym.baseType(type) -> Hyponym instance Hyponym#name() -> String
Hyponym#subtype(options) -> Hyponym instance
Hyponym#supertype() -> Hyponym instance
Hyponym#validate(value Any) -> Boolean
Hyponym#assert(value Any, returns Boolean):
if not Hyponym.validate(value)
if returns then returns Error
else throws Error
Hyponym#serialize(value Any) -> AnyPrimitive types can be found on the Hyponym namespace directly, such as Hyponym.Number.
- Number
- String
- Boolean
- Object
- Date
- Array
- RegExp
- Function
- Error
Complex types can be required from Hyponym by requiring them from the lib directory, like so:
var Alphanumeric = require('hyponym/lib/alphanumeric'),
ArrayList = require('hyponym/lib/array-list');- Alphanumberic
- ArrayList
- Choice (one of a list of types)
- Either
- Integer
- Maybe
- Range
- Struct
- Tuple
Is your favorite type missing? Open an issue and I'll see what I can do for you. Or go pro and submit an implementation of your own. (See Contributing.)
Contributions are incredibly welcome as long as they are standardly applicable and pass the tests (or break bad ones). Tests are written in Mocha and assertions are done with the Node.js core assert module.
# running tests
npm run testFollow me on Twitter for updates or just for the lolz and please check out my other repositories if I have earned it. I thank you for reading.