solid-ctrl-flow 1.0.58 → 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
@@ -19,6 +19,12 @@ export declare class Atom<T> {
19
19
  * @param f Function that creates a new value based on the current one
20
20
  */
21
21
  update<V extends T>(f: (prev: T) => V): V;
22
+ /**
23
+ * Creates a new {@link Atom} that applies a conversion to the current one
24
+ * @param to Conversion function from {@link S} to {@link D}
25
+ * @param from Conversion function from {@link D} to {@link S}
26
+ */
27
+ convert<R>(to: (x: T) => R, from: (x: R) => T): Atom<R>;
22
28
  /**
23
29
  * Creates an {@link Atom} that forwards an {@link Accessor} to another {@link Atom}
24
30
  * @param f The reactive {@link Accessor} to the {@link Atom} to forward
@@ -30,12 +36,12 @@ export declare class Atom<T> {
30
36
  */
31
37
  static from<T>([get, set]: Signal<T>): Atom<T>;
32
38
  /**
33
- * Creates a new {@link Atom} that applies a conversion to another {@link Atom}
34
- * @param atom The {@link Atom} to which to apply the conversion
35
- * @param to Conversion function from {@link S} to {@link D}
36
- * @param from Conversion function from {@link D} to {@link S}
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}
37
43
  */
38
- static convert<S, D>(atom: Atom<S>, to: (x: S) => D, from: (x: D) => S): Atom<D>;
44
+ static prop<T, K extends keyof T>(obj: Accessor<T>, k: (x: NamesOf<T>) => K): Atom<T[K]>;
39
45
  /**
40
46
  * Creates a bindable data source.
41
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
@@ -150,6 +156,19 @@ export declare function memoProps<T>(obj: T, keys?: undefined, equals?: Equals):
150
156
 
151
157
  export declare function memoProps<T, K extends readonly (keyof T)[]>(obj: T, keys: K, equals?: Equals): Pick<T, K[number]>;
152
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
+
153
172
  /**
154
173
  * Allows the use of {@link Case}s inside of {@link Switch}es.
155
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)
@@ -243,12 +245,20 @@ class Atom {
243
245
  update(f) {
244
246
  return this.value = f(untrack(this.get));
245
247
  }
248
+ /**
249
+ * Creates a new {@link Atom} that applies a conversion to the current one
250
+ * @param to Conversion function from {@link S} to {@link D}
251
+ * @param from Conversion function from {@link D} to {@link S}
252
+ */
253
+ convert(to, from) {
254
+ return new Atom(() => to(this.value), (v) => this.value = from(v));
255
+ }
246
256
  /**
247
257
  * Creates an {@link Atom} that forwards an {@link Accessor} to another {@link Atom}
248
258
  * @param f The reactive {@link Accessor} to the {@link Atom} to forward
249
259
  */
250
260
  static unwrap(f) {
251
- return new this(() => f().value, (x) => f().value = x);
261
+ return new this(() => f().value, (v) => f().value = v);
252
262
  }
253
263
  /**
254
264
  * Creates an {@link Atom} based on a {@link Signal}
@@ -258,13 +268,14 @@ class Atom {
258
268
  return new this(get, (v) => set(() => v));
259
269
  }
260
270
  /**
261
- * Creates a new {@link Atom} that applies a conversion to another {@link Atom}
262
- * @param atom The {@link Atom} to which to apply the conversion
263
- * @param to Conversion function from {@link S} to {@link D}
264
- * @param from Conversion function from {@link D} to {@link S}
265
- */
266
- static convert(atom, to, from) {
267
- return new this(() => to(atom.value), (x) => atom.value = from(x));
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);
268
279
  }
269
280
  static source(bind, f = createSignal) {
270
281
  return this.unwrap(createMemo(on(bind, (x) => x ?? Atom.from(f()))));
@@ -288,6 +299,7 @@ export {
288
299
  createExtractor,
289
300
  debug,
290
301
  memoProps,
302
+ nameOf,
291
303
  runWithContext,
292
304
  splitAndMemoProps,
293
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)
@@ -245,12 +247,20 @@
245
247
  update(f) {
246
248
  return this.value = f(solidJs.untrack(this.get));
247
249
  }
250
+ /**
251
+ * Creates a new {@link Atom} that applies a conversion to the current one
252
+ * @param to Conversion function from {@link S} to {@link D}
253
+ * @param from Conversion function from {@link D} to {@link S}
254
+ */
255
+ convert(to, from) {
256
+ return new Atom(() => to(this.value), (v) => this.value = from(v));
257
+ }
248
258
  /**
249
259
  * Creates an {@link Atom} that forwards an {@link Accessor} to another {@link Atom}
250
260
  * @param f The reactive {@link Accessor} to the {@link Atom} to forward
251
261
  */
252
262
  static unwrap(f) {
253
- return new this(() => f().value, (x) => f().value = x);
263
+ return new this(() => f().value, (v) => f().value = v);
254
264
  }
255
265
  /**
256
266
  * Creates an {@link Atom} based on a {@link Signal}
@@ -260,13 +270,14 @@
260
270
  return new this(get, (v) => set(() => v));
261
271
  }
262
272
  /**
263
- * Creates a new {@link Atom} that applies a conversion to another {@link Atom}
264
- * @param atom The {@link Atom} to which to apply the conversion
265
- * @param to Conversion function from {@link S} to {@link D}
266
- * @param from Conversion function from {@link D} to {@link S}
267
- */
268
- static convert(atom, to, from) {
269
- return new this(() => to(atom.value), (x) => atom.value = from(x));
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);
270
281
  }
271
282
  static source(bind, f = solidJs.createSignal) {
272
283
  return this.unwrap(solidJs.createMemo(solidJs.on(bind, (x) => x ?? Atom.from(f()))));
@@ -288,6 +299,7 @@
288
299
  exports2.createExtractor = createExtractor;
289
300
  exports2.debug = debug;
290
301
  exports2.memoProps = memoProps;
302
+ exports2.nameOf = nameOf;
291
303
  exports2.runWithContext = runWithContext;
292
304
  exports2.splitAndMemoProps = splitAndMemoProps;
293
305
  exports2.untrackCall = untrackCall;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "solid-ctrl-flow",
3
- "version": "1.0.58",
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
@@ -171,14 +171,17 @@ Customizable and simplified wrappers for reactive states
171
171
  > #### `Atom.update()`
172
172
  > Like the `Setter` overload of a `Signal` that takes a function with the previous value
173
173
  >
174
+ > #### `Atom.convert()`
175
+ > Creates a new `Atom` that applies a conversion to the current one
176
+ >
174
177
  > #### `Atom.unwrap()`
175
178
  > An `Atom`-specific (optimized) version of `unwrap()` that allows the destructuring of its result
176
179
  >
177
180
  > #### `Atom.from()`
178
- > Creates an `Atom` from a `Signal`
179
- >
180
- > #### `Atom.convert()`
181
- > Creates a new `Atom` that applies a conversion to another `Atom`
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