Skip to content

Commit

Permalink
reduce dependencies and code complexity (#20)
Browse files Browse the repository at this point in the history
Remove a few dependencies

Reduce some code complexities and general code cleanup
  • Loading branch information
eddiemonge authored and SBoudrias committed Feb 11, 2017
1 parent 41b7f28 commit 69639ae
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 52 deletions.
100 changes: 50 additions & 50 deletions index.js
Expand Up @@ -11,21 +11,39 @@
'use strict';

var fs = require('fs');
var _ = require('lodash');
var pathExists = require('path-exists');
var toArray = require('lodash.toarray');
var pathExists = fs.existsSync;

function isFunction(obj) {
return typeof obj === 'function';
}

function extractMethods(methods) {
return _.isArray(methods) ? methods : Object.keys(methods).filter(function (method) {
return _.isFunction(methods[method]);
});
return Array.isArray(methods) ?
methods : Object.keys(methods).filter(function (method) {
return isFunction(methods[method]);
});
}

function convertArgs(args) {
if (args.length > 1) {
return [toArray(args)];
}
var arg = args[0];
return Array.isArray(arg) ? arg : [arg];
}

function readFile(filename, json) {
var file = fs.readFileSync(filename, 'utf8');
return json ? JSON.parse(file) : file;
}

// Extend the native assert module
var assert = module.exports = require('assert');

/**
* Assert that a file exists
* @param {String} path - path to a file
* @param {String} path - path to a file
* @example
* assert.file('templates/user.hbs');
*
Expand All @@ -38,18 +56,15 @@ var assert = module.exports = require('assert');
*/

assert.file = function () {
var args = _.toArray(arguments);
args = _.isString(args[0]) ? args : args[0];

args.forEach(function (file) {
var here = pathExists.sync(file);
convertArgs(arguments).forEach(function (file) {
var here = pathExists(file);
assert.ok(here, file + ', no such file or directory');
});
};

/**
* Assert that a file doesn't exist
* @param {String} file - path to a file
* @param {String} file - path to a file
* @example
* assert.noFile('templates/user.hbs');
*
Expand All @@ -62,19 +77,16 @@ assert.file = function () {
*/

assert.noFile = function () {
var args = _.toArray(arguments);
args = _.isString(args[0]) ? args : args[0];

args.forEach(function (file) {
var here = pathExists.sync(file);
convertArgs(arguments).forEach(function (file) {
var here = pathExists(file);
assert.ok(!here, file + ' exists');
});
};

/**
* Assert that a file's content matches a regex or string
* @param {String} file - path to a file
* @param {Regex|String} reg - regex / string that will be used to search the file
* @param {String} file - path to a file
* @param {Regex|String} reg - regex / string that will be used to search the file
* @example
* assert.fileContent('models/user.js', /App\.User = DS\.Model\.extend/);
* assert.fileContent('models/user.js', 'App.User = DS.Model.extend');
Expand All @@ -92,14 +104,11 @@ assert.noFile = function () {
*/

assert.fileContent = function () {
var args = _.toArray(arguments);
var pairs = _.isString(args[0]) ? [args] : args[0];

pairs.forEach(function (pair) {
convertArgs(arguments).forEach(function (pair) {
var file = pair[0];
var regex = pair[1];
assert.file(file);
var body = fs.readFileSync(file, 'utf8');
var body = readFile(file);

var match = false;
if (typeof regex === 'string') {
Expand All @@ -114,8 +123,8 @@ assert.fileContent = function () {

/**
* Assert that a file's content does not match a regex / string
* @param {String} file - path to a file
* @param {Regex|String} reg - regex / string that will be used to search the file
* @param {String} file - path to a file
* @param {Regex|String} reg - regex / string that will be used to search the file
* @example
* assert.noFileContent('models/user.js', /App\.User = DS\.Model\.extend/);
* assert.noFileContent('models/user.js', 'App.User = DS.Model.extend');
Expand All @@ -132,14 +141,11 @@ assert.fileContent = function () {
*/

assert.noFileContent = function () {
var args = _.toArray(arguments);
var pairs = _.isString(args[0]) ? [args] : args[0];

pairs.forEach(function (pair) {
convertArgs(arguments).forEach(function (pair) {
var file = pair[0];
var regex = pair[1];
assert.file(file);
var body = fs.readFileSync(file, 'utf8');
var body = readFile(file);

if (typeof regex === 'string') {
assert.ok(body.indexOf(regex) === -1, file + ' matched \'' + regex + '\'.');
Expand All @@ -152,7 +158,7 @@ assert.noFileContent = function () {

/**
* Assert that two strings are equal after standardization of newlines
* @param {String} value - a string
* @param {String} value - a string
* @param {String} expected - the expected value of the string
* @example
* assert.textEqual('I have a yellow cat', 'I have a yellow cat');
Expand All @@ -168,40 +174,36 @@ assert.textEqual = function (value, expected) {

/**
* Assert an Object implements an interface
* @param {Object} subject - subject implementing the façade
* @param {Object|Array} methods - a façace, hash or array of keys to be implemented
* @param {Object} subject - subject implementing the façade
* @param {Object|Array} methods - a façace, hash or array of keys to be implemented
*/

assert.implement = function (subject, methods) {
methods = extractMethods(methods);

var pass = methods.filter(function (method) {
return !_.isFunction(subject[method]);
var pass = extractMethods(methods).filter(function (method) {
return !isFunction(subject[method]);
});

assert.ok(pass.length === 0, 'expected object to implement methods named: ' + pass.join(', '));
};

/**
* Assert an Object doesn't implements any method of an interface
* @param {Object} subject - subject not implementing the methods
* @param {Object|Array} methods - hash or array of method names to be implemented
* @param {Object} subject - subject not implementing the methods
* @param {Object|Array} methods - hash or array of method names to be implemented
*/

assert.notImplement = function (subject, methods) {
methods = extractMethods(methods);

var pass = methods.filter(function (method) {
return _.isFunction(subject[method]);
var pass = extractMethods(methods).filter(function (method) {
return isFunction(subject[method]);
});

assert.ok(pass.length === 0, 'expected object to not implement any methods named: ' + pass.join(', '));
};

/**
* Assert an object contains the provided keys
* @param {Object} obj Object that should match the given pattern
* @param {Object} content An object of key/values the object should contains
* @param {Object} obj Object that should match the given pattern
* @param {Object} content An object of key/values the object should contains
*/

assert.objectContent = function (obj, content) {
Expand Down Expand Up @@ -239,8 +241,7 @@ assert.noObjectContent = function (obj, content) {
*/

assert.JSONFileContent = assert.jsonFileContent = function (filename, content) {
var obj = JSON.parse(fs.readFileSync(filename, 'utf8'));
assert.objectContent(obj, content);
assert.objectContent(readFile(filename, true), content);
};

/**
Expand All @@ -250,6 +251,5 @@ assert.JSONFileContent = assert.jsonFileContent = function (filename, content) {
*/

assert.noJSONFileContent = assert.noJsonFileContent = function (filename, content) {
var obj = JSON.parse(fs.readFileSync(filename, 'utf8'));
assert.noObjectContent(obj, content);
assert.noObjectContent(readFile(filename, true), content);
};
3 changes: 1 addition & 2 deletions package.json
Expand Up @@ -24,8 +24,7 @@
"prepublish": "gulp prepublish"
},
"dependencies": {
"lodash": "^4.17.4",
"path-exists": "^3.0.0"
"lodash.toarray": "^4.4.0"
},
"devDependencies": {
"eslint-config-xo-space": "^0.15.0",
Expand Down

0 comments on commit 69639ae

Please sign in to comment.