microtype

0.0.8 • Public • Published

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: [14th of September, 2020]: Added _$$ and __$$. Also fixed some typos in README.md.

  • 0.0.7: [14th of September, 2020]: Internal changes. Moved constraint definitions to a separate constant.

  • 0.0.6: [14th of September, 2020]: Fixed README.md Dates were incorrect, Some example issues.

  • 0.0.5: [13th of September, 2020]: Added Changelog. You are looking at it right now.

  • 0.0.4: [13th of September, 2020]: Moved constraint definition to _$. Now you can have custom constraint types (by using __$)

  • 0.0.3 [12th of September, 2020]: API change. Added support for functions as constraints. Looks like this: _$(a, {type: "string"}, b, {func: x => x < 10}, () => "a isd a string, b is less than 10")

  • 0.0.2 [10th of September, 2020]: Added README.md

  • 0.0.1 [10th of September, 2020]: Initial version. Only support type checking: like so _$(a, "string", b, "number", () => "a is a string, b is a number")

Readme

Keywords

none

Package Sidebar

Install

npm i microtype

Weekly Downloads

0

Version

0.0.8

License

MIT

Unpacked Size

8.87 kB

Total Files

7

Last publish

Collaborators

  • somerandomdev49