Skip to content

Commit

Permalink
Do not use isNumericKey - it is broken for objects
Browse files Browse the repository at this point in the history
  • Loading branch information
ftrimble committed Apr 13, 2019
1 parent d194f6d commit 11fad8c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
18 changes: 3 additions & 15 deletions src/map.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
const DEFAULT_MAP_PARALLELISM = 10;

/**
* Converts numeric keys to actual numbers - `for in` will always provide string keys, even
* for array indexes or numeric keys of objects
*/
function asNumericKey(key: string): string | number {
// tslint:disable-next-line:no-any (isNaN for some reason is not typed to accept strings)
if (!isNaN(key as any)) {
return Number(key);
} else {
return key;
}
}

/**
* Produces a new collection of values by mapping each value in coll through the iteratee
* function. The iteratee is called with an item from coll and a callback for when it has finished
Expand Down Expand Up @@ -86,8 +73,9 @@ export async function mapLimit<V>(input: any, limit: number, iteratee: any): Pro
return [];
}

const isArray = input.length !== undefined;
const size = (() => {
if (input.length !== undefined) {
if (isArray) {
return input.length;
}

Expand All @@ -103,7 +91,7 @@ export async function mapLimit<V>(input: any, limit: number, iteratee: any): Pro

let i = 0;
for (const key in input) {
const possiblyNumericKey = asNumericKey(key);
const possiblyNumericKey = isArray ? i : key;
allValues[size - 1 - i] = [input[key], i, possiblyNumericKey];
++i;
}
Expand Down
8 changes: 8 additions & 0 deletions test/filter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ test('filters arrays with indices', async t => {
t.deepEqual(output, [2]);
});

test('filters objects with numeric keys', async t => {
const input = { 1: 'asdf', 2: 'abcd' };
const output = await promiseUtils.filter(input, async (value, i) => {
return (i as any) === 1 || (i as any) === 2;
});
t.deepEqual(output, []);
});

test('filters objects', async t => {
const input = { a: 1, b: 2 };
const output = await promiseUtils.filter(input, async (value: any) => {
Expand Down

0 comments on commit 11fad8c

Please sign in to comment.