forked from DefinitelyTyped/DefinitelyTyped
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stampit.d.ts
155 lines (138 loc) · 6.58 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
// Type definitions for stampit
// Project: https://github.com/ericelliott/stampit
// Definitions by: Vasyl Boroviak <https://github.com/koresar>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
declare var stampit: stampit.Stampit;
declare module stampit {
interface Stampit {
/**
* Return a factory (akaStamp) function that will produce new objects using the
* prototypes that are passed in or composed.
* @param methods A map of method names and bodies for delegation.
* @param state A map of property names and values to clone for each new object.
* @param enclose A closure (function) used to create private data and privileged methods.
* */
(methods?:{}, state?:{}, enclose?:{(...encloseArgs:any[]): void}[]):stampit.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.
*/
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.
*/
mixIn(destination:any, ...source:any[]): any;
/**
* Alias for mixIn.
* 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.
*/
extend(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.
*/
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.
*/
convertConstructor(Constructor:any): Stamp;
}
/**
* A factory function that will produce new objects using the
* prototypes that are passed in or composed.
*/
export interface Stamp {
/**
* Just like calling 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;
/**
* 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;
/**
* An object map containing the fixed prototypes.
*/
fixed: Fixed;
/**
* Add methods to the methods prototype. Chainable.
* @param methods Object(s) containing map of method names and bodies for delegation.
* @return Self.
*/
methods(...methods:{}[]): Stamp;
/**
* Take n objects and add them to the state prototype. Changes `this` object. Chainable.
* @param states Object(s) containing map of property names and values to clone for each new object.
* @return Self.
*/
state(...states:{}[]): 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.
* Changes `this` object. Chainable.
* @param functions Closures (functions) used to create private data and privileged methods.
* @return Self.
*/
enclose(...functions:{(...encloseArgs:any[]): void}[]): 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.
* Changes `this` object. Chainable.
* @param methods Function properties of these objects will be treated as closure functions.
* @return Self.
*/
enclose(...methods:{}[]): 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;
}
/**
* An object map containing the fixed prototypes.
*/
interface Fixed {
methods: {};
state: {};
enclose: {(...encloseArgs:any[]): void}[];
}
}
declare module "stampit" {
export = stampit;
}