UNPKG

@taraai/read-write

Version:

Synchronous NoSQL/Firestore for React

139 lines (108 loc) 3.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.combineReducers = combineReducers; exports.createReducer = createReducer; exports.getDotStrPath = getDotStrPath; exports.getSlashStrPath = getSlashStrPath; exports.pathFromMeta = pathFromMeta; exports.pathToArr = pathToArr; exports.preserveValuesFromState = preserveValuesFromState; exports.updateItemInArray = updateItemInArray; var _isBoolean = _interopRequireDefault(require("lodash/isBoolean")); var _pick = _interopRequireDefault(require("lodash/pick")); var _replace = _interopRequireDefault(require("lodash/replace")); var _trimStart = _interopRequireDefault(require("lodash/trimStart")); var _flatten = _interopRequireDefault(require("lodash/flatten")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function pathToArr(path) { return path ? path.split(/\//).filter(p => !!p) : []; } function getSlashStrPath(path) { return (0, _trimStart.default)((0, _replace.default)(path, /[.]/g, '/'), '/'); } function getDotStrPath(path) { return pathToArr(path).join('.'); } function combineReducers(reducers) { return function () { let state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; let action = arguments.length > 1 ? arguments[1] : undefined; return Object.keys(reducers).reduce((nextState, key) => { nextState[key] = reducers[key](state[key], action); return nextState; }, {}); }; } function pathFromMeta(meta) { if (!meta) { throw new Error('Action meta is required to build path for reducers.'); } const { collection, collectionGroup, doc, subcollections, storeAs } = meta; if (storeAs) { return doc ? [storeAs, doc] : [storeAs]; } if (meta.path) { return meta.path.split('/'); } if (!collection && !collectionGroup) { throw new Error('Collection or Collection Group is required to construct reducer path.'); } let basePath = [collection || collectionGroup]; if (doc) { basePath = [...basePath, doc]; } if (!subcollections) { return basePath; } const mappedCollections = subcollections.map(pathFromMeta); return [...basePath, ...(0, _flatten.default)(mappedCollections)]; } function updateItemInArray(array, itemId, updateItemCallback) { let matchFound = false; const modified = Array.isArray(array) ? array.map(item => { if (!item || item.id !== itemId) { return item; } matchFound = true; const updatedItem = updateItemCallback(item); return updatedItem; }) : []; if (!matchFound) { modified.push(updateItemCallback({ id: itemId })); } return modified; } function createReducer(initialState, handlers) { return function reducer() { let state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : initialState; let action = arguments.length > 1 ? arguments[1] : undefined; if (handlers.hasOwnProperty(action.type)) { return handlers[action.type](state, action); } return state; }; } function preserveValuesFromState(state, preserveSetting, nextState) { if ((0, _isBoolean.default)(preserveSetting)) { return nextState ? { ...state, ...nextState } : state; } if (typeof preserveSetting === 'function') { return preserveSetting(state, nextState); } if (Array.isArray(preserveSetting)) { return (0, _pick.default)(state, preserveSetting); } throw new Error('Invalid preserve parameter. It must be an Object or an Array.'); }