Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions types/underscore/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5100,7 +5100,7 @@ declare module _ {
* Wrapped type `any[]`.
* @see _.map
**/
map<TResult>(iterator: _.ListIterator<T, TResult>, context?: any): _Chain<TResult>;
map<TResult>(iterator: _.ListIterator<T, TResult>, context?: any): _Chain<TResult, TResult[]>;

/**
* Wrapped type `any[]`.
Expand All @@ -5112,7 +5112,7 @@ declare module _ {
* Wrapped type `any[]`.
* @see _.map
**/
map<TResult>(iterator: _.ObjectIterator<T, TResult>, context?: any): _Chain<TResult>;
map<TResult>(iterator: _.ObjectIterator<T, TResult>, context?: any): _Chain<TResult, TResult[]>;

/**
* @see _.map
Expand Down
40 changes: 40 additions & 0 deletions types/underscore/underscore-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,46 @@ _(stooges)
.indexBy('age')
.value()['40'].age;

let pensioners: string[] = _.chain(stooges)
.filter(p => p.age >= 60)
.map(p => p.name)
.value();

var usersData: _.Dictionary<{ age: number; name: string }> = {
'user id': { name: 'moe', age: 40 },
'other user Id': { name: 'larry', age: 50 },
'fake id': { name: 'curly', age: 60 },
};

let youngPeopleId: string[] = _.chain(usersData)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test should actually fail, as performing filter on a a Dictionary returns T[], thus performing map on it will need the following iterator:

(value: T, index: number, list: T[]): TResult

The reason this test passes now is because of missing signature of filter for _Chain which should be:

filter(iterator: _.ObjectIterator<T, boolean>, context?: any): _Chain<T, T[]>;

But fixing it will not even help as the entire _Chain interface is malformed :-( and I'm working on a wide fix for it.
I would rather you only add tests that use the fixed map method

Copy link
Copy Markdown
Contributor Author

@Radullus Radullus Jul 1, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have right. I will stick in this test to 'map' function. I interface definition I was trying to test.

.map((p, k: string) => k)
.value();

let usersTable: { age: number; name: string; id: string }[] = _.chain(usersData)
.map<{ age: number; name: string; id: string }>((p, k: string) => {
return { id: k, ...p };
})
.value();

// Test map function with _ChainOfArrays<>
let usersTable_2 /*: { age: number; name: string; id: string }[][]*/ = _.chain(usersData)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did you remove the type here?
It is best to use the flatten() method here and then get the value

Copy link
Copy Markdown
Contributor Author

@Radullus Radullus Jul 1, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because the type by the underscore should be like this one in the comment, But with current typescript definition, There is missing one level of array nest.
{ age: number; name: string; id: string }[][] // type return by underscore
{ age: number; name: string; id: string }[] // type with is calculate base of current definitions
It would require more changes in the underscore typescript definition. And I'm not certain what should be changed?
interface _ChainOfArrays<T> extends _Chain<T[]>
to
interface _ChainOfArrays<T> extends _Chain<T[], T[][]>
it will not match with data return from function 'groupBy'

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct. Lets leave it be for now

.map<{ age: number; name: string; id: string }>((p, k: string) => {
return [{ id: k, ...p }];
})
.value();

let usersTable_3 /*: { score: number; fullName: string; login: string }[][]*/ = _.chain(usersTable)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here - why did you remove the type?

.map<{ score: number; fullName: string; login: string }>(p => {
return [
{
login: p.id,
fullName: p.name,
score: p.age,
},
];
})
.value();

_.countBy([1, 2, 3, 4, 5], (num) => (num % 2 == 0) ? 'even' : 'odd');

_.shuffle([1, 2, 3, 4, 5, 6]);
Expand Down