From 1e3835e65e25802c16eb82796ca11f34e4d1fa9e Mon Sep 17 00:00:00 2001 From: Andrei Alecu Date: Wed, 6 Jan 2016 19:25:44 +0200 Subject: [PATCH 1/4] underscore: fix chain with Dictionary I added a test for this, it would previously fail to compile. Also, using `_.values()` on a Dictionary would lose strong typing on the result, returning `any[]`. This now properly returns `T[]` in that case. --- underscore/underscore-tests.ts | 17 +++++++++++++++++ underscore/underscore.d.ts | 8 ++++++++ 2 files changed, 25 insertions(+) diff --git a/underscore/underscore-tests.ts b/underscore/underscore-tests.ts index 051e000d0..24815e1ce 100644 --- a/underscore/underscore-tests.ts +++ b/underscore/underscore-tests.ts @@ -479,3 +479,20 @@ _.chain(obj).map(function (value, key) { empty[key] = value; console.log("vk", value, key); }); + +function strong_typed_values_tests() { + var dictionaryLike: { [k: string] : {title: string, value: number} } = { + 'test' : { title: 'item1', value: 5 }, + 'another' : { title: 'item2', value: 8 }, + 'third' : { title: 'item3', value: 10 } + }, + empty = {}; + + _.chain(dictionaryLike).values().filter((r) => { + return r.value >= 8; + }).map((r) => { + return [r.title, true]; + }).object().value(); + + _.values<{title: string, value: number}>(dictionaryLike); +} diff --git a/underscore/underscore.d.ts b/underscore/underscore.d.ts index 8cf98071b..5b7e6a2f3 100644 --- a/underscore/underscore.d.ts +++ b/underscore/underscore.d.ts @@ -1192,6 +1192,13 @@ interface UnderscoreStatic { **/ keys(object: any): string[]; + /** + * Return all of the values of the object's properties. + * @param object Retrieve the values of all the properties on this object. + * @return List of all the values on `object`. + **/ + values(object: _.Dictionary): T[]; + /** * Return all of the values of the object's properties. * @param object Retrieve the values of all the properties on this object. @@ -1641,6 +1648,7 @@ interface UnderscoreStatic { * @return Wrapped `obj`. **/ chain(obj: T[]): _Chain; + chain(obj: _.Dictionary): _Chain; chain(obj: T): _Chain; } From dfe3e8d966bc27bc896728b3b0b773d868277142 Mon Sep 17 00:00:00 2001 From: Andrei Alecu Date: Thu, 7 Jan 2016 16:31:07 +0200 Subject: [PATCH 2/4] Also fix `_(dictionary)` --- underscore/underscore-tests.ts | 4 ++++ underscore/underscore.d.ts | 1 + 2 files changed, 5 insertions(+) diff --git a/underscore/underscore-tests.ts b/underscore/underscore-tests.ts index 24815e1ce..b57e6e37e 100644 --- a/underscore/underscore-tests.ts +++ b/underscore/underscore-tests.ts @@ -494,5 +494,9 @@ function strong_typed_values_tests() { return [r.title, true]; }).object().value(); + _(dictionaryLike).each((x) => { + console.log(x.title); + console.log(x.value.toFixed()); + }) _.values<{title: string, value: number}>(dictionaryLike); } diff --git a/underscore/underscore.d.ts b/underscore/underscore.d.ts index 5b7e6a2f3..468c32102 100644 --- a/underscore/underscore.d.ts +++ b/underscore/underscore.d.ts @@ -76,6 +76,7 @@ interface UnderscoreStatic { * as the first parameter can be invoked through this function. * @param key First argument to Underscore object functions. **/ + (value: _.Dictionary): Underscore; (value: Array): Underscore; (value: T): Underscore; From 07b956ceeacbe3c6cb5c5787813ea161697329a8 Mon Sep 17 00:00:00 2001 From: Andrei Alecu Date: Thu, 7 Jan 2016 16:36:06 +0200 Subject: [PATCH 3/4] Formatting. --- underscore/underscore-tests.ts | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/underscore/underscore-tests.ts b/underscore/underscore-tests.ts index b57e6e37e..661daf8c6 100644 --- a/underscore/underscore-tests.ts +++ b/underscore/underscore-tests.ts @@ -481,22 +481,22 @@ _.chain(obj).map(function (value, key) { }); function strong_typed_values_tests() { - var dictionaryLike: { [k: string] : {title: string, value: number} } = { - 'test' : { title: 'item1', value: 5 }, - 'another' : { title: 'item2', value: 8 }, - 'third' : { title: 'item3', value: 10 } - }, - empty = {}; + var dictionaryLike: { [k: string] : {title: string, value: number} } = { + 'test' : { title: 'item1', value: 5 }, + 'another' : { title: 'item2', value: 8 }, + 'third' : { title: 'item3', value: 10 } + }; - _.chain(dictionaryLike).values().filter((r) => { - return r.value >= 8; - }).map((r) => { - return [r.title, true]; - }).object().value(); + _.chain(dictionaryLike).values().filter((r) => { + return r.value >= 8; + }).map((r) => { + return [r.title, true]; + }).object().value(); - _(dictionaryLike).each((x) => { - console.log(x.title); - console.log(x.value.toFixed()); - }) - _.values<{title: string, value: number}>(dictionaryLike); + _(dictionaryLike).each((x) => { + console.log(x.title); + console.log(x.value.toFixed()); + }); + + _.values<{title: string, value: number}>(dictionaryLike); } From 2684cdb76471a68c691245c1d6e6f5e2f9a54f36 Mon Sep 17 00:00:00 2001 From: Andrei Alecu Date: Thu, 7 Jan 2016 16:48:50 +0200 Subject: [PATCH 4/4] Fix `size()` when in `chain()` --- underscore/underscore-tests.ts | 5 +++-- underscore/underscore.d.ts | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/underscore/underscore-tests.ts b/underscore/underscore-tests.ts index 661daf8c6..74cef85ec 100644 --- a/underscore/underscore-tests.ts +++ b/underscore/underscore-tests.ts @@ -493,10 +493,11 @@ function strong_typed_values_tests() { return [r.title, true]; }).object().value(); - _(dictionaryLike).each((x) => { + var x: number = _(dictionaryLike).chain().filter((x) => { console.log(x.title); console.log(x.value.toFixed()); - }); + return x.title == 'item1'; + }).size().value(); _.values<{title: string, value: number}>(dictionaryLike); } diff --git a/underscore/underscore.d.ts b/underscore/underscore.d.ts index 468c32102..b17785a0f 100644 --- a/underscore/underscore.d.ts +++ b/underscore/underscore.d.ts @@ -2824,7 +2824,7 @@ interface _Chain { * Wrapped type `any`. * @see _.size **/ - size(): _Chain; + size(): _ChainSingle; /********* * Arrays *