Skip to content

Commit

Permalink
Convert classes to ES6.
Browse files Browse the repository at this point in the history
  • Loading branch information
pahen committed Jul 4, 2016
1 parent 40cdf73 commit 8134400
Show file tree
Hide file tree
Showing 7 changed files with 439 additions and 478 deletions.
3 changes: 0 additions & 3 deletions lib/graph.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
'use strict';

/**
* Module dependencies.
*/
const exec = require('child_process').exec;
const cyclic = require('./cyclic');
const graphviz = require('graphviz');
Expand Down
192 changes: 91 additions & 101 deletions lib/madge.js
Original file line number Diff line number Diff line change
@@ -1,123 +1,113 @@
'use strict';

/**
* Module dependencies.
*/
const cyclic = require('./cyclic');
const CJS = require('./parse/cjs');
const AMD = require('./parse/amd');
const ES6 = require('./parse/es6');
const graph = require('./graph');

/**
* Expose factory function.
* @api public
* @param {String|Array|Object} src
* @param {Object} opts
* @return {Madge}
*/
module.exports = function (src, opts) {
return new Madge(src, opts);
};
class Madge {
/**
* Class constructor.
* @constructor
* @api public
* @param {String|Array|Object} src
* @param {Object} opts
*/
constructor(src, opts) {
let tree = [];

/**
* Class constructor.
* @constructor
* @api public
* @param {String|Array|Object} src
* @param {Object} opts
*/
function Madge(src, opts) {
let tree = [];
this.opts = opts || {};
this.opts.format = String(this.opts.format || 'cjs').toLowerCase();

this.opts = opts || {};
this.opts.format = String(this.opts.format || 'cjs').toLowerCase();
if (typeof src === 'object' && !Array.isArray(src)) {
this.tree = src;
return;
}

if (typeof src === 'object' && !Array.isArray(src)) {
this.tree = src;
return;
}
if (typeof src === 'string') {
src = [src];
}

if (typeof src === 'string') {
src = [src];
}
if (src && src.length) {
tree = this.parse(src);
}

if (src && src.length) {
tree = this.parse(src);
this.tree = tree;
}

this.tree = tree;
}
/**
* Parse the given source folder(s).
* @param {Array|Object} src
* @return {Object}
*/
parse(src) {
if (this.opts.format === 'cjs') {
return new CJS(src, this.opts, this).tree;
} else if (this.opts.format === 'amd') {
return new AMD(src, this.opts, this).tree;
} else if (this.opts.format === 'es6') {
return new ES6(src, this.opts, this).tree;
} else {
throw new Error('invalid module format "' + this.opts.format + '"');
}
}

/**
* Parse the given source folder(s).
* @param {Array|Object} src
* @return {Object}
*/
Madge.prototype.parse = function (src) {
if (this.opts.format === 'cjs') {
return new CJS(src, this.opts, this).tree;
} else if (this.opts.format === 'amd') {
return new AMD(src, this.opts, this).tree;
} else if (this.opts.format === 'es6') {
return new ES6(src, this.opts, this).tree;
} else {
throw new Error('invalid module format "' + this.opts.format + '"');
/**
* Return the module dependency graph as an object.
* @api public
* @return {Object}
*/
obj() {
return this.tree;
}
};

/**
* Return the module dependency graph as an object.
* @api public
* @return {Object}
*/
Madge.prototype.obj = function () {
return this.tree;
};
/**
* Return the modules that has circular dependencies.
* @api public
* @return {Object}
*/
circular() {
return cyclic(this.tree);
}

/**
* Return the modules that has circular dependencies.
* @api public
* @return {Object}
*/
Madge.prototype.circular = function () {
return cyclic(this.tree);
};
/**
* Return a list of modules that depends on the given module.
* @api public
* @param {String} id
* @return {Array|Object}
*/
depends(id) {
return Object.keys(this.tree).filter((module) => {
if (this.tree[module]) {
return this.tree[module].reduce((acc, dependency) => {
if (dependency === id) {
acc = module;
}
return acc;
}, false);
}
}, this);
}

/**
* Return a list of modules that depends on the given module.
* @api public
* @param {String} id
* @return {Array|Object}
*/
Madge.prototype.depends = function (id) {
return Object.keys(this.tree).filter((module) => {
if (this.tree[module]) {
return this.tree[module].reduce((acc, dependency) => {
if (dependency === id) {
acc = module;
}
return acc;
}, false);
}
}, this);
};
/**
* Return the module dependency graph as DOT output.
* @api public
* @return {String}
*/
dot() {
return graph.dot(this.tree);
}

/**
* Return the module dependency graph as DOT output.
* @api public
* @return {String}
*/
Madge.prototype.dot = function () {
return graph.dot(this.tree);
};
/**
* Return the module dependency graph as a PNG image.
* @api public
* @param {Object} opts
* @param {Function} callback
*/
image(opts, callback) {
graph.image(this.tree, opts, callback);
}
}

/**
* Return the module dependency graph as a PNG image.
* @api public
* @param {Object} opts
* @param {Function} callback
*/
Madge.prototype.image = function (opts, callback) {
graph.image(this.tree, opts, callback);
};
module.exports = (src, opts) => new Madge(src, opts);
Loading

0 comments on commit 8134400

Please sign in to comment.