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

aassert

v0.2.3

Published

Type assertion library for js. Both primitive and custom types supported.

Downloads

5

Readme

#aassert

V: 0.2.3

is a library for defensive programming through javascript ducktype checking. The idea is to check the strictly minimum requirements for a set of values. It can be used as:

  • aa.A: a comprenhesive validator (return an object whose keys are the invalid values, and their value the name of the error). When used in primitive values it will return undefined if there is no error.
  • aa.B: a boolean compliance checker (return true if the conditions are met and false otherwise). This is simply a convenience wraper for aa.a.
  • aa.E: as a super anal type asserter which will throw a type error if the conditions aren't met. As expected, the anal way allows chaining.

##Installation

bower install aassert

##Usage

###Primitives (Out of the box types) The primitive types supported by aassert are: number, boolean, string, array, object, function and undefined. null is treated as undefined

These primitives have the following shorthand names:

number: n
boolean: b
string: s
array: a
object: o
function: f
undefined: u

To check if a value value is a of type number just call aa.B.number(value) if you are on the keystroke saving hype just call aa.B.n(value). It will return the boolean value false if there value is not valid.

All calls to aa.E are chainable, so you can perform aa.E.n(myNumber).s(myString)..., this is particularly useful when dealing with a parameter list.

##Design choices

  • NanN is not a number, even though typeof(NaN) returns number.
  • null is not an object, even though typeof(null) returns object.
  • null is considered undefined.
  • An array is an object and an array.

##Custom types

To define your own types using the primitives as building blocks simply use de aa.define passing a JSON object like this:

aa.define( "dog", {
	breed: "string",
	age: "number",
	name: "string",
	tricks: "array"
});

from there you can simply do: aa.A.dog(myDog), aa.B.dog(myDog) and `aa.E.dog(myDog)``. All three methods are created on the run.

Of course you can reuse type definitions in other types:

aa.define( "petOwner", {
	pet: "dog",
	name: "string",
	phone: "number"
});

And, as expected if pet is not a complying dog aa.B.petOwner(petOwner) will return false and aa.E.petOwner(petOwner) will throw a type exception.

Or create nested types:

class: {
	teacher: {
		id: "number",
		name: "string"
	}
	id: "number"
}

You can also keep the type definitions in a JSON file, and import them as follows:

aa.import(filePath, callback);

###Ad-hoc types If there's a type that will only be used once, maybe defining a global type is way too much. For those cases the aa.A.c(object, typeDescriptor), aa.B.c(object, typeDescriptor) and aa.E.c(object, typeDescriptor) methods are available. For example:

aa.B.c({name: "name",
	nested: {name: "nested_name",
		superNested: {age:3}}},

	{name:"string",
	nested:{name:"string",
	superNested: {age: "number"}}})

will return true. ###Circular dependencies(Not yet implemented) aassert will allow circular dependencies. Say for example:

dog: {
	...
	owner: "petOwner"
	...
}

and

petOwner:{
	...
	pet: "dog"
	...
}

are valid type definitions that won't fail.

##Work to be done

  • Allow circular dependencies. It should work like this:

    class:{ teacher: "teacher", id: "number" } teacher: { id: "number", name: "string" }

##Changelog

0.2.0:

  • Moved inside aa the calls so only one object is exposed to global namespace.
  • The old aa calls are now boolean calls that are accessible as aa.B.
  • The new new assertion mode aa.A. will return a detailed description of the type mismatches if any.
  • The old strict calls AA. are now aa.E. calls, as they throw an __E__xception.