0.1.2 • Published 8 months ago

@wonderlandlabs/walrus v0.1.2

Weekly downloads
-
License
MIT
Repository
-
Last release
8 months ago

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

exampletypeformfamilytypeOf
undefinedTypeEnum.undefinedFormEnum.voidFormEnum.void'undefined'
nullTypeEnum.nullFormEnum.voidFormEnum.void'object'
falseTypeEnum.booleanFormEnum.scalarFormEnum.scalar'boolean'
'Oddity'TypeEnum.stringFormEnum.scalarFormEnum.scalar'string'
2001TypeEnum.numberFormEnum.scalarFormEnum.scalar'number'
Symbol('loaded')TypeEnum.symbolFormEnum.scalarFormEnum.scalar'symbol'
function(){} / () => {}TypeEnum.functionFormEnum.functionFormEnum.function'function'
[1, 2, 3]TypeEnum.arrayFormEnum.arrayFormEnum.container'object'
new Map(['x', 1'], ['y', 2])TypeEnum.mapFormEnum.mapFormEnum.container'object'
new Set(['a', 3, false])TypeEnum.setFormEnum.setFormEnum.container'object'
{a: 1, b: 2}TypeEnum.objectFormEnum.objectFormEnum.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.

0.1.0

8 months ago

0.1.2

8 months ago

0.0.3

8 months ago

0.1.1

8 months ago

0.0.9

8 months ago

0.0.8

8 months ago

0.0.5

8 months ago

0.0.4

8 months ago

0.0.7

8 months ago

0.0.6

8 months ago

0.0.2

2 years ago

0.0.1

2 years ago