Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

gengojs-core-modules

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

gengojs-core-modules - npm Package Compare versions

Comparing version 2.0.9 to 2.1.0

9

CHANGELOG.md

@@ -0,1 +1,10 @@

2.0.9 / 2015-10-08
==================
* Release v2.0.9.
* Fix
Fixed issue where plugins not overriding correctly.
Added tests.
Updated change log.
2.0.8 / 2015-10-05

@@ -2,0 +11,0 @@ ==================

242

lib/modules/plugify.js
import _ from 'lodash';
import debug from 'gengojs-debug';
let log = debug('core');

@@ -13,26 +12,75 @@ /**

constructor(plugins, options, defaults) {
log.debug(`class: ${Plugify.name}`, `process: constructor`);
// Type stack to keep track which type has been plugged in
this.types = ['api', 'backend', 'parser', 'header', 'localize', 'router'];
// Local options
this.options = {};
// Initialize the plugin
this.plugins = this.init();
this.register(plugins, options, defaults);
this.bundle();
_.forEach(this.plugins, (value, key) => {
log.info(
`class: ${Plugify.name}`,
`plugins: type - ${key} typeof - ${typeof value}`);
log.debug(`class: ${Plugify.name}`, `process: constructor`);
// Local options
this.options = {};
this.defaults = {};
// Initialize the plugins
this.plugins = (() => {
if (_.isEmpty(defaults)) {
_.forEach(['api', 'backend', 'parser', 'header', 'localize', 'router'], item => {
this.defaults[this.normalize(item)] = () => {};
});
return this.defaults;
}
_.forOwn(defaults, (value, key) => {
if (_.isFunction(value) && _.isPlainObject(value()))
this.defaults[key] = value();
});
_.defaultsDeep(options, this.options);
return this.defaults;
})();
this.register(plugins);
_.forOwn(this.plugins, (value, key) => {
var name = value.package ? value.package.name : '';
log.info(
`class: ${Plugify.name}`,
`plugins: name - ${name}, type - ${key}, typeof - ${typeof value}`);
});
_.defaultsDeep(options, this.options);
}
/**
* Registers the plugin
* @param {Function | Array | Object} The plugin to register
*/
register(plugins) {
log.debug(`class: ${Plugify.name}`, `process: register`);
var process = (plugin) => {
if (_.isPlainObject(plugin)) {
if (!_.has(plugin, 'main') && (() => {
return _.forEach(Object.keys(plugin), key =>
key === 'api' || key === 'parser' || key === 'backend' ||
key === 'header' || key === 'localize' || key === 'router');
})()) {
if (_.forOwn(plugin, value => this.assert((() => {
return _.isFunction(value) ? value() :
_.isPlainObject(value) ? value : undefined;
})())))
_.forOwn(plugin, value => {
value = _.isFunction(value) ? value() :
_.isPlainObject(value) ? value : undefined;
this.setPlugin(value);
});
} else {
if (this.assert(plugin)) {
this.setPlugin(plugin);
}
}
}
};
if (_.isArray(plugins)) {
_.forEach(plugins, plugin => {
plugin = _.isFunction(plugin) ? plugin() :
_.isPlainObject(plugin) ? plugin : undefined;
process(plugin);
});
} else if (_.isFunction(plugins)) {
process(plugins());
} else if (_.isPlainObject(plugins)) process(plugins);
}
/**
* Sets the attributes in the plugin
* Sets the attributes of the plugin
* @param {Object} plugin The plugin to set its attributes.
* @param {Object} options The options to apply
*/
setAttributes(plugin) {
log.debug(`class: ${Plugify.name}`, `process: setAttributes`);
setPlugin(plugin) {
log.debug(`class: ${Plugify.name}`, `process: setPlugin`);
var {

@@ -42,11 +90,10 @@ main, defaults

var {
name, type
type
} = plugin.package;
type = this.normalize(type);
if (this.plugins[type]) this.plugins[type] = {};
// Set the plugin fn
this.plugins[type][name] = main;
this.plugins[type] = main;
// Set the package
this.plugins[type][name].package = plugin.package;
// Insert plugins as callbacks
this.plugins[type].push(main);
this.plugins[type].package = plugin.package;
// Set the default options

@@ -63,54 +110,5 @@ if (!this.options[type])

normalize(str) {
log.debug(`class: ${Plugify.name}`, `process: normalize`);
return str.toLowerCase().replace('-', '');
}
/**
* Initializes the plugin's stack
* @return {Object} The plugin statck
* @private
*/
init() {
log.debug(`class: ${Plugify.name}`, `process: init`);
return _.assign({}, {
parser: [],
router: [],
backend: [],
api: [],
header: [],
localize: []
});
}
/**
* Asserts that the plugins follows the definition and
* creates an array of plugin(s)
* @param {Object | Array| Function} plugins - The plugins to assert.
* @return {Array} An array of plugins
*/
plugs(plugins) {
log.debug(`class: ${Plugify.name}`, `process: plugs`);
var plugs = [];
// 'plugins' is a plain object
if (_.isPlainObject(plugins)) {
// A single ship exists
if (_.has(plugins, 'main')) plugs.push(plugins);
else _.forOwn(plugins, (ship) => {
try {
// Assert that ship is a function
if (!_.isFunction(ship)) throw new Error('Uh oh! The ship must be a function!');
if (!_.isPlainObject(ship())) throw new Error('Whoops! Did the ship forget to return a plain object?');
} catch (error) {
log.error(`class: ${Plugify.name}`,
`error: ${error.stack || error.toString() }`);
}
plugs.push(ship());
});
}
if (_.isArray(plugins)) plugs = plugins;
if (_.isFunction(plugins)) {
if (!_.isPlainObject(plugins())) throw new Error('Whoops! Did the ship forget to return a plain object?');
plugs.push(plugins());
}
return plugs;
}
/**
* Asserts the plugin is in proper format.

@@ -121,84 +119,16 @@ * @param {object} plugin - The plugin to assert.

assert(plugin) {
log.debug(`class: ${Plugify.name}`, `process: assert`);
try {
if (_.has(plugin, 'main')) throw new Error('Whoops! Did you forget the main function?');
if (_.has(plugin, 'package')) throw new Error('Whoops! Did you forget the package?');
if (_.has(plugin.package, 'type')) throw new Error('Whoops! Did you forget the "type" of plugin?');
if (_.has(plugin.package, 'name')) throw new Error('Whoops! Did you forget the "name" of plugin?');
if (!_.has(plugin.package, 'defaults')) throw new Error('Whoops! Did you forget to add "defaults"?');
if (_.has(plugin, 'defaults')) throw new Error('Whoops! Did you forget to add the "defaults"?');
} catch (error) {
log.error(`class: ${Plugify.name}`,
`error: ${error.stack || error.toString() }`);
}
log.debug(`class: ${Plugify.name}`, `process: assert`);
try {
if (!plugin) throw new Error('Whoops! Did you forget to ship your plugin?');
if (!_.has(plugin, 'main')) throw new Error('Whoops! Did you forget the main function?');
if (!_.has(plugin, 'package')) throw new Error('Whoops! Did you forget the package?');
if (!_.has(plugin.package, 'type')) throw new Error('Whoops! Did you forget the "type" of plugin?');
if (!_.has(plugin.package, 'name')) throw new Error('Whoops! Did you forget the "name" of plugin?');
if (!_.has(plugin, 'defaults')) throw new Error('Whoops! Did you forget to add the "defaults"?');
} catch (error) {
log.error(`class: ${Plugify.name}`,
error.stack || error.toString());
return false;
}
/**
* Registers the plugin
* @param {Object} plugins The plugin to register
* @param {Object} options The options to apply
* @param {Object} defaults The default plugins
*/
register(plugins, defaults) {
var plugs = this.plugs(plugins);
// Register and then restrict the
// plugins to one plugin per type
// and add defaults if none exist
_.forEach(plugs, function(plugin) {
// Assert
if (!_.isEmpty(defaults)) this.assert(plugin);
else log
.warn(`class: ${Plugify.name}`, `process: register`)
.warn('Defaults is empty! Possibly in testing mode?');
var type = this.normalize(plugin.package.type);
// If the default plugin already exists
// then remove the default and replace it with
// the user defined plugin
if (this.plugins[type].length === 1) {
if (defaults && defaults[type])
this.plugins[type].pop();
// Set the plugin attributes
this.setAttributes(plugin);
// If there are multiple plugins of the same type
// restrict it to one plugin
} else if (this.plugins[type].length > 1) {
var length = this.plugins[type].length - 1;
while (length !== 0) {
if (defaults && defaults[type])
this.plugins[type].pop();
length--;
}
// Since no there are no default plugins,
// just add the plugin to the stack
} else {
this.setAttributes(plugin);
}
}, this);
}
/**
* Bundles the plugins and transforms the plugin
* stack from an array to an object. It also makes sure
* that the stack has a fn placeholder to prevent an undefined
* object from being used as a function
* @private
*/
bundle() {
// Remove the plugin from array
// and set it as the root
// e.g. this.plugins.backend => array
// becomes this.plugins.backend => object
var plugs = this.plugins;
_.forEach(plugs, (plugin, type) => {
if (plugin[0]) {
// Get the index of the type from the types stack
var index = this.types.indexOf(this.normalize(type));
// Remove the type from the stack since it is registered
if (index > -1) this.types.splice(index, 1);
// Register the plugin
this.plugins[this.normalize(type)] = plugin[0];
}
});
// Set the placeholder
_.forEach(this.types, (type) => {
this.plugins[this.normalize(type)] = () => {};
});
return true;
}

@@ -210,3 +140,3 @@ }

* of Plugify
* @param {Object | Function} plugins [description]
* @param {Object | Function | Array} plugins The user plugins or plugins to override the default
* @param {Object} options The options to apply to the plugins

@@ -216,3 +146,3 @@ * @param {Object} defaults The default plugins

*/
function plugify(plugins, options = {}, defaults = {}) {
function plugify(plugins = {}, options = {}, defaults = {}) {
'use strict';

@@ -219,0 +149,0 @@ return new Plugify(plugins, options, defaults).plugins;

{
"name": "gengojs-core-modules",
"version": "2.0.9",
"version": "2.1.0",
"description": "gengo.js core modules is a set of modules that helps the core to function properly.",

@@ -5,0 +5,0 @@ "main": "src/modules/index.js",

@@ -30,18 +30,40 @@ 'use strict';

var Plugify = (function () {
/**
* plugins - The user's plugins ( [], function, {} )
* options - The user's options ( {} )
* defaults - The gengojs-default-pack ( { //...// }, {} )
*
* Psuedo code:
* case 1: 'plugins' may be an array of random plugins.
* -> assert that each plugin are properly shipped
* ->
*/
function Plugify(plugins, options, defaults) {
var _this = this;
_classCallCheck(this, Plugify);
log.debug('class: ' + Plugify.name, 'process: constructor');
// Type stack to keep track which type has been plugged in
this.types = ['api', 'backend', 'parser', 'header', 'localize', 'router'];
// Local options
this.options = {};
// Initialize the plugin
this.plugins = this.init();
this.register(plugins, options, defaults);
this.bundle();
_lodash2['default'].forEach(this.plugins, function (value, key) {
log.info('class: ' + Plugify.name, 'plugins: type - ' + key + ' typeof - ' + typeof value);
this.defaults = {};
// Initialize the plugins
this.plugins = (function () {
if (_lodash2['default'].isEmpty(defaults)) {
_lodash2['default'].forEach(['api', 'backend', 'parser', 'header', 'localize', 'router'], function (item) {
_this.defaults[_this.normalize(item)] = function () {};
});
return _this.defaults;
}
_lodash2['default'].forOwn(defaults, function (value, key) {
if (_lodash2['default'].isFunction(value) && _lodash2['default'].isPlainObject(value())) _this.defaults[key] = value();
});
return _this.defaults;
})();
this.register(plugins);
_lodash2['default'].forOwn(this.plugins, function (value, key) {
var name = value['package'] ? value['package'].name : '';
log.info('class: ' + Plugify.name, 'plugins: name - ' + name + ', type - ' + key + ', typeof - ' + typeof value);
});
_lodash2['default'].defaultsDeep(options, this.options);

@@ -53,3 +75,3 @@ }

* of Plugify
* @param {Object | Function} plugins [description]
* @param {Object | Function | Array} plugins The user plugins or plugins to override the default
* @param {Object} options The options to apply to the plugins

@@ -60,25 +82,59 @@ * @param {Object} defaults The default plugins

/**
* Sets the attributes in the plugin
* @param {Object} plugin The plugin to set its attributes.
* @param {Object} options The options to apply
*/
_createClass(Plugify, [{
key: 'register',
value: function register(plugins) {
var _this2 = this;
_createClass(Plugify, [{
key: 'setAttributes',
value: function setAttributes(plugin) {
log.debug('class: ' + Plugify.name, 'process: setAttributes');
log.debug('class: ' + Plugify.name, 'process: register');
var process = function process(plugin) {
if (_lodash2['default'].isPlainObject(plugin)) {
if (!_lodash2['default'].has(plugin, 'main') && (function () {
return _lodash2['default'].forEach(Object.keys(plugin), function (key) {
return key === 'api' || key === 'parser' || key === 'backend' || key === 'header' || key === 'localize' || key === 'router';
});
})()) {
if (_lodash2['default'].forOwn(plugin, function (value) {
return _this2.assert((function () {
return _lodash2['default'].isFunction(value) ? value() : _lodash2['default'].isPlainObject(value) ? value : undefined;
})());
})) _lodash2['default'].forOwn(plugin, function (value) {
value = _lodash2['default'].isFunction(value) ? value() : _lodash2['default'].isPlainObject(value) ? value : undefined;
_this2.setPlugin(value);
});
} else {
if (_this2.assert(plugin)) {
_this2.setPlugin(plugin);
}
}
}
};
if (_lodash2['default'].isArray(plugins)) {
_lodash2['default'].forEach(plugins, function (plugin) {
plugin = _lodash2['default'].isFunction(plugin) ? plugin() : _lodash2['default'].isPlainObject(plugin) ? plugin : undefined;
process(plugin);
});
} else if (_lodash2['default'].isFunction(plugins)) {
process(plugins());
} else if (_lodash2['default'].isPlainObject(plugins)) process(plugins);
}
/**
* Sets the attributes in the plugin
* @param {Object} plugin The plugin to set its attributes.
* @param {Object} options The options to apply
*/
}, {
key: 'setPlugin',
value: function setPlugin(plugin) {
log.debug('class: ' + Plugify.name, 'process: setPlugin');
var main = plugin.main;
var defaults = plugin.defaults;
var _plugin$package = plugin['package'];
var name = _plugin$package.name;
var type = _plugin$package.type;
var type = plugin['package'].type;
type = this.normalize(type);
if (this.plugins[type]) this.plugins[type] = {};
// Set the plugin fn
this.plugins[type][name] = main;
this.plugins[type] = main;
// Set the package
this.plugins[type][name]['package'] = plugin['package'];
// Insert plugins as callbacks
this.plugins[type].push(main);
this.plugins[type]['package'] = plugin['package'];
// Set the default options

@@ -97,3 +153,2 @@ if (!this.options[type]) this.options[type] = defaults;

value: function normalize(str) {
log.debug('class: ' + Plugify.name, 'process: normalize');
return str.toLowerCase().replace('-', '');

@@ -103,54 +158,2 @@ }

/**
* Initializes the plugin's stack
* @return {Object} The plugin statck
* @private
*/
}, {
key: 'init',
value: function init() {
log.debug('class: ' + Plugify.name, 'process: init');
return _lodash2['default'].assign({}, {
parser: [],
router: [],
backend: [],
api: [],
header: [],
localize: []
});
}
/**
* Asserts that the plugins follows the definition and
* creates an array of plugin(s)
* @param {Object | Array| Function} plugins - The plugins to assert.
* @return {Array} An array of plugins
*/
}, {
key: 'plugs',
value: function plugs(plugins) {
log.debug('class: ' + Plugify.name, 'process: plugs');
var plugs = [];
// 'plugins' is a plain object
if (_lodash2['default'].isPlainObject(plugins)) {
// A single ship exists
if (_lodash2['default'].has(plugins, 'main')) plugs.push(plugins);else _lodash2['default'].forOwn(plugins, function (ship) {
try {
// Assert that ship is a function
if (!_lodash2['default'].isFunction(ship)) throw new Error('Uh oh! The ship must be a function!');
if (!_lodash2['default'].isPlainObject(ship())) throw new Error('Whoops! Did the ship forget to return a plain object?');
} catch (error) {
log.error('class: ' + Plugify.name, 'error: ' + (error.stack || error.toString()));
}
plugs.push(ship());
});
}
if (_lodash2['default'].isArray(plugins)) plugs = plugins;
if (_lodash2['default'].isFunction(plugins)) {
if (!_lodash2['default'].isPlainObject(plugins())) throw new Error('Whoops! Did the ship forget to return a plain object?');
plugs.push(plugins());
}
return plugs;
}
/**
* Asserts the plugin is in proper format.

@@ -165,85 +168,14 @@ * @param {object} plugin - The plugin to assert.

try {
if (_lodash2['default'].has(plugin, 'main')) throw new Error('Whoops! Did you forget the main function?');
if (_lodash2['default'].has(plugin, 'package')) throw new Error('Whoops! Did you forget the package?');
if (_lodash2['default'].has(plugin['package'], 'type')) throw new Error('Whoops! Did you forget the "type" of plugin?');
if (_lodash2['default'].has(plugin['package'], 'name')) throw new Error('Whoops! Did you forget the "name" of plugin?');
if (!_lodash2['default'].has(plugin['package'], 'defaults')) throw new Error('Whoops! Did you forget to add "defaults"?');
if (_lodash2['default'].has(plugin, 'defaults')) throw new Error('Whoops! Did you forget to add the "defaults"?');
if (!plugin) throw new Error('Whoops! Did you forget to ship your plugin?');
if (!_lodash2['default'].has(plugin, 'main')) throw new Error('Whoops! Did you forget the main function?');
if (!_lodash2['default'].has(plugin, 'package')) throw new Error('Whoops! Did you forget the package?');
if (!_lodash2['default'].has(plugin['package'], 'type')) throw new Error('Whoops! Did you forget the "type" of plugin?');
if (!_lodash2['default'].has(plugin['package'], 'name')) throw new Error('Whoops! Did you forget the "name" of plugin?');
if (!_lodash2['default'].has(plugin, 'defaults')) throw new Error('Whoops! Did you forget to add the "defaults"?');
} catch (error) {
log.error('class: ' + Plugify.name, 'error: ' + (error.stack || error.toString()));
log.error('class: ' + Plugify.name, error.stack || error.toString());
return false;
}
return true;
}
/**
* Registers the plugin
* @param {Object} plugins The plugin to register
* @param {Object} options The options to apply
* @param {Object} defaults The default plugins
*/
}, {
key: 'register',
value: function register(plugins, defaults) {
var plugs = this.plugs(plugins);
// Register and then restrict the
// plugins to one plugin per type
// and add defaults if none exist
_lodash2['default'].forEach(plugs, function (plugin) {
// Assert
if (!_lodash2['default'].isEmpty(defaults)) this.assert(plugin);else log.warn('class: ' + Plugify.name, 'process: register').warn('Defaults is empty! Possibly in testing mode?');
var type = this.normalize(plugin['package'].type);
// If the default plugin already exists
// then remove the default and replace it with
// the user defined plugin
if (this.plugins[type].length === 1) {
if (defaults && defaults[type]) this.plugins[type].pop();
// Set the plugin attributes
this.setAttributes(plugin);
// If there are multiple plugins of the same type
// restrict it to one plugin
} else if (this.plugins[type].length > 1) {
var length = this.plugins[type].length - 1;
while (length !== 0) {
if (defaults && defaults[type]) this.plugins[type].pop();
length--;
}
// Since no there are no default plugins,
// just add the plugin to the stack
} else {
this.setAttributes(plugin);
}
}, this);
}
/**
* Bundles the plugins and transforms the plugin
* stack from an array to an object. It also makes sure
* that the stack has a fn placeholder to prevent an undefined
* object from being used as a function
* @private
*/
}, {
key: 'bundle',
value: function bundle() {
var _this = this;
// Remove the plugin from array
// and set it as the root
// e.g. this.plugins.backend => array
// becomes this.plugins.backend => object
var plugs = this.plugins;
_lodash2['default'].forEach(plugs, function (plugin, type) {
if (plugin[0]) {
// Get the index of the type from the types stack
var index = _this.types.indexOf(_this.normalize(type));
// Remove the type from the stack since it is registered
if (index > -1) _this.types.splice(index, 1);
// Register the plugin
_this.plugins[_this.normalize(type)] = plugin[0];
}
});
// Set the placeholder
_lodash2['default'].forEach(this.types, function (type) {
_this.plugins[_this.normalize(type)] = function () {};
});
}
}]);

@@ -254,4 +186,5 @@

function plugify(plugins) {
function plugify() {
'use strict';
var plugins = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];

@@ -258,0 +191,0 @@ var defaults = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];

@@ -13,13 +13,6 @@ var assert = require('chai').assert;

assert.isObject(plugins);
assert.notDeepEqual(plugins, {
parser: [],
router: [],
backend: [],
api: [],
header: [],
localize: []
});
assert.equal(plugins.parser.package.name, 'mocha-parser');
plugins = plugify((function(){
return {
main:function(){},
main:function ship(){},
package: { name : 'override-parser', 'type': 'parser' },

@@ -32,2 +25,3 @@ defaults: {

assert.equal(plugins.parser.package.name, 'override-parser');
assert.equal(plugins.router.package.name, 'mocha-router');
});

@@ -34,0 +28,0 @@ });

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc