Functional imports when using multiple collections is fiddly and messy because you end up with loads of imports to manage in every source file, and you have to use aliasing a lot to account for commonalities among function names (e.g. freeze, get, etc.). I've discovered that a good pattern is to create an internal collections script that imports all of the required functions and exports them as unified collection types/namespaces. It's better for this to be done by the consuming application, because you lose the ability to tree-shake unused imports, so you want to make sure you only expose the methods you're actually using.
In the docs area, I want a page that lets you select all the collections and methods you want, then automatically generates a script with all of imports and exports ready to go.
Example script that would be generated:
export {batch, isMutable, isImmutable} from '@collectable/core';
import {
List as _List,
fromArray as list_fromArray,
size as list_size,
get as list_get,
iterate as list_iterate,
skip as list_skip,
take as list_take,
} from '@collectable/list';
export type List<T> = _List<T>;
export namespace List {
export const fromArray = list_fromArray;
export const size = list_size;
export const get = list_get;
export const iterate = list_iterate;
export const skip = list_skip;
export const take = list_take;
}
import {
Map as _Map,
empty as map_empty,
entries as map_entries,
keys as map_keys,
get as map_get,
set as map_set,
thaw as map_thaw,
isFrozen as map_isFrozen,
update as map_update,
} from '@collectable/map';
export type Map<K, V> = _Map<K, V>;
export namespace Map {
export const empty = map_empty;
export const entries = map_entries;
export const keys = map_keys;
export const get = map_get;
export const set = map_set;
export const thaw = map_thaw;
export const isFrozen = map_isFrozen;
export const update = map_update;
}
import {
SortedMap as _SortedMap,
SortedMapEntry,
empty as smap_empty,
get as smap_get,
set as smap_set,
thaw as smap_thaw,
isFrozen as smap_isFrozen,
update as smap_update,
} from '@collectable/sorted-map';
export type SortedMap<K, V> = _SortedMap<K, V>;
export namespace SortedMap {
export const empty = smap_empty;
export const get = smap_get;
export const set = smap_set;
export const thaw = smap_thaw;
export const isFrozen = smap_isFrozen;
export const update = smap_update;
export function KeyComparator(a: SortedMapEntry<any, any, any>, b: SortedMapEntry<any, any, any>): number {
return a.key - b.key;
}
}
import {
Set as _Set,
empty as set_empty,
add as set_add,
remove as set_remove,
values as set_values,
isEmpty as set_isEmpty,
} from '@collectable/set';
export type Set<T> = _Set<T>;
export namespace Set {
export const empty = set_empty;
export const add = set_add;
export const remove = set_remove;
export const values = set_values;
export const isEmpty = set_isEmpty;
}
import {
SortedSet as _SortedSet,
empty as sset_empty,
add as sset_add,
remove as sset_remove,
} from '@collectable/sorted-set';
export type SortedSet<T> = _SortedSet<T>;
export namespace SortedSet {
export const empty = sset_empty;
export const add = sset_add;
export const remove = sset_remove;
}
Functional imports when using multiple collections is fiddly and messy because you end up with loads of imports to manage in every source file, and you have to use aliasing a lot to account for commonalities among function names (e.g.
freeze,get, etc.). I've discovered that a good pattern is to create an internal collections script that imports all of the required functions and exports them as unified collection types/namespaces. It's better for this to be done by the consuming application, because you lose the ability to tree-shake unused imports, so you want to make sure you only expose the methods you're actually using.In the docs area, I want a page that lets you select all the collections and methods you want, then automatically generates a script with all of imports and exports ready to go.
Example script that would be generated: