From 712a2395ec23ba29dedcda3f5c6d1e4d19ad983e Mon Sep 17 00:00:00 2001 From: Sebastian Rindom Date: Wed, 16 Jun 2021 08:07:12 +0200 Subject: [PATCH] docs: tests + docs --- .../medusa-core-utils/src/transform-idable-fields.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/medusa-core-utils/src/transform-idable-fields.js b/packages/medusa-core-utils/src/transform-idable-fields.js index 826d082caa..eabfaeace5 100644 --- a/packages/medusa-core-utils/src/transform-idable-fields.js +++ b/packages/medusa-core-utils/src/transform-idable-fields.js @@ -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} 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] }