forked from DefinitelyTyped/DefinitelyTyped
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stampit.d.ts
299 lines (254 loc) · 9.77 KB
/
stampit.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
// Type definitions for stampit 2.1
// Project: https://github.com/stampit-org/stampit
// Definitions by: Vasyl Boroviak <https://github.com/koresar>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
/**
* Function used as .init() argument.
*/
interface Init {
(ctx:Context): any | Promise;
}
interface Promise {
then(resolve:(result: any) => any|Promise, reject:(reason: any | Error) => any|Promise): Promise
}
/**
* The .init() function argument.
*/
interface Context {
/**
* The object which has been just instantiated.
*/
instance: any;
/**
* The stamp the object has been instantiated with.
*/
stamp: Stamp;
/**
* The arguments list passed to the stamp.
*/
args: any[];
}
/**
* An object map containing the fixed prototypes.
*/
interface Fixed {
methods: {};
/**
* @deprecated Use .refs() instead.
*/
state: {};
refs: {};
/**
* @deprecated Use .init() instead.
*/
enclose: Init[];
init: Init[];
props: {};
static: {};
}
interface Options {
/**
* A hash containing methods (functions) of any future created instance.
*/
methods?: {} | {}[];
/**
* A hash containing references to the object. This hash will be shallow mixed into any future created instance.
*/
refs?: {} | {}[];
/**
* Initialization function which will be called per each newly created instance.
*/
init?: Init | Init[];
/**
* Properties which will be deeply (but safely, no data override) merged into any future created instance.
*/
props?: {} | {}[];
/**
* Properties which will be mixed to the new and any other stamp which this stamp will be composed with.
*/
static?: {} | {}[];
}
/**
* A factory function that will produce new objects using the
* prototypes that are passed in or composed.
*/
interface Stamp {
/**
* Invokes the stamp and returns a new object instance.
* @param state Properties you wish to set on the new objects.
* @param encloseArgs The remaining arguments are passed to all .enclose() functions.
* WARNING Avoid using two different .enclose() functions that expect different arguments.
* .enclose() functions that take arguments should not be considered safe to compose
* with other .enclose() functions that also take arguments. Taking arguments with
* an .enclose() function is an anti-pattern that should be avoided, when possible.
* @return A new object composed of the Stamps and prototypes provided.
*/
(state?:{}, ...encloseArgs:any[]): any | Promise;
/**
* Just like calling stamp(), stamp.create() invokes the stamp and returns a new instance.
* @param state Properties you wish to set on the new objects.
* @param encloseArgs The remaining arguments are passed to all .enclose() functions.
* WARNING Avoid using two different .enclose() functions that expect different arguments.
* .enclose() functions that take arguments should not be considered safe to compose
* with other .enclose() functions that also take arguments. Taking arguments with
* an .enclose() function is an anti-pattern that should be avoided, when possible.
* @return A new object composed of the Stamps and prototypes provided.
*/
create(state?:{}, ...encloseArgs:any[]): any | Promise;
/**
* An object map containing the fixed prototypes.
*/
fixed: Fixed;
/**
* Add methods to the methods prototype. Creates and returns new Stamp. Chainable.
* @param methods Object(s) containing map of method names and bodies for delegation.
* @return A new Stamp.
*/
methods(...methods:{}[]): Stamp;
/**
* Take n objects and add them to the state prototype. Creates and returns new Stamp. Chainable.
* @param states Object(s) containing map of property names and values to clone for each new object.
* @return A new Stamp.
*/
refs(...states:{}[]): Stamp;
/**
* Take n objects and merge them (but safely, no data override) to the of any future created instance.
* Creates and returns new Stamp. Chainable.
* @param objects Object(s) to merge for each new object.
* @return A new Stamp.
*/
props(...objects:{}[]): Stamp;
/**
* @deprecated Use .refs() instead.
*/
state(...states:{}[]): Stamp;
/**
* @deprecated Use .init() instead.
*/
enclose(...functions:Init[]): Stamp;
/**
* @deprecated Use .init() instead.
*/
enclose(...functions:{}[]): Stamp;
/**
* Take n functions, an array of functions, or n objects and add the functions to the enclose prototype.
* Functions passed into .enclose() are called any time an object is instantiated.
* That happens when the stamp function is invoked, or when the .create() method is called.
* Creates and returns new Stamp. Chainable.
* @param functions Closures (functions) used to create private data and privileged methods.
* @return A new Stamp.
*/
init(...functions:Init[]): Stamp;
/**
* Take n functions, an array of functions, or n objects and add the functions to the enclose prototype.
* Functions passed into .enclose() are called any time an object is instantiated.
* That happens when the stamp function is invoked, or when the .create() method is called.
* Creates and returns new Stamp. Chainable.
* @param functions Function properties of these objects will be treated as closure functions.
* @return A new Stamp.
*/
init(...functions:{}[]): Stamp;
/**
* Take n objects and add them to a new stamp and any future stamp it composes with.
* Creates and returns new Stamp. Chainable.
* @param statics Object(s) containing map of property names and values to mixin into each new stamp.
* @return A new Stamp.
*/
static(...statics:{}[]): Stamp;
/**
* Take one or more Stamps and
* combine them with `this` to produce and return a new Stamp.
* Combining overrides properties with last-in priority.
* NOT chainable.
* @param stamps Stampit factories, aka Stamps.
* @return A new Stamp composed from arguments and `this`.
*/
compose(...stamps:Stamp[]): Stamp;
}
/**
* Return a factory (akaStamp) function that will produce new objects using the
* prototypes that are passed in or composed.
* @param {object} options Stampit options object containing refs, methods, init, props, and static.
* @param {object} options.methods A map of method names and bodies for delegation.
* @param {object} options.refs A map of property names and values to clone for each new object.
* @param {object} options.props A map of property names and values to clone for each new object.
* @param {function} options.init A closure(s) (function(s)) used to create private data and privileged methods.
* @param {object} options.static A map of properties to mixin into new and other stamp it will compose with.
* */
declare function stampit(options?: Options): Stamp
declare module stampit {
/**
* A shortcut methods for stampit().methods()
* @param methods Object(s) containing map of method names and bodies for delegation.
* @return A new Stamp.
*/
export function methods(...methods:{}[]): Stamp;
/**
* A shortcut methods for stampit().refs()
* @param states Object(s) containing map of property names and values to clone for each new object.
* @return A new Stamp.
*/
export function refs(...states:{}[]): Stamp;
/**
* A shortcut methods for stampit().props()
* @param states Object(s) to merge for each new object.
* @return A new Stamp.
*/
export function props(...states:{}[]): Stamp;
/**
* A shortcut methods for stampit().init()
* @param functions Closures (functions) used to create private data and privileged methods.
* @return A new Stamp.
*/
export function init(...functions:Init[]): Stamp;
/**
* A shortcut methods for stampit().static()
* @param statics Object(s) containing map of property names and values to mixin into each new stamp (NOT OBJECT).
* @return A new Stamp.
*/
export function static(...statics:{}[]): Stamp;
/**
* Take two or more Stamps and combine them to produce a new Stamp.
* Combining overrides properties with last-in priority.
* @param stamps Stamps produced by stampit.
* @return A new Stamp made of all the given.
*/
export function compose(...stamps:Stamp[]): Stamp;
/**
* Take a destination object followed by one or more source objects,
* and copy the source object properties to the destination object,
* with last in priority overrides.
* @param destination An object to copy properties to.
* @param source Objects to copy properties from.
* @return The destination object.
*/
export function mixin(destination:any, ...source:any[]): any;
/**
* Alias for mixin()
*/
export function mixIn(destination:any, ...source:any[]): any;
/**
* Alias for mixin()
*/
export function extend(destination:any, ...source:any[]): any;
/**
* Alias for mixin()
*/
export function assign(destination:any, ...source:any[]): any;
/**
* Check if an object is a Stamp.
* @param obj An object to check.
* @return true if the object is a Stamp; otherwise - false.
*/
export function isStamp(obj:any): boolean;
/**
* Take an old-fashioned JS constructor and return a Stamp
* that you can freely compose with other Stamps.
* @param Constructor Old-fashioned constructor function.
* @return A new Stamp based on the given constructor.
*/
export function convertConstructor(Constructor:any): Stamp;
}
declare module "stampit" {
export = stampit;
}