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-factories

v0.3.0

Published

Lots of custom types for robustness and predictability

Downloads

11

Readme

type-factories

Lots of custom types for robustness and predictability

Content

Another type library?

There is so much boiler plate when handling data: Validating, converting, looping on attributes, plucking, dealing with undefined, and so on... This library provides predictable type factories. The capability of setting and getting data is removed from the user and instead data is always preprocessed on setting. Standard methods such as toString, valueOf, toPrecision, toExponential... are defined when needed so that console calls and implicit conversions work as expected (for the most part).

Available factories

makeValueType(BaseType, handler)

Types generated from this factory are scalar/1D in nature. They handle numbers and strings for the most part. The handler parameters allows to set conditions on teh contained data.

import {makeValueType} from 'type-factories';

const Integer = makeValueType(Number, {
  set (target, key, value) {
    if (key === 'value') {
      target.value = value === undefined ? 0 : Math.trunc(Number(value));
    } else {
      target[key] = value;
    }
    return true;
  }
});

Integer(2.4) === 2; // true

const i = new Integer('36.9');
i + i * 2.5 === 126;  // not 129.15

makeDataType(Types)

Types generated from this factory are a mixte bag of Value types. They are objects with strongly typed public properties.

import {makeDataType} from 'type-factories';

const Person = makeDataType({
  name: String,
  age: Number,
  female: Boolean
});

const person = new Person({
  name: 'John Doe',
  age: '55'
});

person.name === 'John Doe'; // true
person.age === 55; // true
person.female === false; // true

person.value = {
  name: 'John H. Doe',
  age: 56
};

person.name === 'John H. Doe'; // true
person.age === 56; // true

person.age = {
  name: 'Patrick Gray',
  age: '10',
};
person.name === 'John H. Doe'; // not 'Patrick Gray'
person.age === 10; // true

### makeListType(Type, handler)

Types from this factory are vectorial/multiD in nature. They handle list of same Value type instances. The handler helps override some default behaviors

import {makeListType} from 'type-factories';

const Players = makeListType({
  number: Number,
  name: String
});

const players = new Players({
  number: 1,
  name: 'Kasparov'
},
{
  number: 2,
  name: 'Karpov'
});

players.push({
  number: '3',
  name: 'Fischer'
});

players.length === 3;
players[2].number === 3; // not '3'

License

type-factories is MIT licensed.

© 2017 Jason Lenoble