-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from snyk/fix/object-fromEntries-not-supported…
…-node-10 fix: object.fromEntries not supported node10
- Loading branch information
Showing
5 changed files
with
82 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// We are using the same types that Object.fromEntries has | ||
// eslint-disable-next-line import/prefer-default-export, @typescript-eslint/no-explicit-any | ||
export function fromEntries(entries: Iterable<readonly any[]>): any; | ||
export function fromEntries<T = any>( | ||
entries: Iterable<readonly [PropertyKey, T]>, | ||
): { | ||
[k in PropertyKey]: T; | ||
} { | ||
return [...entries].reduce((obj, [key, val]) => { | ||
obj[key] = val; // eslint-disable-line no-param-reassign | ||
return obj; | ||
}, {}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { fromEntries } from '../src/lib/utils'; | ||
describe('fromEntries', () => { | ||
it('Object transformations', async () => { | ||
// Arrange | ||
const obj = { a: 1, b: 2, c: 3 }; | ||
const expected = { a: 2, b: 4, c: 6 }; | ||
|
||
// Action | ||
const fromEntriesRes = fromEntries(Object.entries(obj).map(([key, val]) => [key, val * 2])); | ||
|
||
// Assert | ||
expect(fromEntriesRes).toMatchObject(expected); | ||
}); | ||
|
||
it('Converting an Array to an Object', async () => { | ||
// Arrange | ||
const arr = [ | ||
['0', 'a'], | ||
['1', 'b'], | ||
['2', 'c'], | ||
]; | ||
const expected = { 0: 'a', 1: 'b', 2: 'c' }; | ||
|
||
// Action | ||
const fromEntriesRes = fromEntries(arr); | ||
|
||
// Assert | ||
expect(fromEntriesRes).toMatchObject(expected); | ||
}); | ||
|
||
it('Converting a Map to an Object', async () => { | ||
// Arrange | ||
const map = new Map([ | ||
['foo', 12], | ||
['baz', 42], | ||
]); | ||
const expected = { foo: 12, baz: 42 }; | ||
|
||
// Action | ||
const fromEntriesRes = fromEntries(map); | ||
|
||
// Assert | ||
expect(fromEntriesRes).toMatchObject(expected); | ||
}); | ||
|
||
it('Duplicate key', async () => { | ||
// Arrange | ||
const arr = [ | ||
['foo', 1], | ||
['foo', 2], | ||
]; | ||
const expected = { foo: 2 }; | ||
|
||
// Action | ||
const fromEntriesRes = fromEntries(arr); | ||
|
||
// Assert | ||
expect(fromEntriesRes).toMatchObject(expected); | ||
}); | ||
}); |