@alt3/sequelize-to-json-schemas
Version:
Convert Sequelize models into various JSON Schema variants (using the Strategy Pattern)
61 lines (51 loc) • 1.56 kB
JavaScript
/**
* Native alternatives for lodash.
*
* @comment disabling eslint rules so examples stay identical to github
*
* @see {@link https://github.com/you-dont-need/You-Dont-Need-Lodash-Underscore}
* @see {@link https://xebia.com/blog/you-might-not-need-lodash-in-your-es2015-project/}
*/
/* eslint-disable func-names */
/* eslint-disable no-prototype-builtins */
/* eslint-disable no-param-reassign */
/* eslint-disable unicorn/prevent-abbreviations */
/* eslint-disable unicorn/no-array-reduce */
const capitalize = function (string) {
if (typeof string !== 'string') {
throw new TypeError("The 'string' argument for _capitalize() must be a string");
}
return string.charAt(0).toUpperCase() + string.slice(1);
};
const pick = function (object, keys) {
return keys.reduce((obj, key) => {
if (object && object.hasOwnProperty(key)) {
obj[key] = object[key];
}
return obj;
}, {});
};
// ---------------------------------------------------
// omit alternative taken from
// https://dustinpfister.github.io/2019/08/19/lodash_omit/
// ---------------------------------------------------
const inProps = function (key, props) {
// eslint-disable-next-line unicorn/prefer-includes
return props.some((omitKey) => {
return omitKey === key;
});
};
const omit = function (object, properties) {
const newObject = {};
for (const key of Object.keys(object)) {
if (!inProps(key, properties)) {
newObject[key] = object[key];
}
}
return newObject;
};
module.exports = {
capitalize,
omit,
pick,
};