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

@bedard/utils

v0.33.1

Published

[![Build status](https://img.shields.io/github/actions/workflow/status/scottbedard/utils/test.yml?branch=main)](https://github.com/scottbedard/utils/actions) [![Codecov](https://img.shields.io/codecov/c/github/scottbedard/utils)](https://codecov.io/gh/sco

Downloads

849

Readme

@bedard/utils

Build status Codecov NPM Bundle size License

This repository acts as my own personal lodash. It contains a number of utility types and functions that I found myself duplicating between projects. I do not anticipate breaking changes, but I'm also not ruling it out. Upgrade with caution.

Installation

The recommended installation method is via NPM.

npm install @bedard/utils

Alternatively, the functions maybe be accessed via a CDN. When using the CDN, the library will be exposed globally as BedardUtils.

<script src="https://unpkg.com/@bedard/utils"></script>

Functions

Animation

Color

Math

Utility

addVectors

Add vectors together.

import { addVectors } from '@bedard/utils'

addVectors([1, 2], [3, 4]) // [4, 6]

View source →

angleFrom

Calculate angled distance from a vector. An angle of 0 degrees will track along the X axis, with positive angles rotating counter-clockwise.

import { angleFrom } from '@bedard/utils'

angleFrom([0, 0], 90, 1) // [0, 1] (approximate)

View source →

bilerp

Bi-linear interpolation between vectors.

import { bilerp } from '@bedard/utils'

bilerp([0, 0], [10, 10], 0.5) // [5, 5]

View source →

cols

Chunk a square matrix into columns. Note that the source matrix must be provided in row-major order.

import { cols } from '@bedard/utils'

cols([
  0, 1, 2,
  3, 4, 5,
  6, 7, 8,
]) // [[0, 3, 6], [1, 4, 7], [2, 5, 8]]

View source →

createEasingFunction

Create an easing function from cubic-bezier points. Note that the included easing functions do not use this utility. They are simpler mathematical functions, and do not produce quite the same results as easing functions created using this utility.

import { createEasingFunction } from '@bedard/utils'

const linear = createEasingFunction(0, 0, 1, 1)

linear(0.5) // 0.5

View source →

deepEqual

Deep object comparison.

import { deepEqual } from '@bedard/utils'

deepEqual({ foo: { bar: 1 }}, { foo: { bar: 1 }}) // true

View source →

degreesToRadians

Convert degrees to radians.

import { degreesToRadians } from '@bedard/utils'

degreesToRadians(180) // 3.141592653589793

View source →

easeInSine

Easing function that follows this curve.

import { easeInSine } from '@bedard/utils'

easeInSine(0.5) // 0.2928932188134524

View source →

easeOutSine

Easing function that follows this curve.

import { easeOutSine } from '@bedard/utils'

easeOutSine(0.5) // 0.7071067811865475

View source →

easeInOutSine

Easing function that follows this curve.

import { easeInOutSine } from '@bedard/utils'

easeInOutSine(0.5) // 0.49999999999999994

View source →

easeInQuad

Easing function that follows this curve.

import { easeInQuad } from '@bedard/utils'

easeInQuad(0.5) // 0.25

View source →

easeOutQuad

Easing function that follows this curve.

import { easeOutQuad } from '@bedard/utils'

easeOutQuad(0.5) // 0.75

View source →

easeInOutQuad

Easing function that follows this curve.

import { easeInOutQuad } from '@bedard/utils'

easeInOutQuad(0.5) // 0.5

View source →

easeInCubic

Easing function that follows this curve.

import { easeInCubic } from '@bedard/utils'

easeInCubic(0.5) // 0.125

View source →

easeOutCubic

Easing function that follows this curve.

import { easeOutCubic } from '@bedard/utils'

easeOutCubic(0.5) // 0.875

View source →

easeInOutCubic

Easing function that follows this curve.

import { easeInOutCubic } from '@bedard/utils'

easeInOutCubic(0.5) // 0.5

View source →

easeInQuart

Easing function that follows this curve.

import { easeInQuart } from '@bedard/utils'

easeInQuart(0.5) // 0.0625

View source →

easeOutQuart

Easing function that follows this curve.

import { easeOutQuart } from '@bedard/utils'

easeOutQuart(0.5) // 0.9375

View source →

easeInOutQuart

Easing function that follows this curve.

import { easeInOutQuart } from '@bedard/utils'

easeInOutQuart(0.5) // 0.5

View source →

easeInQuint

Easing function that follows this curve.

import { easeInQuint } from '@bedard/utils'

easeInQuint(0.5) // 0.03125

View source →

easeOutQuint

Easing function that follows this curve.

import { easeOutQuint } from '@bedard/utils'

easeOutQuint(0.5) // 0.96875

View source →

easeInOutQuint

Easing function that follows this curve.

import { easeInOutQuint } from '@bedard/utils'

easeInOutQuint(0.5) // 0.5

View source →

easeInExpo

Easing function that follows this curve.

import { easeInExpo } from '@bedard/utils'

easeInExpo(0.5) // 0.03125

View source →

easeOutExpo

Easing function that follows this curve.

import { easeOutExpo } from '@bedard/utils'

easeOutExpo(0.5) // 0.96875

View source →

easeInOutExpo

Easing function that follows this curve.

import { easeInOutExpo } from '@bedard/utils'

easeInOutExpo(0.5) // 0.5

View source →

easeInCirc

Easing function that follows this curve.

import { easeInCirc } from '@bedard/utils'

easeInCirc(0.5) // 0.1339745962155614

View source →

easeOutCirc

Easing function that follows this curve.

import { easeOutCirc } from '@bedard/utils'

easeOutCirc(0.5) // 0.8660254037844386

View source →

easeInOutCirc

Easing function that follows this curve.

import { easeInOutCirc } from '@bedard/utils'

easeInOutCirc(0.5) // 0.5

View source →

easeInBack

Easing function that follows this curve.

import { easeInBack } from '@bedard/utils'

easeInBack(0.5) // -0.08769750000000004

View source →

easeOutBack

Easing function that follows this curve.

import { easeOutBack } from '@bedard/utils'

easeOutBack(0.5) // 1.0876975

View source →

easeInOutBack

Easing function that follows this curve.

import { easeInOutBack } from '@bedard/utils'

easeInOutBack(0.5) // 0.5

View source →

easeInElastic

Easing function that creates an elastic-in effect.

import { easeInElastic } from '@bedard/utils'

easeInElastic(0.5) // -0.015625000000000045

View source →

easeOutElastic

Easing function that creates an elastic-out effect.

import { easeOutElastic } from '@bedard/utils'

easeOutElastic(0.5) // 1.015625

View source →

easeInOutElastic

Easing function that creates an elastic-in-out effect.

import { easeInOutElastic } from '@bedard/utils'

easeInOutElastic(0.5) // 0.5

View source →

easeInBounce

Easing function that creates a bounce-in effect.

import { easeInBounce } from '@bedard/utils'

easeInBounce(0.5) // 0.234375

View source →

easeOutBounce

Easing function that creates a bounce-out effect.

import { easeOutBounce } from '@bedard/utils'

easeOutBounce(0.5) // 0.765625

View source →

easeInOutBounce

Easing function that creates a bounce-in-out effect.

import { easeInOutBounce } from '@bedard/utils'

easeInOutBounce(0.5) // 0.5

View source →

entries

Type safe wrapper around Object.entries

import { entries } from '@bedard/utils'

entries({ foo: 'bar' }) // [['foo', 'bar']]

View source →

flattenCols

Flatten an array of columns to a matrix in row-major order.

import { flattenCols } from '@bedard/utils'

flattenCols([
  [0, 3, 6],
  [1, 4, 7],
  [2, 5, 8],
]) // [0, 1, 2, 3, 4, 5, 6, 7, 8]

View source →

flattenRows

Flatten an array of rows to a matrix in row-major order.

import { flattenRows } from '@bedard/utils'

flattenRows([
  [0, 1, 2],
  [3, 4, 5],
  [6, 7, 8],
]) // [0, 1, 2, 3, 4, 5, 6, 7, 8]

View source →

flip

Convert between rows and columns. A good way to visualize this operation is holding a card by the top-left and bottom-right corners and flipping it over.

import { flip } from '@bedard/utils'

flip([
  [0, 1, 2],
  [3, 4, 5],
  [6, 7, 8],
]) // [[0, 3, 6], [1, 4, 7], [2, 5, 8]]

View source →

hslToRgb

Convert [hue, saturation, lightness, alpha?] to [red, green, blue, alpha] values.

import { hslToRgb } from '@bedard/utils'

hslToRgb([0, 1, 0.5]) // [255, 0, 0, 1]

View source →

intersect

Intersect two-dimensional lines. Returns undefined if lines are parellel.

import { intersect } from '@bedard/utils'

intersect([[-1, 0], [1, 0]], [[0, 1], [0, -1]]) // [0, 0]

View source →

isEmail

A fairly permissive email test where the value must be shaped [email protected], and contain exactly one @ characters.

import { isEmail } from '@bedard/utils'

isEmail('[email protected]') // true

View source →

isEven

Test if a number is even.

import { isEven } from '@bedard/utils'

isEven(2) // true

View source →

keys

Type-safe wrapper around Object.keys

import { keys } from '@bedard/utils'

keys({ foo: 'bar' }) // ['foo']

View source →

lerp

Linear interpolation between numbers.

import { lerp } from '@bedard/utils'

lerp(0, 10, 0.5) // 5

View source →

lerpColors

Linear interpolation between colors.

import { lerpColors } from '@bedard/utils'

lerpColors('#000000', '#ffffff') // '#808080'

View source →

lerpVectors

Linear interpolation between two vectors. This function is similar to bilerp, but for vectors of any size, or even vectors of different sizes.

import { lerpVectors } from '@bedard/utils'

lerpVectors([0, 0, 0], [1, 2, 3], 0.5) // [0.5, 1, 1.5]

View source →

measure

Measure the distance between two vectors.

import { measure } from '@bedard/utils'

// arguments can be provided as a pair of vectors
measure([0, 0], [3, 4]) // 5

// or as a single line argument
measure([[0, 0], [3, 4]]) // 5

View source →

parse

A type-safe wrapper around JSON.parse. This utility is complimented by stringify, Json, and UnwrapJson.

import { stringify } from '@bedard/utils'

stringify({ foo: 'bar' }) // '{"foo":"bar"}' as Json<{ foo: string }>

View source →

parseColor

Parse an RGB string to [red, green, blue, alpha] values. An error will be thrown if the value is not valid.

import { parseColor } from '@bedard/utils'

parseColor('#123456') // [18, 52, 86, 0]

View source →

polygon

Create a regular polygon. The first argument defines the number of points, with the second defining the circumradius. Points start from the 12 o'clock position, and rotate counter-clockwise around the origin.

import { polygon } from '@bedard/utils'

polygon(4, 1) // [[0, 1], [-1, 0], [0, -1], [1, 0]] (approximate)

View source →

radiansToDegrees

Convert radians to degrees.

import { radiansToDegrees } from '@bedard/utils'

radiansToDegrees(Math.PI) // 180

View source →

roll

Roll the start of an array forwards or backwards.

import { roll } from '@bedard/utils'

roll([0, 1, 2], 1) // [1, 2, 0]

View source →

rgbToHsl

Convert [red, green, blue, alpha?] to [hue, saturation, lightness, alpha] values.

import { rgbToHsl } from '@bedard/utils'

rgbToHsl([255, 0, 0]) // [0, 1, 0.5, 1]

View source →

rotateMatrix

Rotate a square matrix. Positive values apply clockwise rotations.

import { rotateMatrix } from '@bedard/utils'

rotateMatrix([
  0, 1, 2,
  3, 4, 5,
  6, 7, 8,
], 1) // [6, 3, 0, 7, 4, 1, 8, 5, 2]

View source →

rotateVector

Rotate a vector counter-clockwise around the origin.

import { rotateVector } from '@bedard/utils'

rotateVector([0, 1], 90) // [-1, 0] (approximate)

View source →

rows

Chunk a square matrix into rows. Note that the source matrix must be provided in row-major order.

import { rows } from '@bedard/utils'

rows([
  0, 1, 2,
  3, 4, 5,
  6, 7, 8,
]) // [[0, 1, 2], [3, 4, 5], [6, 7, 8]]

View source →

scale

Multiple a vector by a number.

import { scale } from '@bedard/utils'

scale([1, 2], 2) // [2, 4]

View source →

slope

Calculate the slope of a line.

import { slope } from '@bedard/utils'

// arguments can be provided as a pair of vectors
slope([0, 0], [1, 1]) // 1

// or as a single line argument
slope([[0, 0], [1, 1]]) // 1

View source →

stringify

A type-safe wrapper around JSON.stringify. This utility is complimented by parse, Json, and UnwrapJson

import { parse, stringify } from '@bedard/utils'

const json = stringify({ foo: 'bar' })

const obj = parse(json) // { foo: string }

View source →

timeout

Resolve a promise after a given duration

import { timeout } from '@bedard/utils'

await timeout(100)

View source →

stringifyColor

Convert [red, green, blue, alpha?] values to string using hexadecimal notation.

import { stringifyColor } from '@bedard/utils'

stringifyColor([255, 0, 0]) // #ff0000

View source →

toKeyedObjects

Create keyed objects from an array.

import { toKeyedObjects } from '@bedard/utils'

toKeyedObjects([1, 2], 'foo') // [{ foo: 1 }, { foo: 2 }]

View source →

Types

Abs<T>

Type the absolute value of T

import { Abs } from '@bedard/utils'

type Test = Abs<-5> // 5

View source →

AddDigits<T, U>

Add two decimal digits

import { AddDigits } from '@bedard/utils'

type Test = AddDigits<1, 2> // 3

View source →

AllEqual<Sources, Value>

Types true if all Sources equal Value.

import { AllEqual } from '@bedard/utils'

type Test1 = AllEqual<[1, 1], 1> // true
type Test2 = AllEqual<[1, 2], 1> // false

View source →

AND<T, U>

Logical AND gate

import { AND } from '@bedard/utils'

type Test = AND<1, 1> // 1

View source →

Bit

Union of bit values

import { Bit } from '@bedard/utils'

type Test = Bit // 0 | 1

View source →

BreakWords<T>

Explode a string by common word breaks. This currently includes spaces, hyphens, underscores, and casing changes.

import { BreakWords } from '@bedard/utils'

type Test = BreakWords<'one twoThree-four'> // ['one', 'two', 'Three', 'four']

View source →

Byte

A union of all numbers that can be represented by a single byte (0 - 255).

import { Byte } from '@bedard/utils'

const n: Byte = 255

View source →

CamelCase<T>

Convert a string to camel case.

import { CamelCase } from '@bedard/utils'

type Test = CamelCase<'foo-bar'> // 'fooBar'

View source →

CamelCaseKeys<T>

Camel case object keys.

import { CamelCaseKeys } from '@bedard/utils'

type Test = CamelCaseKeys<{ foo_bar: any }> // { fooBar: any }

View source →

CamelCaseKeysDeep<T>

Deeply camel case object keys.

import { CamelCaseKeysDeep } from '@bedard/utils'

type Test = CamelCaseKeysDeep<{ foo_bar: { one_two: any }}> // { fooBar: { oneTwo: any }}

View source →

Decimal

Union of decimal values

import { Decimal } from '@bedard/utils'

type Test = Decimal // 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

View source →

Digits<T>

Explode the digits of a positive integer

import { Digits } from '@bedard/utils'

type Test = Digits<123> // [1, 2, 3]

View source →

Difference<A, B>

Elements of A that are not elements of B. For unions, this is the same as the Exclude utility.

import { Difference } from '@bedard/utils'

type Test = Difference<{ a: any, b: any }, { b: any, c: any }> // { a: any }

View source →

Either<T, U>

Create an exclusive or between two types. Note that for objects, this differs from a union type in that keys are strictly matched.

import { Either } from '@bedard/utils'

type Test = Either<{ foo: any }, { bar: any }>

const a: Test = { foo } // pass
const b: Test = { bar } // pass
const c: Test = { foo, bar } // fail

Additionally, a tuple can be provided for a chained XOR.

type Test = XOR<[1, 2, 3]>

const a: Test = 1 // pass
const b: Test = 2 // pass
const c: Test = 3 // pass
const d: Test = 4 // fail

View source →

Equal<A, B>

Types true if A and B are equal. This is mainly used with Expect to verify that types are working as expected. See NotEqual for the inverse of this type.

import { Expect, Equal } from '@bedard/utils'

type Test = Expect<Equal<number, number>>

View source →

Expect<T>

Verify that T is true. This allows for assertions to be made using the type system. See Equal and NotEqual for more usage examples.

import { Expect } from '@bedard/utils'

type Test = Expect<true>

View source →

First<T>

Extract the first element of a string or array.

import { First } from '@bedard/utils'

type Test1 = First<'abc'> // 'a'
type Test2 = First<[1, 2, 3]>, // 1

View source →

Hex

A hexadecimal character

import { Hex } from '@bedard/utils'

const char: Hex = 'a'

View source →

HexColor<T>

Validate a hexadecimal color value

import { HexColor } from '@bedard/utils'

const color = <T>(val: HexColor<T>) => val

color('#abc')

View source →

Intersection<A, B>

The intersection of A and B, giving preference to A.

import { Intersection } from '@bedard/utils'

type Test = Intersection<{ a: any, b: number }, { c: string, d: any }> // { b: number }

View source →

IsFloat<T>

Test if number is a float

import { IsInteger } from '@bedard/utils'

type Test = IsFloat<1.5> // true

View source →

IsInteger<T>

Test if number is an integer

import { IsInteger } from '@bedard/utils'

type Test = IsInteger<1> // true

View source →

IsNegative<T>

Test if number is less than zero

import { IsNegative } from '@bedard/utils'

type Test = IsNegative<-1> // true

View source →

IsPositive<T>

Test if number is greater than zero

import { IsPositive } from '@bedard/utils'

type Test = IsPositive<1> // true

View source →

Join<Parts, Delimeter>

Join Parts by Delimeter. This type is the opposite of Split.

import { Join } from '@bedard/utils'

type Test1 = Join<['a', 'b', 'c']> // 'abc'

type Test2 = Join<['a', 'b', 'c'], '.'> // 'a.b.c'

View source →

Json<T>

Encodes a JSON string with underlying type information. This utility is complimented by parse, stringify, and UnwrapJson.

import { Json } from '@bedard/utils'

type Test = Json<{ foo: 'bar' }> // string

View source →

KebabCase<T>

Convert a string to kebab case.

import { KebabCase } from '@bedard/utils'

type Test = KebabCase<'fooBar'> // 'foo-bar'

View source →

KebabCaseKeys<T>

Kebab case object keys.

import { KebabCaseKeys } from '@bedard/utils'

type Test = KebabCaseKeys<{ foo_bar: any }> // { 'foo-bar': any }

View source →

KebabCaseKeysDeep<T>

Deeply kebab case object keys.

import { KebabCaseKeysDeep } from '@bedard/utils'

type Test = KebabCaseKeysDeep<{ foo_bar: { one_two: any }}> // { 'foo-bar': { 'one-two': any }}

View source →

Last<T>

Extract the last element of a string or array.

import { Last } from '@bedard/utils'

type Test1 = Last<'abc'> // 'c'
type Test2 = Last<[1, 2, 3]>, // 3

View source →

LastDigit<T>

Get the last digit of a number

import { LastDigit } from '@bedard/utils'

type Test1 = LastDigit<12> // 2
type Test2 = LastDigit<1.5> // 5

View source →

Line<T>

Describes a straight line between two vectors of length T.

import { Line } from '@bedard/utils'

type Test = Line<2> // [[number, number], [number, number]]

View source →

MapCapitalize<T>

Capitalize the first letter of each string.

import { MapCapitalize } from '@bedard/utils'

type Test = MapLowercase<['foo', 'bar']> // ['Foo', 'Bar']

View source →

MapLowercase<T>

Map strings to lowercase.

import { MapLowercase } from '@bedard/utils'

type Test = MapLowercase<['A', 'B']> // ['a', 'b']

View source →

MapUppercase<T>

Map strings to uppercase.

import { MapUppercase } from '@bedard/utils'

type Test = MapUppercase<['a', 'b']> // ['A', 'B']

View source →

MaybeArray<T>

Singular or array of T

import { MaybeArray } from '@bedard/utils'

type Test = MaybeArray<number> // number | number[]

View source →

Methods<T>

Create a string union of methods from T. This is the inverse of Properties<T>

import { Methods } from '@bedard/utils'

type Test = Methods<{ foo: string, bar(): any }> // 'bar'

View source →

MUX<T, U, S>

Logical MUX gate

import { MUX } from '@bedard/utils'

type Test = MUX<1, 0, 0> // 1

View source →

NAND<T, U>

Logical NAND gate

import { NAND } from '@bedard/utils'

type Test = NAND<0, 0> // 1

View source →

Negate<T>

Reverse the sign of a number

import { Negate } from '@bedard/utils'

type Test = Negate<1> // -1

View source →

NOR<T, U>

Logical NOR gate

import { NOR } from '@bedard/utils'

type Test = NOR<0, 0> // 1

View source →

NOT<T>

Reverse the Bit value of T.

import { NOT } from '@bedard/utils'

type Test = NOT<1> // /0

View source →

Not<T>

Reverse the boolean value of T.

import { Not } from '@bedard/utils'

type Test = Not<true> // false

View source →

OmitStartsWith<T, U>

Omit keys of T that start with U.

import { OmitStartsWith } from '@bedard/utils'

type Test = OmitStartsWith<{ FooOne: void; FooTwo: void; BarThree: void }, 'Bar'> // { FooOne: void; FooTwo: void }

View source →

OmitType<T, U>

Omit keys of T that extend U. This is the inverse of PickType<T, U>.

import { OmitType } from '@bedard/utils'

type Test = OmitType<{ foo: string, bar: number }, string> // { bar: number }

View source →

Opaque<T, Token>

Opaque type T with an optional Token. For more on opaque types, this article is a great place to start.

import { Opaque } from '@bedard/utils'

type USD = Opaque<number, 'usd'>

const dollars = 5 as USD

View source →

OptionalKeys<T, U>

Get optional keys from T, or make keys U of T optional.

import { OptionalKeys } from '@bedard/utils'

type Test1 = OptionalKeys<{ foo?: any, bar: any }> // 'foo'

type Test2 = OptionalKeys<{ foo: any, bar: any }, 'foo'> // { foo?: any, bar: any }

View source →

OR<T, U>

Logical OR gate

import { OR } from '@bedard/utils'

type Test = OR<0, 1> // 1

View source →

PadDigits<T, U>

Pad two numeric vectors so they are the same size

import { PadVec } from '@bedard/utils'

type Test = PadDigits<[1, 2], [3]> // [ [1, 2], [0, 3] ]

View source →

PascalCase<T>

Convert a string to pascal case.

import { PascalCase } from '@bedard/utils'

type Test = PascalCase<'foo-bar'> // 'FooBar'

View source →

PascalCaseKeys<T>

Kebab case object keys.

import { PascalCaseKeys } from '@bedard/utils'

type Test = PascalCaseKeys<{ foo_bar: any }> // { 'foo-bar': any }

View source →

PascalCaseKeysDeep<T>

Deeply pascal case object keys.

import { PascalCaseKeysDeep } from '@bedard/utils'

type Test = PascalCaseKeysDeep<{ foo_bar: { one_two: any }}> // { FooBar: { OneTwo: any }}

View source →

PickStartsWith<T, U>

Pick keys of T that start with U.

import { PickStartsWith } from '@bedard/utils'

type Test = PickStartsWith<{ FooOne: void; FooTwo: void ; Bar: void }, 'Foo'> // { FooOne: void; FooTwo: void }

View source →

PickType<T, U>

Pick keys of T that extend U. This is the inverse of OmitType<T, U>.

import { PickType } from '@bedard/utils'

type Test = PickType<{ foo: string, bar: number }, string> // { foo: string }

View source →

Pop<T>

Remove the last element of T.

import { Pop } from '@bedard/utils'

type Test = Pop<['foo', 'bar', 'baz']> // ['foo', 'bar']

View source →

Properties<T>

Create a string union of properties from T. This is the inverse of Methods<T>.

import { Properties } from '@bedard/utils'

type Test = Properties<{ foo: string, bar(): any }> // 'foo'

View source →

RequiredKeys<T, U>

Get required keys from T, or make keys U of T required.

import { RequiredKeys } from '@bedard/utils'

type Test = RequiredKeys<{ foo: any, bar?: any }> // 'foo'

type Test = RequiredKeys<{ foo?: any, bar?: any }, 'foo'> // { foo: any, bar?: any }

View source →

RgbColor<T>

Validate a rgb color

import { RgbColor } from '@bedard/utils'

const rgb = <T>(color: RgbColor<T>) => color

rgb('rgb(0, 0, 0)')

View source →

ScreamingSnakeCase<T>

Convert a string to screaming snake case.

import { ScreamingSnakeCase } from '@bedard/utils'

type Test = ScreamingSnakeCase<'fooBar'> // 'FOO_BAR'

View source →

ScreamingSnakeCaseKeys<T>

Screaming snake case object keys.

import { ScreamingSnakeCaseKeys } from '@bedard/utils'

type Test = ScreamingSnakeCaseKeys<{ foo_bar: any }> // { FOO_BAR: any }

View source →

ScreamingSnakeCaseKeysDeep<T>

Deeply screaming snake case object keys.

import { ScreamingSnakeCaseKeysDeep } from '@bedard/utils'

type Test = ScreamingSnakeCaseKeysDeep<{ foo_bar: { one_two: any }}> // { FOO_BAR: { ONE_TWO: any }}

View source →

Shift<T>

Remove the first element of T.

import { Shift } from '@bedard/utils'

type Test = Shift<['foo', 'bar', 'baz']> // ['bar', 'baz']

View source →

SnakeCase<T>

Convert a string to snake case.

import { SnakeCase } from '@bedard/utils'

type Test = SnakeCase<'fooBar'> // 'foo_bar'

View source →

SnakeCaseKeys<T>

Snake case object keys.

import { SnakeCaseKeys } from '@bedard/utils'

type Test = SnakeCaseKeys<{ fooBar: any }> // { foo_bar: any }

View source →

SnakeCaseKeysDeep<T>

Deeply snake case object keys.

import { SnakeCaseKeysDeep } from '@bedard/utils'

type Test = SnakeCaseKeysDeep<{ fooBar: { oneTwo: any }}> // { foo_bar: { one_two: any }}

View source →

Split<Source, Delimeter>

Split Source by Delimeter. This type is the opposite of Join. Note that to split by multiple delimeters the second argument must be a string[], as unions will create a distributive conditional type.

import { Split } from '@bedard/utils'

type Test1 = Split<'abc'> // ['a', 'b', 'c']

type Test2 = Split<'a.b.c', '.'> // ['a', 'b', 'c']

type Test3 = Split<'a.b-c', ['.', '-']> // ['a', 'b', 'c']

View source →

StartsWith<T, U>

Types true if T starts with U.

import { StartsWith } from '@bedard/utils'

type Test = StartsWith<'FooBar', 'Foo'> // true

View source →

SymmetricDifference<A, B>

The symmetric difference of A and B.

import { SymmetricDifference } from '@bedard/utils'

type Test1 = SymmetricDifference<'a' | 'b', 'b' | 'c'> // 'a' | 'c'

type Test2 = SymmetricDifference<{ a: any, b: any }, { b: any, c: any }> // { a: any, c: any }

View source →

ToNumber<T>

Convert numeric string to number

import { ToNumber } from '@bedard/types'

type Test = ToNumber<'123'> // 123

View source →

Transparent<T>

A type that does not encode any additional data. This is the inverse of Opaque<T>.

import { Transparent } from '@bedard/utils'

type Test = Transparent<string>

View source →

Trim<T>

Trim leading and trailing whitespace

import { Trim } from '@bedard/utils'

type Test = Trim<'  foo bar  '> // 'foo bar'

View source →

UnwrapOpaque<T>

Unwrap the underlying data of an Opaque<T> type.

import { UnwrapOpaque } from '@bedard/utils'

type Test1 = Opaque<string, 'example'>

type Test2 = UnwrapOpaque<Foo> // string

View source →

UnwrapJson<T>

Decodes type information from a Json<T> string.

import { Json, UnwrapJson } from '@bedard/utils'

type UserJson = Json<{ email: string }> // string

type User = UnwrapJson<UserJson> // { email: string }

View source →

ValueOf<T>

Generate a union from the values of T.

import { ValueOf } from '@bedard/utils'

type Test1 = ValueOf<Array<string>> // string

type Test2 = ValueOf<{ foo: number, bar: string }> // number | string

View source →

Vec<T>

Generate a uniform tuple of length T, with numeric values by default.

import { Vec } from '@bedard/utils'

type Test1 = Vec<3> // [number, number, number]

type Test2 = Vec<3, Thing> // [Thing, Thing, Thing]

View source →

Without<A, B>

Prohibit properties of A and omit properties of B.

import { Without } from '@bedard/utils'

type Test = Without<{ foo: any, bar: any }, { bar: any }> // { foo?: never }

View source →

XNOR<T, U>

Logical XNOR gate

import { XNOR } from '@bedard/types'

type Test = XNOR<0, 0> // 1

View source →

XOR<T, U>

Logical XOR gate

import { XOR } from '@bedard/utils'

type Test = XOR<0, 1> // 1

View source →

License

MIT

Copyright (c) 2021-present, Scott Bedard