-
Notifications
You must be signed in to change notification settings - Fork 36
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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
340b34b
Rename string replace and put in helpers
7df11ad
Update String Replace input options
6b2867f
Update input type checking method
fd1a41a
Update string replace test cases to handle error
71c63ed
Update throw error message
e95ce8c
updated phrasing of error
jaredtyap 000f821
Merge branch 'master' into master
thearcticalex 2143b4d
Merge branch 'feature/string-replace'
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 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 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 |
---|---|---|
@@ -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, | ||
}]; |
This file contains 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 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 |
---|---|---|
@@ -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 => { | ||
renderString('{{strReplace "none" 3 "Bob"}}').catch(e => { | ||
renderString('{{strReplace "none" "Bob" object}}').catch(e => { | ||
renderString('{{strReplace string substr newSubstr "3"}}').catch(e => { | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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!