Skip to content

Commit 69639ae

Browse files
eddiemongeSBoudrias
authored andcommitted
reduce dependencies and code complexity (#20)
Remove a few dependencies Reduce some code complexities and general code cleanup
1 parent 41b7f28 commit 69639ae

File tree

2 files changed

+51
-52
lines changed

2 files changed

+51
-52
lines changed

index.js

+50-50
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,39 @@
1111
'use strict';
1212

1313
var fs = require('fs');
14-
var _ = require('lodash');
15-
var pathExists = require('path-exists');
14+
var toArray = require('lodash.toarray');
15+
var pathExists = fs.existsSync;
16+
17+
function isFunction(obj) {
18+
return typeof obj === 'function';
19+
}
1620

1721
function extractMethods(methods) {
18-
return _.isArray(methods) ? methods : Object.keys(methods).filter(function (method) {
19-
return _.isFunction(methods[method]);
20-
});
22+
return Array.isArray(methods) ?
23+
methods : Object.keys(methods).filter(function (method) {
24+
return isFunction(methods[method]);
25+
});
26+
}
27+
28+
function convertArgs(args) {
29+
if (args.length > 1) {
30+
return [toArray(args)];
31+
}
32+
var arg = args[0];
33+
return Array.isArray(arg) ? arg : [arg];
34+
}
35+
36+
function readFile(filename, json) {
37+
var file = fs.readFileSync(filename, 'utf8');
38+
return json ? JSON.parse(file) : file;
2139
}
2240

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

2644
/**
2745
* Assert that a file exists
28-
* @param {String} path - path to a file
46+
* @param {String} path - path to a file
2947
* @example
3048
* assert.file('templates/user.hbs');
3149
*
@@ -38,18 +56,15 @@ var assert = module.exports = require('assert');
3856
*/
3957

4058
assert.file = function () {
41-
var args = _.toArray(arguments);
42-
args = _.isString(args[0]) ? args : args[0];
43-
44-
args.forEach(function (file) {
45-
var here = pathExists.sync(file);
59+
convertArgs(arguments).forEach(function (file) {
60+
var here = pathExists(file);
4661
assert.ok(here, file + ', no such file or directory');
4762
});
4863
};
4964

5065
/**
5166
* Assert that a file doesn't exist
52-
* @param {String} file - path to a file
67+
* @param {String} file - path to a file
5368
* @example
5469
* assert.noFile('templates/user.hbs');
5570
*
@@ -62,19 +77,16 @@ assert.file = function () {
6277
*/
6378

6479
assert.noFile = function () {
65-
var args = _.toArray(arguments);
66-
args = _.isString(args[0]) ? args : args[0];
67-
68-
args.forEach(function (file) {
69-
var here = pathExists.sync(file);
80+
convertArgs(arguments).forEach(function (file) {
81+
var here = pathExists(file);
7082
assert.ok(!here, file + ' exists');
7183
});
7284
};
7385

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

94106
assert.fileContent = function () {
95-
var args = _.toArray(arguments);
96-
var pairs = _.isString(args[0]) ? [args] : args[0];
97-
98-
pairs.forEach(function (pair) {
107+
convertArgs(arguments).forEach(function (pair) {
99108
var file = pair[0];
100109
var regex = pair[1];
101110
assert.file(file);
102-
var body = fs.readFileSync(file, 'utf8');
111+
var body = readFile(file);
103112

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

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

134143
assert.noFileContent = function () {
135-
var args = _.toArray(arguments);
136-
var pairs = _.isString(args[0]) ? [args] : args[0];
137-
138-
pairs.forEach(function (pair) {
144+
convertArgs(arguments).forEach(function (pair) {
139145
var file = pair[0];
140146
var regex = pair[1];
141147
assert.file(file);
142-
var body = fs.readFileSync(file, 'utf8');
148+
var body = readFile(file);
143149

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

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

169175
/**
170176
* Assert an Object implements an interface
171-
* @param {Object} subject - subject implementing the façade
172-
* @param {Object|Array} methods - a façace, hash or array of keys to be implemented
177+
* @param {Object} subject - subject implementing the façade
178+
* @param {Object|Array} methods - a façace, hash or array of keys to be implemented
173179
*/
174180

175181
assert.implement = function (subject, methods) {
176-
methods = extractMethods(methods);
177-
178-
var pass = methods.filter(function (method) {
179-
return !_.isFunction(subject[method]);
182+
var pass = extractMethods(methods).filter(function (method) {
183+
return !isFunction(subject[method]);
180184
});
181185

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

185189
/**
186190
* Assert an Object doesn't implements any method of an interface
187-
* @param {Object} subject - subject not implementing the methods
188-
* @param {Object|Array} methods - hash or array of method names to be implemented
191+
* @param {Object} subject - subject not implementing the methods
192+
* @param {Object|Array} methods - hash or array of method names to be implemented
189193
*/
190194

191195
assert.notImplement = function (subject, methods) {
192-
methods = extractMethods(methods);
193-
194-
var pass = methods.filter(function (method) {
195-
return _.isFunction(subject[method]);
196+
var pass = extractMethods(methods).filter(function (method) {
197+
return isFunction(subject[method]);
196198
});
197199

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

201203
/**
202204
* Assert an object contains the provided keys
203-
* @param {Object} obj Object that should match the given pattern
204-
* @param {Object} content An object of key/values the object should contains
205+
* @param {Object} obj Object that should match the given pattern
206+
* @param {Object} content An object of key/values the object should contains
205207
*/
206208

207209
assert.objectContent = function (obj, content) {
@@ -239,8 +241,7 @@ assert.noObjectContent = function (obj, content) {
239241
*/
240242

241243
assert.JSONFileContent = assert.jsonFileContent = function (filename, content) {
242-
var obj = JSON.parse(fs.readFileSync(filename, 'utf8'));
243-
assert.objectContent(obj, content);
244+
assert.objectContent(readFile(filename, true), content);
244245
};
245246

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

252253
assert.noJSONFileContent = assert.noJsonFileContent = function (filename, content) {
253-
var obj = JSON.parse(fs.readFileSync(filename, 'utf8'));
254-
assert.noObjectContent(obj, content);
254+
assert.noObjectContent(readFile(filename, true), content);
255255
};

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
"prepublish": "gulp prepublish"
2525
},
2626
"dependencies": {
27-
"lodash": "^4.17.4",
28-
"path-exists": "^3.0.0"
27+
"lodash.toarray": "^4.4.0"
2928
},
3029
"devDependencies": {
3130
"eslint-config-xo-space": "^0.15.0",

0 commit comments

Comments
 (0)