1 | ;
|
2 | // The MIT License (MIT)
|
3 | //
|
4 | // Copyright (c) 2022 Firebase
|
5 | //
|
6 | // Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | // of this software and associated documentation files (the "Software"), to deal
|
8 | // in the Software without restriction, including without limitation the rights
|
9 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | // copies of the Software, and to permit persons to whom the Software is
|
11 | // furnished to do so, subject to the following conditions:
|
12 | //
|
13 | // The above copyright notice and this permission notice shall be included in all
|
14 | // copies or substantial portions of the Software.
|
15 | //
|
16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
22 | // SOFTWARE.
|
23 | Object.defineProperty(exports, "__esModule", { value: true });
|
24 | exports.Change = exports.applyFieldMask = void 0;
|
25 | /** @internal */
|
26 | function applyFieldMask(sparseBefore, after, fieldMask) {
|
27 | const before = { ...after };
|
28 | const masks = fieldMask.split(",");
|
29 | for (const mask of masks) {
|
30 | const parts = mask.split(".");
|
31 | const head = parts[0];
|
32 | const tail = parts.slice(1).join(".");
|
33 | if (parts.length > 1) {
|
34 | before[head] = applyFieldMask(sparseBefore === null || sparseBefore === void 0 ? void 0 : sparseBefore[head], after[head], tail);
|
35 | continue;
|
36 | }
|
37 | const val = sparseBefore === null || sparseBefore === void 0 ? void 0 : sparseBefore[head];
|
38 | if (typeof val === "undefined") {
|
39 | delete before[mask];
|
40 | }
|
41 | else {
|
42 | before[mask] = val;
|
43 | }
|
44 | }
|
45 | return before;
|
46 | }
|
47 | exports.applyFieldMask = applyFieldMask;
|
48 | /**
|
49 | * The Cloud Functions interface for events that change state, such as
|
50 | * Realtime Database or Cloud Firestore `onWrite` and `onUpdate` events.
|
51 | *
|
52 | * For more information about the format used to construct `Change` objects, see
|
53 | * {@link ChangeJson} below.
|
54 | *
|
55 | */
|
56 | class Change {
|
57 | /**
|
58 | * Factory method for creating a `Change` from a `before` object and an `after`
|
59 | * object.
|
60 | */
|
61 | static fromObjects(before, after) {
|
62 | return new Change(before, after);
|
63 | }
|
64 | /**
|
65 | * Factory method for creating a `Change` from JSON and an optional customizer
|
66 | * function to be applied to both the `before` and the `after` fields.
|
67 | */
|
68 | static fromJSON(json, customizer = (x) => x) {
|
69 | let before = { ...json.before };
|
70 | if (json.fieldMask) {
|
71 | before = applyFieldMask(before, json.after, json.fieldMask);
|
72 | }
|
73 | return Change.fromObjects(customizer(before || {}), customizer(json.after || {}));
|
74 | }
|
75 | constructor(before, after) {
|
76 | this.before = before;
|
77 | this.after = after;
|
78 | }
|
79 | }
|
80 | exports.Change = Change;
|