assign-gingerly 0.0.82 → 0.0.83

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/README.md CHANGED
@@ -100,7 +100,8 @@ assignFrom adds support for:
100
100
  3. Protocol resolution (`globalThis://`, `localStorage://`, custom sync protocols).
101
101
  4. Handler plugins via the ` =>` operator for custom logic (fire-and-forget in sync mode, awaitable in async mode).
102
102
  5. Looped substitution with `where_x_in` / `where_y_in` / `where_z_in` for expanding template patterns into multiple concrete assignments.
103
- 6. Spread merging via the `"..."` key.
103
+ 6. Dynamic substitutions via the `substitutions` option for injecting runtime string values into path segments. See [docs/substitutions.md](docs/substitutions.md).
104
+ 7. Spread merging via the `"..."` key.
104
105
 
105
106
  Example:
106
107
 
package/assignFrom.js CHANGED
@@ -49,6 +49,7 @@ function resolveTernaryValue(value, source, options) {
49
49
  return getValue(value, source, {
50
50
  withMethods: options.withMethods,
51
51
  aka: options.aka,
52
+ substitutions: options.substitutions,
52
53
  protocols: options.protocols,
53
54
  root: options.root
54
55
  });
@@ -61,6 +62,7 @@ function resolveTernaryValue(value, source, options) {
61
62
  return getValue(value, source, {
62
63
  withMethods: options.withMethods,
63
64
  aka: options.aka,
65
+ substitutions: options.substitutions,
64
66
  protocols: options.protocols,
65
67
  root: options.root
66
68
  });
@@ -381,7 +383,7 @@ function processIdRefNormalKeys(idRefNormalKeys, expandedPattern, target, option
381
383
  const ids = getEffectiveIds(options);
382
384
  if (!ids)
383
385
  return;
384
- const { withMethods, aka, akaMethods, protocols, from } = options;
386
+ const { withMethods, aka, akaMethods, protocols, from, substitutions } = options;
385
387
  for (const key of idRefNormalKeys) {
386
388
  const parsed = parseIdRef(key);
387
389
  if (!parsed)
@@ -391,11 +393,11 @@ function processIdRefNormalKeys(idRefNormalKeys, expandedPattern, target, option
391
393
  continue;
392
394
  const value = expandedPattern[key];
393
395
  if (parsed.remainingPath) {
394
- const resolvedValue = getValues({ __v: value }, from, { withMethods, aka, akaMethods, protocols, root: target, permissionProcessor });
396
+ const resolvedValue = getValues({ __v: value }, from, { withMethods, aka, akaMethods, substitutions, protocols, root: target, permissionProcessor });
395
397
  assignGingerly(el, { [parsed.remainingPath]: resolvedValue.__v }, options, permissionProcessor);
396
398
  }
397
399
  else {
398
- const resolvedValue = getValues(typeof value === 'object' && value !== null ? value : { __v: value }, from, { withMethods, aka, akaMethods, protocols, root: target, permissionProcessor });
400
+ const resolvedValue = getValues(typeof value === 'object' && value !== null ? value : { __v: value }, from, { withMethods, aka, akaMethods, substitutions, protocols, root: target, permissionProcessor });
399
401
  if (!('__v' in resolvedValue)) {
400
402
  assignGingerly(el, resolvedValue, options, permissionProcessor);
401
403
  }
@@ -458,6 +460,7 @@ export function assignFrom(target, pattern, options, permissionProcessor) {
458
460
  withMethods: options.withMethods,
459
461
  aka: options.aka,
460
462
  akaMethods: options.akaMethods,
463
+ substitutions: options.substitutions,
461
464
  protocols: options.protocols,
462
465
  root: target,
463
466
  permissionProcessor
package/assignFrom.ts CHANGED
@@ -58,6 +58,7 @@ function resolveTernaryValue(value: any, source: any, options: AssignFromOptions
58
58
  return getValue(value, source, {
59
59
  withMethods: options.withMethods,
60
60
  aka: options.aka,
61
+ substitutions: options.substitutions,
61
62
  protocols: options.protocols,
62
63
  root: options.root
63
64
  });
@@ -70,6 +71,7 @@ function resolveTernaryValue(value: any, source: any, options: AssignFromOptions
70
71
  return getValue(value, source, {
71
72
  withMethods: options.withMethods,
72
73
  aka: options.aka,
74
+ substitutions: options.substitutions,
73
75
  protocols: options.protocols,
74
76
  root: options.root
75
77
  });
@@ -395,7 +397,7 @@ function processIdRefNormalKeys(
395
397
  const ids = getEffectiveIds(options);
396
398
  if (!ids) return;
397
399
 
398
- const { withMethods, aka, akaMethods, protocols, from } = options;
400
+ const { withMethods, aka, akaMethods, protocols, from, substitutions } = options;
399
401
  for (const key of idRefNormalKeys) {
400
402
  const parsed = parseIdRef(key);
401
403
  if (!parsed) continue;
@@ -407,14 +409,14 @@ function processIdRefNormalKeys(
407
409
  if (parsed.remainingPath) {
408
410
  const resolvedValue = getValues(
409
411
  { __v: value }, from,
410
- { withMethods, aka, akaMethods, protocols, root: target, permissionProcessor }
412
+ { withMethods, aka, akaMethods, substitutions, protocols, root: target, permissionProcessor }
411
413
  );
412
414
  assignGingerly(el, { [parsed.remainingPath]: resolvedValue.__v }, options, permissionProcessor);
413
415
  } else {
414
416
  const resolvedValue = getValues(
415
417
  typeof value === 'object' && value !== null ? value : { __v: value },
416
418
  from,
417
- { withMethods, aka, akaMethods, protocols, root: target, permissionProcessor }
419
+ { withMethods, aka, akaMethods, substitutions, protocols, root: target, permissionProcessor }
418
420
  );
419
421
  if (!('__v' in resolvedValue)) {
420
422
  assignGingerly(el, resolvedValue, options, permissionProcessor);
@@ -486,6 +488,7 @@ export function assignFrom(
486
488
  withMethods: options.withMethods,
487
489
  aka: options.aka,
488
490
  akaMethods: options.akaMethods,
491
+ substitutions: options.substitutions,
489
492
  protocols: options.protocols,
490
493
  root: target,
491
494
  permissionProcessor
@@ -35,6 +35,7 @@ export async function assignFromAsync(target, pattern, options, permissionProces
35
35
  withMethods: options.withMethods,
36
36
  aka: options.aka,
37
37
  akaMethods: options.akaMethods,
38
+ substitutions: options.substitutions,
38
39
  protocols: options.protocols,
39
40
  root: target,
40
41
  permissionProcessor
@@ -47,7 +48,7 @@ export async function assignFromAsync(target, pattern, options, permissionProces
47
48
  if (idRefNormalKeys.length > 0 && (options.pin || options.at)) {
48
49
  const ids = { ...options.pin, ...options.at };
49
50
  const { resolveIdVariable, parseIdRef } = await import('./resolve/resolveIdRef.js');
50
- const { withMethods, aka, akaMethods, protocols, from } = options;
51
+ const { withMethods, aka, akaMethods, protocols, from, substitutions } = options;
51
52
  for (const key of idRefNormalKeys) {
52
53
  const parsed = parseIdRef(key);
53
54
  if (!parsed)
@@ -58,13 +59,13 @@ export async function assignFromAsync(target, pattern, options, permissionProces
58
59
  const value = expandedPattern[key];
59
60
  if (parsed.remainingPath) {
60
61
  // Resolve the RHS value
61
- const resolvedValue = await resolveValues({ __v: value }, from, { withMethods, aka, akaMethods, protocols, root: el, permissionProcessor });
62
+ const resolvedValue = await resolveValues({ __v: value }, from, { withMethods, aka, akaMethods, substitutions, protocols, root: el, permissionProcessor });
62
63
  // Apply remaining path on the resolved element
63
64
  assignGingerly(el, { [parsed.remainingPath]: resolvedValue.__v }, options, permissionProcessor);
64
65
  }
65
66
  else {
66
67
  // No remaining path — resolve and assign directly to the element
67
- const resolvedValue = await resolveValues(typeof value === 'object' && value !== null ? value : { __v: value }, from, { withMethods, aka, akaMethods, protocols, root: el, permissionProcessor });
68
+ const resolvedValue = await resolveValues(typeof value === 'object' && value !== null ? value : { __v: value }, from, { withMethods, aka, akaMethods, substitutions, protocols, root: el, permissionProcessor });
68
69
  if ('__v' in resolvedValue) {
69
70
  // Single value — can't assign to element root without a path
70
71
  }
@@ -69,6 +69,7 @@ export async function assignFromAsync(
69
69
  withMethods: options.withMethods,
70
70
  aka: options.aka,
71
71
  akaMethods: options.akaMethods,
72
+ substitutions: options.substitutions,
72
73
  protocols: options.protocols,
73
74
  root: target,
74
75
  permissionProcessor
@@ -84,7 +85,7 @@ export async function assignFromAsync(
84
85
  if (idRefNormalKeys.length > 0 && (options.pin || options.at)) {
85
86
  const ids = { ...options.pin, ...options.at };
86
87
  const { resolveIdVariable, parseIdRef } = await import('./resolve/resolveIdRef.js');
87
- const { withMethods, aka, akaMethods, protocols, from } = options;
88
+ const { withMethods, aka, akaMethods, protocols, from, substitutions } = options;
88
89
  for (const key of idRefNormalKeys) {
89
90
  const parsed = parseIdRef(key);
90
91
  if (!parsed) continue;
@@ -97,7 +98,7 @@ export async function assignFromAsync(
97
98
  // Resolve the RHS value
98
99
  const resolvedValue = await resolveValues(
99
100
  { __v: value }, from,
100
- { withMethods, aka, akaMethods, protocols, root: el, permissionProcessor }
101
+ { withMethods, aka, akaMethods, substitutions, protocols, root: el, permissionProcessor }
101
102
  );
102
103
  // Apply remaining path on the resolved element
103
104
  assignGingerly(el, { [parsed.remainingPath]: resolvedValue.__v }, options, permissionProcessor);
@@ -106,7 +107,7 @@ export async function assignFromAsync(
106
107
  const resolvedValue = await resolveValues(
107
108
  typeof value === 'object' && value !== null ? value : { __v: value },
108
109
  from,
109
- { withMethods, aka, akaMethods, protocols, root: el, permissionProcessor }
110
+ { withMethods, aka, akaMethods, substitutions, protocols, root: el, permissionProcessor }
110
111
  );
111
112
  if ('__v' in resolvedValue) {
112
113
  // Single value — can't assign to element root without a path
@@ -303,13 +303,25 @@ const raConfig = {
303
303
  }
304
304
  }
305
305
 
306
+ // withAttrs configuration for parsing element attributes
307
+ const withAttrs = {
308
+ base: 'user-counter',
309
+ count: '${base}-count',
310
+ _count: {
311
+ instanceOf: 'Number',
312
+ valIfNull: 0,
313
+ },
314
+ username: '${base}-username',
315
+ };
316
+
306
317
  /** @type {ElMakerConfig<AP>} */
307
318
  const features = {
308
319
  assignFeatures: {
309
320
  roundabout: {
310
321
  customData: {
311
322
  raConfig,
312
- }
323
+ },
324
+ withAttrs
313
325
  },
314
326
  templateMaker: {}
315
327
  }
@@ -361,6 +373,20 @@ const raConfig = {
361
373
  };
362
374
  ```
363
375
 
376
+ ## How can I set focus after a delay?
377
+
378
+ Work is underway to improve the DX a bit, but for now:
379
+
380
+ ```JS
381
+ {
382
+ delay:10, //milliseconds
383
+ ifAllOf: ['expanded'],
384
+ assign: {
385
+ set($.querySelector('a').focus()).to({}),
386
+ }
387
+ },
388
+ ```
389
+
364
390
  ## Step 8
365
391
 
366
392
  Run `node el-maker.mjs` (or `npm run build-el-maker` if your `package.json` includes a watch script) to regenerate `el-maker.json`.
@@ -267,6 +267,17 @@ export interface IAssignGingerlyOptions {
267
267
  */
268
268
  aka?: Record<string, string>;
269
269
 
270
+ /**
271
+ * Value substitutions for path segments.
272
+ * Each key names a placeholder; the value is a `?.`-delimited path resolved
273
+ * against the source (`from`) object. The resolved string value replaces any
274
+ * matching whole path segment in RHS path strings before path evaluation.
275
+ *
276
+ * Substitution values must be strings and must not contain the `?.` sequence,
277
+ * otherwise an error is thrown.
278
+ */
279
+ substitutions?: Record<string, string>;
280
+
270
281
  /**
271
282
  * Shorthand for binding method aliases from the source object.
272
283
  * Each entry maps an alias to a method name and is normalized into
@@ -357,6 +368,16 @@ export interface AssignFromOptions {
357
368
  /** Alias mappings for path segments */
358
369
  aka?: Record<string, string>;
359
370
 
371
+ /**
372
+ * Value substitutions for path segments.
373
+ * Each key names a placeholder; the value is a `?.`-delimited path resolved
374
+ * against `from`. The resolved string value replaces any matching whole path
375
+ * segment in RHS path strings before path evaluation.
376
+ *
377
+ * Substitution values must be strings and must not contain `?.`.
378
+ */
379
+ substitutions?: Record<string, string>;
380
+
360
381
  /** AbortSignal for cleanup */
361
382
  signal?: AbortSignal;
362
383
 
@@ -2,6 +2,7 @@ import {RAConfig} from '../roundabout/types.js';
2
2
  import {FontFaceFeatureConfig} from '../font-face-feature/types.js';
3
3
  import {CustomData as TSCD} from '../truth-sourcer/types.js';
4
4
  import {CustomData as FUCD} from '../face-up/types.js';
5
+ import {AttrPatterns} from '../assign-gingerly/types.js';
5
6
 
6
7
  export interface ElMakerConfig<AllProps = any, TActions = AllProps> {
7
8
  assignFeatures: {
@@ -10,6 +11,7 @@ export interface ElMakerConfig<AllProps = any, TActions = AllProps> {
10
11
  customData: {
11
12
  raConfig: RAConfig<AllProps, TActions, TActions>
12
13
  }
14
+ withAttrs: AttrPatterns<AllProps>
13
15
  },
14
16
  fontMgr?: {
15
17
  spawn?: string,
@@ -161,6 +161,13 @@ export interface RAConfig<
161
161
  initialPropVals?: Partial<{[key in keyof TProps & string]: unknown}>,
162
162
  }
163
163
 
164
+ // export interface RoundaboutFeatureConfig<
165
+ // TProps = unknown, TActions = TProps, ETProps = TProps,
166
+ // TCustomData = unknown, TEvents extends string = string>{
167
+ // RAConfig: RAConfig<TProps, TActions, ETProps, TCustomData, TEvents>,
168
+
169
+ // }
170
+
164
171
  export interface RoundaboutOptions<TProps = unknown, TActions = TProps, ETProps = TProps, EventTypes extends string = string> extends RAConfig<TProps, TActions, ETProps, unknown, EventTypes> {
165
172
  vm?: TProps & TActions & RoundaboutReady,
166
173
  //for enhanced elements, pass in the container, referenced via $0.
@@ -0,0 +1,43 @@
1
+ import { FeatureSpawnContext } from "../assign-gingerly/types";
2
+
3
+ /**
4
+ * Public properties of the SwipeDismissFeature.
5
+ * These can be set directly on the feature instance or initialized via attributes.
6
+ */
7
+ export interface SwipeDismissProps {
8
+ /** Axis along which the dismiss gesture is measured. */
9
+ axis: 'x' | 'y';
10
+ /**
11
+ * Direction that counts toward dismissal.
12
+ * 1 = right/down, -1 = left/up.
13
+ * Set to 'both' to allow swiping in either direction (e.g. toasts/snackbars).
14
+ */
15
+ direction: 1 | -1 | 'both';
16
+ /** Fraction of the panel size that triggers commit. */
17
+ distanceThreshold: number;
18
+ /** Velocity threshold in px/ms; a fast flick commits even under distanceThreshold. */
19
+ velocityThreshold: number;
20
+ /** CSS selector for the drag handle. Defaults to the host element. */
21
+ handleSelector: string | null;
22
+ /** CSS selector for the panel that visually follows the drag. Defaults to the handle. */
23
+ panelSelector: string | null;
24
+ /** Called on every pointermove with the current delta and fraction of the threshold. */
25
+ onProgress: ((deltaPx: number, fraction: number) => void) | null;
26
+ /** Called when the gesture crosses the commit threshold. */
27
+ onCommit: (() => void) | null;
28
+ /** Called when the gesture is released before the commit threshold. */
29
+ onCancel: (() => void) | null;
30
+ }
31
+
32
+ /**
33
+ * Internal state of the feature.
34
+ */
35
+ export interface AllProps extends SwipeDismissProps {
36
+ /** WeakRef to the host custom element. */
37
+ hostRef: WeakRef<Element>;
38
+ }
39
+
40
+ export type AP = AllProps;
41
+ export type PAP = Partial<AllProps>;
42
+
43
+ export { FeatureSpawnContext };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "assign-gingerly",
3
- "version": "0.0.82",
3
+ "version": "0.0.83",
4
4
  "description": "This package provides a utility function for carefully merging one object into another.",
5
5
  "homepage": "https://github.com/bahrus/assign-gingerly#readme",
6
6
  "bugs": {
@@ -171,6 +171,7 @@ export async function processHandlerCommands(target, handlerKeys, pattern, optio
171
171
  resolvedParams = getValues(config.get, options.from, {
172
172
  withMethods: options.withMethods,
173
173
  aka: options.aka,
174
+ substitutions: options.substitutions,
174
175
  protocols: options.protocols,
175
176
  root: target
176
177
  });
@@ -180,6 +181,7 @@ export async function processHandlerCommands(target, handlerKeys, pattern, optio
180
181
  const asyncResolved = await resolveValues(config.resolve, options.from, {
181
182
  withMethods: options.withMethods,
182
183
  aka: options.aka,
184
+ substitutions: options.substitutions,
183
185
  protocols: options.protocols,
184
186
  root: target
185
187
  });
@@ -192,6 +192,7 @@ export async function processHandlerCommands(
192
192
  resolvedParams = getValues(config.get, options.from, {
193
193
  withMethods: options.withMethods,
194
194
  aka: options.aka,
195
+ substitutions: options.substitutions,
195
196
  protocols: options.protocols,
196
197
  root: target
197
198
  });
@@ -201,6 +202,7 @@ export async function processHandlerCommands(
201
202
  const asyncResolved = await resolveValues(config.resolve, options.from, {
202
203
  withMethods: options.withMethods,
203
204
  aka: options.aka,
205
+ substitutions: options.substitutions,
204
206
  protocols: options.protocols,
205
207
  root: target
206
208
  });
@@ -67,6 +67,45 @@ function applyAliases(path, aliasMap) {
67
67
  const substituted = parts.map(part => aliasMap.get(part) ?? part);
68
68
  return substituted.join('?.');
69
69
  }
70
+ /**
71
+ * Apply value substitutions to a path string.
72
+ * Replaces complete tokens between `?.` delimiters with their resolved values.
73
+ * Substitutions are applied before aliases.
74
+ */
75
+ function applySubstitutions(path, substitutionMap) {
76
+ if (!substitutionMap || substitutionMap.size === 0)
77
+ return path;
78
+ const parts = path.split('?.');
79
+ const substituted = parts.map(part => substitutionMap.get(part) ?? part);
80
+ return substituted.join('?.');
81
+ }
82
+ /**
83
+ * Resolve substitution values declared in options.substitutions.
84
+ * Each substitution path is resolved against the source object without
85
+ * applying substitutions itself, to avoid infinite recursion.
86
+ * Resolved values must be strings and must not contain `?.`.
87
+ */
88
+ function resolveSubstitutions(substitutions, source, options) {
89
+ const map = new Map();
90
+ if (!substitutions)
91
+ return map;
92
+ for (const [name, path] of Object.entries(substitutions)) {
93
+ // Resolve the substitution path against the source, but do not apply
94
+ // substitutions to that path. Root references ($0) are also disabled
95
+ // for substitution paths so values are sourced from `from` only.
96
+ const resolved = getValue(path, source, options
97
+ ? { ...options, substitutions: undefined, root: undefined }
98
+ : undefined);
99
+ if (typeof resolved !== 'string') {
100
+ throw new Error(`Substitution '${name}' must resolve to a string, got ${typeof resolved}`);
101
+ }
102
+ if (resolved.includes('?.')) {
103
+ throw new Error(`Substitution '${name}' resolved to a string containing '?.', which would alter the path structure: '${resolved}'`);
104
+ }
105
+ map.set(name, resolved);
106
+ }
107
+ return map;
108
+ }
70
109
  /**
71
110
  * Resolve a special root-reference token at the start of a string.
72
111
  * '$0' refers to the first argument passed to assignFrom / resolveValues.
@@ -159,12 +198,13 @@ function getProtocolValue(value, protocols, options) {
159
198
  * Resolve path strings and protocols within an array (synchronous).
160
199
  * Recurses into nested arrays and plain objects.
161
200
  */
162
- function getArray(arr, source, aliasMap, withMethods, protocols, options) {
201
+ function getArray(arr, source, aliasMap, withMethods, protocols, options, substitutionMap) {
163
202
  const permissionProcessor = options?.permissionProcessor;
164
203
  const result = [];
165
204
  for (const item of arr) {
166
205
  if (typeof item === 'string' && item.startsWith('?.')) {
167
- const aliased = applyAliases(item, aliasMap);
206
+ const substituted = applySubstitutions(item, substitutionMap);
207
+ const aliased = applyAliases(substituted, aliasMap);
168
208
  const parts = parseCachedPath(aliased);
169
209
  result.push(parts.length === 0 ? source : navigatePath(source, parts, withMethods, permissionProcessor));
170
210
  }
@@ -174,7 +214,8 @@ function getArray(arr, source, aliasMap, withMethods, protocols, options) {
174
214
  result.push(source);
175
215
  }
176
216
  else {
177
- const aliased = applyAliases(rootRef.path, aliasMap);
217
+ const substitutedPath = applySubstitutions(rootRef.path, substitutionMap);
218
+ const aliased = applyAliases(substitutedPath, aliasMap);
178
219
  const normalizedPath = aliased.startsWith('?.') ? aliased : (aliased ? `?.${aliased}` : '?.');
179
220
  const parts = parseCachedPath(normalizedPath);
180
221
  result.push(parts.length === 0 ? rootRef.source : navigatePath(rootRef.source, parts, withMethods, permissionProcessor));
@@ -184,7 +225,7 @@ function getArray(arr, source, aliasMap, withMethods, protocols, options) {
184
225
  result.push(getProtocolValue(item, protocols, options));
185
226
  }
186
227
  else if (Array.isArray(item)) {
187
- result.push(getArray(item, source, aliasMap, withMethods, protocols, options));
228
+ result.push(getArray(item, source, aliasMap, withMethods, protocols, options, substitutionMap));
188
229
  }
189
230
  else if (item && typeof item === 'object') {
190
231
  const proto = Object.getPrototypeOf(item);
@@ -215,12 +256,14 @@ function getArray(arr, source, aliasMap, withMethods, protocols, options) {
215
256
  */
216
257
  export function getValues(pattern, source, options) {
217
258
  const { aliasMap, withMethods } = normalizeAliasOptions(options);
259
+ const substitutionMap = resolveSubstitutions(options?.substitutions, source, options);
218
260
  const protocols = options?.protocols;
219
261
  const permissionProcessor = options?.permissionProcessor;
220
262
  const result = {};
221
263
  for (const [key, value] of Object.entries(pattern)) {
222
264
  if (typeof value === 'string' && value.startsWith('?.')) {
223
- const aliased = applyAliases(value, aliasMap);
265
+ const substituted = applySubstitutions(value, substitutionMap);
266
+ const aliased = applyAliases(substituted, aliasMap);
224
267
  const parts = parseCachedPath(aliased);
225
268
  result[key] = parts.length === 0 ? source : navigatePath(source, parts, withMethods, permissionProcessor);
226
269
  }
@@ -230,7 +273,8 @@ export function getValues(pattern, source, options) {
230
273
  result[key] = source;
231
274
  }
232
275
  else {
233
- const aliased = applyAliases(rootRef.path, aliasMap);
276
+ const substitutedPath = applySubstitutions(rootRef.path, substitutionMap);
277
+ const aliased = applyAliases(substitutedPath, aliasMap);
234
278
  const normalizedPath = aliased.startsWith('?.') ? aliased : (aliased ? `?.${aliased}` : '?.');
235
279
  const parts = parseCachedPath(normalizedPath);
236
280
  result[key] = parts.length === 0 ? rootRef.source : navigatePath(rootRef.source, parts, withMethods, permissionProcessor);
@@ -240,7 +284,7 @@ export function getValues(pattern, source, options) {
240
284
  result[key] = getProtocolValue(value, protocols, options);
241
285
  }
242
286
  else if (Array.isArray(value)) {
243
- result[key] = getArray(value, source, aliasMap, withMethods, protocols, options);
287
+ result[key] = getArray(value, source, aliasMap, withMethods, protocols, options, substitutionMap);
244
288
  }
245
289
  else if (typeof value === 'object' && value !== null) {
246
290
  const proto = Object.getPrototypeOf(value);
@@ -274,10 +318,12 @@ export function getValue(path, source, options) {
274
318
  else if (!path.startsWith('?.')) {
275
319
  return path;
276
320
  }
277
- let aliased = path;
321
+ const substitutionMap = resolveSubstitutions(options?.substitutions, source, options);
322
+ const substituted = applySubstitutions(path, substitutionMap);
323
+ let aliased = substituted;
278
324
  const { aliasMap } = normalizeAliasOptions(options);
279
325
  if (aliasMap.size > 0) {
280
- aliased = applyAliases(path, aliasMap);
326
+ aliased = applyAliases(substituted, aliasMap);
281
327
  }
282
328
  const normalizedPath = aliased.startsWith('?.') ? aliased : (aliased ? `?.${aliased}` : '?.');
283
329
  const parts = parseCachedPath(normalizedPath);
@@ -85,6 +85,58 @@ function applyAliases(path: string, aliasMap: Map<string, string>): string {
85
85
  return substituted.join('?.');
86
86
  }
87
87
 
88
+ /**
89
+ * Apply value substitutions to a path string.
90
+ * Replaces complete tokens between `?.` delimiters with their resolved values.
91
+ * Substitutions are applied before aliases.
92
+ */
93
+ function applySubstitutions(path: string, substitutionMap?: Map<string, string>): string {
94
+ if (!substitutionMap || substitutionMap.size === 0) return path;
95
+ const parts = path.split('?.');
96
+ const substituted = parts.map(part => substitutionMap.get(part) ?? part);
97
+ return substituted.join('?.');
98
+ }
99
+
100
+ /**
101
+ * Resolve substitution values declared in options.substitutions.
102
+ * Each substitution path is resolved against the source object without
103
+ * applying substitutions itself, to avoid infinite recursion.
104
+ * Resolved values must be strings and must not contain `?.`.
105
+ */
106
+ function resolveSubstitutions(
107
+ substitutions: Record<string, string> | undefined,
108
+ source: any,
109
+ options: GetValuesOptions | undefined
110
+ ): Map<string, string> {
111
+ const map = new Map<string, string>();
112
+ if (!substitutions) return map;
113
+
114
+ for (const [name, path] of Object.entries(substitutions)) {
115
+ // Resolve the substitution path against the source, but do not apply
116
+ // substitutions to that path. Root references ($0) are also disabled
117
+ // for substitution paths so values are sourced from `from` only.
118
+ const resolved = getValue(
119
+ path,
120
+ source,
121
+ options
122
+ ? { ...options, substitutions: undefined, root: undefined }
123
+ : undefined
124
+ );
125
+ if (typeof resolved !== 'string') {
126
+ throw new Error(
127
+ `Substitution '${name}' must resolve to a string, got ${typeof resolved}`
128
+ );
129
+ }
130
+ if (resolved.includes('?.')) {
131
+ throw new Error(
132
+ `Substitution '${name}' resolved to a string containing '?.', which would alter the path structure: '${resolved}'`
133
+ );
134
+ }
135
+ map.set(name, resolved);
136
+ }
137
+ return map;
138
+ }
139
+
88
140
  /**
89
141
  * Resolve a special root-reference token at the start of a string.
90
142
  * '$0' refers to the first argument passed to assignFrom / resolveValues.
@@ -200,13 +252,15 @@ function getArray(
200
252
  aliasMap: Map<string, string>,
201
253
  withMethods: Set<string> | undefined,
202
254
  protocols: Record<string, (key: string) => any> | undefined,
203
- options?: GetValuesOptions
255
+ options?: GetValuesOptions,
256
+ substitutionMap?: Map<string, string>
204
257
  ): any[] {
205
258
  const permissionProcessor = options?.permissionProcessor;
206
259
  const result: any[] = [];
207
260
  for (const item of arr) {
208
261
  if (typeof item === 'string' && item.startsWith('?.')) {
209
- const aliased = applyAliases(item, aliasMap);
262
+ const substituted = applySubstitutions(item, substitutionMap);
263
+ const aliased = applyAliases(substituted, aliasMap);
210
264
  const parts = parseCachedPath(aliased);
211
265
  result.push(parts.length === 0 ? source : navigatePath(source, parts, withMethods, permissionProcessor));
212
266
  } else if (typeof item === 'string' && item.startsWith('$0')) {
@@ -214,7 +268,8 @@ function getArray(
214
268
  if (rootRef === null) {
215
269
  result.push(source);
216
270
  } else {
217
- const aliased = applyAliases(rootRef.path, aliasMap);
271
+ const substitutedPath = applySubstitutions(rootRef.path, substitutionMap);
272
+ const aliased = applyAliases(substitutedPath, aliasMap);
218
273
  const normalizedPath = aliased.startsWith('?.') ? aliased : (aliased ? `?.${aliased}` : '?.');
219
274
  const parts = parseCachedPath(normalizedPath);
220
275
  result.push(parts.length === 0 ? rootRef.source : navigatePath(rootRef.source, parts, withMethods, permissionProcessor));
@@ -222,7 +277,7 @@ function getArray(
222
277
  } else if (typeof item === 'string' && protocols && hasProtocol(item)) {
223
278
  result.push(getProtocolValue(item, protocols, options));
224
279
  } else if (Array.isArray(item)) {
225
- result.push(getArray(item, source, aliasMap, withMethods, protocols, options));
280
+ result.push(getArray(item, source, aliasMap, withMethods, protocols, options, substitutionMap));
226
281
  } else if (item && typeof item === 'object') {
227
282
  const proto = Object.getPrototypeOf(item);
228
283
  if (proto === Object.prototype || proto === null) {
@@ -255,6 +310,7 @@ export function getValues(
255
310
  options?: GetValuesOptions
256
311
  ): Record<string, any> {
257
312
  const { aliasMap, withMethods } = normalizeAliasOptions(options);
313
+ const substitutionMap = resolveSubstitutions(options?.substitutions, source, options);
258
314
 
259
315
  const protocols = options?.protocols;
260
316
  const permissionProcessor = options?.permissionProcessor;
@@ -262,7 +318,8 @@ export function getValues(
262
318
  const result: Record<string, any> = {};
263
319
  for (const [key, value] of Object.entries(pattern)) {
264
320
  if (typeof value === 'string' && value.startsWith('?.')) {
265
- const aliased = applyAliases(value, aliasMap);
321
+ const substituted = applySubstitutions(value, substitutionMap);
322
+ const aliased = applyAliases(substituted, aliasMap);
266
323
  const parts = parseCachedPath(aliased);
267
324
  result[key] = parts.length === 0 ? source : navigatePath(source, parts, withMethods, permissionProcessor);
268
325
  } else if (typeof value === 'string' && value.startsWith('$0')) {
@@ -270,7 +327,8 @@ export function getValues(
270
327
  if (rootRef === null) {
271
328
  result[key] = source;
272
329
  } else {
273
- const aliased = applyAliases(rootRef.path, aliasMap);
330
+ const substitutedPath = applySubstitutions(rootRef.path, substitutionMap);
331
+ const aliased = applyAliases(substitutedPath, aliasMap);
274
332
  const normalizedPath = aliased.startsWith('?.') ? aliased : (aliased ? `?.${aliased}` : '?.');
275
333
  const parts = parseCachedPath(normalizedPath);
276
334
  result[key] = parts.length === 0 ? rootRef.source : navigatePath(rootRef.source, parts, withMethods, permissionProcessor);
@@ -278,7 +336,7 @@ export function getValues(
278
336
  } else if (typeof value === 'string' && protocols && hasProtocol(value)) {
279
337
  result[key] = getProtocolValue(value, protocols, options);
280
338
  } else if (Array.isArray(value)) {
281
- result[key] = getArray(value, source, aliasMap, withMethods, protocols, options);
339
+ result[key] = getArray(value, source, aliasMap, withMethods, protocols, options, substitutionMap);
282
340
  } else if (typeof value === 'object' && value !== null) {
283
341
  const proto = Object.getPrototypeOf(value);
284
342
  if (proto === Object.prototype || proto === null) {
@@ -314,10 +372,13 @@ export function getValue(
314
372
  return path;
315
373
  }
316
374
 
317
- let aliased = path;
375
+ const substitutionMap = resolveSubstitutions(options?.substitutions, source, options);
376
+
377
+ const substituted = applySubstitutions(path, substitutionMap);
378
+ let aliased = substituted;
318
379
  const { aliasMap } = normalizeAliasOptions(options);
319
380
  if (aliasMap.size > 0) {
320
- aliased = applyAliases(path, aliasMap);
381
+ aliased = applyAliases(substituted, aliasMap);
321
382
  }
322
383
 
323
384
  const normalizedPath = aliased.startsWith('?.') ? aliased : (aliased ? `?.${aliased}` : '?.');
@@ -267,6 +267,17 @@ export interface IAssignGingerlyOptions {
267
267
  */
268
268
  aka?: Record<string, string>;
269
269
 
270
+ /**
271
+ * Value substitutions for path segments.
272
+ * Each key names a placeholder; the value is a `?.`-delimited path resolved
273
+ * against the source (`from`) object. The resolved string value replaces any
274
+ * matching whole path segment in RHS path strings before path evaluation.
275
+ *
276
+ * Substitution values must be strings and must not contain the `?.` sequence,
277
+ * otherwise an error is thrown.
278
+ */
279
+ substitutions?: Record<string, string>;
280
+
270
281
  /**
271
282
  * Shorthand for binding method aliases from the source object.
272
283
  * Each entry maps an alias to a method name and is normalized into
@@ -357,6 +368,16 @@ export interface AssignFromOptions {
357
368
  /** Alias mappings for path segments */
358
369
  aka?: Record<string, string>;
359
370
 
371
+ /**
372
+ * Value substitutions for path segments.
373
+ * Each key names a placeholder; the value is a `?.`-delimited path resolved
374
+ * against `from`. The resolved string value replaces any matching whole path
375
+ * segment in RHS path strings before path evaluation.
376
+ *
377
+ * Substitution values must be strings and must not contain `?.`.
378
+ */
379
+ substitutions?: Record<string, string>;
380
+
360
381
  /** AbortSignal for cleanup */
361
382
  signal?: AbortSignal;
362
383