Skip to content

Commit 43a56d8

Browse files
committed
[hygiene] Coerce major Id arguments
Addresses #27
1 parent 880c831 commit 43a56d8

File tree

4 files changed

+218
-56
lines changed

4 files changed

+218
-56
lines changed

src/common.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import {SortKey} from './common.d';
1+
import {Id, SortKey} from './common.d';
2+
import {EMPTY_STRING} from './common/strings';
23

34
export const defaultSorter = (sortKey1: SortKey, sortKey2: SortKey): number =>
45
sortKey1 < sortKey2 ? -1 : 1;
6+
7+
export const id = (key: unknown): Id => EMPTY_STRING + key;

src/common/map.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export const mapEnsure = <Key, Value>(
4141
getDefaultValue: () => Value,
4242
): Value => {
4343
if (!collHas(map, key)) {
44-
map.set(key, getDefaultValue());
44+
mapSet(map, key, getDefaultValue());
4545
}
4646
return mapGet(map, key) as Value;
4747
};

src/store.ts

Lines changed: 94 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,14 @@ import {
3737
jsonParse,
3838
jsonString,
3939
} from './common/other';
40-
import {DEFAULT, EMPTY_OBJECT, NUMBER, TYPE, getTypeOf} from './common/strings';
40+
import {
41+
DEFAULT,
42+
EMPTY_OBJECT,
43+
EMPTY_STRING,
44+
NUMBER,
45+
TYPE,
46+
getTypeOf,
47+
} from './common/strings';
4148
import {Id, IdOrNull, Ids, Json} from './common.d';
4249
import {
4350
IdMap,
@@ -71,6 +78,7 @@ import {
7178
arrayFilter,
7279
arrayForEach,
7380
arrayHas,
81+
arrayMap,
7482
arrayPair,
7583
arrayPush,
7684
} from './common/array';
@@ -85,6 +93,7 @@ import {
8593
collSize4,
8694
} from './common/coll';
8795
import {getListenerFunctions} from './common/listeners';
96+
import {id} from './common';
8897

8998
type SchemaMap = IdMap2<CellSchema>;
9099
type RowMap = IdMap<Cell>;
@@ -361,7 +370,7 @@ export const createStore: typeof createStoreDecl = (): Store => {
361370
);
362371

363372
const getNewRowId = (tableMap: TableMap | undefined): Id => {
364-
const rowId = '' + nextRowId++;
373+
const rowId = EMPTY_STRING + nextRowId++;
365374
if (!collHas(tableMap, rowId)) {
366375
return rowId;
367376
}
@@ -587,8 +596,11 @@ export const createStore: typeof createStoreDecl = (): Store => {
587596
}
588597
};
589598

590-
const fluentTransaction = (actions?: () => unknown): Store => {
591-
transaction(actions);
599+
const fluentTransaction = (
600+
actions: (...idArgs: Id[]) => unknown,
601+
...args: unknown[]
602+
): Store => {
603+
transaction(() => actions(...arrayMap(args, id)));
592604
return store;
593605
};
594606

@@ -602,28 +614,29 @@ export const createStore: typeof createStoreDecl = (): Store => {
602614
const getTableIds = (): Ids => mapKeys(tablesMap);
603615

604616
const getTable = (tableId: Id): Table =>
605-
mapToObj<RowMap, Row>(mapGet(tablesMap, tableId), mapToObj);
617+
mapToObj<RowMap, Row>(mapGet(tablesMap, id(tableId)), mapToObj);
606618

607-
const getRowIds = (tableId: Id): Ids => mapKeys(mapGet(tablesMap, tableId));
619+
const getRowIds = (tableId: Id): Ids =>
620+
mapKeys(mapGet(tablesMap, id(tableId)));
608621

609622
const getRow = (tableId: Id, rowId: Id): Row =>
610-
mapToObj(mapGet(mapGet(tablesMap, tableId), rowId));
623+
mapToObj(mapGet(mapGet(tablesMap, id(tableId)), id(rowId)));
611624

612625
const getCellIds = (tableId: Id, rowId: Id): Ids =>
613-
mapKeys(mapGet(mapGet(tablesMap, tableId), rowId));
626+
mapKeys(mapGet(mapGet(tablesMap, id(tableId)), id(rowId)));
614627

615628
const getCell = (tableId: Id, rowId: Id, cellId: Id): CellOrUndefined =>
616-
mapGet(mapGet(mapGet(tablesMap, tableId), rowId), cellId);
629+
mapGet(mapGet(mapGet(tablesMap, id(tableId)), id(rowId)), id(cellId));
617630

618631
const hasTables = (): boolean => !collIsEmpty(tablesMap);
619632

620-
const hasTable = (tableId: Id): boolean => collHas(tablesMap, tableId);
633+
const hasTable = (tableId: Id): boolean => collHas(tablesMap, id(tableId));
621634

622635
const hasRow = (tableId: Id, rowId: Id): boolean =>
623-
collHas(mapGet(tablesMap, tableId), rowId);
636+
collHas(mapGet(tablesMap, id(tableId)), id(rowId));
624637

625638
const hasCell = (tableId: Id, rowId: Id, cellId: Id): boolean =>
626-
collHas(mapGet(mapGet(tablesMap, tableId), rowId), cellId);
639+
collHas(mapGet(mapGet(tablesMap, id(tableId)), id(rowId)), id(cellId));
627640

628641
const getJson = (): Json => jsonString(tablesMap);
629642

@@ -635,21 +648,32 @@ export const createStore: typeof createStoreDecl = (): Store => {
635648
);
636649

637650
const setTable = (tableId: Id, table: Table): Store =>
638-
fluentTransaction(() =>
639-
validateTable(table, tableId) ? setValidTable(tableId, table) : 0,
651+
fluentTransaction(
652+
(tableId) =>
653+
validateTable(table, tableId) ? setValidTable(tableId, table) : 0,
654+
tableId,
640655
);
641656

642657
const setRow = (tableId: Id, rowId: Id, row: Row): Store =>
643-
fluentTransaction(() =>
644-
validateRow(tableId, rowId, row)
645-
? setValidRow(tableId, getOrCreateTable(tableId), rowId, row)
646-
: 0,
658+
fluentTransaction(
659+
(tableId, rowId) =>
660+
validateRow(id(tableId), id(rowId), row)
661+
? setValidRow(
662+
id(tableId),
663+
getOrCreateTable(id(tableId)),
664+
id(rowId),
665+
row,
666+
)
667+
: 0,
668+
tableId,
669+
rowId,
647670
);
648671

649672
const addRow = (tableId: Id, row: Row): Id | undefined =>
650673
transaction(() => {
651674
let rowId: Id | undefined = undefined;
652675
if (validateRow(tableId, rowId, row)) {
676+
tableId = id(tableId);
653677
setValidRow(
654678
tableId,
655679
getOrCreateTable(tableId),
@@ -661,38 +685,46 @@ export const createStore: typeof createStoreDecl = (): Store => {
661685
});
662686

663687
const setPartialRow = (tableId: Id, rowId: Id, partialRow: Row): Store =>
664-
fluentTransaction(() => {
665-
if (validateRow(tableId, rowId, partialRow, 1)) {
666-
const table = getOrCreateTable(tableId);
667-
objForEach(partialRow, (cell, cellId) =>
668-
setCellIntoDefaultRow(tableId, table, rowId, cellId, cell),
669-
);
670-
}
671-
});
688+
fluentTransaction(
689+
(tableId, rowId) => {
690+
if (validateRow(tableId, rowId, partialRow, 1)) {
691+
const table = getOrCreateTable(tableId);
692+
objForEach(partialRow, (cell, cellId) =>
693+
setCellIntoDefaultRow(tableId, table, rowId, cellId, cell),
694+
);
695+
}
696+
},
697+
tableId,
698+
rowId,
699+
);
672700

673701
const setCell = (
674702
tableId: Id,
675703
rowId: Id,
676704
cellId: Id,
677705
cell: Cell | MapCell,
678706
): Store =>
679-
fluentTransaction(() =>
680-
ifNotUndefined(
681-
getValidatedCell(
682-
tableId,
683-
rowId,
684-
cellId,
685-
isFunction(cell) ? cell(getCell(tableId, rowId, cellId)) : cell,
686-
),
687-
(validCell) =>
688-
setCellIntoDefaultRow(
707+
fluentTransaction(
708+
(tableId, rowId, cellId) =>
709+
ifNotUndefined(
710+
getValidatedCell(
689711
tableId,
690-
getOrCreateTable(tableId),
691712
rowId,
692713
cellId,
693-
validCell,
714+
isFunction(cell) ? cell(getCell(tableId, rowId, cellId)) : cell,
694715
),
695-
),
716+
(validCell) =>
717+
setCellIntoDefaultRow(
718+
tableId,
719+
getOrCreateTable(tableId),
720+
rowId,
721+
cellId,
722+
validCell,
723+
),
724+
),
725+
tableId,
726+
rowId,
727+
cellId,
696728
);
697729

698730
const setJson = (json: Json): Store => {
@@ -717,15 +749,19 @@ export const createStore: typeof createStoreDecl = (): Store => {
717749
const delTables = (): Store => fluentTransaction(() => setValidTables({}));
718750

719751
const delTable = (tableId: Id): Store =>
720-
fluentTransaction(() =>
721-
collHas(tablesMap, tableId) ? delValidTable(tableId) : 0,
752+
fluentTransaction(
753+
(tableId) => (collHas(tablesMap, tableId) ? delValidTable(tableId) : 0),
754+
tableId,
722755
);
723756

724757
const delRow = (tableId: Id, rowId: Id): Store =>
725-
fluentTransaction(() =>
726-
ifNotUndefined(mapGet(tablesMap, tableId), (tableMap) =>
727-
collHas(tableMap, rowId) ? delValidRow(tableId, tableMap, rowId) : 0,
728-
),
758+
fluentTransaction(
759+
(tableId, rowId) =>
760+
ifNotUndefined(mapGet(tablesMap, tableId), (tableMap) =>
761+
collHas(tableMap, rowId) ? delValidRow(tableId, tableMap, rowId) : 0,
762+
),
763+
tableId,
764+
rowId,
729765
);
730766

731767
const delCell = (
@@ -734,14 +770,18 @@ export const createStore: typeof createStoreDecl = (): Store => {
734770
cellId: Id,
735771
forceDel?: boolean,
736772
): Store =>
737-
fluentTransaction(() =>
738-
ifNotUndefined(mapGet(tablesMap, tableId), (tableMap) =>
739-
ifNotUndefined(mapGet(tableMap, rowId), (rowMap) =>
740-
collHas(rowMap, cellId)
741-
? delValidCell(tableId, tableMap, rowId, rowMap, cellId, forceDel)
742-
: 0,
773+
fluentTransaction(
774+
(tableId, rowId, cellId) =>
775+
ifNotUndefined(mapGet(tablesMap, tableId), (tableMap) =>
776+
ifNotUndefined(mapGet(tableMap, rowId), (rowMap) =>
777+
collHas(rowMap, cellId)
778+
? delValidCell(tableId, tableMap, rowId, rowMap, cellId, forceDel)
779+
: 0,
780+
),
743781
),
744-
),
782+
tableId,
783+
rowId,
784+
cellId,
745785
);
746786

747787
const delSchema = (): Store =>
@@ -862,7 +902,7 @@ export const createStore: typeof createStoreDecl = (): Store => {
862902
);
863903

864904
const forEachRow = (tableId: Id, rowCallback: RowCallback): void =>
865-
collForEach(mapGet(tablesMap, tableId), (rowMap, rowId) =>
905+
collForEach(mapGet(tablesMap, id(tableId)), (rowMap, rowId) =>
866906
rowCallback(rowId, (cellCallback) => mapForEach(rowMap, cellCallback)),
867907
);
868908

@@ -871,7 +911,7 @@ export const createStore: typeof createStoreDecl = (): Store => {
871911
rowId: Id,
872912
cellCallback: CellCallback,
873913
): void =>
874-
mapForEach(mapGet(mapGet(tablesMap, tableId), rowId), cellCallback);
914+
mapForEach(mapGet(mapGet(tablesMap, id(tableId)), id(rowId)), cellCallback);
875915

876916
const addTablesListener = (listener: TablesListener, mutator?: boolean): Id =>
877917
addListener(listener, tablesListeners[mutator ? 1 : 0]);

0 commit comments

Comments
 (0)