From b6a21c54e0737293872e7bad241897d89a70704b Mon Sep 17 00:00:00 2001 From: Nick Evans Date: Wed, 19 Oct 2016 17:54:00 +0200 Subject: [PATCH] Added typings for _.defaultTo to lodash. --- lodash/lodash-tests.ts | 119 +++++++++++++++++++++++++++++++++++++++++ lodash/lodash.d.ts | 28 ++++++++++ 2 files changed, 147 insertions(+) diff --git a/lodash/lodash-tests.ts b/lodash/lodash-tests.ts index f0f958b0c8..8d6a2120fd 100644 --- a/lodash/lodash-tests.ts +++ b/lodash/lodash-tests.ts @@ -11109,6 +11109,125 @@ namespace TestConstant { } } +// _.defaultTo +namespace TestDefaultTo { + { + let result: number; + result = _.defaultTo(42, 42); + result = _.defaultTo(undefined, 42); + result = _.defaultTo(null, 42); + result = _.defaultTo(NaN, 42); + } + + { + let result: string; + result = _.defaultTo('a', 'default'); + result = _.defaultTo(undefined, 'default'); + result = _.defaultTo(null, 'default'); + } + + { + let result: boolean; + result = _.defaultTo(true, true); + result = _.defaultTo(undefined, true); + result = _.defaultTo(null, true); + } + + { + let result: string[]; + result = _.defaultTo(['a'], ['default']); + result = _.defaultTo(undefined, ['default']); + result = _.defaultTo(null, ['default']); + } + + { + let result: {a: string}; + result = _.defaultTo<{a: string}>({a: 'a'}, {a: 'a'}); + result = _.defaultTo<{a: string}>(undefined, {a: 'a'}); + result = _.defaultTo<{a: string}>(null, {a: 'a'}); + } + + { + let result: _.LoDashImplicitObjectWrapper; + result = _(42).defaultTo(42); + result = _(undefined).defaultTo(42); + result = _(null).defaultTo(42); + result = _(NaN).defaultTo(42); + } + + { + let result: _.LoDashImplicitObjectWrapper; + result = _('a').defaultTo('default'); + result = _(null).defaultTo('default'); + result = _(NaN).defaultTo('default'); + } + + { + let result: _.LoDashImplicitObjectWrapper; + result = _(true).defaultTo(true); + result = _(undefined).defaultTo(true); + result = _(null).defaultTo(true); + result = _(NaN).defaultTo(true); + } + + { + let result: _.LoDashImplicitObjectWrapper; + result = _(['a']).defaultTo(['default']); + result = _(undefined).defaultTo(['default']); + result = _(null).defaultTo(['default']); + result = _(NaN).defaultTo(['default']); + } + + { + let result: _.LoDashImplicitObjectWrapper<{ a: string }>; + result = _({ a: 'a' }).defaultTo({a : 'a'}); + result = _(undefined).defaultTo({a : 'a'}); + result = _(null).defaultTo({a : 'a'}); + result = _(NaN).defaultTo({a : 'a'}); + } + + { + let result: _.LoDashExplicitObjectWrapper; + result = _(42).chain().defaultTo(42); + result = _(undefined).chain().defaultTo(42); + result = _(null).chain().defaultTo(42); + result = _(NaN).chain().defaultTo(42); + } + + { + let result: _.LoDashExplicitObjectWrapper; + result = _('a').chain().defaultTo('default'); + result = _(undefined).chain().defaultTo('default'); + result = _(null).chain().defaultTo('default'); + result = _(NaN).chain().defaultTo('default'); + } + + { + let result: _.LoDashExplicitObjectWrapper; + result = _(true).chain().defaultTo(true); + result = _(undefined).chain().defaultTo(true); + result = _(null).chain().defaultTo(true); + result = _(NaN).chain().defaultTo(true); + } + + { + let result: _.LoDashExplicitObjectWrapper; + result = _(['a']).chain().defaultTo(['default']); + result = _(undefined).chain().defaultTo(['default']); + result = _(null).chain().defaultTo(['default']); + result = _(NaN).chain().defaultTo(['default']); + } + + { + let result: _.LoDashExplicitObjectWrapper<{ a: string }>; + result = _({ a: 'a' }).chain().defaultTo({a : 'a'}); + result = _(undefined).chain().defaultTo({a : 'a'}); + result = _(null).chain().defaultTo({a : 'a'}); + result = _(NaN).chain().defaultTo({a : 'a'}); + } + +} + // _.identity namespace TestIdentity { { diff --git a/lodash/lodash.d.ts b/lodash/lodash.d.ts index 3305528a5e..5033c24e3d 100644 --- a/lodash/lodash.d.ts +++ b/lodash/lodash.d.ts @@ -18206,6 +18206,34 @@ declare module _ { constant(): LoDashExplicitObjectWrapper<() => TResult>; } + //_.defaultTo + interface LoDashStatic { + /** + * Checks `value` to determine whether a default value should be returned in + * its place. The `defaultValue` is returned if `value` is `NaN`, `null`, + * or `undefined`. + * + * @param value The value to check. + * @param defaultValue The default value. + * @returns Returns the resolved value. + */ + defaultTo(value: T, defaultValue: T): T; + } + + interface LoDashImplicitWrapperBase { + /** + * @see _.defaultTo + */ + defaultTo(value: TResult): LoDashImplicitObjectWrapper; + } + + interface LoDashExplicitWrapperBase { + /** + * @see _.defaultTo + */ + defaultTo(value: TResult): LoDashExplicitObjectWrapper; + } + //_.identity interface LoDashStatic { /**