This repository has been archived on 2020-08-24. You can view files and clone it, but cannot push or open issues or pull requests.
pouchtype/src/utils.js

31 lines
852 B
JavaScript

export function deepAssign(to, ...rest) {
let updated = false;
for (let src of rest) {
for (let prop in src) {
const value = src[prop];
const oldValue = to[prop];
if (typeof value === 'object' && !Array.isArray(value)) {
if (typeof oldValue !== 'object') {
to[prop] = {};
updated = true;
}
updated = deepAssign(to[prop], value) || updated;
} else if (value === undefined && to[prop] !== undefined) {
delete to[prop];
} else if (value !== oldValue) {
updated = true;
to[prop] = value;
}
}
}
return updated;
}
export const pouchDocHash = d => (isObject(d) ? `${d._id}:${d._rev}` : d);
export const pouchDocArrayHash = arr =>
Array.isArray(arr) ? arr.map(pouchDocHash).join('?') : arr;
export function isObject(obj) {
return typeof obj === 'object' && !Array.isArray(obj) && obj !== null;
}