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

v1.0.3

Published

the run-time type infomation of object

Downloads

17

Readme

type-info npm

Build Status Code Climate Test Coverage downloads license

This run-time type information can be streamable via using toObject() method to generate the parametric type object, or using JSON.stringify directly. It can also be used to validate the value of the type.

Creating the new type is very simple and easy through this framework. Just we need to understand the basic concepts of the following.

Concepts

  • Type Factory: collects all type info classes and objects.
  • Primitive Types
    • All registered types are primitive types.
  • Virtual Types
    • It's a object of a primitive type.
    • It can not be registered.
    • It could be unlimited number of virtual types.
  • Type Attributes: first determine(define) these attributes of the type, before creating a new type. It's used to constrain the Type. All types have the name and required attributes.
    • name (string): the type name.
      • required = true: it must be required.
      • enumerable = false: it can not be enumerable.
    • required (boolean): the attribute whether is required(must be exists, not optional).
  • Value: the value with corresponding to the type information.

Quick starts

  1. npm install type-info

    var TypeFactory = require('type-info')
  2. get the type object

    var TNumber = TypeFactory('Number')
  3. create the virtual type

    var TPositiveNumber =
      TypeFactory('Number', {min:0, cached: 'PositiveNumber'})
  4. validate a value

    assert.notOk(TPositiveNumber.isValid(-1))
    assert.ok(TPositiveNumber.isValid(1))
  5. create the value

    var n = TPositiveNumber.create(123)
    assert.ok(n.isValid())
    assert.equal(Number(n) + 3, 126)
    var bool = TypeFactory('Boolean').create(true)
    assert.equal(Number(bool), 1)

Known Types:

  • String Type
    • min: the minimum string length
    • max: the maximum string length
  • Number Type
    • min: the minimum number
    • max: the maximum number
    • Integer Type
    • Float Type
  • Date Type
    • min: the minimum date to limit
    • max: the maximum date to limit
  • Boolean Type
    • It is a special number type. you can cast to number.
    • 0 means false, 1 means true.
    • boolNames: the boolean value string names.
      • defaults to {true:['true', 'yes'], false: ['false,'no']}
      • enumerable: false
  • Function Type
    • globalScope: the set of variables this function can access to.
    • global: the set of local functions this function can access to. It can not be exported.
  • [Array Type][array-type]
    • min: the minimum array length to limit
    • max: the maximum array length to limit
    • of: the each elment's type of the array to limit.
  • Object Type
    • attributes: the object attribute list.
      • It's used to constrain the value of the object.
  • Class Type: a special object type. the value of class is the constructor of this class.
    • TODO: not fined.

Cache the Virtual Types

Install cache-factory first.

npm install cache-factory

TypeFactory = require 'type-info'
cacheable = require 'cache-factory'

# apply the cache-able ability to TypeFactory
cacheable TypeFactory

# now cache the virtual types with name.
passwordType = TypeFactory 'String', min:6, cached: {name: 'Password'}
# or no named it:
passwordType1 = TypeFactory 'String', min:6, cached: true

p2 = TypeFactory 'String', min:6, cached: true
p3 = TypeFactory 'String', min:6, cached: {name: 'Password'}
assert p2 is passwordType1
assert p3 is passwordType
assert passwordType isnt passwordType1

more detail see cache-factory

Changes

v1.0.0

  • use the abstract-type package.
  • The pacakge just collects types only.

v0.8.0

  • [Type] $attributes to hold the type's meta attributes.
    • the type's meta attributes definition in the src/attributes directory.
  • [ObjectType] add AttributeType to defineAttribute
  • [ObjectType] use the primitive type of attribute if possible.
  • [Type] isSame method to compare parametric object
  • remove the parent attribute
  • remove the encoding from type-info
    • remove Type::encode method
    • remove Type::decode method
  • [Type] Type(aTypeName, aOptions):
    • get the global instance if no aOptions or aOptions is same as the original default value of attributes.
    • create a new type object instance else

v0.7.0

  • add JSON.stringify(aTypeObject) supports
  • add Type.createFrom(string, encoding) static(class) method
  • remove Type::_encode method
  • remove Type::_decode method
  • remove Type::_isEncoded method
  • add Value::_encode, Value::_decode optional methods
    • make sure the value can be converted to json correctly.
  • add Value::fromJson(string)
  • add Value::createFromJson(string)
  • add Value::toString(aOptions)
  • add Value.tryGetTypeName(aValue)
  • Type::mergeOptions(options, exclude, serialized) distinguish serialized and non-serialized parameters

TODO

Usage

See abstract-type.

TypeFactory  = require 'type-info'
Value = TypeFactory.Value

# get number type info object
# you can treat it as a global temporary type object.
num = TypeFactory 'Number'
assert.equal num, TypeFactory('Number')

# get a new number type info object(Virtual Type):
# create a virtual type object always if the options exists:
number = TypeFactory 'Number', min:1, max:6
assert.notEqual number, num

# get Number Type Class(Primitive Type):
NumberType = TypeFactory.registeredClass 'Number'

# create a number value:
n = Value(2) # try to guess the value type.
# n = Value(2, number)
# n = number.create(2)
# n = number.createValue(2)

assert.ok    n.isValid()
assert.equal n+2, 4
assert.throw number.validate.bind(number, 13)

n.assign 5 # n = 5
assert.equal n+2, 7

# assign a new type options to the number type:
number.assign min:3, max:10
# or number.min = 3, number.max = 10

API

See abstract-type.

License

MIT