0.0.8 • Published 5 years ago

microtype v0.0.8

Weekly downloads
-
License
MIT
Repository
-
Last release
5 years ago

MicroType

(minitype)

A small typecheck thing.

Now there are functions __$$ and _$$: they return true if all constraints are satisfied, otherwise false. (_$ and __$ depend on __$$).

The general syntax is:

__$(errorHandler, constraints, value0, constraint0, value1, constraint1, ..., func);
// see actual implementation for defaults in source
// Defaults are in '_$'.

// Where errorHandler is:
// (gets called when any constraint is not satisfied)
Function = () => ... // Throw a TypeError

// Where 'constraints' is:
Object = {
	type: String => ..., // Checks type. (uses typeof)
	func: Function => ..., // A filter function (must return true for the constraint to be satisfied)
	inst: Object => ..., // Checks is value is an instance of specified type (uses instanceof)
}

// Where 'constraint' is:
// 'constraints' define possible fields of 'constraint'.
// (All fields are optional)
Object

// Where value is:
Object

// Where func is:
// (gets called when all constraints are satisfied)
Function

Example:

const { _$ } = require("microtype");

const test = (a, b) => _$(a, {inst: Array}, b, {type: "string", func: x => x ? x.startsWith("Hello") : false}, () => {
	// here, a is an array, b is a string.
	console.log(a[0], b);
});

test([12, 53], "Hello!"); // -> 12 Hello!
test(12, "Hello!"); // -> TypeError!
test([12, 56], "Bye!"); // -> TypeError!
test("hello", null); // -> TypeError

Changelog

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.3

5 years ago

0.0.4

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago