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

@danielnarey/data-types

v0.2.3

Published

[Deprecated] Simple type checking and conversion for standard JS data types

Downloads

7

Readme

@danielnarey/data-types

[Deprecated] Simple type checking and conversion for standard JS data types

Deprecation Warning: This experimental library is no longer in active development and will not be updated in response to Node.js version releases or security vulnerabilities identified in the dependency tree.

Install

npm i @danielnarey/data-types

Functions for Type Checking

The checkSync and checkAsync modules include type-checking functions for JavaScript primitives and built-in data structures.

Usage

CommonJS require:

const { checkSync, checkAsync } = require('@danielnarey/data-types');

ES6 import (using Babel):

import { checkSync, checkAsync } from '@danielnarey/data-types';

API

Included in checkSync and checkAsync:

  • isString
  • isNumber
  • isBoolean
  • isSymbol
  • isDate
  • isRegExp
  • isFunction
  • isObject
  • isArray
  • isSet
  • isMap
  • isWeakSet
  • isWeakMap
  • isStringArray
  • isNumberArray
  • isBooleanArray
  • isDateArray
  • isFunctionArray
  • isObjectArray
  • isTypedArray
  • isIntTypedArray
  • isUintTypedArray
  • isFloatTypedArray
  • isBigIntTypedArray

Included in checkSync only:

  • whatType: Returns the argument's primitive data type or object prototype as an upper-camel-case string

Included in checkAsync only:

  • isRejected: Returns a promise resolving to true if the argument is a promise that rejects

Sync vs Async

Calling checkSync.isString checks whether the value passed to the function is a string and returns true or false; calling checkAsync.isString awaits the value passed to the function before performing the check. With the async version, either a string or a promise resolving to a string returns a promise resolving to true; any other value (including a rejected promise) returns a promise resolving to false.

Checking for Single-typed Standard Arrays

The is__Array functions perform a check on each element of a standard array using an optimized implementation of every. Note that for the checkAsync version, the array may be wrapped in a promise, but the elements must not be or the function will return false.

Checking for TypedArray Buffer Types

The isTypedArray functions test whether the value is one of 11 built-in prototypes corresponding to underlying data buffers. The is__TypedArray functions test against subsets of these prototypes. See MDN for more information about the TypedArray data structure.

Functions for Type Conversion

The convertSync and convertAsync modules contain curried functions allow the user to specify type and value mappings for converting other primitives to string, number, boolean, or Date values.

Usage

CommonJS require:

const { convertSync, convertAsync } = require('@danielnarey/data-types');

ES6 import (using Babel):

import { convertSync, convertAsync } from '@danielnarey/data-types';

API

Included in convertSync and convertAsync:

  • toString: uses JSON.stringify() for unmapped values
  • toNumber: uses Number() for unmapped values
  • toBoolean: uses Boolean() as default test
  • toDate: uses new Date() as default parser

Parameters and Defaults

Parameters for convertSync version:

  • toString(typeMap)(valueMap)(value)
  • toNumber(typeMap)(valueMap)(value)
  • toBoolean(typeMap)(test)(value)
  • toDate(typeMap)(parser)(value)

Parameters for convertAsync version:

  • toString(typeMap)(valueMap)(defaultValue)(value)
  • toNumber(typeMap)(valueMap)(defaultValue)(value)
  • toBoolean(typeMap)(test)(defaultValue)(value)
  • toDate(typeMap)(parser)(defaultValue)(value)

typeMap

  • a Map that maps primitive/prototype names (as returned by checkSync.whatType) to result values
  • default mappings are provided for 'Undefined' and 'Null' primitives as follows:
    • string: ''
    • number: NaN
    • boolean: false
    • Date: new Date(NaN)

valueMap (toString, toNumber)

  • a Map that maps individual values of any input type to result values
  • ignored (set to null) by default
  • note that type mappings override value mappings

test (toBoolean)

  • a function that accepts any value and returns true or false
  • defaults to the built-in Boolean function, which returns true if the value is truthy and false otherwise
  • note that type mappings override test return values

parser (toDate)

  • a function that accepts a string or number and return a Date
  • defaults to x => new Date(x)
  • note that type mappings override parser return values

defaultValue (convertAsync versions)

  • a default value to return if the argument is a promise that rejects
  • if not specified, will default to the following:
    • string: ''
    • number: NaN
    • boolean: false
    • Date: new Date(NaN)

Accessing Data Type Names

The types module provides a way to access data type names (corresponding to type-checking functions in this package) via dot notation (i.e., string literals).

Usage

CommonJS require:

const { types } = require('@danielnarey/data-types');

ES6 import (using Babel):

import { types } from '@danielnarey/data-types';

API

Returns the corresponding type name:

types.String \\--> 'String'

Returns an array of type names:

Object.keys(types) or Object.values(types)