Useful Object
Typescript util to add useful methods to global Object type.
Installation
npm install useful-object --save
Usage
Object
get
import "useful-object";
....
const obj: any = {
name: {
firstName: "Avi",
lastName: "Punes"
}
};
obj.get("name.firstName");
Need type safety? use getSafe
interface MyInterface {
name: {
firstName: string;
lastName: string;
};
}
const obj: MyInterface = {
name: {
firstName: "Avi",
lastName: "Punes"
}
};
const firstName: string = obj.getSafe<MyInterface, string>(
obj => obj.name.firstName
);
const lastName: string = obj.getSafe((obj: MyInterface) => obj.name.lastName);
toPromise
const firstName: string = await obj.get("name.firstName").toPromise();
toObservable
obj.get("name.firstName")
.toObservable()
.subscribe((firstName: string) => console.log(firstName));
Promise
delay
const firstName: string = await obj
.get("name.firstName")
.toPromise()
.delay(1000);
Array
subset
const array = [1, 50, 3, 10];
array.subset("0..1");
array.subset("*..1");
array.subset("1..2");
array.subset("2..*");
array.subset("*..*");
Test
npm test