@solid-primitives/utils 2.0.3 → 2.1.0

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.
package/dist/index.cjs CHANGED
@@ -49,6 +49,7 @@ __export(src_exports, {
49
49
  createTriggerCache: () => createTriggerCache,
50
50
  entries: () => entries,
51
51
  forEachEntry: () => forEachEntry,
52
+ handleDiffArray: () => handleDiffArray,
52
53
  isClient: () => isClient,
53
54
  isDev: () => isDev,
54
55
  isObject: () => isObject,
@@ -205,6 +206,37 @@ function createStaticStore(init) {
205
206
  };
206
207
  return [store, setter];
207
208
  }
209
+ function handleDiffArray(current, prev, handleAdded, handleRemoved) {
210
+ const currLength = current.length;
211
+ const prevLength = prev.length;
212
+ let i = 0;
213
+ if (!prevLength) {
214
+ for (; i < currLength; i++)
215
+ handleAdded(current[i]);
216
+ return;
217
+ }
218
+ if (!currLength) {
219
+ for (; i < prevLength; i++)
220
+ handleRemoved(prev[i]);
221
+ return;
222
+ }
223
+ for (; i < prevLength; i++) {
224
+ if (prev[i] !== current[i])
225
+ break;
226
+ }
227
+ let prevEl;
228
+ let currEl;
229
+ prev = prev.slice(i);
230
+ current = current.slice(i);
231
+ for (prevEl of prev) {
232
+ if (!current.includes(prevEl))
233
+ handleRemoved(prevEl);
234
+ }
235
+ for (currEl of current) {
236
+ if (!prev.includes(currEl))
237
+ handleAdded(currEl);
238
+ }
239
+ }
208
240
  // Annotate the CommonJS export names for ESM import in node:
209
241
  0 && (module.exports = {
210
242
  access,
@@ -223,6 +255,7 @@ function createStaticStore(init) {
223
255
  createTriggerCache,
224
256
  entries,
225
257
  forEachEntry,
258
+ handleDiffArray,
226
259
  isClient,
227
260
  isDev,
228
261
  isObject,
package/dist/index.d.ts CHANGED
@@ -258,5 +258,14 @@ declare type StaticStoreSetter<T extends Readonly<AnyStatic>> = {
258
258
  * ```
259
259
  */
260
260
  declare function createStaticStore<T extends Readonly<AnyStatic>>(init: T): [access: T, write: StaticStoreSetter<T>];
261
+ /**
262
+ * Handle items removed and added to the array by diffing it by refference.
263
+ *
264
+ * @param current new array instance
265
+ * @param prev previous array copy
266
+ * @param handleAdded called once for every added item to array
267
+ * @param handleRemoved called once for every removed from array
268
+ */
269
+ declare function handleDiffArray<T>(current: T[], prev: T[], handleAdded: (item: T) => void, handleRemoved: (item: T) => void): void;
261
270
 
262
- export { AccessReturnTypes, AnyClass, AnyFunction, AnyObject, AnyStatic, DeepPartialAny, Definite, Directive, ExtractIfPossible, Falsy, FalsyValue, ItemsOf, ItemsOfMany, Many, MaybeAccessor, MaybeAccessorValue, Modify, ModifyDeep, Mutable, NonIterable, Noop, OnAccessEffectFunction, Position, PrimitiveValue, RequiredKeys, SetterValue, Simplify, StaticStoreSetter, Tail, Trigger, TriggerCache, Truthy, UnboxLazy, UnionToIntersection, Values, access, accessArray, accessWith, asAccessor, asArray, chain, clamp, compare, createCallbackStack, createMicrotask, createProxy, createStaticStore, createTrigger, createTriggerCache, entries, forEachEntry, isClient, isDev, isObject, isProd, isServer, keys, noop, ofClass, onRootCleanup, promiseTimeout, raceTimeout, warn, withAccess };
271
+ export { AccessReturnTypes, AnyClass, AnyFunction, AnyObject, AnyStatic, DeepPartialAny, Definite, Directive, ExtractIfPossible, Falsy, FalsyValue, ItemsOf, ItemsOfMany, Many, MaybeAccessor, MaybeAccessorValue, Modify, ModifyDeep, Mutable, NonIterable, Noop, OnAccessEffectFunction, Position, PrimitiveValue, RequiredKeys, SetterValue, Simplify, StaticStoreSetter, Tail, Trigger, TriggerCache, Truthy, UnboxLazy, UnionToIntersection, Values, access, accessArray, accessWith, asAccessor, asArray, chain, clamp, compare, createCallbackStack, createMicrotask, createProxy, createStaticStore, createTrigger, createTriggerCache, entries, forEachEntry, handleDiffArray, isClient, isDev, isObject, isProd, isServer, keys, noop, ofClass, onRootCleanup, promiseTimeout, raceTimeout, warn, withAccess };
package/dist/index.js CHANGED
@@ -157,6 +157,37 @@ function createStaticStore(init) {
157
157
  };
158
158
  return [store, setter];
159
159
  }
160
+ function handleDiffArray(current, prev, handleAdded, handleRemoved) {
161
+ const currLength = current.length;
162
+ const prevLength = prev.length;
163
+ let i = 0;
164
+ if (!prevLength) {
165
+ for (; i < currLength; i++)
166
+ handleAdded(current[i]);
167
+ return;
168
+ }
169
+ if (!currLength) {
170
+ for (; i < prevLength; i++)
171
+ handleRemoved(prev[i]);
172
+ return;
173
+ }
174
+ for (; i < prevLength; i++) {
175
+ if (prev[i] !== current[i])
176
+ break;
177
+ }
178
+ let prevEl;
179
+ let currEl;
180
+ prev = prev.slice(i);
181
+ current = current.slice(i);
182
+ for (prevEl of prev) {
183
+ if (!current.includes(prevEl))
184
+ handleRemoved(prevEl);
185
+ }
186
+ for (currEl of current) {
187
+ if (!prev.includes(currEl))
188
+ handleAdded(currEl);
189
+ }
190
+ }
160
191
  export {
161
192
  access,
162
193
  accessArray,
@@ -174,6 +205,7 @@ export {
174
205
  createTriggerCache,
175
206
  entries,
176
207
  forEachEntry,
208
+ handleDiffArray,
177
209
  isClient,
178
210
  isDev,
179
211
  isObject,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@solid-primitives/utils",
3
- "version": "2.0.3",
3
+ "version": "2.1.0",
4
4
  "description": "A bunch of reactive utility types and functions, for building primitives with Solid.js",
5
5
  "author": "Damian Tarnawski @thetarnav <gthetarnav@gmail.com>",
6
6
  "license": "MIT",