Skip to content

Instantly share code, notes, and snippets.

@fostyfost
Created April 30, 2020 16:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fostyfost/4dee26dc0cc09babf8ab9defd5c860e6 to your computer and use it in GitHub Desktop.
Save fostyfost/4dee26dc0cc09babf8ab9defd5c860e6 to your computer and use it in GitHub Desktop.
ImmutableJS: `toOrderedMap` util
/* eslint-disable @typescript-eslint/no-explicit-any */
import { fromJS, OrderedMap } from 'immutable';
interface ObjectWithId {
id: number;
[property: string]: any;
}
export const toOrderedMap = <T = any>(arr: ObjectWithId[]): OrderedMap<number, T> => {
return arr.reduce(
(acc, item: ObjectWithId) => acc.set(item.id, fromJS(item) as T),
OrderedMap() as OrderedMap<number, T>,
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment