@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 Mayberight
: right Maybecompo
: Function to be calculated for comparisononBothNothing
: boolean to return if both Maybe's are Nothing
Returns boolean