Negotiate Pushing Milestone

    @lexriver/data-types
    TypeScript icon, indicating that this package has built-in type declarations

    2.0.0 • Public • Published

    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

    Install

    npm i @lexriver/data-types

    DownloadsWeekly Downloads

    3

    Version

    2.0.0

    License

    MIT

    Unpacked Size

    60.1 kB

    Total Files

    13

    Last publish

    Collaborators

    • lexriver