-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.d.ts
372 lines (324 loc) · 14.5 KB
/
main.d.ts
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
declare module "uneval.js" {
/**
* Optional conversion settings.
*/
export type Opts = {
/**
* Maps specific object instances to the given string
*/
namespace: Map<any, string>,
/**
* If `false` there will be no un-needed spacing character.
* @default true
*/
pretty?: boolean,
/**
* The string to use as a space.
* If `false` is given the string will be `""`
* @default " "
*/
space?: string | boolean,
/**
* The string to use as a line feed character.
* If `false` is given the string will be `""`
* @default "\n"
*/
endl?: string | boolean,
/**
* The string to use as a tab character.
* If a number "n" is given the value will be the "space" setting's value repeated "n" times.
* If `false` is given the string will be `""`
* @default "\t"
*/
tab?: string | number | boolean,
/**
* If `true` allows custom conversions.
* @default true
*/
custom?: boolean,
/**
* If `false` every method will use the safe definition.
* @default true
*/
method?: boolean,
/**
* If `false` or `uneval.utils.fromProxy()` is not defined, proxy values will be considered as normal objects.
* @default true
*/
proxy?: boolean,
/**
* If `true`, when needed, object protorypes will be included in the generated source.
* @default true
*/
proto?: boolean,
/**
* The maximum level in which object inner properties will be calculated.
* @default Infinite
*/
depth?: number,
/**
* If `false` the source will not generate a copy of the input object, but a function to generate multiple ones.
* @default true
*/
call?: boolean,
/**
* If `true` named function and object at top level will be wrapped in parenthesis.
* @default true
*/
safe?: boolean,
/**
* If `false` and the "call" setting is `true` it will not put the wrapper function, even if needed.
* @default true
*/
func?: boolean,
/**
* The name of the eventual cache object.
* @default "x"
*/
val?: string,
/**
* Only in the `uneval.write()` function.
* If `false` the input will be considered as already object source.
* @default true
*/
conv?: boolean,
/**
* Only in the `uneval.write()` function.
* If the value is a string it will be put before the generated object.
* If the value is an object the "pre" property will be put before the generated object and "post" at the end.
* @default { pre: "module.exports = ", post: ";" }
*/
export?: string | { pre?: string, post?: string }
};
/**
* An object containing a value and a structure that will manage a special object.
*/
declare class Delegate {
/**
* Same as the `uneval.scan()` function but returns a `Delegate` created with "obj" and the generated `Struct`.
* @param obj The object to scan
* @param opts An object containing the preferences of the scanning
* @param cache A map containing the structures of the traversed objects
* @param prev A way for the function to communicate to the parent call (About circular references); If it's `false` then it does not check the internal properties
*/
static from(obj: any, opts?: Opts, cache?: Map<any, Struct>, prev?: (cir: Circular) => number | undefined): Delegate;
/**
* @param value An object that will represent another
* @param struct The structure of "value"
*/
constructor(
public value: any,
public struct: IStruct
);
}
export type IDelegate = Delegate | [ Delegate, Delegate ] | { name: string } | {
value: string,
method: boolean,
native: boolean,
global: boolean
};
/**
* An object containing the informations about a circular reference.
*/
declare class Circular {
/**
* @param key The key of the property inside the object represented by "inner" that has "outer" as value
* @param struct Object that represents the structure of "key"
* @param inner Structure of the inner object that contains a reference to an outer one
* @param outer Outer object
* @param delegate Eventual pair value-struct that will contain the value to set, in the case that the inner object's property cannot be set but only initialized during the creation of the inner object
*/
constructor(
public key: string | symbol | null,
public struct: IStruct,
public inner: IStruct,
public outer?: any,
public delegate?: IDelegate
);
}
/**
* An object that represents the structure of another.
*/
declare class Struct {
/**
* All parameters are optional.
* @param delegate Eventual pair value-struct that will represents the current object
* @param proto Structure of the eventual prototype of the object
* @param id Eventual index of the object inside of the cache
* @param sub Object that maps the object properties to a pair of structs that represent respectively the structure of the key and the value of the property
* @param cir List of the circular references that have the current object as value
*/
constructor(
public delegate?: IDelegate,
public proto?: IStruct,
public id?: string | number,
public sub?: Map<string | symbol, [ Struct, Struct ]>,
public cir?: Circular[]
);
/**
* Generates the "id" of the current `Struct` (if not already present) and returns it.
* @param opts An object containing the preferences of the conversion
*/
ref(opts: Opts): number;
}
export type IStruct = Struct | number | null;
/**
* Convert an object to its source code.
* @param obj The object to stringify
* @param opts An object containing the preferences of the conversion
* @param level The tabs to put before each line
*/
declare function uneval(obj: any, opts?: Opts, level?: string): string;
declare const _: typeof uneval & {
uneval: typeof _,
/**
* Save to file.
* @param name The name of the file
* @param obj The object to save
* @param opts The preferences of the conversion
* @returns Whatever `fs.writeFileSync()` returns
*/
write(name: string, obj: any, opts?: Opts, level?: string): void,
/**
* Stringifies an object given its structure.
* This function do the reference checking for the `uneval.utils.source()` function.
* @param obj The object to stringify
* @param struct The object representing the structure of "obj"
* @param opts An object containing the preferences of the conversion
* @param level The tabs to put before each line
*/
source(obj: any, struct: IStruct, opts?: Opts, level?: string): string,
/**
* Returns the structure object of "obj".
* @param obj The object to scan
* @param opts An object containing the preferences of the scanning
* @param cache A map containing the structures of the traversed objects
* @param prev A way for the function to communicate to the parent call (About circular references); If it's `false` then it does not check the internal properties
*/
scan(obj: any, opts?: Opts, cache?: Map<any, Struct>, prev?: (cir: Circular) => boolean): IStruct,
/**
* Gets the details of a proxy.
* If "x" is not a proxy it returns `null` or `undefined`.
* @param x The proxy
* @returns An array containing the [[Target]] and [[Handler]] of "x"
*/
fromProxy?(x: Proxy): [ object, object ]
/**
* Core functions and values that make `uneval()` work.
*/
utils: {
/** If you want to define a custom scan function you just need to put it in the object (or in its prototype) using this symbol as the key. */
customScan: symbol,
/** If you want to define a custom source function you just need to put it in the object (or in its prototype) using this symbol as the key. */
customSource: symbol,
/**
* Gets if the passed function source code defines an object method, like:
*
* func() {
* // ...
* }
*/
method: RegExp,
/**
* Returns the value of the first property of "x".
* Should be used with objects that have only one property.
* @param x The outer object
*/
first(x: object): any,
/**
* Same as `Object.assign()` but it sets special or non enumerable properties too.
* @param a The object in which to set the descriptors of "b"
* @param b The source object
* @returns "a"
*/
assign<A, B>(a: A, b: B): A & B,
/** Unpack this in your settings object to visualize spacing characters with colors. */
showFormatting: { space: string, tab: string, endl: string },
/** Object that maps class prototypes to a predicate that tells if the passed property should be serialized in an object that has that prototype. */
managedProtos: Map<any, (x: string | number | symbol) => boolean>,
Struct: typeof Struct,
Circular: typeof Circular,
Delegate: typeof Delegate,
/**
* Returns the best code to define a key in an object.
* @param str The key to define
* @param obj True if is to define inside of an object, False if is outside
* @param val Eventual code to put inside the square brackets of a symbol key definition
*/
key(str: string | symbol, obj?: boolean, struct?: IStruct, opts?: Opts): string,
/**
* Eventually caches a reference.
* @param struct The structure of the object
* @param opts An object containing the preferences of the conversion
* @returns The code to save the object to the cache
*/
def(struct: IStruct, opts: Opts): string,
/**
* Get if "key" would be put in in the array part or the object part of an array.
* @param {String|Symbol|Number} key The value to check
*/
index(key: string | symbol | number): boolean,
/**
* Stringifies an object or a primitive.
* @param obj The object to stringify
* @param struct The object representing the structure of "obj"
* @param opts An object containing the preferences of the conversion
* @param level The tabs to put before each line
*/
source(obj: any, struct: IStruct, opts?: Opts, level?: string): string,
/**
* Nested object types (Function, Objects and Arrays) with special notation.
*/
nested: {
/**
* Stringifies unmanaged objects and arrays.
* @param obj The object to stringify
* @param struct The object representing the structure of "obj"
* @param opts An object containing the preferences of the conversion
* @param level The tabs to put before each line
*/
circular(obj: any, struct: IStruct, opts: Opts, level: string): string,
/**
* Stringifies [[Target]] and [[Handler]] of a proxy.
* The proxy prototype is not inside `uneval.utils.managedProtos` because it does not exists.
* @param obj The proxy to stringify
* @param struct The object representing the structure of "obj"
* @param opts An object containing the preferences of the conversion
* @param level The tabs to put before each line
*/
proxy(obj: any | Proxy, struct: IStruct, opts: Opts, level: string): string,
/**
* Assigns the prototype of an object.
* @param obj The object to which the prototype will be assigned
* @param struct The object representing the structure of "obj"
* @param opts An object containing the preferences of the conversion
* @param level The tabs to put before each line
*/
proto(obj: any, struct: IStruct, opts: Opts, level: string): string,
/**
* Generates the source of a managed object and, if it has special properties, puts it inside of an `uneval.utils.assign()` call with its default object source.
* @param obj The object to stringify
* @param struct The object representing the structure of "obj"
* @param opts An object containing the preferences of the conversion
* @param level The tabs to put before each line
*/
assign(obj: any, struct: IStruct, opts: Opts, level: string): string,
/**
* Stringifies an array's managed part.
* @param obj The array
* @param struct The object representing the structure of "obj"
* @param opts An object containing the preferences of the conversion
*/
array(obj: any, struct: IStruct, opts: Opts, level: string): string,
/**
* Stringifies a function managed part.
* @param obj The function
* @param struct The object representing the structure of "obj"
* @param opts An object containing the preferences of the conversion
*/
function(obj: any, struct: IStruct, opts: Opts): string
}
}
};
export = _;
}