UNPKG

redux-resource

Version:
58 lines (44 loc) 1.73 kB
'use strict'; exports.__esModule = true; var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; exports.default = setResourceMeta; // Update the meta for `resources` function setResourceMeta(options) { const { resources, newMeta, meta, mergeMeta, initialResourceMeta = {} } = options; const next = _extends({}, meta); if (!resources) { return meta; } let mergeMetaOption = typeof mergeMeta !== 'undefined' ? mergeMeta : true; const resourcesArray = Array.isArray(resources) ? resources : Object.values(resources); if (!resourcesArray.length) { return next; } resourcesArray.forEach(resource => { const id = typeof resource === 'object' ? resource.id : resource; // If we have no ID for this resource, or if its not a number or string, // then we bail. This currently isn't logging so that we don't double-blast // the user with meta **and** attribute update problems. If the ID check // is moved into the success reducers directly, then we may be able to // remove these typeof checks for efficiency. if (!id && id !== 0 || typeof id !== 'string' && typeof id !== 'number') { return; } const currentMeta = next[id]; let startMeta; if (currentMeta) { startMeta = _extends({}, initialResourceMeta, currentMeta); } else { startMeta = initialResourceMeta; } const baseMeta = mergeMetaOption ? startMeta : initialResourceMeta; next[id] = _extends({}, baseMeta, newMeta); }); return next; }