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

@lexriver/data-types

v2.0.0

Published

Package for checking type of a variable

Downloads

12

Readme

DataTypes

Package for checking type of a variable

Install

npm install @lexriver/data-types

Import

import {DataTypes} from '@lexriver/data-types'

Usage

DataTypes.isFunction(x:any):boolean

Check if variable is a function

DataTypes.isClass(x:any):boolean

Check if variable is a class

DataTypes.isClassInstance(classInstance:any, className:any):boolean

Check if variable is instance of a class, internally the same as classInstance instanceof className

DataTypes.isDate(date: any): boolean

Check if variable is instance of Date

DataTypes.isObject(o: any): boolean

Check if variable is object

DataTypes.isObjectWithKeys(o:any): boolean

Check if variable is object with keys

DataTypes.isObjectWithKeys({a:'a'}) // true
DataTypes.isObjectWithKeys({}) // false
DataTypes.isObjectWithKeys('some string') // false

DataTypes.isString(x: any): boolean

Check if variable is a string

DataTypes.isNumber(x: any): boolean

Check if variable is a number

DataTypes.isNullOrUndefined(x: any): boolean

Check if variable is null or undefined

DataTypes.isBoolean(x: any): boolean

Check if variable is a boolean

DataTypes.isArray(x: any): boolean

Check if variable is array

DataTypes.isPrimitive(x: any): boolean

Check if variable is undefined or null or boolean or string or symbol

DataTypes.isEqual(x: any, y: any): boolean

Check if two variables are the same

DataTypes.isEqual(1, 1) // true
DataTypes.isEqual(1, '1') // false
DataTypes.isEqual('a', 'a') // true
DataTypes.isEqual('a', '1') // false
DataTypes.isEqual(1.11, 1.11) // true
DataTypes.isEqual(new Date('2019-11-12'), new Date('2019-11-12')) // true
DataTypes.isEqual(new Date('2019-11-12'), new Date('2019-11-13')) // false
const x = new Date()
const y = x
DataTypes.isEqual(x, y) // true
DataTypes.isEqual(
    [1,'1',new Date('2019-11-12')],
    [1,'1',new Date('2019-11-12')]
) //true
DataTypes.isEqual(
    [1,'1',new Date('2019-11-12'), 1],
    [1,'1',new Date('2019-11-12')]
) //false
DataTypes.isEqual(
    [1,'1',new Date('2019-11-12')],
    [1,'1',new Date('2019-11-12')]
) //true
DataTypes.isEqual(
    {a:'aa', b:'bb'},
    {b: 'bb', a: 'aa'}
) //true
DataTypes.isEqual(
    {a:'aa', b:'bb'},
    {a: 'aa', b:1}
) //false
    const x = {
        a:'aa', 
        b:'bb', 
        c: new Date('2019-11-12'), 
        d: [
            1,
            2,
            '3tt', 
            new Date('2019-11-13'), 
            {
                a: 'aa', 
                b: {
                    bb:'bb', 
                    dd:'dd'
                }
            }
        ],
        e: {
            a:'aaa', 
            b: {
                bb:'bb',
                dd: new Date('2019-11-13'),
                ee: 123
            }
        }
    }
    const y = {
        a:'aa', 
        b:'bb', 
        c: new Date('2019-11-12'), 
        d: [
            1,
            2,
            '3tt', 
            new Date('2019-11-13'), 
            {
                a: 'aa', 
                b: {
                    bb:'bb', 
                    dd:'dd'
                }
            }
        ],
        e: {
            a:'aaa', 
            b: {
                bb:'bb',
                dd: new Date('2019-11-13'),
                ee: 123
            }
        }
    }
    DataTypes.isEqual(x,y) // true

DataTypes.isObjectContainsObject(p:{bigObject:Object, smallObject:Object, ignoreCaseInStringValues?:boolean, ignoreEmptySmallObject?:boolean}):boolean

Check if object contains another object

Parameters

  • bigObject:Object - big object to check
  • smallObject:Object - small object
  • ignoreCaseInStringValues?:boolean - ignore case for strings when compare
  • ignoreEmptySmallObject?:boolean - if true the function returns false if small object is empty

Examples

DataTypes.isObjectContainsObject({
    bigObject: {a:'a', b:true, c:3, d:false}, 
    smallObject: {a:'a', b:true, c:3}
}) // true
DataTypes.isObjectContainsObject({
    bigObject: {a:'a', b:{b1:'b1', b2:'b2'}, c:'c', d:new Date(2019,12,10)}, 
    smallObject: {a:'a', b:{b1:'b1'}, d:new Date(2019,12,10)}
}) // true
DataTypes.isObjectContainsObject({
    bigObject: {a:'a', b:{b1:'b1', b2:'b2'}, c:'c'}, 
    smallObject: {}
}) // true

DataTypes.isObjectContainsObject({
    bigObject: {a:'a', b:{b1:'b1', b2:'b2'}, c:'c'}, 
    smallObject: {},
    ignoreEmptySmallObject: true
}) // false
DataTypes.isObjectContainsObject({ 
    bigObject: {a:'aaa', b:'b', d:new Date(2019,10,11)}, 
    smallObject: {a: 'AAA'}, 
    ignoreCaseInStringValues: true
}) //true

DataTypes.isObjectContainsObject({ 
    bigObject: {a:'aaa', b:'b', d:new Date(2019,10,11)}, 
    smallObject: {a: 'AAA'}, 
    ignoreCaseInStringValues: false
}) //false
DataTypes.isObjectContainsObject({
    bigObject: {date:new Date(2019,10,11)},
    smallObject: {date:new Date(2019,10,10)}
}) // false

DataTypes.isObjectContainsObject({
    bigObject: {date:new Date(2019,10,11)},
    smallObject: {date:new Date(2019,10,11)}
}) // true

DataTypes.filterObjectByKeys(o: any, keysToCopy: (key: string) => boolean, recursive?: boolean): object

Returns a new object only with keys specified by predicate function

Parameters

  • o: any - object to filter
  • keysToCopy: (key: string) => boolean - function to check each property for object
  • recursive?: boolean - if true, then perform a deep clone with filter

Example

    const date = new Date()
    const input = {
        a: 'a',
        b: 'b',
        _c: '_c',
        _d: 'd',
        e: {
            e1: 'e1',
            _e2: '_e2'
        },
        f: [
            { f1: 'f1' },
            { _f2: 'f2' }
        ],
        g: date,
        _h: date,
        i: 34,
        _j: 45
    }
    const output = DataTypes.filterObjectByKeys(input, k => k[0] !== '_', true)
/*
    output = {
        a: 'a',
        b: 'b',
        e: {
            e1: 'e1',
        },
        f: [
            { f1: 'f1' },
            {}
        ],
        g: date,
        i: 34
    }
*/    

DataTypes.isValidJsonObject(json: any)

Check if no instances of classes in object

    const x = {
        a: 'a',
        b: 'b',
        c: {
            c1:'c1',
            c2: 23234,
            c3: new Date()
        },
        d:[
            'd1', 'd2', 'd3'
        ]
    }
    DataTypes.isValidJsonObject(x) // true
    class MyClass{
        constructor(public a:string){
        }
    }

    const x = new MyClass('x')

    DataTypes.isValidJsonObject({a:x}) // false

DataTypes.toJson(o:any)

Convert any object to valid json object

DataTypes.isValueExistsInEnum(value:any, EnumType:any): boolean

Check if value exists in enum

enum EnumForTest{
    First = 'first',
    Second = 'second'
}

DataTypes.isValueExistsInEnum(EnumForTest.First, EnumForTest) // true
DataTypes.isValueExistsInEnum('first', EnumForTest) // true
DataTypes.isValueExistsInEnum('First', EnumForTest) // false

enum EnumForTestNumber{
    First, // =0
    Second // =1
}

DataTypes.isValueExistsInEnum(1, EnumForTestNumber) // true
DataTypes.isValueExistsInEnum(100, EnumForTestNumber) // false
DataTypes.isValueExistsInEnum(undefined, EnumForTestNumber) // false