mirror of
https://github.com/nvms/prsm.git
synced 2025-12-16 16:10:54 +00:00
31 lines
904 B
TypeScript
31 lines
904 B
TypeScript
import dot from "dot-wild";
|
|
import { Collection } from "../..";
|
|
import { ensureArray, isObject } from "../../utils";
|
|
|
|
// { $push: { b: 2, c: 3 } }
|
|
// { $push: { b: [2, 3] } }
|
|
// { $push: { "a.b.c": 2 }}
|
|
// { $push: { "a.b.c": [2, 3] }}
|
|
export function $push<T>(source: T[], modifiers: any, query: object, collection: Collection<T>): T[] {
|
|
const mods = ensureArray(modifiers);
|
|
|
|
return mods.reduce((acc, mod) => {
|
|
if (isObject(mod)) {
|
|
return Object.keys(mod).reduce((docs, key) => {
|
|
return docs.map((doc) => {
|
|
const original = dot.get(doc, key);
|
|
const value = mod[key];
|
|
if (original !== undefined) {
|
|
const newValue = Array.isArray(value) ? original.concat(value) : original.concat([value]);
|
|
return dot.set(doc, key, newValue);
|
|
}
|
|
return doc;
|
|
});
|
|
}, acc);
|
|
}
|
|
return acc;
|
|
}, source);
|
|
}
|
|
|
|
|