Skip to content

Commit f65495a

Browse files
committed
refactor: replace lodash helpers with native alternatives
1 parent ed03982 commit f65495a

4 files changed

Lines changed: 68 additions & 67 deletions

File tree

‎lib/RollingFileWriteStream.js‎

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const debug = require("debug")("streamroller:RollingFileWriteStream");
2-
const _ = require("lodash");
32
const async = require("async");
43
const fs = require("fs-extra");
54
const zlib = require("zlib");
@@ -146,7 +145,7 @@ class RollingFileWriteStream extends Writable {
146145
keepFileExt: false,
147146
alwaysIncludePattern: false
148147
};
149-
const options = _.defaults({}, rawOptions, defaultOptions);
148+
const options = Object.assign({}, defaultOptions, rawOptions);
150149
if (options.maxSize <= 0) {
151150
throw new Error(`options.maxSize (${options.maxSize}) should be > 0`);
152151
}
@@ -198,11 +197,13 @@ class RollingFileWriteStream extends Writable {
198197
_getExistingFiles(cb) {
199198
fs.readdir(this.fileObject.dir, (e, files) => {
200199
debug(`_getExistingFiles: files=${files}`);
201-
const existingFileDetails = _.chain(files)
200+
const existingFileDetails = (files || [])
202201
.map(n => this.fileNameParser(n))
203-
.compact()
204-
.sortBy(n => (n.timestamp ? n.timestamp : newNow().getTime()) - n.index)
205-
.value();
202+
.filter(n => n !== undefined && n !== null);
203+
204+
const getKey = n => (n.timestamp ? n.timestamp : newNow().getTime()) - n.index
205+
existingFileDetails.sort((a, b) => getKey(a) - getKey(b))
206+
206207
cb(null, existingFileDetails);
207208
});
208209
}
@@ -274,7 +275,11 @@ class RollingFileWriteStream extends Writable {
274275
date: this.state.currentDate,
275276
index: 0
276277
});
277-
const ops = _.pick(this.options, ["flags", "encoding", "mode"]);
278+
const ops = {
279+
flags: this.options.flags,
280+
encoding: this.options.encoding,
281+
mode: this.options.mode,
282+
};
278283
this.currentFileStream = fs.createWriteStream(filePath, ops);
279284
this.currentFileStream.on("error", e => {
280285
this.emit("error", e);
@@ -291,8 +296,7 @@ class RollingFileWriteStream extends Writable {
291296
this.options.numToKeep > 0 &&
292297
existingFileDetails.length > this.options.numToKeep
293298
) {
294-
const fileNamesToRemove = _.slice(
295-
existingFileDetails.map(f => f.filename),
299+
const fileNamesToRemove = existingFileDetails.map(f => f.filename).slice(
296300
0,
297301
existingFileDetails.length - this.options.numToKeep - 1
298302
);
@@ -306,7 +310,7 @@ class RollingFileWriteStream extends Writable {
306310
_deleteFiles(fileNames, done) {
307311
debug(`files to delete: ${fileNames}`);
308312
async.each(
309-
_.map(fileNames, f => path.format({ dir: this.fileObject.dir, base: f })),
313+
fileNames.map(f => path.format({ dir: this.fileObject.dir, base: f })),
310314
fs.unlink,
311315
done
312316
);

‎lib/fileNameParser.js‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
const debug = require("debug")("streamroller:fileNameParser");
22
const FILENAME_SEP = ".";
33
const ZIP_EXT = ".gz";
4-
const _ = require("lodash");
54
const format = require("date-format");
65

76
module.exports = ({ file, keepFileExt, pattern }) => {
87
// All these functions take two arguments: f, the filename, and p, the result placeholder
98
// They return the filename with any matching parts removed.
109
// The "zip" function, for instance, removes the ".gz" part of the filename (if present)
1110
const zip = (f, p) => {
12-
if (_.endsWith(f, ZIP_EXT)) {
11+
if (f.endsWith(ZIP_EXT)) {
1312
debug("it is gzipped");
1413
p.isCompressed = true;
1514
return f.slice(0, -1 * ZIP_EXT.length);
@@ -18,23 +17,23 @@ module.exports = ({ file, keepFileExt, pattern }) => {
1817
};
1918

2019
const extAtEnd = f => {
21-
if (_.startsWith(f, file.name) && _.endsWith(f, file.ext)) {
20+
if (f.startsWith(file.name) && f.endsWith(file.ext)) {
2221
debug("it starts and ends with the right things");
2322
return f.slice(file.name.length + 1, -1 * file.ext.length);
2423
}
2524
return f;
2625
};
2726

2827
const extInMiddle = f => {
29-
if (_.startsWith(f, file.base)) {
28+
if (f.startsWith(file.base)) {
3029
debug("it starts with the right things");
3130
return f.slice(file.base.length + 1);
3231
}
3332
return f;
3433
};
3534

3635
const dateAndIndex = (f, p) => {
37-
const items = _.split(f, FILENAME_SEP);
36+
const items = f.split(FILENAME_SEP);
3837
let indexStr = items[items.length - 1];
3938
debug("items: ", items, ", indexStr: ", indexStr);
4039
let dateStr = f;

‎package.json‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@
4040
"async": "^2.6.3",
4141
"date-format": "^2.1.0",
4242
"debug": "^4.1.1",
43-
"fs-extra": "^8.1.0",
44-
"lodash": "^4.17.14"
43+
"fs-extra": "^8.1.0"
4544
},
4645
"engines": {
4746
"node": ">=8.0"

0 commit comments

Comments
 (0)