@applicaster/zapp-react-native-utils 15.1.16-rc.1 → 15.1.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -47,4 +47,17 @@ describe("reorderByIds", () => {
47
47
 
48
48
  expect(result.map((i) => i.key)).toEqual(["c", "b", "a"]);
49
49
  });
50
+
51
+ it("reorders duplicate items when using unique slot identifiers via getId", () => {
52
+ const duplicateItems = [
53
+ { id: "x", slot: "s1", name: "First X" },
54
+ { id: "y", slot: "s2", name: "Y" },
55
+ { id: "x", slot: "s3", name: "Second X" },
56
+ ];
57
+
58
+ const result = reorderByIds(duplicateItems, ["s3", "s1"], (i) => i.slot);
59
+
60
+ expect(result).toHaveLength(3);
61
+ expect(result.map((i) => i.name)).toEqual(["Second X", "Y", "First X"]);
62
+ });
50
63
  });
@@ -130,7 +130,7 @@ export const anyTruthy = (xs: boolean[]) => xs.some(Boolean);
130
130
  export function reorderByIds<T>(
131
131
  items: T[],
132
132
  orderedIds: string[],
133
- getId: (item: T) => string | string[] = (item: any) => item?.id
133
+ getId: (item: T) => string = (item: any) => item?.id
134
134
  ): T[] {
135
135
  if (!items || items.length === 0) return [];
136
136
  if (!orderedIds || orderedIds.length === 0) return [...items];
@@ -138,14 +138,16 @@ export function reorderByIds<T>(
138
138
  const itemMap = new Map<string, T>();
139
139
 
140
140
  items.forEach((item) => {
141
- const rawId = getId(item);
142
- const keys = Array.isArray(rawId) ? rawId : [rawId];
143
-
144
- keys.forEach((key) => {
145
- if (key !== undefined && key !== null && key !== "") {
146
- itemMap.set(String(key), item);
147
- }
148
- });
141
+ const id = getId(item);
142
+
143
+ if (
144
+ id !== undefined &&
145
+ id !== null &&
146
+ id !== "" &&
147
+ !itemMap.has(String(id))
148
+ ) {
149
+ itemMap.set(String(id), item);
150
+ }
149
151
  });
150
152
 
151
153
  const reorderedSubset: T[] = [];
@@ -155,48 +157,29 @@ export function reorderByIds<T>(
155
157
  const strId = String(id);
156
158
  const item = itemMap.get(strId);
157
159
 
158
- if (item && !reorderedSubset.includes(item)) {
160
+ if (item && !orderedIdsSet.has(strId)) {
159
161
  reorderedSubset.push(item);
160
- const rawId = getId(item);
161
- const keys = Array.isArray(rawId) ? rawId : [rawId];
162
-
163
- keys.forEach((k) => {
164
- if (k !== undefined && k !== null && k !== "") {
165
- orderedIdsSet.add(String(k));
166
- }
167
- });
162
+ orderedIdsSet.add(strId);
168
163
  }
169
164
  });
170
165
 
171
166
  if (reorderedSubset.length === 0) return [...items];
172
167
 
173
168
  let subsetIdx = 0;
174
- const result: T[] = [];
175
-
176
- items.forEach((item) => {
177
- const rawId = getId(item);
178
- const keys = Array.isArray(rawId) ? rawId : [rawId];
179
-
180
- const isMatched = keys.some(
181
- (k) =>
182
- k !== undefined &&
183
- k !== null &&
184
- k !== "" &&
185
- orderedIdsSet.has(String(k))
186
- );
187
169
 
188
- if (isMatched) {
189
- if (subsetIdx < reorderedSubset.length) {
190
- result.push(reorderedSubset[subsetIdx++]);
191
- }
192
- } else {
193
- result.push(item);
170
+ return items.map((item) => {
171
+ const id = getId(item);
172
+
173
+ if (
174
+ id !== undefined &&
175
+ id !== null &&
176
+ id !== "" &&
177
+ orderedIdsSet.has(String(id)) &&
178
+ subsetIdx < reorderedSubset.length
179
+ ) {
180
+ return reorderedSubset[subsetIdx++];
194
181
  }
195
- });
196
-
197
- while (subsetIdx < reorderedSubset.length) {
198
- result.push(reorderedSubset[subsetIdx++]);
199
- }
200
182
 
201
- return result;
183
+ return item;
184
+ });
202
185
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@applicaster/zapp-react-native-utils",
3
- "version": "15.1.16-rc.1",
3
+ "version": "15.1.17",
4
4
  "description": "Applicaster Zapp React Native utilities package",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -27,7 +27,7 @@
27
27
  },
28
28
  "homepage": "https://github.com/applicaster/quickbrick#readme",
29
29
  "dependencies": {
30
- "@applicaster/applicaster-types": "15.1.16-rc.1",
30
+ "@applicaster/applicaster-types": "15.1.17",
31
31
  "buffer": "^5.2.1",
32
32
  "camelize": "^1.0.0",
33
33
  "dayjs": "^1.11.10",