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

ts-utility-functions

v2.0.0-alpha.9

Published

Utility functions for TypeScript

Downloads

317

Readme

ts-utility-functions

Utility functions for TypeScript

Installation

npm install ts-utility-functions

Usage

String

str.is

str.is('')          // true
str.is(123)         // false
vals.filter(str.is) // filter out non-string values

str.isStartWith

str.isStartWith('abc')('a')       // true
str.isStartWith('abc')('b')       // false
vals.filter(str.isStartWith('a')) // filter out values that do not start with 'a'

Types

assert.type, assert.never

import { assert } from 'ts-utility-functions';

// assert.type
type Currency = 'USD' | 'EUR';
const currency: Currency = 'USD';

switch (currency) {
  case 'USD':
    break;
  case 'EUR':
    break;
  default:
    assert.type<never>(currency); // compilation - ok, runtime - ok
    assert.never(currency);       // compilation - ok, runtime - ok
}

switch (currency) {
  case 'USD':
    break;
  default:
    assert.type<never>(currency); // compilation - error, runtime - ok
    assert.never(currency);       // compilation - error, runtime - ok
}

Object

obj.copyIntersectingFields

const a = { a: 1, b: 2, c: 3 }
const b = { a: 0, b: 0 }
copyIntersectingFields(a, b, ['a', 'b'])
console.log(b) // { a: 1, b: 2 }

obj.copyIntersectingFields.toNew

const a = { a: 1, b: 2, c: 3 }
class B {
  constructor(public a: number, public b: number) {}
}
const b = copyIntersectingFields.toNew(a, B, ['a', 'b'])
console.log(b) // A { a: 1, b: 2 }

obj.merge

const a = { a: 1, b: 2 }
const b = { b: 3, c: 4 }
const c = { c: 5, d: 6 }
const d = merge(a, b, c)
console.log(d) // { a: 1, b: 3, c: 5, d: 6 }

obj.vals

const a = { a: 1, b: 2, c: '3' }
const b = vals(a)
console.log(b) // [1, 2, '3'] as (string | number)[]

const a = { a: 1, b: 2, c: '3' } as const
const b = vals(a)
console.log(b) // [1, 2, '3'] as (1 | 2 | '3')[]

obj.keys

const a = { a: 1, b: 2, c: 3 }
const b = keys(a)
console.log(b) // ['a', 'b', 'c'] as ('a', 'b', 'c')[]

Function

fn.noop

const a = fn.noop() // does nothing (returns undefined)

fn.noopAsync

const a = await fn.noopAsync() // does nothing (returns Promise<undefined>)

Array

arr.is

arr.is([])          // true
arr.is([1, 2, 3])   // true
vals.filter(arr.is) // filter out non-array values

arr.isNot

arr.isNot([])          // false
arr.isNot([1, 2, 3])   // false
vals.filter(arr.isNot) // filter out array values

arr.req

arr.req([])          // []
arr.req([1, 2, 3])   // [1, 2, 3]
arr.req(null)        // throws error

arr.isEmpty

arr.isEmpty([])          // true
arr.isEmpty([1, 2, 3])   // false
vals.filter(arr.isEmpty) // filter out non-empty arrays

arr.isNotEmpty

arr.isNotEmpty([])          // false
arr.isNotEmpty([1, 2, 3])   // true
vals.filter(arr.isNotEmpty) // filter out empty arrays

arr.isEmptySafe

arr.isEmptySafe([])          // true
arr.isEmptySafe(null)        // true
arr.isEmptySafe(undefined)   // true
arr.isEmptySafe([1, 2, 3])   // false
vals.filter(arr.isEmptySafe) // filter out non-empty arrays

arr.isNotEmptySafe

arr.isNotEmptySafe([])          // false
arr.isNotEmptySafe(null)        // false
arr.isNotEmptySafe(undefined)   // false
arr.isNotEmptySafe([1, 2, 3])   // true
vals.filter(arr.isNotEmptySafe) // filter out empty arrays