-
Notifications
You must be signed in to change notification settings - Fork 233
wip: add support for lazily replacing variables #1388
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Pranav2612000
wants to merge
1
commit into
postmanlabs:develop
Choose a base branch
from
Pranav2612000:feat/add-support-for-lazy-replacer
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,6 +64,46 @@ _.assign(SuperString.prototype, /** @lends SuperString.prototype */ { | |
return this; | ||
}, | ||
|
||
async replaceAsync (regex, fn) { | ||
var replacements = 0; // maintain a count of tokens replaced | ||
|
||
// to ensure we do not perform needless operations in the replacement, we use multiple replacement functions | ||
// after validating the parameters | ||
const replacerFn = _.isFunction(fn) ? | ||
function () { | ||
replacements += 1; | ||
|
||
return fn.apply(this, arguments); | ||
} : | ||
// this case is returned when replacer is not a function (ensures we do not need to check it) | ||
/* istanbul ignore next */ | ||
function () { | ||
replacements += 1; | ||
|
||
return fn; | ||
}; | ||
|
||
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 */ } | ||
} | ||
Comment on lines
+86
to
+99
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.replacements = replacements; // store the last replacements | ||
replacements && (this.substitutions += 1); // if any replacement is done, count that some substitution was made | ||
|
||
return this; | ||
}, | ||
|
||
/** | ||
* @returns {String} | ||
*/ | ||
|
@@ -153,6 +193,34 @@ _.assign(Substitutor.prototype, /** @lends Substitutor.prototype */ { | |
// } | ||
|
||
return value; | ||
}, | ||
|
||
/** | ||
* @param {SuperString} value - | ||
* @returns {String} | ||
*/ | ||
async parseAsync (value) { | ||
// convert the value into a SuperString so that it can return tracking results during replacements | ||
value = new SuperString(value); | ||
|
||
// get an instance of a replacer function that would be used to replace ejs like variable replacement | ||
// tokens | ||
var replacer = Substitutor.replacer(this); | ||
|
||
// replace the value once and keep on doing it until all tokens are replaced or we have reached a limit of | ||
// replacements | ||
do { | ||
// eslint-disable-next-line no-await-in-loop | ||
value = await value.replaceAsync(Substitutor.REGEX_EXTRACT_VARS, replacer); | ||
} while (value.replacements && (value.substitutions < Substitutor.VARS_SUBREPLACE_LIMIT)); | ||
|
||
// @todo: uncomment this code, and try to raise a warning in some way. | ||
// do a final check that if recursion limits are reached then replace with blank string | ||
// if (value.substitutions >= Substitutor.VARS_SUBREPLACE_LIMIT) { | ||
// value = value.replace(Substitutor.REGEX_EXTRACT_VARS, E); | ||
// } | ||
|
||
return value.toString(); | ||
} | ||
}); | ||
|
||
|
@@ -225,6 +293,9 @@ _.assign(Substitutor, /** @lends Substitutor */ { | |
var r = substitutor.find(token); | ||
|
||
r && _.isFunction(r) && (r = r()); | ||
if (r && _.isFunction(r.value)) { | ||
return r.get(); | ||
} | ||
r && _.isFunction(r.toString) && (r = r.toString()); | ||
|
||
return Substitutor.NATIVETYPES[(typeof r)] ? r : match; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.