0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-20 22:42:53 -05:00

Extracted trimAndLowerCase utility to shared utility module

refs #9866
This commit is contained in:
Nazar Gargol 2018-11-19 18:08:48 +01:00 committed by Naz Gargol
parent 3d7b4dc0f5
commit da7fdfeae6
5 changed files with 51 additions and 24 deletions

View file

@ -1,18 +1,9 @@
const debug = require('ghost-ignition').debug('api:shared:serializers:input:all');
const _ = require('lodash');
const utils = require('../../utils');
const INTERNAL_OPTIONS = ['transacting', 'forUpdate'];
const trimAndLowerCase = (params) => {
params = params || '';
if (_.isString(params)) {
params = params.split(',');
}
return params.map((item) => {
return item.trim().toLowerCase();
});
};
/**
* Transform into model readable language.
*/
@ -21,17 +12,17 @@ module.exports = {
debug('serialize all');
if (frame.options.include) {
frame.options.withRelated = trimAndLowerCase(frame.options.include);
frame.options.withRelated = utils.options.trimAndLowerCase(frame.options.include);
delete frame.options.include;
}
if (frame.options.fields) {
frame.options.columns = trimAndLowerCase(frame.options.fields);
frame.options.columns = utils.options.trimAndLowerCase(frame.options.fields);
delete frame.options.fields;
}
if (frame.options.formats) {
frame.options.formats = trimAndLowerCase(frame.options.formats);
frame.options.formats = utils.options.trimAndLowerCase(frame.options.formats);
}
if (frame.options.formats && frame.options.columns) {

View file

@ -0,0 +1,5 @@
module.exports = {
get options() {
return require('./options');
}
};

View file

@ -0,0 +1,15 @@
const _ = require('lodash');
const trimAndLowerCase = (params) => {
params = params || '';
if (_.isString(params)) {
params = params.split(',');
}
return params.map((item) => {
return item.trim().toLowerCase();
});
};
module.exports.trimAndLowerCase = trimAndLowerCase;

View file

@ -4,6 +4,7 @@ const Promise = require('bluebird'),
_ = require('lodash'),
permissions = require('../../services/permissions'),
validation = require('../../data/validation'),
sharedUtils = require('../shared/utils'),
common = require('../../lib/common');
let utils;
@ -246,16 +247,7 @@ utils = {
};
},
trimAndLowerCase(params) {
params = params || '';
if (_.isString(params)) {
params = params.split(',');
}
return params.map((item) => {
return item.trim().toLowerCase();
});
},
trimAndLowerCase: sharedUtils.options.trimAndLowerCase,
prepareInclude(include, allowedIncludes) {
return _.intersection(this.trimAndLowerCase(include), allowedIncludes);

View file

@ -0,0 +1,24 @@
const should = require('should');
const optionsUtil = require('../../../../../server/api/shared/utils/options');
describe('Unit: api/shared/util/options', () => {
it('returns an array with empty string when no parameters are passed', () => {
optionsUtil.trimAndLowerCase().should.eql(['']);
});
it('returns single item array', () => {
optionsUtil.trimAndLowerCase('butter').should.eql(['butter']);
});
it('returns multiple items in array', () => {
optionsUtil.trimAndLowerCase('peanut, butter').should.eql(['peanut', 'butter']);
});
it('lowercases and trims items in the string', () => {
optionsUtil.trimAndLowerCase(' PeanUt, buTTer ').should.eql(['peanut', 'butter']);
});
it('accepts parameters in form of an array', () => {
optionsUtil.trimAndLowerCase([' PeanUt', ' buTTer ']).should.eql(['peanut', 'butter']);
});
});