mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-08-13 13:30:28 +00:00
stampit v2 update
This commit is contained in:
Vendored
+155
@@ -0,0 +1,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;
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
/// <reference path="stampit-1.2.0.d.ts" />
|
||||
|
||||
var a = stampit().enclose(() => {
|
||||
var a = 'a';
|
||||
this.getA = () => {
|
||||
return a;
|
||||
};
|
||||
});
|
||||
a(); // Object -- so far so good.
|
||||
a().getA(); // "a"
|
||||
|
||||
|
||||
var b = stampit().enclose(function () {
|
||||
var a = 'b';
|
||||
this.getB = function () {
|
||||
return a;
|
||||
};
|
||||
});
|
||||
|
||||
|
||||
var c = stampit.compose(a, b);
|
||||
var foo = c(); // we won't throw this one away...
|
||||
foo.getA(); // "a"
|
||||
foo.getB(); // "b"
|
||||
|
||||
|
||||
// Some more privileged methods, with some private data.
|
||||
// Use stampit.mixIn() to make this feel declarative:
|
||||
var availability = stampit().enclose(function () {
|
||||
var isOpen = false; // private
|
||||
|
||||
return stampit.mixIn(this, {
|
||||
open: function open() {
|
||||
isOpen = true;
|
||||
return this;
|
||||
},
|
||||
close: function close() {
|
||||
isOpen = false;
|
||||
return this;
|
||||
},
|
||||
isOpen: function isOpenMethod() {
|
||||
return isOpen;
|
||||
}
|
||||
});
|
||||
});
|
||||
// Hre's a mixin with public methods, and some state:
|
||||
var membership = stampit({
|
||||
members: {},
|
||||
add: function (member: any) {
|
||||
this.members[member.name] = member;
|
||||
return this;
|
||||
},
|
||||
getMember: function (name: any) {
|
||||
return this.members[name];
|
||||
}
|
||||
},
|
||||
{
|
||||
members: {}
|
||||
});
|
||||
// Let's set some defaults:
|
||||
var defaults = stampit().state({
|
||||
name: 'The Saloon',
|
||||
specials: 'Whisky, Gin, Tequila'
|
||||
});
|
||||
|
||||
// Classical inheritance has nothing on this. No parent/child coupling. No deep inheritance hierarchies.
|
||||
// Just good, clean code reusability.
|
||||
var bar = stampit.compose(defaults, availability, membership);
|
||||
// Note that you can override state on instantiation:
|
||||
var myBar = bar({name: 'Moe\'s'});
|
||||
// Silly, but proves that everything is as it should be.
|
||||
myBar.add({name: 'Homer' }).open().getMember('Homer');
|
||||
|
||||
|
||||
|
||||
var myStamp = stampit().methods({
|
||||
foo: function () {
|
||||
return 'foo';
|
||||
},
|
||||
methodOverride: function () {
|
||||
return false;
|
||||
}
|
||||
}).methods({
|
||||
bar: function () {
|
||||
return 'bar'
|
||||
},
|
||||
methodOverride: function () {
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
myStamp.state({
|
||||
foo: {bar: 'bar'},
|
||||
stateOverride: false
|
||||
}).state({
|
||||
bar: 'bar',
|
||||
stateOverride: true
|
||||
});
|
||||
|
||||
myStamp.enclose(function () {
|
||||
var secret = 'foo';
|
||||
|
||||
this.getSecret = function () {
|
||||
return secret;
|
||||
};
|
||||
}).enclose(function () {
|
||||
this.a = true;
|
||||
}).enclose({
|
||||
bar: function bar() {
|
||||
this.b = true;
|
||||
}
|
||||
}, {
|
||||
baz: function baz() {
|
||||
this.c = true;
|
||||
}
|
||||
});
|
||||
|
||||
var obj = myStamp.create();
|
||||
obj.getSecret && obj.a && obj.b && obj.c; // true
|
||||
|
||||
var newStamp = stampit(null, { defaultNum: 1 }).compose(myStamp);
|
||||
|
||||
|
||||
|
||||
var obj1 = stampit().methods({
|
||||
a: function () { return 'a'; }
|
||||
}, {
|
||||
b: function () { return 'b'; }
|
||||
}).create();
|
||||
|
||||
var obj2 = stampit().state({
|
||||
a: 'a'
|
||||
}, {
|
||||
b: 'b'
|
||||
}).create();
|
||||
|
||||
var obj = defaults.compose(newStamp, membership, availability).create();
|
||||
|
||||
|
||||
|
||||
// The old constructor / class thing...
|
||||
var Constructor = function Constructor() {
|
||||
this.thing = 'initialized';
|
||||
};
|
||||
Constructor.prototype.foo = function foo() { return 'foo'; };
|
||||
|
||||
// The conversion
|
||||
var oldskool = stampit.convertConstructor(Constructor);
|
||||
|
||||
// A new stamp to compose with...
|
||||
var newskool = stampit().methods({
|
||||
bar: function bar() { return 'bar'; }
|
||||
// your methods here...
|
||||
}).enclose(function () {
|
||||
this.baz = 'baz';
|
||||
});
|
||||
|
||||
// Now you can compose those old constructors just like you could
|
||||
// with any other stamp...
|
||||
var myThing = stampit.compose(oldskool, newskool);
|
||||
|
||||
var t = myThing();
|
||||
|
||||
t.thing; // 'initialized',
|
||||
|
||||
t.foo(); // 'foo',
|
||||
|
||||
t.bar(); // 'bar'
|
||||
+39
-30
@@ -1,7 +1,8 @@
|
||||
/// <reference path="stampit.d.ts" />
|
||||
import stampit = require('./stampit.d');
|
||||
|
||||
var a = stampit().enclose(() => {
|
||||
var a = 'a';
|
||||
var a = stampit().init((options) => {
|
||||
var a = options.args[0];
|
||||
this.getA = () => {
|
||||
return a;
|
||||
};
|
||||
@@ -10,7 +11,7 @@ a(); // Object -- so far so good.
|
||||
a().getA(); // "a"
|
||||
|
||||
|
||||
var b = stampit().enclose(function () {
|
||||
var b = stampit().init(function () {
|
||||
var a = 'b';
|
||||
this.getB = function () {
|
||||
return a;
|
||||
@@ -26,7 +27,7 @@ foo.getB(); // "b"
|
||||
|
||||
// Some more privileged methods, with some private data.
|
||||
// Use stampit.mixIn() to make this feel declarative:
|
||||
var availability = stampit().enclose(function () {
|
||||
var availability = stampit().init(function () {
|
||||
var isOpen = false; // private
|
||||
|
||||
return stampit.mixIn(this, {
|
||||
@@ -43,22 +44,25 @@ var availability = stampit().enclose(function () {
|
||||
}
|
||||
});
|
||||
});
|
||||
// Hre's a mixin with public methods, and some state:
|
||||
|
||||
// Here's a mixin with public methods, and some refs:
|
||||
var membership = stampit({
|
||||
methods: {
|
||||
members: {},
|
||||
add: function (member: any) {
|
||||
add: function (member:any) {
|
||||
this.members[member.name] = member;
|
||||
return this;
|
||||
},
|
||||
getMember: function (name: any) {
|
||||
getMember: function (name:any) {
|
||||
return this.members[name];
|
||||
}
|
||||
},
|
||||
{
|
||||
refs: {
|
||||
members: {}
|
||||
});
|
||||
}
|
||||
});
|
||||
// Let's set some defaults:
|
||||
var defaults = stampit().state({
|
||||
var defaults = stampit().refs({
|
||||
name: 'The Saloon',
|
||||
specials: 'Whisky, Gin, Tequila'
|
||||
});
|
||||
@@ -66,11 +70,10 @@ var defaults = stampit().state({
|
||||
// Classical inheritance has nothing on this. No parent/child coupling. No deep inheritance hierarchies.
|
||||
// Just good, clean code reusability.
|
||||
var bar = stampit.compose(defaults, availability, membership);
|
||||
// Note that you can override state on instantiation:
|
||||
// Note that you can override refs on instantiation:
|
||||
var myBar = bar({name: 'Moe\'s'});
|
||||
// Silly, but proves that everything is as it should be.
|
||||
myBar.add({name: 'Homer' }).open().getMember('Homer');
|
||||
|
||||
myBar.add({name: 'Homer'}).open().getMember('Homer');
|
||||
|
||||
|
||||
var myStamp = stampit().methods({
|
||||
@@ -89,23 +92,23 @@ var myStamp = stampit().methods({
|
||||
}
|
||||
});
|
||||
|
||||
myStamp.state({
|
||||
myStamp.refs({
|
||||
foo: {bar: 'bar'},
|
||||
stateOverride: false
|
||||
}).state({
|
||||
refsOverride: false
|
||||
}).refs({
|
||||
bar: 'bar',
|
||||
stateOverride: true
|
||||
refsOverride: true
|
||||
});
|
||||
|
||||
myStamp.enclose(function () {
|
||||
myStamp.init(function () {
|
||||
var secret = 'foo';
|
||||
|
||||
this.getSecret = function () {
|
||||
return secret;
|
||||
};
|
||||
}).enclose(function () {
|
||||
}).init(function () {
|
||||
this.a = true;
|
||||
}).enclose({
|
||||
}).init({
|
||||
bar: function bar() {
|
||||
this.b = true;
|
||||
}
|
||||
@@ -118,17 +121,20 @@ myStamp.enclose(function () {
|
||||
var obj = myStamp.create();
|
||||
obj.getSecret && obj.a && obj.b && obj.c; // true
|
||||
|
||||
var newStamp = stampit(null, { defaultNum: 1 }).compose(myStamp);
|
||||
|
||||
var newStamp = stampit({refs: {defaultNum: 1}}).compose(myStamp);
|
||||
|
||||
|
||||
var obj1 = stampit().methods({
|
||||
a: function () { return 'a'; }
|
||||
a: function () {
|
||||
return 'a';
|
||||
}
|
||||
}, {
|
||||
b: function () { return 'b'; }
|
||||
b: function () {
|
||||
return 'b';
|
||||
}
|
||||
}).create();
|
||||
|
||||
var obj2 = stampit().state({
|
||||
var obj2 = stampit().refs({
|
||||
a: 'a'
|
||||
}, {
|
||||
b: 'b'
|
||||
@@ -137,21 +143,24 @@ var obj2 = stampit().state({
|
||||
var obj = defaults.compose(newStamp, membership, availability).create();
|
||||
|
||||
|
||||
|
||||
// The old constructor / class thing...
|
||||
var Constructor = function Constructor() {
|
||||
this.thing = 'initialized';
|
||||
};
|
||||
Constructor.prototype.foo = function foo() { return 'foo'; };
|
||||
Constructor.prototype.foo = function foo() {
|
||||
return 'foo';
|
||||
};
|
||||
|
||||
// The conversion
|
||||
var oldskool = stampit.convertConstructor(Constructor);
|
||||
|
||||
// A new stamp to compose with...
|
||||
var newskool = stampit().methods({
|
||||
bar: function bar() { return 'bar'; }
|
||||
bar: function bar() {
|
||||
return 'bar';
|
||||
}
|
||||
// your methods here...
|
||||
}).enclose(function () {
|
||||
}).init(function () {
|
||||
this.baz = 'baz';
|
||||
});
|
||||
|
||||
@@ -165,4 +174,4 @@ t.thing; // 'initialized',
|
||||
|
||||
t.foo(); // 'foo',
|
||||
|
||||
t.bar(); // 'bar'
|
||||
t.bar(); // 'bar'
|
||||
|
||||
Vendored
+276
-134
@@ -1,155 +1,297 @@
|
||||
// Type definitions for stampit
|
||||
// Project: https://github.com/ericelliott/stampit
|
||||
// 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
|
||||
|
||||
declare var stampit: stampit.Stampit;
|
||||
/**
|
||||
* Function used as .init() argument.
|
||||
*/
|
||||
interface Init {
|
||||
(ctx:Context): any | Promise;
|
||||
}
|
||||
|
||||
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;
|
||||
interface Promise {
|
||||
then(resolve:(result: any) => any|Promise, reject:(reason: any | Error) => any|Promise): Promise
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
/**
|
||||
* The .init() function argument.
|
||||
*/
|
||||
interface Context {
|
||||
/**
|
||||
* The object which has been just instantiated.
|
||||
*/
|
||||
instance: any;
|
||||
|
||||
/**
|
||||
* A factory function that will produce new objects using the
|
||||
* prototypes that are passed in or composed.
|
||||
* The stamp the object has been instantiated with.
|
||||
*/
|
||||
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;
|
||||
stamp: Stamp;
|
||||
|
||||
/**
|
||||
* 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;
|
||||
/**
|
||||
* The arguments list passed to the stamp.
|
||||
*/
|
||||
args: any[];
|
||||
}
|
||||
|
||||
/**
|
||||
* An object map containing the fixed prototypes.
|
||||
*/
|
||||
fixed: Fixed;
|
||||
/**
|
||||
* An object map containing the fixed prototypes.
|
||||
*/
|
||||
interface Fixed {
|
||||
methods: {};
|
||||
|
||||
/**
|
||||
* 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;
|
||||
/**
|
||||
* @deprecated Use .refs() instead.
|
||||
*/
|
||||
state: {};
|
||||
|
||||
/**
|
||||
* 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;
|
||||
refs: {};
|
||||
|
||||
/**
|
||||
* 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;
|
||||
/**
|
||||
* @deprecated Use .init() instead.
|
||||
*/
|
||||
enclose: Init[];
|
||||
|
||||
/**
|
||||
* 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;
|
||||
init: Init[];
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
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.
|
||||
*/
|
||||
interface Fixed {
|
||||
methods: {};
|
||||
state: {};
|
||||
enclose: {(...encloseArgs:any[]): void}[];
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
declare module "stampit" {
|
||||
export = stampit;
|
||||
}
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
export = stampit;
|
||||
|
||||
Reference in New Issue
Block a user