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

@wonderlandlabs/walrus

v0.1.2

Published

utility class for type checking, text ops

Downloads

15

Readme

WALRUS

Walrus is a medley of utilities I keep on needing or using, so here I am centralizing them for consistency.

Text functions

addBefore/addAfter(base: string, append: string) : string

Routines for ensuring or removing a phrase (such asd a domain) in front of or after a string

humanize (phrase: string) => string

This is for "breaking up" parameters or SQL fields into sentence form by replacing "_-" with single spaces

ucFirst (phrase: string) => string

Replaces the first letter in a string with an uppercase character

entitle (phrase: string) => string

This is a blend of humanize and ucFirst; it splits strings into sentence form then capitalizes each word.

Type functions: describe (value: any) => TypeDef

The 'typeof' paradigm is too coarse for some purposes and too broad for others. While Typescript does a good job of static checking, many times, values are not fixed at design time, so its good to know precisely what they are at runtime.

The main utility of the type library is describe(value) which takes in any value and returns a TypeDef describer.

The primary reason for this utility is that there are a lot of "overlapping" types in javascript: null, array, and POJOs are all objects, as are Sets and Maps. But also there are times when you want to know whether an item is in a specific category (such as "scalar") without caring about whether it is an integer or string. Or for example, there is a collective family of "void" that encompasses undefined and null.

Here is the complete matrix of describe outputs depending on whether you query the "form", "family" or "type" of the output:

Possible TypeDef outputs of type.describe(value), by example input

| example | type | form | family | typeOf | |---------------------------------|---------------------|-------------------|--------------------|-------------| | undefined | TypeEnum.undefined | FormEnum.void | FormEnum.void | 'undefined' | | null | TypeEnum.null | FormEnum.void | FormEnum.void | 'object' | | false | TypeEnum.boolean | FormEnum.scalar | FormEnum.scalar | 'boolean' | | 'Oddity' | TypeEnum.string | FormEnum.scalar | FormEnum.scalar | 'string' | | 2001 | TypeEnum.number | FormEnum.scalar | FormEnum.scalar | 'number' | | Symbol('loaded') | TypeEnum.symbol | FormEnum.scalar | FormEnum.scalar | 'symbol' | | function(){} / () => {} | TypeEnum.function | FormEnum.function | FormEnum.function | 'function' | | [1, 2, 3] | TypeEnum.array | FormEnum.array | FormEnum.container | 'object' | | new Map(['x', 1'], ['y', 2]) | TypeEnum.map | FormEnum.map | FormEnum.container | 'object' | | new Set(['a', 3, false]) | TypeEnum.set | FormEnum.set | FormEnum.container | 'object' | | {a: 1, b: 2} | TypeEnum.object | FormEnum.object | FormEnum.container | 'object' |

type in one call gives you a definitive unique type assignation for any value;

TypeDef -- the return value from describe -- has the following signature:


{
type: string
typeOf: string  // note the camelCased "typeOf"
form: string
family: string
}
  • type is the richest descriptor; it includes the set 'string','number','boolean','symbol','array','map','object','set','null','undefined','function'
  • typeOf is exactly the same result as the typeof operator
  • form compresses all the scalar types into the descriptor 'scalar' and undefined and null are both considered 'void'
  • family considers all the advanced types (array, Map) to be 'container' So it has one of three values: 'scalar', 'container', 'void'.

you can extract the type of the typedef (to a string) by passing true to the second argument, or you can extract a named field by passing it as a string.

Examples:

type.describe(null) =  {"type":"null","form":"void","typeOf":"object"}
type.describe(3) =  {"type":"number","form":"scalar","typeOf":"number"}
type.describe([]) =  {"type":"array","form":"array","typeOf":"object"}

// passing 'true' is the eqivalent of passing 'type' as the selector

type.describe(null, true) =  "null"
type.describe(3, true) =  "number"
type.describe([], true) =  "array"

type.describe(null, "family") =  "void"
type.describe(3, "family") =  "scalar"
type.describe([], "family") =  "container"

describeNumber (value) => string

returns one of a possible value describing a (potentially) numeric value in detail: 'integer','decimal','infinite','nan'.

Why would we want this if we have typescript?

while you could define container and scalar types you still would have to write some fairly complex type guards to enforce them. the describe method is useful in writing cleaner type guards; for instance instead of constantly having to assert arg && typeof arg === 'object' && !Array.isArray(arg) in your typeguards, you can simmply assert type.describe(arg, true) ==== TypeInum.object and be assured that the input is truly an object type and not one of the many quasi-object variations like Sets or Maps.