Useful Object
Typescript util to add useful methods to global Object type.
Installation
npm install useful-object --save
Usage
get
import "useful-object";
....
const obj: any = {
name: {
firstName: "Avi",
lastName: "Punes"
}
};
obj.get("name.firstName");
Need type safety?
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();
const firstName: string = await obj
.get("name.firstName")
.toPromise()
.delay(1000);
toObservable
obj.get("name.firstName")
.toObservable()
.subscribe((firstName: string) => console.log(firstName));
Test
npm test