core.constructor
v1.0.4
Published
a constructor for a bare skeleton that can be extended by plugins
Downloads
854
Maintainers
Readme
Core
Kind: global class
- Core
- new Core(options)
- .typeOf(thing) ⇒ string
- .isUndefined(thing) ⇒ boolean
- .isNull(thing) ⇒ boolean
- .isBoolean(thing) ⇒ boolean
- .isNumber(thing) ⇒ boolean
- .isString(thing) ⇒ boolean
- .isDate(thing) ⇒ boolean
- .isArray(thing) ⇒ boolean
- .isObject(thing) ⇒ boolean
- .isFunction(thing) ⇒ boolean
- .assign(target, source, assignFunc) ⇒ object
- .extend(properties) ⇒ object
- .channel(name, array) ⇒ undefined
- .tap(name, func) ⇒ undefined
- .fire(name, data, callback) ⇒ undefined
- .plugin(definition, callback) ⇒ undefined
new Core(options)
| Param | Type | Description | | --- | --- | --- | | options | object | instance options. | | options.name | string | a unique name for the instance. | | options.plugins | array | an array of plugins to initialize on the instance. | | options.extend | object | if provided, this object will be merged in to the new instance. |
Example
var core = new Core({
name: 'client-core',
plugins: [
require('./pluginA'),
require('./pluginB')
],
extend: {
doStuff(){ ... }
}
});
core.typeOf(thing) ⇒ string
Returns the correct native type in javascript ( unlike the 'typeof' operator ).
Kind: instance method of Core
Returns: string - The native javascript type - 'undefined', 'null', 'boolean', 'number', 'string', 'array', 'object' or 'function'.
| Param | Type | Description | | --- | --- | --- | | thing | any | anything you want. |
Example
typeof null; // 'object'
typeof []; // 'object'
core.typeOf(null); // 'null'
core.typeOf([]); // 'array'
core.isUndefined(thing) ⇒ boolean
Checks if a value is undefined.
Kind: instance method of Core
Returns: boolean - - true if 'thing' is undefined. false otherwise.
| Param | Type | Description | | --- | --- | --- | | thing | any | anything you want. |
Example
core.isUndefined(null); // false
core.isNull(thing) ⇒ boolean
Checks if a value is null.
Kind: instance method of Core
Returns: boolean - - true if 'thing' is null. false otherwise.
| Param | Type | Description | | --- | --- | --- | | thing | any | anything you want. |
Example
core.isNull(null); // true
core.isBoolean(thing) ⇒ boolean
Checks if a value is a boolean.
Kind: instance method of Core
Returns: boolean - - true if 'thing' is boolean. false otherwise.
| Param | Type | Description | | --- | --- | --- | | thing | any | anything you want. |
Example
core.isBoolean(false); // true
core.isBoolean(''); // false
core.isNumber(thing) ⇒ boolean
Checks if a value is a number.
Kind: instance method of Core
Returns: boolean - - true if 'thing' is a number. false otherwise.
| Param | Type | Description | | --- | --- | --- | | thing | any | anything you want. |
Example
core.isNumber('35'); // false
core.isNumber(35); // true
core.isString(thing) ⇒ boolean
Checks if a value is a string.
Kind: instance method of Core
Returns: boolean - - true if 'thing' is a string. false otherwise.
| Param | Type | Description | | --- | --- | --- | | thing | any | anything you want. |
Example
core.isString('35'); // true
core.isString(35); // false
core.isDate(thing) ⇒ boolean
Checks if a value is a date object.
Kind: instance method of Core
Returns: boolean - - true if 'thing' is an array. false otherwise.
| Param | Type | Description | | --- | --- | --- | | thing | any | anything you want. |
Example
core.isDate('6/3/81'); // false
core.isDate(new Date('6/3/81')); // true
core.isArray(thing) ⇒ boolean
Checks if a value is an array.
Kind: instance method of Core
Returns: boolean - - true if 'thing' is an array. false otherwise.
| Param | Type | Description | | --- | --- | --- | | thing | any | anything you want. |
Example
core.isArray({}); // false
core.isArray([]); // true
core.isObject(thing) ⇒ boolean
Checks if a value is an object.
Kind: instance method of Core
Returns: boolean - - true if 'thing' is an object. false otherwise.
| Param | Type | Description | | --- | --- | --- | | thing | any | anything you want. |
Example
core.isObject({}); // true
core.isObject([]); // false
core.isFunction(thing) ⇒ boolean
Checks if a value is a function.
Kind: instance method of Core
Returns: boolean - - true if 'thing' is a function. false otherwise.
| Param | Type | Description | | --- | --- | --- | | thing | any | anything you want. |
Example
core.isFunction({}); // false
core.isFunction(e => {}); // true
core.assign(target, source, assignFunc) ⇒ object
Copies all properties from 'source' to 'target', similar to Object.assign.
Kind: instance method of Core
Returns: object - - The target object ( the first parameter ).
| Param | Type | Description | | --- | --- | --- | | target | object | The target object. properties will be copied to this object. | | source | object | A source, or a number of source objects. | | assignFunc | function | A function that will be called for each property assignment. if provided, the assigned value will be the return value of this function. |
Example
core.assign({}, {a: 1, b: 2}, (property, key, source) => property + 1); // { a: 2, b: 3 }
core.extend(properties) ⇒ object
Copies all members in 'properties' to the core instance.
Kind: instance method of Core
Returns: object - - Returns the target object ( the first parameter ).
| Param | Type | Description | | --- | --- | --- | | properties | object | An |
Example
core.extend({
getData(){ return this.myData; },
myData: 45
});
core.getData(); // 45.
core.myData; // 45.
core.channel(name, array) ⇒ undefined
Adds a new channel to the channels namespace object.
Kind: instance method of Core
| Param | Type | Description | | --- | --- | --- | | name | string | The name of the channel. | | array | array | Optional array of functions. |
Example
core.channel('collection');
core.channels.collection; // [].
core.tap(name, func) ⇒ undefined
tap to a channel.
Kind: instance method of Core
| Param | Type | Description | | --- | --- | --- | | name | string | The name of the channel. | | func | function | A function to attach to the channel. |
Example
core.channel('dataType');
core.tap('dataType', (dataType, done) => {
dataType.test = 'ok';
done(dataType);
});
core.fire('dataType', {}, (dataType) => {
dataType.test; // 'ok'
});
core.fire(name, data, callback) ⇒ undefined
Runs data through a named channel.
Kind: instance method of Core
| Param | Type | Description | | --- | --- | --- | | name | string | The name of the channel. | | data | any | Data to be passed through the channel. | | callback | function | A function that will be called when the job completes. |
Example
core.channel('dataType', (dataType, done) => {
dataType.test = 'ok';
done(dataType);
});
core.fire('dataType', {}, (dataType) => {
dataType.test; // 'ok'
});
core.plugin(definition, callback) ⇒ undefined
Adds a plugin to core instance.
Kind: instance method of Core
| Param | Type | Description | | --- | --- | --- | | definition | object | The plugin definition. | | definition.name | string | The name of the plugin. | | callback | function | A function that will be called when the job completes. |
Example
core.plugin({
name: 'myPlugin',
extend: {
getData(){ return core.plugins.myPlugin.data; }
},
init(def, done){
done({ data: 47 })
}
});
core.getData(); // 47.