Skip to content

wip: add support for lazily replacing variables - #1388

Draft
Pranav2612000 wants to merge 1 commit into
postmanlabs:developfrom
Pranav2612000:feat/add-support-for-lazy-replacer
Draft

Pranav2612000 wants to merge 1 commit into
postmanlabs:developfrom
Pranav2612000:feat/add-support-for-lazy-replacer

Conversation

@Pranav2612000

Copy link
Copy Markdown
Contributor

No description provided.

@Pranav2612000
Pranav2612000 force-pushed the feat/add-support-for-lazy-replacer branch from 1770756 to 2219c42 Compare November 25, 2024 05:54
@Pranav2612000 Pranav2612000 reopened this Nov 25, 2024
Comment thread lib/collection/property.js
Comment thread lib/collection/variable.js Outdated
_.has(options, 'system') && (this.system = options.system);
_.has(options, 'disabled') && (this.disabled = options.disabled);
_.has(options, 'description') && (this.describe(options.description));
_.has(options, 'lazy') && (this.lazy = options.lazy);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@appurva21 Thoughts on this approach?
The advantage is that it makes it clear which variables are to be resolved lazily, and which are to be replaced immediately. The disadvantage is that lazy has no meaning for replaceSubstitutions ( non lazy ), and makes the replaceSubstitutionsLazy interface more complicated.

Some other approaches I thought about

  • A) lazily replace all values ( when invoked through replaceSubstitutionsLazy ) but this increases the amount of memory needed, and slightly slows down the function.
  • B) Only lazily replace Promise values. This is a good solution but there is no reliable method of identifying if something is a promise or not. ( Duck typing is not accurate )
  • C) Only lazily replace function values. Could be a good tradeoff but has the same problems as (A), also makes the code slightly less intuitive.

@Pranav2612000
Pranav2612000 force-pushed the feat/add-support-for-lazy-replacer branch from 7d554c9 to 9507de0 Compare November 25, 2024 07:01
@Pranav2612000
Pranav2612000 force-pushed the feat/add-support-for-lazy-replacer branch 2 times, most recently from a2042c1 to 8873d7f Compare January 15, 2025 14:49
@Pranav2612000
Pranav2612000 force-pushed the feat/add-support-for-lazy-replacer branch from 8873d7f to 46fac10 Compare January 15, 2025 14:51
Comment thread lib/superstring/index.js
Comment on lines +86 to +99
let index = 0,
match;

while ((match = regex.exec(this.value.slice(index)))) {
try {
// eslint-disable-next-line no-await-in-loop
let value = await replacerFn(...match);

index += match.index;
this.value = this.value.slice(0, index) + value + this.value.slice(index + match[0].length);
index += match[0].length;
}
catch (_err) { /* empty */ }
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

custom string replace logic instead of using String.replace ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace ) as it does not support async replacer fn

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant