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

tspace-utils

v1.0.4

Published

tspace-utils is a library functions and methods helper

Downloads

156

Readme

tspace-utils

NPM version NPM downloads

tspace-utils is a library functions and methods helper can be used with TypeScript.
Its always support the latest TypeScript and JavaScript features and provide additional features that help you to develop.

Install

Install with npm:

npm install tspace-utils --save

Basic Usage

Validate

import { Validate , Rule } from 'tspace-utils'

const data = {
    useWhen : 'hello when'
    email : 'test@g',
    mobile : '045811xxxx',
    username : 'username',
    password : '',
    passwordConfirm : 'password',
    enum : 'test enum',
    arrayObject : [
        {
            id : "1",
            name : '',
            email : '[email protected]',
        },
        {
            id : "xx",
            name : 'qwqw',
            email : 'test',
        },
        {
            id : "",
            name : '',
            email : 'x.com',
        }
    ],
    object: {
        id : "",
        name : '',
        email : '[email protected]',
        nestObject: {
            test : ""
        }
    },
    func : 3,
    funcs : [3,3]
}

const validate = new Validate(data)

validate.check((Rule : Rule) => {   
    return {
        //  .when if condition is true will working
        useWhen : new Rule().when(true, (Rule : Rule) => {
            return new Rule().required(),
        }),
        email : new Rule().required().email(),
        mobile : new Rule().required().number().numberStrict().max(10),
        username : new Rule().required().min(3),
        password : new Rule().required().confirm('passwordConfirm')
        .regex('(?=.*[A-Za-z])(?=.*\\d)(?!.*[ก-๙])[\\S\\d]{8,}$','format is invalid'),
        passwordConfirm : new Rule().required(),
        array : new Rule().array().string().maxLength(10).minLength(1),
        enum : new Rule().enum(['test1','test2'])
        arrayObject : new Rule().arrayObject((Rule : Rule) => {
            return {
                id : new Rule().required(),
                name : new Rule().required().number(),
                email : new Rule().required().email()
            }
        }),
        object : new Rule().object((Rule : Rule) => {
            return {
                id : new Rule().required(),
                name : new Rule().required(),
                email : new Rule().required().email(),
                nestObject : new Rule().object((Rule : Rule) => {
                    return {
                        id : new Rule().required(),
                        name : new Rule().required(),
                        email : new Rule().required().email(),
                    }
                })
            }
        }),
        func : (v) => {
            if(v !== 3 ) return 'Number is not equal to 3'
            
            return null
        },
        funcs : [
            (v) => {
                if(!Array.isArray(v)) return 'Value is not an array'
                return null
            },

            new Rule().required().enum(['test1','test2']),
        ],
    }      
})

if(validate.fails()) {
    console.log(validate.errorsToString())
    console.log(validate.errors())
    throw ...
}

// or u can validate using Promise functions
const dataForPromise = {
    dataPromise : "data for promise"
    data : "data for function"
}

const validatePromise = new Validate(dataForPromise)

function sumPromise (a, b) {
    return new Promise((resolve) => resolve(a + b))
}

function sum (a, b) {
    return a + b
}

validatePromise.checkPromise((rule : Rule) => {   
    return {
        dataPromise : [
            async (v) => {

                if(await sumPromise(1,3) !== 3) return 'Number is not equal to 3'

                return null
            },
            new Rule().required().enum(['test1','test2']),
        ],
        data : (v) => {
            if(sum(1,3) !== 3) return 'Number is not equal to 3'

            return null
        },
    }      
})

if(validatePromise.fails()) {
    console.log(validatePromise.errorsToString())
    console.log(validatePromise.errors())
    throw ...
}

Collection

import { Collection } from 'tsapce-utils'

    const data = [
        {
            id : 3,
            username : 'test3',
            email : null,
            created_at : '2022-06-23 12:30:00',
            updatedAt : '2022-06-23 12:30:00'
        },
        {
            id : 2,
            username : 'test2',
            email : null,
            created_at : '2022-06-21 12:30:00',
            updatedAt : '2022-06-22 12:30:00'
        },
        {
            id : 1,
            username : 'test1',
            email : '[email protected]',
            created_at : '2022-06-23 15:00:00',
            updatedAt : '2022-06-25 12:30:00'
        },
        {
            id : 5,
            username : 'test5',
            email : '[email protected]',
            created_at : '2022-06-11 17:00:00',
            updatedAt : '2022-06-22 12:30:00'
        },
        {
            id : 4,
            username : 'test4',
            email : '[email protected]',
            created_at : '2022-06-22 18:00:00',
            updatedAt : '2022-06-25 18:30:00'
        }
    ]
    
    const map = new Collection(data).map((item: { username: string }) => {
        return { ...item,username : item.username.toUpperCase() }
    }).get()

    const find = new Collection(data).find((item: { id: number }) => item.id === 1).get()

    const each = new Collection(data).each((collect: Collection) => {
        collect.where('id',1).concat([{ id : 10 , username : 'test10' }])
    }).get()
    
    const filter = new Collection(data).filter((item: { id : number }) => item.id === 1).get()

    const reduce = new Collection(data).reduce(( prev:number, cur : { id : number}) => prev + cur.id , 0).get()

    const when = new Collection(data).when(true, (collect: Collection) => {
        collect.map((item: { id : number } , index: number) => item.id * index)
    }).get()

    const whereBetween = new Collection(data).whereBetween('id',[1,2]).get()
    const whereNotBetween = new Collection(data).whereNotBetween('id',[1,2]).get()
    const whereNull = new Collection(data).whereNull('email').get()
    const whereNotNull = new Collection(data).whereNotNull('email').get()
    const where = new Collection(data).where('id',1).where('username','test1').get()
    const whereNotIn = new Collection(data).whereNotIn('id',[1,2,3,4]).get()
    const whereIn = new Collection(data).whereIn('id',[1,2,3,4]).get()
    const count = new Collection(data).count()
    const avg = new Collection(data).avg('id')
    const json = new Collection(data).toJson()
    const sum = new Collection(data).sum('id')
    const max =  new Collection(data).max('id')
    const min = new Collection(data).min('id')
    const chunk = new Collection(data).chunk(3)
    const oldest = new Collection(data).oldest('created_at').get()
    const latest = new Collection(data).latest('created_at').get()
    const limit = new Collection(data).limit(3).get()
    const except = new Collection(data).except('email').get()
    const snakeCase = new Collection(data).snakeCase().get()
    const camelCase = new Collection(data).camelCase().get()

Logger

import { Logger } from 'tspace-utils'

// basic Logger
new Logger().info('info Hello logger')
new Logger().warn('warn Hello logger')
new Logger().debug('debug Hello logger')
 try {
     throw new Error('keeping logs')
 } 
 catch (err) {
   new Logger().error(err)
 } 
// create new folder
new Logger().folder('new-logs').maxLengthInFile(1000).info('info Hello logger')
new Logger().folder('logs/new-logs').info('info Hello logger')
// read logs
new Logger().readLog('new-logs')
new Logger().folder('logs/new-logs').readLog(name)
new Logger().readLogsInFolder(folder)

Time


import { Time } from 'tspace-utils'

new Time().now()
new Time().formatDate('yyyy-mm-dd').today()
new Time().formatDate('yyyy/mm/dd').toDate()
new Time().minusYears(10).addMonths(1).addDays(10).addHours(2).addMinutes(30).onlyTime().toString()
new Time().minusYears(10).addMonths(1).minusDays(10).addHours(2).addMinutes(30).onlyDate().toString()
new Time().minusYears(50).addMonths(1).minusDays(10).addHours(2).toTimestamp()

Test


import { Test, StressTest } from 'tspace-utils'

    const sum = (a , b) => a + b

    new Test().describe('test sum').expect(() => sum(1,3)).toBe(4)
    new Test().describe('test sum').expect(() => sum(2,3)).toBe("5")

    // testing api performance
    const func = () => axios.get(url)
    console.log(await new StressTest().each(500).test(func))

Benchmark


import { Benchmark } from 'tspace-utils'

    function sumPromise1 (a , b) {
        return new Promise((resolve) => {
            setTimeout(() => {
                resolve(a + b)
            }, 1000)
        })
    }

    function sumPromise2 (a , b) {
        return new Promise((resolve) => {
            setTimeout(() => {
                resolve(a + b)
            }, 3000)
        })
    }

    await new Benchmark()
        .add('test sum 1 + 2' , () =>  sumPromise2(1,2))
        .add('test sum 1000 + 2000' , () =>  sumPromise(1000,2000))
        .add('test sum 2000 + 2000' , () =>  sumPromise2(2000,2000))
        .run()

Other

import {createState , typeOf , isEmpty , deepEqual , snakeCase , camelCase } from 'tspace-utils'

const [ state , setState] = createState([])
console.log(state())

setState({
    add : 'state'
})

console.log(state())

console.log(typeOf([]))
console.log(typeOf({}))

console.log(isEmpty([]))
console.log(isEmpty({}))
console.log(isEmpty(""))
console.log(isEmpty(null))

console.log(snakeCase('snakeCase')) // 'snake_case'
console.log(camelCase('camel_case')) // 'camelCase'


console.log( 
    deepEqual(
        { a : [ 2, 3 ], b : [ 4 ] },
        { a : [ 2, 3 ], b : [ '4' ] }
    ),
    deepEqual(
        new Map([[1,3]]),
        new Map([['1',3]])
    ),
    deepEqual(
        null,
        undefined
    ),
    deepEqual(
        new Date('1999-01-01 00:00:0000'),
        new Date('1999-01-02 00:00:0000'),
    )
)