npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tychecker

v0.7.0

Published

Check data type

Downloads

22

Readme

tychecker - a type checker

⚠️ tycheck is still in development, originally created for shortlnk project, it evolve for the moment at the same time as shortlnk. But its development is to think for a general use and integrable by other projects.

Features

  • [x] String Validator Function
  • [x] Object Validator Function
    • [x] Object Entry Validator Function
    • [x] Object Entry Validator for multiple keys
  • [x] Number Validator Function
    • [x] Number Validator Function support Big Int
  • [x] Date Validator Function
  • [x] Array Validator Function
    • [x] Array Entry Validator Function
  • [-] Pipeline System
  • [ ] Create configuration with OpenAPI file

Installation guide

npm install tychecker

Documentation

global uses

import on CommonJS

const { stringValidator } = require("tychecker");

import on EcmaScript

import { stringValidator } from "tychecker";

simple example

const str = "Hello World";
const { length } = str;

const validator = stringValidator({
  equLength: length,
});

console.log(validator(str)); // true

References

Functions

stringValidator(config)

Parameter config StringValidatorConfig | Properties | Type | Required | Description | |------------|--------|----------|----------------| | equLength | Number | | exact string length | | minLength | Number | | minimum string length | | maxLength | Number | | maximum string length | | regex | Regex | | Applying regex test |

Return StringValidatorFn

const str = "Hello World!";
const validator = stringValidator({ equLength: str.length, regex: /Hello/ });
validator(str);

objectValidator(config)

Parameters config ObjectValidatorConfig | Properties | Type | Required | Description | |------------------|----------------------------------------------------------------------------------------------------------------|----------|----------------------------------------| | allowEmptyObject | Boolean | | allow empty object | | minLength | Number | | minimum keys count | | maxLength | Number | | maximum keys count | | equLength | Number | | exact keys count | | entries | EntryObjectInstance[] | EntryObjectValidatorConfig[] | | Target one key of object for testing data type |

Return ObjectValidatorFn

const obj = { name: "bob", age: 24 };
const validator = objectValidator({
  allowEmptyObject: true,
  equKeys: Object.keys(obj).length,
  entries: [entryObjectValidator({
    key: "name",
    required: true,
  })],
});
const result = validator(obj);

entryObjectValidator(config)

Parameter config EntryObjectValidatorConfig | Properties | Type | Required | Description | |------------|------------------------------------------------------|----------|---------------------------------------------------------------------| | key | ObjectKeyType | ObjectKeyType[] | symbol | yes | Target one key of object | | Required | Boolean | | If key is required on object | | dataType | DataType | DataType[] | | Check target value type | | validator | ValidatorFn | | Apply Validator function | | validators | ValidatorFn[] | | Apply each Validator function, process pass when once Validator return true |

Return EntryObjectInstance

const obj = { name: "bob", age: 24 };
const entryObjectValidatorFn = entryObjectValidator({
  key: "name",
  required: true,
  dataType: "string",
});
const validator = objectValidator({
  entries: [entryObjectValidatorFn],
});
const result = validator(obj);

dataTypeChecker(data, types)

Parameter data any

Parameter types DataType | DataType[]

Return Boolean

const str = "Hello World!";
dataTypeChecker(str, "string");

numberValidatorFunction(config)

Parameter config NumberValidatorConfig | Properties | Type | Required | Description | |--------------------|----------------------|----------|------------------------------------------------------------------| | equAt | Number | BigInt | | Equal at entry number|bigint | | minAt | Number | BigInt | | Minimum at entry number|bigint | | maxAt | Number | BigInt | | Maximum at entry number|bigint | | transformStringTo | 'number' | 'bigint' | | Transform entry string to number or bigint before apply validation tests | | allowBigInt | Boolean | | Allow BigInt type on entry | | mustBeBigInt | Boolean | | Entry must be a BigInt                            | | allowFloat | Boolean | | Allow Float on entry | | allowInfinite | Boolean | | Allow Infinite on entry | | allowNoSafeInteger | Boolean | | Allow No Safe Integer on entry | | allowNegatifAmout | Boolean | | Allow Negatif Amount on entry |

Return NumberValidatorFn

const num = 5;
const validator = numberValidator({ equAt: num });
validator(str);

dateValidator(config)

Parameter config DateValidatorConfig | Properties | Type | Required | Description | |--------------------|----------------------|----------|-----------------| | transformDate | Boolean | | transform entry to Date | | equAt | Date | Number | | Equal to date | | minAt | Date | Number | | Minimum to date | | maxAt | Date | Number | | Maximum to date |

Return DateValidatorFn

const date = new Date('2023-06-04T02:23:08.719Z');
const validator = dateValidator({ equAt: date });
validator(str);

Objects

regex

| Property | Type | | -------- | ----- | | email | Tegex |

const { stringValidator, regex } = require("tycheck");

stringValidator({
  regex: regex.email,
});

Types

DataType

Type [String] - string | number | array | object | bigint| function| symbol| undefined | date| null| boolean | regex

StringValidatorFn

Type [Function] - (str: string) => boolean

ObjectValidatorFn

Type [Function] - (obj: any) => boolean

EntryObjectInstanceValidatorFn

Type [Function] - (obj: any) => boolean

NumberValidatorFn

Type [Function] - (num: string | number | bigint) => boolean

DateValidatorFn

Type [Function] - (date: Date | number | string) => boolean

ValidatorFn

Type [Function] - StringValidatorFn | ObjectValidatorConfig | NumberValidatorFnDateValidatorFn

ObjectKeyType

Type [Object] - string | number | symbol

ObjectValidatorConfig

Type [Object] | properties | Type | Required | Description | |------------------|----------------------------------------------------------------------------------------------------------------|----------|----------------------------------------| | allowEmptyObject | Boolean | | allow empty object | | minLength | Number | | minimum keys count | | maxLength | Number | | maximum keys count | | equLength | Number | | exact keys count | | entries | EntryObjectInstance[] | EntryObjectValidatorConfig | | Target one key of object for testing data type |

EntryObjectValidatorConfig

Type [Object] | Property | Type | Required | Description | |------------|------------------------------------------------------|----------|---------------------------------------------------------------------| | key | ObjectKeyType | ObjectKeyType[] | regex | yes | Target one key of object | | Required | Boolean | | If key is required on object | | dataType | DataType | DataType[] | | Check target value type | | validator | ValidatorFn | | Apply Validator function | | validators | ValidatorFn[] | | Apply each Validator function, process pass when once Validator return true |

EntryObjectInstance

type [Object] | Property | Type | Required | readonly | Description | |-------------|----------|----------|----------|---------------------------------------------------| | _tyInstance | Boolean | yes | yes | Object created by entryObjectValidator function | | validator | Function | yes | yes | Apply Validator function on target value |

StringValidatorConfig

type [Object]

| Property | Type | Required | descriptio | | --------- | ------ | -------- | --------------------------------- | | equLength | Number | | Equal string length | | minLength | Number | | Minimum string length | | maxLength | Number | | Maximum string length | | regex | Regex | | Apply Regex test method on string |

NumberValidatorConfig

type [Object]

| Properties | Type | Required | Description | |--------------------|----------------------|----------|------------------------------------------------------------------| | equAt | Number | BigInt | | Equal at entry number|bigint | | minAt | Number | BigInt | | Minimum at entry number|bigint | | maxAt | Number | BigInt | | Maximum at entry number|bigint | | transformStringTo | 'number' | 'bigint' | | Transform entry string to number or bigint before apply validation tests | | allowBigInt | Boolean | | Allow BigInt type on entry | | mustBeBigInt | Boolean | | Entry must be a BigInt                            | | allowFloat | Boolean | | Allow Float on entry | | allowInfinite | Boolean | | Allow Infinite on entry | | allowNoSafeInteger | Boolean | | Allow No Safe Integer on entry | | allowNegatifAmout | Boolean | | Allow Negatif Amount on entry |

DateValidatorConfig

type [Object]

| Properties | Type | Required | Description | |--------------------|----------------------|----------|-----------------| | transformDate | Boolean | | transform entry to Date | | equAt | Date | Number | | Equal to date | | minAt | Date | Number | | Minimum to date | | maxAt | Date | Number | | Maximum to date |

Example

Multiple type Object validator

const { objectValidatorn entryObjectValidator } = require('tychecker');

const obj = (i) => ({
    foo: ['bar', 1, {}][i]
});

const validator = objectValidator({
    entries: [
        entryObjectValidator({
            key: 'foo',
            dataType: ['string', 'number']
        })
    ]
})

validator(obj(0)); // true
validator(obj(1)); // true
validator(obj(2)); // false

Deep data type Validator

const { objectValidator, entryObjectValidator, stringValidator } = require("tychecker");

const obj = {
  foo: "bar",
};

const validator = objectValidator({
  entries: [
    entryObjectValidator({
      key: "foo",
      dataType: "string",
      validator: stringValidator({
        minLength: 2,
      }),
    }),
  ],
});

validator(obj); // true

Deep Object Validator

const { objectValidator, entryObjectValidator } = require("tychecker");

const deepObject = {
  result: {
    foo: "bar",
  },
};

const validator = objectValidator({
  entries: [
    entryObjectValidator({
      key: "result",
      validator: objectValidator({
        entries: [
          entryObjectValidator({
            key: "foo",
            equKeys: 1,
          }),
        ],
      }),
    }),
  ],
});

validator(deepObject); // true

Use Object instead of entryObjectValidator function

const { objectValidator } = require("tychecker");

const obj = {
  foo: "bar",
};

const validator = objectValidator({
  entries: [{
    key: "foo",
    equKeys: 1,
  }],
});

validator(obj)

Multiple keys selector on entry validator function

const { entryObjectValidator } = require("tychecker");

const obj = {
  hello: 'world',
  foo: "bar",
};

const validator = entryObjectValidator({
  entries: [
    entryObjectValidator({
      key: ['hello', 'foo'],
      dataType: 'string'
    }),
  ]
});

validator(obj);

Credit

© 2023 - MIT