Skip to content

Commit

Permalink
toSequelizeWildcards - use regex instead of splitting string into arr…
Browse files Browse the repository at this point in the history
…ay, iterating, replacing, and rejoining
  • Loading branch information
iamigo committed Aug 1, 2017
1 parent fd21561 commit bb2abed
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion api/v1/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module.exports = {
OFFSET_EQ: 'offset=',
POSTGRES_UUID_RE:
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i,
QUERY_PARAM_WILDCARD: '*',
QUERY_PARAM_REPLACE_ALL_REGEX: /\*/g,
SEQ_DEFAULT_SCOPE: 'defaultScope',
SEQ_DESC: 'DESC',
SEQ_LIKE: '$iLike',
Expand Down
7 changes: 3 additions & 4 deletions api/v1/helpers/verbs/findUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,8 @@ function escapeUnderscoreLiterals(val) {
* @returns {String} the transformed value
*/
function toSequelizeWildcards(val) {
const chars = val.split(constants.EMPTY_STRING);
const arr = chars.map((ch) =>
(ch === constants.QUERY_PARAM_WILDCARD ? constants.SEQ_WILDCARD : ch));
return arr.join(constants.EMPTY_STRING);
return val.replace(constants.QUERY_PARAM_REPLACE_ALL_REGEX,
constants.SEQ_WILDCARD);
} // toSequelizeWildcards

/**
Expand Down Expand Up @@ -353,4 +351,5 @@ module.exports = {
getNextUrl,
options,
filterArrFromArr, // for testing
toSequelizeWildcards, // for testing
}; // exports
10 changes: 10 additions & 0 deletions tests/api/v1/helpers/findUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,14 @@ describe('build options object: ', () => {
opts.where.name.$iLike = '%n\\%am\\_e%';
expect(options(params, props)).to.deep.equal(opts);
});

it.only('toSequelizeWildcards', (done) => {
expect(fu.toSequelizeWildcards('abc')).to.be.equal('abc');
expect(fu.toSequelizeWildcards('*abc')).to.be.equal('%abc');
expect(fu.toSequelizeWildcards('abc*')).to.be.equal('abc%');
expect(fu.toSequelizeWildcards('*a*b*c*')).to.be.equal('%a%b%c%');
expect(fu.toSequelizeWildcards('***a')).to.be.equal('%%%a');
done();
});
});

0 comments on commit bb2abed

Please sign in to comment.