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

Conversation

thearcticalex
Copy link
Contributor

@thearcticalex thearcticalex commented Feb 14, 2022

What? Why?

This is an inline string replacement helper that gives the user more control in how many occurrences of the substring to be replaced.

How was it tested?

It was tested by giving several test cases including inputs that are out of bound.

{{strReplace string substr newSubstr}} would replace all substring by default

{{strReplace string substr newSubstr count}} would replace count of occurrences.

Invalid string input would return invalid.
Invalid count would return original string and extra count (count > occurrences) would regard as replace all.


cc @bigcommerce/storefront-team

Copy link
Contributor

@jairo-bc jairo-bc left a comment

Choose a reason for hiding this comment

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

Added few suggestions, but otherwise the approach looks good to me


const factory = () => {
return function(str, substr, newSubstr, iteration) {
if (typeof str !== 'string' || typeof substr !== 'string' || typeof newSubstr !== 'string') {
Copy link
Contributor

Choose a reason for hiding this comment

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

In case you want to use this helper with concat or replace helper this condition won't pass.
To unlock that possibility, please use https://github.com/bigcommerce/paper-handlebars/blob/master/helpers/lib/common.js#L12 before type checking.

const factory = () => {
return function(str, substr, newSubstr, iteration) {
if (typeof str !== 'string' || typeof substr !== 'string' || typeof newSubstr !== 'string') {
return 'Invalid Input';
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you would rather want to see here an Error, rather than a "Invalid Input" line.
Please take a look at TypeError and maybe passing some arguments there, so it will be easier to debug

@thearcticalex
Copy link
Contributor Author

thearcticalex commented Feb 15, 2022

@jairo-bc Thanks!
Updated according to the above comments.

@thearcticalex thearcticalex reopened this Feb 15, 2022
runTestCases([
{
input: '{{strReplace 5 5 5}}',
output: 'Invalid Input',
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this test case also should be reworked to accommodate errors throwing

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes sir! Updated test case in the new commit.

], done);
});

it('should only handle string', function(done) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Please change the test name to more meaningful description to what it doest

});

it('should only handle string', 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!

@jairo-bc jairo-bc merged commit d4a4188 into bigcommerce:master Mar 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants