useful-object
Advanced tools
Comparing version 0.1.3 to 0.2.0
@@ -5,5 +5,9 @@ import { Observable } from "rxjs"; | ||
get(path: string, def?: any): any; | ||
getSafe<T, R>(fn: (obj: T) => R, def?: R): any; | ||
toObservable(): Observable<any>; | ||
toPromise(): Promise<any>; | ||
} | ||
interface Promise<T> { | ||
delay(ms: number): Promise<T>; | ||
} | ||
} |
@@ -8,2 +8,10 @@ "use strict"; | ||
}; | ||
Object.prototype.getSafe = function take(fn, def) { | ||
var returnValue = def; | ||
try { | ||
returnValue = fn(this); | ||
} | ||
catch (error) { } | ||
return returnValue; | ||
}; | ||
Object.prototype.toObservable = function () { | ||
@@ -16,1 +24,5 @@ return rxjs_1.of(this); | ||
}; | ||
Promise.prototype.delay = function (ms) { | ||
var _this = this; | ||
return new Promise(function (resolve) { return setTimeout(function () { return resolve(_this); }, ms); }); | ||
}; |
{ | ||
"name": "useful-object", | ||
"version": "0.1.3", | ||
"version": "0.2.0", | ||
"description": "Typescript util to add useful methods to global Object type.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -13,3 +13,3 @@ # Useful Object | ||
```sh | ||
```typescript | ||
import "useful-object"; // 16 KB | ||
@@ -29,2 +29,23 @@ | ||
## Need type safety? | ||
```typescript | ||
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); // Avi | ||
const lastName: string = obj.getSafe((obj: MyInterface) => obj.name.lastName); // Punes | ||
``` | ||
## Test | ||
@@ -31,0 +52,0 @@ |
2969
38
54