docs: tests + docs

This commit is contained in:
Sebastian Rindom
2021-06-16 08:07:12 +02:00
parent 88d96a29fd
commit 712a2395ec

View File

@@ -1,8 +1,18 @@
/**
* Takes an object and a list of fields to tranform in that object. If the field
* exists on the object and its value is a string it will append `_id` to the
* field name. This is used when allowing API calls to hold either ID or object
* values in the payload. The method returns a new object with the
* transformation.
* @param {Object} obj - the object to transform
* @param {Array<String>} fields - the fields to apply transformation to
* @returns {Object} the transformed object
*/
export const transformIdableFields = (obj, fields) => {
const ret = { ...obj }
for (const key of fields) {
if (key in obj && typeof key === "string") {
if (key in obj && typeof ret[key] === "string") {
ret[`${key}_id`] = ret[key]
delete ret[key]
}