mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-20 22:42:53 -05:00
parent
3d7b4dc0f5
commit
da7fdfeae6
5 changed files with 51 additions and 24 deletions
|
@ -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) {
|
||||
|
|
5
core/server/api/shared/utils/index.js
Normal file
5
core/server/api/shared/utils/index.js
Normal file
|
@ -0,0 +1,5 @@
|
|||
module.exports = {
|
||||
get options() {
|
||||
return require('./options');
|
||||
}
|
||||
};
|
15
core/server/api/shared/utils/options.js
Normal file
15
core/server/api/shared/utils/options.js
Normal 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;
|
|
@ -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);
|
||||
|
|
24
core/test/unit/api/shared/util/options_spec.js
Normal file
24
core/test/unit/api/shared/util/options_spec.js
Normal 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']);
|
||||
});
|
||||
});
|
Loading…
Add table
Reference in a new issue