File: index.d.ts

package info (click to toggle)
node-object-path 0.11.8%2B~0.11.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 392 kB
  • sloc: javascript: 1,333; sh: 5; makefile: 2
file content (141 lines) | stat: -rwxr-xr-x 4,180 bytes parent folder | download
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Type definitions for objectPath 0.11
// Project: https://github.com/mariocasciaro/object-path
// Definitions by: Paulo Cesar <https://github.com/pocesar>
//                 BendingBender <https://github.com/BendingBender>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3

declare const objectPath: objectPath.ObjectPathStatic & {
    withInheritedProps: objectPath.ObjectPathStatic;
    create(options?: objectPath.Options): objectPath.ObjectPathStatic;
};

declare namespace objectPath {
    interface Options {
        includeInheritedProps?: boolean | undefined;
    }

    type Path = Array<number | string> | number | string;

    interface ObjectPathStatic {
        /**
         * Binds an object
         */
        <T extends object>(object: T): ObjectPathBound<T>;

        /**
         * Deletes a member from object or array
         */
        del(object: object, path: Path): { [key: string]: any };

        /**
         * Tests path existence
         */
        has(object: object, path: Path): boolean;

        /**
         * Get a path from an object
         */
        get(object: object, path: Path): any;
        get<TResult>(object: object, path: Path, defaultValue: TResult): TResult;

        /**
         * Set a path to a value
         * @return Any existing value on the path if any
         */
        set<TResult = any>(
            object: object,
            path: Path,
            value: TResult,
            doNotReplace?: boolean
        ): TResult | undefined;

        /**
         * Create (if path isn't an array) and push the value to it. Can push unlimited number of values
         */
        push(object: object, path: Path, ...items: any[]): void;

        /**
         * Get the first non undefined property
         */
        coalesce<TResult>(object: object, paths: Path | Path[], defaultValue: TResult): TResult;
        coalesce<TResult = any>(
            object: object,
            paths: Path | Path[],
            defaultValue?: TResult
        ): TResult | undefined;

        /**
         * Empty a path. Arrays are set to length 0, objects have all elements deleted, strings
         * are set to empty, numbers to 0, everything else is set to null
         */
        empty(object: object, path: Path): any;

        /**
         * Set a value if it doesn't exist, do nothing if it does
         */
        ensureExists<TResult>(object: object, path: Path, defaultValue: TResult): TResult;
        ensureExists<TResult = any>(
            object: object,
            path: Path,
            defaultValue?: TResult
        ): TResult | undefined;

        /**
         * Insert an item in an array path
         */
        insert(object: object, path: Path, value: any, at?: number): void;
    }

    interface ObjectPathBound<T extends object> {
        /**
         * @see objectPath.del
         */
        del(path: Path): { [key: string]: any };

        /**
         * @see objectPath.has
         */
        has(path: Path): boolean;

        /**
         * @see objectPath.get
         */
        get(path: Path): any;
        get<TResult>(path: Path, defaultValue: TResult): TResult;

        /**
         * @see objectPath.set
         */
        set<TResult = any>(path: Path, value: TResult, doNotReplace?: boolean): TResult | undefined;

        /**
         * @see objectPath.push
         */
        push(path: Path, ...items: any[]): void;

        /**
         * @see objectPath.coalesce
         */
        coalesce<TResult>(paths: Path | Path[], defaultValue: TResult): TResult;
        coalesce<TResult = any>(paths: Path | Path[], defaultValue?: TResult): TResult | undefined;

        /**
         * @see objectPath.empty
         */
        empty(path: Path): any;

        /**
         * @see objectPath.ensureExists
         */
        ensureExists<TResult>(path: Path, defaultValue: TResult): TResult;
        ensureExists<TResult = any>(path: Path, defaultValue?: TResult): TResult | undefined;

        /**
         * @see objectPath.insert
         */
        insert(path: Path, value: any, at?: number): void;
    }
}

export = objectPath;