-
Notifications
You must be signed in to change notification settings - Fork 56
Topics/support option list #4
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
Conversation
can pass a complete array instead of just single values
69581cb
to
67ac75d
Compare
Btw i think this way is much better {
search: 'string',
replace: 'value',
} for multiple values {
multiple: [
{
search: 'string',
replace: 'value',
},
{
search: 'string2',
replace: 'value2',
},
],
} The code will look like this: var utils = require('loader-utils');
function processQuery(source, query) {
source = source.split(query.search).join(query.replace);
}
module.exports = function(source) {
this.cacheable();
var query = utils.parseQuery(this.query);
if (Array.isArray(query.multiple)) {
query.multiple.forEach(function(query) {
processQuery(source, query);
});
} else if (typeof query.search !== 'undefined' && typeof query.replace !== 'undefined') {
processQuery(source, query);
}
return source;
}; |
hm |
Oh and your proposal is missing support for regex + flags which would break backward. I prefer to leave this as it is =) |
I said "much better" not for performance, but for structure with/without "multiple" property:) |
ah yea okay. This leads me to another thought. Why not doing a regex replacement and in addition array replacement ? Will use your idea with multiple key to do that |
ah well nope, dont have enough time to tinker here so long =) will leave it as it is for now |
String#split supports regex as argument, and we don't need "flag" option anymore:) And here is 1.0.0 release:D |
ah yea good point ! |
And here is the winner;) var utils = require('loader-utils');
function processQuery(source, query) {
if (typeof query.search !== 'undefined' && typeof query.replace !== 'undefined') {
return source.split(query.search).join(query.replace);
} else {
return source;
}
}
module.exports = function(source) {
this.cacheable();
var query = utils.parseQuery(this.query);
if (Array.isArray(query.multiple)) {
for (var i = 0; i < query.multiple.length; i++) {
source = processQuery(source, query.multiple[i]);
}
} else {
source = processQuery(source, query);
}
return source;
}; |
ah okay, i did it a bit different but basically the same. I think you have a bug inside your proposal anyway. Afaik source is a string and not passed by reference so you need to return it from processQuery. |
Oops you're right! I've wrote this code in Github, and didn't test it at all. Fixed last proposal |
no worries. I didnt test it as well but looks good to me. We will see what @Va1 is saying =) |
Just fixed last bug in processQuery in cases when condition of query properties isn't valid |
yea i have seen that as well and already fixed that in my commit https://github.com/easybiblabs/string-replace-loader/commit/adef248908916da6a57be2434a9789759dcb9358 👍 |
Good job, the solution is little bit different but main idea is the same:) |
yup, i prefer to to return instead of else when possible. Thanks for collaboration =) |
Hey, guys! This all looks fine, I do love the idea of a better interface multiple replacement, I wander why did not I add this feature before. Though, I am feeling like using So, I will merge this now, then apply some fixes, then fix the tests, then bump the npm version. Will let you know by posting a message here. Thanks a lot, |
@Va1 You are welcome;) |
oh man sorry, didnt see there are tests available. I would have adjusted them otherwise :/ Thanks for merging ! |
OK, so. The reason of having |
Done! |
nice thanks ! What a pitty that you added lodash again just to check for undefined and array. Can you please remove lodash again ? Those checks are easily done with vanilly js :) |
Be able to use an array for replacements
for #1 (comment)