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

v1.2.10

Published

Helper class to force (input) types, mainly for pure JavaScript environments

Downloads

159

Readme

Type insurance

Helper class to force (input) types, mainly for pure JavaScript environments

npm build coverage Known Vulnerabilities dependencies size types unicorn xo license

Key notes / highlights

  • Multi type container
  • Ensures a type for a variable declaration (in opposition to type checking)
  • e.g. when using the type 'any'
  • Tailored for non type sensitive environments
  • Useful when working with uncertain data from third party API's
  • Returns actually false for empty objects (or any other implicitely falsy value)
  • Simplistic approach
  • Doesn't extend the built in .prototypes
  • Clean and focused

Install

$ npm install type-insurance

Usage (real world example)

import TypeInsurance from 'type-insurance';

export const stripHTML = input => {
	const castInput = new TypeInsurance(input);
	return castInput.string.replace(/<[^>]*>/g, '');
}

This little utility function sanitizes an input parameter from HTML tags. TypeInsurance is used to force the regex to obtain a string no matter what.

Usage (overview)

import TypeInsurance from 'type-insurance';
// or using CommonJS
//const TypeInsurance = require('type-insurance');

const input = new TypeInsurance('foo');

console.log(input.string);  // "foo" 
console.log(input.number);  // 440071440
console.log(input.boolean); // true
console.log(input.array);   // ["f", "o", "o"]
console.log(input.object);  // { key: "foo" }

const arr = new TypeInsurance([1, 2, 3]);

console.log(arr.string);  // "[1, 2, 3]" 
console.log(arr.number);  // 6
console.log(arr.boolean); // true
console.log(arr.array);   // [1, 2, 3]
console.log(arr.object);  // { 0: 1, 1: 2, 3: 2 }

// ...

Assumptions

"Object", hereinafter is understood as a "real" object, meaning the the intersection of the sets "objects" and "not arrays".

All inputs should return themselves when requesting its original type. And all falsy values except false should map to

  • '' (string)
  • 0 (number)
  • false (boolean)
  • [] (array)
  • {} (object)

The inputs [] and {} should be treated as falsy inputs.

A non-empty string should map to

  • The decimal representation of a hash conversion, unless the string contains only digits (number)
  • true (bool)
  • An array containing the string on the first index and its single letters on the consecutive indices (array)
  • An object with the default key and the string as the value (object)

where the edge case 'false' should return false when requesting the .boolean property. Numbers shall work in an analogous fashion. 0 especially should yield false.

Respectively, true|false should return '1'|'0', 1|0 and [true|false].

API

Constructor: new TypeInsurance(input, [options])

A class instance of TypeInsurnace holds the properties .string, .number, .boolean, .array and .object, each in turn holding the accordingly typed values mapped from the input value.

Options get passed in as an object. Available options are:

  • defaultKey (default: "key") - Specifies the default key for implicitly generated objects from strings and numbers
  • hashObjects (default: false) - The .number prop returns a hash for object inputs
  • stringifyBoolsVerbatim (default: false) - The .string prop returns stringified versions ('true'|'false') for boolean inputs

Properties

.string

Returns a string generated from the input of the constructor.

| Input type | Output | | :---: | :--- | | string | unchanged input | | number | Series of stringified digits | | boolean | '1' / '0' | | array | Stringified version of the array content | | object | Stringified version of the object content |

.number

Returns a number generated from the input of the constructor.

| Input type | Output | | :---: | :--- | | string | Hash converted to decimal | | number | unchanged input | | boolean | 1 / 0 | | array | Sum of all elemets | | object | Sum of all object values |

.boolean

Returns a number generated from the input of the constructor.

| Input type | Output | | :---: | :--- | | string | true if input is non-empty | | number | false if input === 0 | | boolean | unchanged input | | array | false if input === [] | | object | false if input === {} |

.array

Returns an array generated from the input of the constructor.

| Input type | Output | | :---: | :--- | | string | Array containing the single letters of the input | | number | Array containing the digits of the input number | | boolean | see 'string' | | array | unchanged input | | object | Object.values(input) |

.object

Returns an array generated from the input of the constructor.

| Input type | Output | | :---: | :--- | | string | Object containing the key value pair [defaultKey]=input | | number | see above line | | boolean | see above line | | array | {...input} | | object | unchanged input |

Keywords

  • data
  • interface
  • type
  • types
  • convert
  • safety

Dependencies

Related

Maintainer