mailtrain/server/lib/helpers.js

39 lines
655 B
JavaScript
Raw Normal View History

'use strict';
module.exports = {
enforce,
cleanupFromPost,
filterObject,
castToInteger
};
function enforce(condition, message) {
if (!condition) {
throw new Error(message);
}
}
2017-03-19 12:36:57 +00:00
function cleanupFromPost(value) {
return (value || '').toString().trim();
}
function filterObject(obj, allowedKeys) {
const result = {};
for (const key in obj) {
if (allowedKeys.has(key)) {
result[key] = obj[key];
}
}
return result;
}
function castToInteger(id) {
const val = parseInt(id);
if (!Number.isInteger(val)) {
throw new Error('Invalid id');
}
return val;
}