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

type-instance

v0.0.2

Published

Validate types

Downloads

28

Readme

Type Instance

Description

Don't want to use TypeScript to validate types? Use this!

Examples

const types = require("type-instance");

types.string("hi"); //no throws
types.string(0); //TypeError
types.number(0); //no throws
types.buffer(Buffer.alloc(0)); //no throws
types.object({}); //no throws
types.array([0, "hi", 1], "value", [types.string, types.number], ["arg1", "arg2", "arg3"]); //no throws
types.gen(content => console.log(content), [types.string], ["content"])()//TypeError

How to Use Methods

To use the methods, u need to import the module with require("type-instance") and now u just need to use properties from the imported object, ex: require("type-instance").function

Methods

string(value, name)

Description

Check is value is a string

Arguments

value [Any]

String you want to check

name [String] {Default: "value"}

The value's name

Throws

TypeError if value is not a string

object(value, name)

Description

Check is value is an object

Arguments

value [Any]

Value you want to check

name [String] {Default: "value"}

The value's name

Throws

TypeError if value is not an object

object#notArray(value, name)

Description

Check is value is an object

Arguments

value [Any]

Value you want to check

name [String] {Default: "value"}

The value's name

Throws

TypeError if value is not an object or if value is an array

function(value, name)

Description

Check is value is a function

Arguments

value [Any]

Value you want to check

name [String] {Default: "value"}

The function's name

Throws

TypeError if value is not a function

function#async(value, name)

Description

Check is value is an async function

Arguments

value [Any]

Value you want to check

name [String] {Default: "value"}

The function's name

Throws

TypeError if value is not an async function

function#sync(value, name)

Description

Check is value is a sync function

Arguments

value [Any]

Value you want to check

name [String] {Default: "value"}

The function's name

Throws

TypeError if value is not a sync function

array(value, name, argsType, argsName)

Description

Check is value is an array

Arguments

value [Any]

Value you want to check

name [String] {Default: "value"}

argsType[Array] {Default: []}

The value's name

argsName [Array] {Default: []}

Throws

TypeError if value is not an array

number(value, name)

Description

Check is value is a number

Arguments

value [Any]

Value you want to check

name [String] {Default: "value"}

The value's name

Throws

TypeError if value is not a number

buffer(value, name)

Description

Check is value is a buffer

Arguments

value [Any]

Value you want to check

name [String] {Default: "value"}

The value's name

Throws

TypeError if value is not a buffer

instanceof(constructor, constructorName, valueName)

Description

Create a new function to check does value is an instance of a constructor

Arguments

constructor [Function]

Function to check against

constructorName [String] {Default: "Function"}

Constructor's name

valueName [String] {Not Required}

Value's name

Returns

[Function: f](value)

Description

To check does value is an instance of the constructor

Arguments

value [Any]

Value to check

Throws

TypeError if value is not an instance of the constructor

type(value, name)

Description

Check is value is a type or instance validator

Arguments

value [Any]

Value you want to check

name [String] {Default: "value"}

The value's name

Throws

TypeError if value is not a type validator or not an instance validator

gen(func, argsType, argsName)

Description

Create a new function from old function. When the new function called, it will check arguments do they have right type/are they an instance of right constructor. Then call old function with it's arguments.

Arguments

func [Function]

argsType [Array] {Default: []}

argsName [Array] {Default: []}

Returns

[Function (Anonymous)](...args)

noError(type, value)

Description

Check value without throwing an error

Arguments

type [Function]

Type/instance validator function such as string, object and etc

value [Any]

Value you want to check

Returns

Boolean

new Object(object)

Arguments

object [Object] {Default: {}}

Description

Create new custom object

Example

const customObject = new Object;

customObject.addSetter(function(prop, value, end) { 
    return end(value === "Hello World" ? "No" : value);
}, "first");
customObject.addGetter(function(prop, value, end) {
    return end(value === "Hello World" ? "No" : value);
}, "first");

customObject.object.first = "Hello World";

console.log(customObject.object.first); //No

Object#addSetter(setter, path)

Description

Add setter function

Arguments

setter [Function]

Arguments
prop [String]
value [Any]
set [Function]

path [String] {Default: ""}

Object#addGetter(getter, path)

Description

Add getter function

Arguments

getter [Function]

Arguments
prop [String]
value [Any]
get [Function]

path [String] {Default: ""}

Object#removeSetters(setter, path)

Description

Remove setter functions

Arguments

setter [Function]

path [String] {Default: ""}

Returns

Number

Object#removeGetters(getter, path)

Description

Remove getter functions

Arguments

getter [Function]

path [String] {Default: ""}

Returns

Number

Object#object

Buy me a Coffee

PayPal: https://paypal.me/nekomaru76

Author

NPM: nekomaru76 GitHub: NekoMaru76 Discord: Gaia#9524