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

@board_tester/ts_utilities

v2.0.7

Published

Utility collection

Downloads

2

Readme

Utilities

Some useful utilities for ts:

  • Maybe: Nullability Helper for TypeScript
  • Arrays: Removing duplicates with Predicate
  • Browser: Find out used browser ("edge" | "opera" | "chrome" | "ie" | "firefox" | "safari" | "other")
  • Id: Generator uuid v4
  • Objects: Merge or combine objects
  • Parameter: Definition of parameter
  • Reviver: Interface helper to parse JSON
  • TimeDate: Creator of moment (includes Moment)
  • URL: URL Builder including parameters

Glossary

Usage

Installation:

npm install @board_tester/ts_utilities

Import

import { Maybe, Nothing, Array, Browser, Id, Objects, ..... } from '@board_tester/ts_utilities';

Maybe

const variable: number = 1;
Maybe.of(variable); //=> Just<number>(1)

const variable: number | undefined = undefined;
Maybe.of(variable); //=> Nothing

const variable: number | undefined = undefined;
const defaultValue: number = 0;
Maybe.of(variable, defaultValue); //=> Just<number>(0)

const variable: number | undefined = undefined;
const defaultValue: Maybe<number> = Maybe.of(5);
Maybe.of(variable, defaultValue); //=> Just<number>(5)

const variables: number[] = [ undefined, null, 2, 1];
Maybe.ofs(variables); //=> Just<number>(2) 

#deprecated, save use Maybe.of
const variable: string = 'hello';
Just(variable); //=> Just<string>('hello')

#deprecated, save use Maybe.of
const variable: string | undefined = undefined;
Just(variable); //=> throws Error
Nothing(); //=> Nothing

Id

const id : Id = Id.generate();

Browser

const browser : Browser = Browser.BROWSER;

Arrays

Arrays.removeDuplicates<T>(values: T[], predicate: (a: T, b: T) => boolean = (a, b) => a == b): T[]

Objects

Objects.merge(source1: any, source2: any): any
Objects.combine(source1: any, source2: any, target: any = {}): any

TimeDate

TimeDate.fromDate = (date: Date | Moment) => Moment;

TimeDate.fromMillis = (millis: number) => Moment;

TimeDate.fromString = (time: string, format: Maybe<string>) => Moment;

TimeDate.fromYearAndWeek = (year: number, week: number) => Moment

URL

URL.build(url: string, parameter: Maybe<Parameter>, withFalseValues = false): string

Reviver


// example for Moment @see Reviver.ts
class MomentReviver implements Reviver {

    responsible(_: any, value: any): boolean {

        return typeof value === 'string' && moment(value, true).isValid();

    }

    revive(_: any, value: any): moment.Moment {

    return moment(value);

    }

}

const map: Reviver[] = [new MomentReviver(), ......... ];

export const JSONReviver = (key: any, value: any): any => {

    return Maybe.of(map.find(reviver => reviver.responsible(key, value))).map(reviver => reviver.revive(key, value)).safe(() => value);

}

JSON.parse(json, JSONReviver);

Documentation

Functions on Maybe instances

MaybeInstance.map(f: (value: T) => R) => Maybe<R>

  • f: Function for map T -> R

Returns Maybe[R]

MaybeInstance.fmap(f: (value: T) => Maybe<R>) => Maybe<R>

  • f: Function for fmap T -> Maybe[R]

Returns Maybe[R]

MaybeInstance.isJust() => type is Just

Returns Type is Just

MaybeInstance.isNothing() => type is Nothing

Returns Type is Nothing

MaybeInstance.if(f: (value: T) => void) => Maybe<T>

  • f: Function to compute with value if present

Returns Maybe[T]

MaybeInstance.else(f: () => void) => Maybe<T>

  • f: Function to compute if no value is present

Returns Maybe[T]

MaybeInstance.reset(f: (value: T) => void) => Nothing<T>

  • f: Function to compute with value if present, then reset to Nothing

Returns Nothing[T]

MaybeInstance.safe(f: () => T) => T

  • f: Function to compute to create returnValue if no value is present

Returns [T]

MaybeInstance.unsafe() => T

Returns [T] (throws Error if no value is present)

MaybeInstance.unwrap() => T | undefined

Returns [T] if value is present or undefined

MaybeInstance.or(ifNothing: Maybe<T> | T) => Maybe<T>

  • ifNothing: Value or Maybe if instance is Nothing

Returns Maybe[T]

Maybe.compare(left: Maybe<T>,right: Maybe<T>, comp: ((left: T, right: T) => boolean), onBothNothing?: boolean)

  • left : first Maybe
  • right: right Maybe
  • compo: Function to be calculated for comparison
  • onBothNothing: boolean to return if both Maybe's are Nothing

Returns boolean