Skip to content

Commit 61ec24c

Browse files
authored
Extract common utils from both adapter files (#952)
* Extract common utils from both adapter files * Remove unused import * Use array return for better minification
1 parent 7db3e48 commit 61ec24c

File tree

4 files changed

+76
-56
lines changed

4 files changed

+76
-56
lines changed

src/entities/sorted_state_adapter.ts

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ import {
88
} from './models'
99
import { createStateOperator } from './state_adapter'
1010
import { createUnsortedStateAdapter } from './unsorted_state_adapter'
11-
import { selectIdValue } from './utils'
11+
import {
12+
selectIdValue,
13+
ensureEntitiesArray,
14+
splitAddedUpdatedEntities
15+
} from './utils'
1216

1317
export function createSortedStateAdapter<T>(
1418
selectId: IdSelector<T>,
@@ -25,14 +29,12 @@ export function createSortedStateAdapter<T>(
2529
}
2630

2731
function addManyMutably(
28-
newModels: T[] | Record<EntityId, T>,
32+
newEntities: T[] | Record<EntityId, T>,
2933
state: R
3034
): void {
31-
if (!Array.isArray(newModels)) {
32-
newModels = Object.values(newModels)
33-
}
35+
newEntities = ensureEntitiesArray(newEntities)
3436

35-
const models = newModels.filter(
37+
const models = newEntities.filter(
3638
model => !(selectIdValue(model, selectId) in state.entities)
3739
)
3840

@@ -41,14 +43,15 @@ export function createSortedStateAdapter<T>(
4143
}
4244
}
4345

44-
function setAllMutably(models: T[] | Record<EntityId, T>, state: R): void {
45-
if (!Array.isArray(models)) {
46-
models = Object.values(models)
47-
}
46+
function setAllMutably(
47+
newEntities: T[] | Record<EntityId, T>,
48+
state: R
49+
): void {
50+
newEntities = ensureEntitiesArray(newEntities)
4851
state.entities = {}
4952
state.ids = []
5053

51-
addManyMutably(models, state)
54+
addManyMutably(newEntities, state)
5255
}
5356

5457
function updateOneMutably(update: Update<T>, state: R): void {
@@ -86,24 +89,14 @@ export function createSortedStateAdapter<T>(
8689
}
8790

8891
function upsertManyMutably(
89-
entities: T[] | Record<EntityId, T>,
92+
newEntities: T[] | Record<EntityId, T>,
9093
state: R
9194
): void {
92-
if (!Array.isArray(entities)) {
93-
entities = Object.values(entities)
94-
}
95-
96-
const added: T[] = []
97-
const updated: Update<T>[] = []
98-
99-
for (const entity of entities) {
100-
const id = selectIdValue(entity, selectId)
101-
if (id in state.entities) {
102-
updated.push({ id, changes: entity })
103-
} else {
104-
added.push(entity)
105-
}
106-
}
95+
const [added, updated] = splitAddedUpdatedEntities<T>(
96+
newEntities,
97+
selectId,
98+
state
99+
)
107100

108101
updateManyMutably(updated, state)
109102
addManyMutably(added, state)

src/entities/unsorted_state_adapter.ts

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ import {
99
createStateOperator,
1010
createSingleArgumentStateOperator
1111
} from './state_adapter'
12-
import { selectIdValue } from './utils'
12+
import {
13+
selectIdValue,
14+
ensureEntitiesArray,
15+
splitAddedUpdatedEntities
16+
} from './utils'
1317

1418
export function createUnsortedStateAdapter<T>(
1519
selectId: IdSelector<T>
@@ -27,25 +31,27 @@ export function createUnsortedStateAdapter<T>(
2731
state.entities[key] = entity
2832
}
2933

30-
function addManyMutably(entities: T[] | Record<EntityId, T>, state: R): void {
31-
if (!Array.isArray(entities)) {
32-
entities = Object.values(entities)
33-
}
34+
function addManyMutably(
35+
newEntities: T[] | Record<EntityId, T>,
36+
state: R
37+
): void {
38+
newEntities = ensureEntitiesArray(newEntities)
3439

35-
for (const entity of entities) {
40+
for (const entity of newEntities) {
3641
addOneMutably(entity, state)
3742
}
3843
}
3944

40-
function setAllMutably(entities: T[] | Record<EntityId, T>, state: R): void {
41-
if (!Array.isArray(entities)) {
42-
entities = Object.values(entities)
43-
}
45+
function setAllMutably(
46+
newEntities: T[] | Record<EntityId, T>,
47+
state: R
48+
): void {
49+
newEntities = ensureEntitiesArray(newEntities)
4450

4551
state.ids = []
4652
state.entities = {}
4753

48-
addManyMutably(entities, state)
54+
addManyMutably(newEntities, state)
4955
}
5056

5157
function removeOneMutably(key: EntityId, state: R): void {
@@ -140,24 +146,14 @@ export function createUnsortedStateAdapter<T>(
140146
}
141147

142148
function upsertManyMutably(
143-
entities: T[] | Record<EntityId, T>,
149+
newEntities: T[] | Record<EntityId, T>,
144150
state: R
145151
): void {
146-
if (!Array.isArray(entities)) {
147-
entities = Object.values(entities)
148-
}
149-
150-
const added: T[] = []
151-
const updated: Update<T>[] = []
152-
153-
for (const entity of entities) {
154-
const id = selectIdValue(entity, selectId)
155-
if (id in state.entities) {
156-
updated.push({ id, changes: entity })
157-
} else {
158-
added.push(entity)
159-
}
160-
}
152+
const [added, updated] = splitAddedUpdatedEntities<T>(
153+
newEntities,
154+
selectId,
155+
state
156+
)
161157

162158
updateManyMutably(updated, state)
163159
addManyMutably(added, state)

src/entities/utils.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { IdSelector } from './models'
1+
import { EntityState, IdSelector, Update, EntityId } from './models'
22

33
export function selectIdValue<T>(entity: T, selectId: IdSelector<T>) {
44
const key = selectId(entity)
@@ -16,3 +16,34 @@ export function selectIdValue<T>(entity: T, selectId: IdSelector<T>) {
1616

1717
return key
1818
}
19+
20+
export function ensureEntitiesArray<T>(
21+
entities: T[] | Record<EntityId, T>
22+
): T[] {
23+
if (!Array.isArray(entities)) {
24+
entities = Object.values(entities)
25+
}
26+
27+
return entities
28+
}
29+
30+
export function splitAddedUpdatedEntities<T>(
31+
newEntities: T[] | Record<EntityId, T>,
32+
selectId: IdSelector<T>,
33+
state: EntityState<T>
34+
): [T[], Update<T>[]] {
35+
newEntities = ensureEntitiesArray(newEntities)
36+
37+
const added: T[] = []
38+
const updated: Update<T>[] = []
39+
40+
for (const entity of newEntities) {
41+
const id = selectIdValue(entity, selectId)
42+
if (id in state.entities) {
43+
updated.push({ id, changes: entity })
44+
} else {
45+
added.push(entity)
46+
}
47+
}
48+
return [added, updated]
49+
}

src/serializableStateInvariantMiddleware.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export function findNonSerializableValue(
5757
const hasIgnoredPaths = ignoredPaths.length > 0
5858

5959
for (const [key, nestedValue] of entries) {
60-
const nestedPath = path ? path + '.' + key : key // path.concat(property)
60+
const nestedPath = path ? path + '.' + key : key
6161

6262
if (hasIgnoredPaths && ignoredPaths.indexOf(nestedPath) >= 0) {
6363
continue

0 commit comments

Comments
 (0)