1.0.0 • Published 6 years ago

type-stuff v1.0.0

Weekly downloads
3
License
ISC
Repository
github
Last release
6 years ago

Type Stuff

NPM

Build Status MaintainabilityTest Coverage Known Vulnerabilities

dependencies devDependencies perrDependencies

Basic Usage

import { typeOf } from 'type-stuff';

class Constructor {}

typeOf(new Constructor): 'constructor';

typeOf(3): 'integer';
typeOf(3.1): 'double';
typeOf(Infinity): 'infinity';
typeOf(NaN): 'nan';

typeOf(() => ''): 'lambda';
typeOf(Constructor): 'class';
typeOf(function() {}): 'function';

typeOf(null): 'null';
typeOf(undefined): 'undefined';

Change Null and Undefined

import { typeOf } from 'type-stuff';

typeOf.setNull('empty');
typeOf.setUndefined('empty');

typeOf(null): 'empty';
typeOf(undefined): 'empty';

Add Validator

import { typeOf } from 'type-stuff';

class Constructor {};

typeOf.validate({
  promiseLike(value) { return typeof value.then === 'function'; },
  array(value, typeOf) { return value instanceof Array && `${typeOf(value[0])}[]`; }
});

typeOf({ then() {} }): 'promiseLike';
typeOf([1]): 'integer[]';

Create typeOf

import { typeOfFactory } from 'type-stuff';

const typeOf = typeOfFactory({ /* validators */ });

typeOf({}): 'unknown';

Set Symbols

import { typeOf, typeOfFactory } from 'type-stuff';

  const factorySymbol = {
    [typeOfFactory.type]: 'factory'
  };
  const functionSymbol = {
    [typeOf.type]: 'function'
  };

  typeOf(factorySymbol): 'factory';
  typeOf(functionSymbol): 'function';

Validators

import {
  constructorValidator,
  functionValidator,
  numberValidator,
  originalValidator,
  interfaceValidator
} from 'type-stuff';

class Stuff {}
originalValidator.original(?) === typeof ?;

constructorValidator.constructor(new Stuff): 'stuff';

functionValidator.lambda(() => {}): true;
functionValidator.class(Stuff): true;

numberValidator.interger(1): true;
numberValidator.double(.1): true;
numberValidator.nan(NaN): true;
numberValidator.infinite(Infinity): true;

interfaceValidator.promiseLike({ then() {} }): true;
interfaceValidator.iterableLike({ [Symbol.iteratro]() {} }): true;
interfaceValidator.monadLike({ flatMap() {}, map() {} }): true;
interfaceValidator.functorLike({ map() {} }): true;
interfaceValidator.eventLike({ on() {} } || { addEventListener() {} }): true;