summaryrefslogtreecommitdiff
path: root/node_modules/@jet/environment/json/reader/object-cursor.js
blob: 8ac84f2b4f5a4ef6e1ed65466aa0b5adaab63149 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ObjectCursor = void 0;
const optional_1 = require("../../types/optional");
const clone_1 = require("../../util/clone");
const key_path_1 = require("./key-path");
const traverse_1 = require("./traverse");
class ObjectCursor {
    /**
     * Create a cursor for an object.
     *
     * @param root - An object to traverse.
     */
    constructor(root) {
        this.values = [root];
        this.keyPaths = [key_path_1.thisKeyPath];
        this.savedDepths = [];
    }
    /**
     * The current value this cursor is pointing at.
     */
    get currentValue() {
        return this.values[this.values.length - 1];
    }
    /**
     * The key path of the value this cursor is pointing at.
     */
    get currentKeyPath() {
        return this.keyPaths[this.keyPaths.length - 1];
    }
    /**
     * Advance this cursor to a given value and the key path which
     * was used to reach it.
     *
     * Use this method to override the internal traversal logic of
     * the cursor as needed. Like `moveTo`, calls to this method can
     * be balanced with calls to `back`.
     *
     * @param value - The new value for the cursor to represent.
     * @param keyPath - The key path used to reach the value.
     */
    interject(value, keyPath) {
        this.values.push(value);
        this.keyPaths.push(keyPath);
    }
    /**
     * Reconfigure this cursor to traverse a given object.
     *
     * @param newRoot - The new root object to traverse.
     * @param keyPath - The key path specifying where the root object came from.
     * Typically this should be `thisKeyPath` (the default value for this parameter.)
     */
    reuse(newRoot, keyPath = key_path_1.thisKeyPath) {
        this.values.length = 0;
        this.values.push(newRoot);
        this.keyPaths.length = 0;
        this.keyPaths.push(keyPath);
        this.savedDepths.length = 0;
    }
    /**
     * Advance this cursor to a new position in the object it is traversing,
     * saving its previous position so that the cursor may be moved back.
     *
     * @param keyPath - A key path referring to a location in the cursor's current value.
     * @returns The new current value of the cursor.
     */
    moveTo(keyPath) {
        const newValue = (0, traverse_1.traverse)(this.currentValue, keyPath);
        this.values.push(newValue);
        this.keyPaths.push(keyPath);
        return newValue;
    }
    /**
     * Rewind this cursor to its previous position in the object it is traversing.
     */
    moveBack() {
        const currentDepth = this.values.length;
        if (currentDepth === 1) {
            throw new Error("Cannot move back past the root of a cursor");
        }
        const numberOfSaves = this.savedDepths.length;
        if (numberOfSaves > 0 && currentDepth <= this.savedDepths[numberOfSaves - 1]) {
            throw new Error("Cannot move back past the most recent saved state");
        }
        this.values.pop();
        this.keyPaths.pop();
    }
    /**
     * Save the current position of this cursor so that it may be restored later.
     *
     * Calls to this method must be balanced with a call to `restoreState`.
     */
    saveState() {
        this.savedDepths.push(this.values.length);
    }
    /**
     * Restore this cursor's position to a previously saved state.
     *
     * Use this method to balance a previous call to `saveState`.
     */
    restoreState() {
        const savedLength = this.savedDepths.pop();
        if ((0, optional_1.isNothing)(savedLength)) {
            throw new Error("Calls to restoreState must balance previous calls to saveState");
        }
        this.values.length = savedLength;
        this.keyPaths.length = savedLength;
    }
    // section Cloneable
    clone() {
        const copy = (0, clone_1.shallowCloneOf)(this);
        copy.values = this.values.slice();
        copy.keyPaths = this.keyPaths.slice();
        copy.savedDepths = this.savedDepths.slice();
        return copy;
    }
}
exports.ObjectCursor = ObjectCursor;
//# sourceMappingURL=object-cursor.js.map