solid-ctrl-flow 1.0.59 → 1.0.60

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.d.ts CHANGED
@@ -35,6 +35,13 @@ export declare class Atom<T> {
35
35
  * @param param0 The {@link Signal} to forward
36
36
  */
37
37
  static from<T>([get, set]: Signal<T>): Atom<T>;
38
+ /**
39
+ * Creates an {@link Atom} based on an object property.
40
+ * The result of {@link k} will be memoized, while {@link obj}'s one won't
41
+ * @param obj The object containing the property
42
+ * @param k A function that returns the key of the property and will be passed to {@link nameOf}
43
+ */
44
+ static prop<T, K extends keyof T>(obj: Accessor<T>, k: (x: NamesOf<T>) => K): Atom<T[K]>;
38
45
  /**
39
46
  * Creates a bindable data source.
40
47
  * If {@link bind} returns an {@link Atom} it gets wrapped, otherwise it creates a {@link Signal} using {@link f} and uses it to store the value until {@link bind}'s value changes
@@ -149,6 +156,19 @@ export declare function memoProps<T>(obj: T, keys?: undefined, equals?: Equals):
149
156
 
150
157
  export declare function memoProps<T, K extends readonly (keyof T)[]>(obj: T, keys: K, equals?: Equals): Pick<T, K[number]>;
151
158
 
159
+ /**
160
+ * Function that returns the name of the field accessed inside of {@link f}.
161
+ * This function is not meant to be used on its own because it has very poor type inference
162
+ * @param f A function that returns a value based on {@link NamesOf}
163
+ * @returns The same thing that {@link f} returned
164
+ */
165
+ export declare const nameOf: <T, const R>(f: (x: NamesOf<T>) => R) => R;
166
+
167
+ /** Type that maps each key of {@link T} to itself */
168
+ export declare type NamesOf<T> = {
169
+ [k in keyof T]: k;
170
+ };
171
+
152
172
  /**
153
173
  * Allows the use of {@link Case}s inside of {@link Switch}es.
154
174
  * Example:
package/dist/index.mjs CHANGED
@@ -67,6 +67,8 @@ function runWithContext(ctx2, value, f) {
67
67
  function untrackCall(f, ...args) {
68
68
  return untrack(() => f.apply(this, args));
69
69
  }
70
+ const PROXY_KEYOF = new Proxy({}, { get: (_, k) => k });
71
+ const nameOf = (f) => f(PROXY_KEYOF);
70
72
  function memoProps(obj, keys = Object.keys(obj), equals = false) {
71
73
  const out = {};
72
74
  for (const elm of keys)
@@ -249,14 +251,14 @@ class Atom {
249
251
  * @param from Conversion function from {@link D} to {@link S}
250
252
  */
251
253
  convert(to, from) {
252
- return new Atom(() => to(this.value), (x) => this.value = from(x));
254
+ return new Atom(() => to(this.value), (v) => this.value = from(v));
253
255
  }
254
256
  /**
255
257
  * Creates an {@link Atom} that forwards an {@link Accessor} to another {@link Atom}
256
258
  * @param f The reactive {@link Accessor} to the {@link Atom} to forward
257
259
  */
258
260
  static unwrap(f) {
259
- return new this(() => f().value, (x) => f().value = x);
261
+ return new this(() => f().value, (v) => f().value = v);
260
262
  }
261
263
  /**
262
264
  * Creates an {@link Atom} based on a {@link Signal}
@@ -265,6 +267,16 @@ class Atom {
265
267
  static from([get, set]) {
266
268
  return new this(get, (v) => set(() => v));
267
269
  }
270
+ /**
271
+ * Creates an {@link Atom} based on an object property.
272
+ * The result of {@link k} will be memoized, while {@link obj}'s one won't
273
+ * @param obj The object containing the property
274
+ * @param k A function that returns the key of the property and will be passed to {@link nameOf}
275
+ */
276
+ static prop(obj, k) {
277
+ const temp = createMemo(() => nameOf(k));
278
+ return new this(() => obj()[temp()], (v) => obj()[temp()] = v);
279
+ }
268
280
  static source(bind, f = createSignal) {
269
281
  return this.unwrap(createMemo(on(bind, (x) => x ?? Atom.from(f()))));
270
282
  }
@@ -287,6 +299,7 @@ export {
287
299
  createExtractor,
288
300
  debug,
289
301
  memoProps,
302
+ nameOf,
290
303
  runWithContext,
291
304
  splitAndMemoProps,
292
305
  untrackCall,
package/dist/index.umd.js CHANGED
@@ -69,6 +69,8 @@
69
69
  function untrackCall(f, ...args) {
70
70
  return solidJs.untrack(() => f.apply(this, args));
71
71
  }
72
+ const PROXY_KEYOF = new Proxy({}, { get: (_, k) => k });
73
+ const nameOf = (f) => f(PROXY_KEYOF);
72
74
  function memoProps(obj, keys = Object.keys(obj), equals = false) {
73
75
  const out = {};
74
76
  for (const elm of keys)
@@ -251,14 +253,14 @@
251
253
  * @param from Conversion function from {@link D} to {@link S}
252
254
  */
253
255
  convert(to, from) {
254
- return new Atom(() => to(this.value), (x) => this.value = from(x));
256
+ return new Atom(() => to(this.value), (v) => this.value = from(v));
255
257
  }
256
258
  /**
257
259
  * Creates an {@link Atom} that forwards an {@link Accessor} to another {@link Atom}
258
260
  * @param f The reactive {@link Accessor} to the {@link Atom} to forward
259
261
  */
260
262
  static unwrap(f) {
261
- return new this(() => f().value, (x) => f().value = x);
263
+ return new this(() => f().value, (v) => f().value = v);
262
264
  }
263
265
  /**
264
266
  * Creates an {@link Atom} based on a {@link Signal}
@@ -267,6 +269,16 @@
267
269
  static from([get, set]) {
268
270
  return new this(get, (v) => set(() => v));
269
271
  }
272
+ /**
273
+ * Creates an {@link Atom} based on an object property.
274
+ * The result of {@link k} will be memoized, while {@link obj}'s one won't
275
+ * @param obj The object containing the property
276
+ * @param k A function that returns the key of the property and will be passed to {@link nameOf}
277
+ */
278
+ static prop(obj, k) {
279
+ const temp = solidJs.createMemo(() => nameOf(k));
280
+ return new this(() => obj()[temp()], (v) => obj()[temp()] = v);
281
+ }
270
282
  static source(bind, f = solidJs.createSignal) {
271
283
  return this.unwrap(solidJs.createMemo(solidJs.on(bind, (x) => x ?? Atom.from(f()))));
272
284
  }
@@ -287,6 +299,7 @@
287
299
  exports2.createExtractor = createExtractor;
288
300
  exports2.debug = debug;
289
301
  exports2.memoProps = memoProps;
302
+ exports2.nameOf = nameOf;
290
303
  exports2.runWithContext = runWithContext;
291
304
  exports2.splitAndMemoProps = splitAndMemoProps;
292
305
  exports2.untrackCall = untrackCall;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "solid-ctrl-flow",
3
- "version": "1.0.59",
3
+ "version": "1.0.60",
4
4
  "description": "Control flow components for solid-js",
5
5
  "author": "AFatNiBBa",
6
6
  "license": "ISC",
package/readMe.md CHANGED
@@ -178,7 +178,10 @@ Customizable and simplified wrappers for reactive states
178
178
  > An `Atom`-specific (optimized) version of `unwrap()` that allows the destructuring of its result
179
179
  >
180
180
  > #### `Atom.from()`
181
- > Creates an `Atom` from a `Signal`
181
+ > Creates an `Atom` based on a `Signal`
182
+ >
183
+ > #### `Atom.prop()`
184
+ > Creates an `Atom` based on an object property
182
185
  >
183
186
  > #### `Atom.source()`
184
187
  > Similiar to `Atom.unwrap()`, but if the `Accessor` doesn't return anything it automatically creates an internal `Signal` in which to store the value
@@ -215,6 +218,9 @@ return <>
215
218
  ### `untrackCall()`
216
219
  Calls a function untracking what happens inside of it but not what gets passed as its argument
217
220
 
221
+ ### `nameOf()`
222
+ Utility function that powers `Atom.prop()`
223
+
218
224
  ### `memoProps()`
219
225
  Creates a partial version of an object with memoized remaining properties
220
226