Skip to content
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

String replace helper with more options #156

Merged
merged 8 commits into from
Mar 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const helpersList = [
'setURLQueryParam',
'snippets',
'stripQuerystring',
'strReplace',
'stylesheet',
'thirdParty',
'toLowerCase',
Expand Down
54 changes: 54 additions & 0 deletions helpers/strReplace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
'use strict';
const common = require('./lib/common.js');
const utils = require('handlebars-utils');

const factory = globals => {
return function(str, substr, newSubstr, iteration) {
str = common.unwrapIfSafeString(globals.handlebars, str);
substr = common.unwrapIfSafeString(globals.handlebars, substr);
newSubstr = common.unwrapIfSafeString(globals.handlebars, newSubstr);
iteration = common.unwrapIfSafeString(globals.handlebars, iteration);

if (!utils.isString(str)){
throw new TypeError("Invalid query parameter string passed to strReplace");
} else if (!utils.isString(substr)){
throw new TypeError("Invalid query paramter substring passed to strReplace");
} else if(!utils.isString(newSubstr)) {
throw new TypeError("Invalid query parameter new substring passed to strReplace");
}

if (typeof iteration !== 'number') {
return str.replace(new RegExp(escapeRegex(substr), 'g'), newSubstr);
}

const occurrence = getOccurrences(str, substr);

if (iteration > 0 && occurrence > 0) {
if (iteration >= occurrence) {
return str.replace(new RegExp(escapeRegex(substr), 'g'), newSubstr);
} else {
let result = str;
for (let i = 0; i < iteration; i++) {
result = result.replace(substr, newSubstr);
}
return result;
}
} else {
return str;
}
};
};

function escapeRegex(string) {
return string.replace(/[-\\^$*+?.()|[\]{}]/g, "\\$&");
}

function getOccurrences(str, substr) {
const matches = str.match(new RegExp(escapeRegex(substr),'g'));
return matches ? matches.length : 0;
}

module.exports = [{
name: 'strReplace',
factory: factory,
}];
1 change: 1 addition & 0 deletions spec/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ describe('helper registration', () => {
'setURLQueryParam',
'snippet',
'stripQuerystring',
'strReplace',
'stylesheet',
'after',
'arrayify',
Expand Down
68 changes: 68 additions & 0 deletions spec/helpers/strReplace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const Lab = require('lab'),
lab = exports.lab = Lab.script(),
describe = lab.experiment,
it = lab.it,
specHelpers = require('../spec-helpers'),
testRunner = require('../spec-helpers').testRunner,
renderString = specHelpers.renderString;;

describe('strReplace helper', function() {
const context = {
string: "My name is Albe Albe Albe",
substr: "Albe",
newSubstr: "Alex",
object: {}
};

const runTestCases = testRunner({context});

it('should replace all by default', function(done) {
runTestCases([
{
input: '{{strReplace string substr newSubstr}}',
output: 'My name is Alex Alex Alex',
},
{
input: '{{strReplace "Your name is none" "none" "Bob"}}',
output: 'Your name is Bob',
},
], done);
});

it('should replace multiple if given quantity', function(done) {
runTestCases([
{
input: '{{strReplace string substr newSubstr -5}}',
output: 'My name is Albe Albe Albe',
},
{
input: '{{strReplace string substr newSubstr 0}}',
output: 'My name is Albe Albe Albe',
},
{
input: '{{strReplace string substr newSubstr 2}}',
output: 'My name is Alex Alex Albe',
},
{
input: '{{strReplace string substr newSubstr 4}}',
output: 'My name is Alex Alex Alex',
},
{
input: '{{strReplace string substr newSubstr 100}}',
output: 'My name is Alex Alex Alex',
},
], done);
});

it('should throw an exception if the parameters have an invalid type', function(done) {
renderString('{{strReplace object "none" "Bob"}}').catch(e => {
Copy link
Contributor

Choose a reason for hiding this comment

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

I would suggest you to use this kind of syntax https://hapi.dev/module/code/api?v=8.0.6#throwtype-message to catch errors and match the errors message expectations

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jairo-bc Hi! I have updated the wording for the description. For catching errors, I am using the same format as other helpers, can you give me more details about how I can improve this please? Thank you!

renderString('{{strReplace "none" 3 "Bob"}}').catch(e => {
renderString('{{strReplace "none" "Bob" object}}').catch(e => {
renderString('{{strReplace string substr newSubstr "3"}}').catch(e => {
done();
});
});
});
});
});
});