Skip to content

Commit 15daf2f

Browse files
committed
refactor: make code base more inmutable
1 parent 7be8e37 commit 15daf2f

4 files changed

Lines changed: 16 additions & 20 deletions

File tree

‎src/errno.js‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,7 @@ const all = [
303303
},
304304
];
305305

306-
module.exports = {};
307-
308-
all.forEach((error) => {
309-
module.exports[error.errno] = error.description;
310-
});
306+
module.exports = all.reduce((obj, error) => ({
307+
...obj,
308+
[error.errno]: error.description,
309+
}));

‎src/removeNPMAbsolutePaths.js‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,26 +98,23 @@ async function processDir(dirPath, opts) {
9898
return arr;
9999
}
100100

101-
if (value.constructor === Array) {
102-
return arr.concat(value);
101+
if (Array.isArray(value)) {
102+
return [...arr, ...value];
103103
}
104104

105-
arr.push(value);
106-
return arr;
105+
return [...arr, value];
107106
}, [{ dirPath, success: true }]);
108107
} catch (err) {
109108
return [{ dirPath, err, success: false }];
110109
}
111110
}
112111

113-
async function removeNPMAbsolutePaths(filePath, opts) {
114-
opts = opts || {}; // eslint-disable-line no-param-reassign
115-
112+
async function removeNPMAbsolutePaths(filePath, opts = {}) {
116113
if (!filePath) {
117114
throw new ProcessingError('Missing path.\nThe first argument should be the path to a directory or a package.json file.');
118115
}
119116

120-
if (opts.fields && (opts.fields.constructor !== Array || opts.fields.length === 0)) {
117+
if (opts.fields && (!Array.isArray(opts.fields) || opts.fields.length === 0)) {
121118
throw new ProcessingError('Invalid option: fields.\nThe fields option should be an array cotaining the names of the specific fields that should be removed.');
122119
}
123120

‎test/cli.js‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ describe('cli', function () {
5252
});
5353

5454
it('set the fields option if --field flag and some fields are passed', function () {
55-
const args = [path, '--fields'].concat(testFields);
55+
const args = [path, '--fields', ...testFields];
5656
const parsedArgs = cli.parseArguments(args);
5757
expect(parsedArgs.path).to.equal(path);
5858
expect(parsedArgs.opts.fields).to.deep.equal(testFields);
@@ -61,17 +61,17 @@ describe('cli', function () {
6161
});
6262

6363
it('ignore unknown arguments', function () {
64-
const args1 = [path, unknownArg1, '--force', unknownArg2, '--fields'].concat(testFields);
65-
const args2 = [path, '--fields'].concat(testFields).concat(['--force', unknownArg1, unknownArg2]);
64+
const args1 = [path, unknownArg1, '--force', unknownArg2, '--fields', ...testFields];
65+
const args2 = [path, '--fields', ...testFields, '--force', unknownArg1, unknownArg2];
6666
const parsedArgs1 = cli.parseArguments(args1);
6767
const parsedArgs2 = cli.parseArguments(args2);
6868
expect(parsedArgs1.ignored).to.deep.equal(parsedArgs2.ignored);
6969
expect(parsedArgs1.ignored).to.deep.equal([unknownArg1, unknownArg2]);
7070
});
7171

7272
it('produce the same result regardless of the arguments passed', function () {
73-
const args1 = [path, unknownArg1, '--force', unknownArg2, '--fields'].concat(testFields);
74-
const args2 = [path, '--fields'].concat(testFields).concat(['--force', unknownArg1, unknownArg2]);
73+
const args1 = [path, unknownArg1, '--force', unknownArg2, '--fields', ...testFields];
74+
const args2 = [path, '--fields', ...testFields, '--force', unknownArg1, unknownArg2];
7575
const parsedArgs1 = cli.parseArguments(args1);
7676
const parsedArgs2 = cli.parseArguments(args2);
7777
expect(parsedArgs1).to.deep.equal(parsedArgs2);

‎test/removeNPMAbsolutePaths.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ describe('removeNPMAbsolutePaths.js', function () {
209209
});
210210

211211
it('return error if fields option is passed but is empty', function () {
212-
const dirPath = path.join(__dirname, 'data' ,'underscore_fields');
212+
const dirPath = path.join(__dirname, 'data', 'underscore_fields');
213213
const opts = {
214214
fields: [],
215215
};
@@ -324,7 +324,7 @@ describe('removeNPMAbsolutePaths.js', function () {
324324

325325
describe('fields', function () {
326326
it('return error if fields option is passed but is not an array', function () {
327-
const filePath = path.join(__dirname, 'data' , 'underscore_fields' , 'module' , 'package.json');
327+
const filePath = path.join(__dirname, 'data', 'underscore_fields', 'module', 'package.json');
328328
const opts = {
329329
fields: 'string_value',
330330
};

0 commit comments

Comments
 (0)