ts-optchain
Advanced tools
Comparing version 0.1.1 to 0.1.2
@@ -18,2 +18,4 @@ "use strict"; | ||
c: [-100, 200, -300], | ||
d: null, | ||
e: { f: false }, | ||
}); | ||
@@ -27,2 +29,3 @@ expect(x.a()).toEqual('hello'); | ||
expect(x.d.e('optional default value')).toEqual('optional default value'); | ||
expect(x.e.f()).toEqual(false); | ||
expect(x.y.z.a.b.c.d.e.f.g.h.i.j.k()).toBeUndefined(); | ||
@@ -42,7 +45,7 @@ }); | ||
expect(index_1.oc(x).c[100].u.v()).toEqual(x.c && x.c[100] && x.c[100].u && x.c[100].u.v); | ||
expect(index_1.oc(x).c[100].u.v(1234)).toEqual(x.c && x.c[100] && x.c[100].u && x.c[100].u.v || 1234); | ||
expect(index_1.oc(x).c[100].u.v(1234)).toEqual((x.c && x.c[100] && x.c[100].u && x.c[100].u.v) || 1234); | ||
expect(index_1.oc(x).e.f()).toEqual(x.e && x.e.f); | ||
expect(index_1.oc(x).e.f('optional default value')).toEqual(x.e && x.e.f || 'optional default value'); | ||
expect(index_1.oc(x).e.g(() => 'Yo Yo')()).toEqual((x.e && x.e.g || (() => 'Yo Yo'))()); | ||
expect(index_1.oc(x).e.f('optional default value')).toEqual((x.e && x.e.f) || 'optional default value'); | ||
expect(index_1.oc(x).e.g(() => 'Yo Yo')()).toEqual(((x.e && x.e.g) || (() => 'Yo Yo'))()); | ||
}); | ||
}); |
@@ -21,3 +21,8 @@ /** | ||
*/ | ||
(defaultValue: Defined<T>): Defined<T>; | ||
(defaultValue: NonNullable<T>): NonNullable<T>; | ||
/** | ||
* Data accessor with null default value. | ||
* @param defaultValue | ||
*/ | ||
(nullDefaultValue: T extends null ? null : never): Defined<T>; | ||
} | ||
@@ -29,3 +34,3 @@ /** | ||
export declare type ObjectWrapper<T> = { | ||
[K in keyof T]-?: OCType<Defined<T[K]>>; | ||
[K in keyof T]-?: OCType<T[K]>; | ||
}; | ||
@@ -49,3 +54,3 @@ /** | ||
*/ | ||
export declare type OCType<T> = IDataAccessor<T> & DataWrapper<T>; | ||
export declare type OCType<T> = IDataAccessor<T> & DataWrapper<NonNullable<T>>; | ||
/** | ||
@@ -52,0 +57,0 @@ * Proxies access to the passed object to support optional chaining w/ default values. |
@@ -9,3 +9,2 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
; | ||
/** | ||
@@ -36,9 +35,6 @@ * Proxies access to the passed object to support optional chaining w/ default values. | ||
function oc(data) { | ||
return new Proxy(((defaultValue) => (data !== undefined ? data : defaultValue)), { | ||
return new Proxy(((defaultValue) => (data == null ? defaultValue : data)), { | ||
get: (target, key) => { | ||
const obj = target(); | ||
if ('object' !== typeof obj) { | ||
return oc(); | ||
} | ||
return oc(obj[key]); | ||
return oc(typeof obj === 'object' ? obj[key] : undefined); | ||
}, | ||
@@ -45,0 +41,0 @@ }); |
{ | ||
"name": "ts-optchain", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Optional Chaining for TypeScript", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -13,4 +13,4 @@ # Optional Chaining for TypeScript | ||
* NodeJS >= 6 | ||
* TypeScript >= 2.8 | ||
- NodeJS >= 6 | ||
- TypeScript >= 2.8 | ||
@@ -46,3 +46,2 @@ ## Example Usage | ||
// Here are a few examples of deep object traversal using (a) optional chaining vs | ||
@@ -66,3 +65,3 @@ // (b) logic expressions. Each of the following pairs are equivalent in | ||
oc(x).c[100].u.v(1234); // 1234 | ||
x.c && x.c[100] && x.c[100].u && x.c[100].u.v || 1234; | ||
(x.c && x.c[100] && x.c[100].u && x.c[100].u.v) || 1234; | ||
@@ -73,3 +72,3 @@ oc(x).e.f(); // undefined | ||
oc(x).e.f('optional default value'); // 'optional default value' | ||
x.e && x.e.f || 'optional default value'; | ||
(x.e && x.e.f) || 'optional default value'; | ||
@@ -79,3 +78,3 @@ // NOTE: working with function value types can be risky. Additional run-time | ||
oc(x).e.g(() => 'Yo Yo')(); // 'Yo Yo' | ||
(x.e && x.e.g || (() => 'Yo Yo'))(); | ||
((x.e && x.e.g) || (() => 'Yo Yo'))(); | ||
``` | ||
@@ -109,3 +108,3 @@ | ||
function getHomeStreet(user: IUser, defaultValue?: string) { | ||
return user.home && user.home.address && user.home.address.street || defaultValue; | ||
return (user.home && user.home.address && user.home.address.street) || defaultValue; | ||
} | ||
@@ -126,5 +125,5 @@ ``` | ||
* Compile-time validation of the path `home.address.street` | ||
* Compile-time validation of the expected type of the value at `home.address.street` | ||
* Development-time code-completion assistance when manipulating the path `home.address.street` using tools like Visual Studio Code. | ||
- Compile-time validation of the path `home.address.street` | ||
- Compile-time validation of the expected type of the value at `home.address.street` | ||
- Development-time code-completion assistance when manipulating the path `home.address.street` using tools like Visual Studio Code. | ||
@@ -195,3 +194,3 @@ ## Solution | ||
* [Optional Chaining for JavaScript (TC39 Proposal)](https://github.com/tc39/proposal-optional-chaining) | ||
- [Optional Chaining for JavaScript (TC39 Proposal)](https://github.com/tc39/proposal-optional-chaining) | ||
@@ -198,0 +197,0 @@ ## License |
12951
173
192