2017-03-04 20:50:44 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
module.exports = {
|
2018-01-28 22:59:05 +00:00
|
|
|
enforce,
|
2018-02-25 19:54:15 +00:00
|
|
|
cleanupFromPost,
|
2018-09-29 11:30:29 +00:00
|
|
|
filterObject,
|
|
|
|
castToInteger
|
2017-03-04 20:50:44 +00:00
|
|
|
};
|
|
|
|
|
2018-02-25 19:54:15 +00:00
|
|
|
function enforce(condition, message) {
|
|
|
|
if (!condition) {
|
|
|
|
throw new Error(message);
|
|
|
|
}
|
2017-03-04 20:50:44 +00:00
|
|
|
}
|
2017-03-19 12:36:57 +00:00
|
|
|
|
2018-02-25 19:54:15 +00:00
|
|
|
function cleanupFromPost(value) {
|
|
|
|
return (value || '').toString().trim();
|
2017-05-03 19:46:49 +00:00
|
|
|
}
|
2017-06-04 11:16:29 +00:00
|
|
|
|
|
|
|
function filterObject(obj, allowedKeys) {
|
|
|
|
const result = {};
|
|
|
|
for (const key in obj) {
|
|
|
|
if (allowedKeys.has(key)) {
|
|
|
|
result[key] = obj[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2018-09-29 11:30:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function castToInteger(id) {
|
|
|
|
const val = parseInt(id);
|
|
|
|
|
|
|
|
if (!Number.isInteger(val)) {
|
|
|
|
throw new Error('Invalid id');
|
|
|
|
}
|
|
|
|
|
|
|
|
return val;
|
2017-06-05 21:59:08 +00:00
|
|
|
}
|