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

typejs-pck

v1.0.1

Published

## Install

Downloads

4

Readme

:warning: WIP

Install

npm i --save typejs-pck

Use

import { defineType, type, typeList, typeVar } from 'typejs-pck';

API

Define a new schema type

defineType(name: String, schema: Object)

const UserType = defineType('UserType', {
  id: 'Number',
  username: 'String',
  email: 'Maybe String',
  premium: 'Boolean',
});

A schema is only an object but with an optional key __typeName. This key is used for error message for a better development experience.

You can do:

const UserType = {
  __typeName: 'UserType',
  id: 'Number',
  username: 'String',
  email: 'Maybe String',
  premium: 'Boolean',
};

But for code flexibility, I recommend to use defineType API.

Check schema type

type(schemaType: Object)(element: Object, typeOptions: Object)

const dummyUser = {
  id: 1,
  username: 'bltnico',
  email: '[email protected]',
  premium: 'yes',
};

type(UserType)(dummyUser);

// [UserType] Invalid type for 'premium' (Boolean) with value: yes

Check variable type

typeVar(schemaString: String)(elementString: String, typeOptions: Object)

typeVar('String')('bltnico');

Check a list

typeList(schemaArray: Array)(elementArray: Array, typeOptions: Object)

function doSomething(str, bool) {
  typeList(['String', 'Boolean'])(arguments);
  // do really something
}

Options

  • strict (default to true): Compare length and key name
  • fatal (default to false): When is true, an error throw an exception

Examples

Type

  • String: 'String'
  • Number: 'Number'
  • Boolean: 'Boolean'
  • Object: 'Object'
  • Array: 'Array'
  • Null or string: 'Maybe String'
  • An array of Number: '[Number]'
  • A string or an array: 'String | Array'
  • An array with String and Number: '[String | Number]'
  • An array with two keys of Object type: '(Object, Object)'

Or use variable type

import { t } from 'typejs-pck';

const {
  string,
  number,
  bool,
  object,
  array,
  arrayOf,
  oneOf,
  maybe,
} = t;
  • String: string
  • Number: number
  • Boolean: bool
  • Object: object
  • Array: array
  • Null or string: maybe(string)
  • An array of Number: arrayOf(number)
  • A string or an array: oneOf(string, array)
  • An array with String and Number: arrayOf(string, number)

Sample code

import { defineType, type, t } from 'type';
const {
  string,
  number,
  bool,  
  maybe,
  objectStruct,
} = t;

const PictureType = defineType('PictureType', {
  thumb: string,
  full: string,
});

const UserType = defineType('UserType', {
  id: number,
  username: string,
  email: maybe(string),
  premium: bool,
  account: objectStruct({ active: bool }),
  picture: PictureType,
});

const dummyUser = {
  id: 1,
  username: 'bltnico',
  email: '[email protected]',
  premium: false,
  account: { 
    active: true
  },
  picture: {
    thumb: 'https://...',
    full: 'https://...',
  },
};

type(UserType)(dummyUser, { fatal: true });

Don't check type in prod env

import { type as originalType } from 'typejs-pck';

const type = schemaType => {
  return (element, options = {}) => {
    isDevMode() && originalType(schemaType)(element, {
      fatal: true,
      strict: true,
      ...options,
    });
  };
};

type(UserType)(dummyUser);