@solid-primitives/utils 2.0.2 → 2.1.1

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
@@ -36,6 +36,7 @@ __export(src_exports, {
36
36
  access: () => access,
37
37
  accessArray: () => accessArray,
38
38
  accessWith: () => accessWith,
39
+ arrayEquals: () => arrayEquals,
39
40
  asAccessor: () => asAccessor,
40
41
  asArray: () => asArray,
41
42
  chain: () => chain,
@@ -49,6 +50,7 @@ __export(src_exports, {
49
50
  createTriggerCache: () => createTriggerCache,
50
51
  entries: () => entries,
51
52
  forEachEntry: () => forEachEntry,
53
+ handleDiffArray: () => handleDiffArray,
52
54
  isClient: () => isClient,
53
55
  isDev: () => isDev,
54
56
  isObject: () => isObject,
@@ -77,6 +79,7 @@ function isObject(value) {
77
79
  return value !== null && (typeof value === "object" || typeof value === "function");
78
80
  }
79
81
  var compare = (a, b) => a < b ? -1 : a > b ? 1 : 0;
82
+ var arrayEquals = (a, b) => a === b || a.length === b.length && a.every((e, i) => e === b[i]);
80
83
  function chain(callbacks) {
81
84
  return (...args) => {
82
85
  for (const callback of callbacks) {
@@ -205,11 +208,43 @@ function createStaticStore(init) {
205
208
  };
206
209
  return [store, setter];
207
210
  }
211
+ function handleDiffArray(current, prev, handleAdded, handleRemoved) {
212
+ const currLength = current.length;
213
+ const prevLength = prev.length;
214
+ let i = 0;
215
+ if (!prevLength) {
216
+ for (; i < currLength; i++)
217
+ handleAdded(current[i]);
218
+ return;
219
+ }
220
+ if (!currLength) {
221
+ for (; i < prevLength; i++)
222
+ handleRemoved(prev[i]);
223
+ return;
224
+ }
225
+ for (; i < prevLength; i++) {
226
+ if (prev[i] !== current[i])
227
+ break;
228
+ }
229
+ let prevEl;
230
+ let currEl;
231
+ prev = prev.slice(i);
232
+ current = current.slice(i);
233
+ for (prevEl of prev) {
234
+ if (!current.includes(prevEl))
235
+ handleRemoved(prevEl);
236
+ }
237
+ for (currEl of current) {
238
+ if (!prev.includes(currEl))
239
+ handleAdded(currEl);
240
+ }
241
+ }
208
242
  // Annotate the CommonJS export names for ESM import in node:
209
243
  0 && (module.exports = {
210
244
  access,
211
245
  accessArray,
212
246
  accessWith,
247
+ arrayEquals,
213
248
  asAccessor,
214
249
  asArray,
215
250
  chain,
@@ -223,6 +258,7 @@ function createStaticStore(init) {
223
258
  createTriggerCache,
224
259
  entries,
225
260
  forEachEntry,
261
+ handleDiffArray,
226
262
  isClient,
227
263
  isDev,
228
264
  isObject,
package/dist/index.d.ts CHANGED
@@ -53,6 +53,10 @@ declare type NonIterable<T> = T & {
53
53
  declare type Definite<T> = {
54
54
  [K in keyof T]-?: T[K];
55
55
  };
56
+ /** An opposite of `Readonly`. Makes all the params mutable. */
57
+ declare type Mutable<T> = {
58
+ -readonly [K in keyof T]: T[K];
59
+ };
56
60
  /** Unwraps the type definition of an object, making it more readable */
57
61
  declare type Simplify<T> = T extends object ? {
58
62
  [K in keyof T]: T[K];
@@ -106,6 +110,10 @@ declare const ofClass: (v: any, c: AnyClass) => boolean;
106
110
  /** Check if value is typeof "object" or "function" */
107
111
  declare function isObject(value: any): value is AnyObject;
108
112
  declare const compare: (a: any, b: any) => number;
113
+ /**
114
+ * Check shallow array equality
115
+ */
116
+ declare const arrayEquals: (a: readonly unknown[], b: readonly unknown[]) => boolean;
109
117
  /**
110
118
  * Returns a function that will call all functions in the order they were chained with the same arguments.
111
119
  */
@@ -254,5 +262,14 @@ declare type StaticStoreSetter<T extends Readonly<AnyStatic>> = {
254
262
  * ```
255
263
  */
256
264
  declare function createStaticStore<T extends Readonly<AnyStatic>>(init: T): [access: T, write: StaticStoreSetter<T>];
265
+ /**
266
+ * Handle items removed and added to the array by diffing it by refference.
267
+ *
268
+ * @param current new array instance
269
+ * @param prev previous array copy
270
+ * @param handleAdded called once for every added item to array
271
+ * @param handleRemoved called once for every removed from array
272
+ */
273
+ declare function handleDiffArray<T>(current: T[], prev: T[], handleAdded: (item: T) => void, handleRemoved: (item: T) => void): void;
257
274
 
258
- export { AccessReturnTypes, AnyClass, AnyFunction, AnyObject, AnyStatic, DeepPartialAny, Definite, Directive, ExtractIfPossible, Falsy, FalsyValue, ItemsOf, ItemsOfMany, Many, MaybeAccessor, MaybeAccessorValue, Modify, ModifyDeep, 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 };
275
+ 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, arrayEquals, 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
@@ -29,6 +29,7 @@ function isObject(value) {
29
29
  return value !== null && (typeof value === "object" || typeof value === "function");
30
30
  }
31
31
  var compare = (a, b) => a < b ? -1 : a > b ? 1 : 0;
32
+ var arrayEquals = (a, b) => a === b || a.length === b.length && a.every((e, i) => e === b[i]);
32
33
  function chain(callbacks) {
33
34
  return (...args) => {
34
35
  for (const callback of callbacks) {
@@ -157,10 +158,42 @@ function createStaticStore(init) {
157
158
  };
158
159
  return [store, setter];
159
160
  }
161
+ function handleDiffArray(current, prev, handleAdded, handleRemoved) {
162
+ const currLength = current.length;
163
+ const prevLength = prev.length;
164
+ let i = 0;
165
+ if (!prevLength) {
166
+ for (; i < currLength; i++)
167
+ handleAdded(current[i]);
168
+ return;
169
+ }
170
+ if (!currLength) {
171
+ for (; i < prevLength; i++)
172
+ handleRemoved(prev[i]);
173
+ return;
174
+ }
175
+ for (; i < prevLength; i++) {
176
+ if (prev[i] !== current[i])
177
+ break;
178
+ }
179
+ let prevEl;
180
+ let currEl;
181
+ prev = prev.slice(i);
182
+ current = current.slice(i);
183
+ for (prevEl of prev) {
184
+ if (!current.includes(prevEl))
185
+ handleRemoved(prevEl);
186
+ }
187
+ for (currEl of current) {
188
+ if (!prev.includes(currEl))
189
+ handleAdded(currEl);
190
+ }
191
+ }
160
192
  export {
161
193
  access,
162
194
  accessArray,
163
195
  accessWith,
196
+ arrayEquals,
164
197
  asAccessor,
165
198
  asArray,
166
199
  chain,
@@ -174,6 +207,7 @@ export {
174
207
  createTriggerCache,
175
208
  entries,
176
209
  forEachEntry,
210
+ handleDiffArray,
177
211
  isClient,
178
212
  isDev,
179
213
  isObject,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@solid-primitives/utils",
3
- "version": "2.0.2",
3
+ "version": "2.1.1",
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",