Skip to content

Commit 24319bc

Browse files
committed
fix: all files ending by a timestamp were validated
1 parent 4343083 commit 24319bc

File tree

2 files changed

+60
-3
lines changed

2 files changed

+60
-3
lines changed

lib/fileNameParser.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,22 @@ module.exports = ({ file, keepFileExt, pattern }) => {
1616
return f;
1717
};
1818

19+
const __NOT_MATCHING__ = "__NOT_MATCHING__";
20+
1921
const extAtEnd = f => {
2022
if (f.startsWith(file.name) && f.endsWith(file.ext)) {
2123
debug("it starts and ends with the right things");
2224
return f.slice(file.name.length + 1, -1 * file.ext.length);
2325
}
24-
return f;
26+
return __NOT_MATCHING__;
2527
};
2628

2729
const extInMiddle = f => {
2830
if (f.startsWith(file.base)) {
2931
debug("it starts with the right things");
3032
return f.slice(file.base.length + 1);
3133
}
32-
return f;
34+
return __NOT_MATCHING__;
3335
};
3436

3537
const dateAndIndex = (f, p) => {
@@ -53,6 +55,7 @@ module.exports = ({ file, keepFileExt, pattern }) => {
5355
// components to minimal values in the current timezone instead of UTC,
5456
// as new Date(0) will do.
5557
const date = format.parse(pattern, dateStr, new Date(0, 0));
58+
if (format.asString(pattern, date) !== dateStr) return f;
5659
p.index = parseInt(indexStr, 10);
5760
p.date = dateStr;
5861
p.timestamp = date.getTime();

test/fileNameParser-test.js

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ describe("fileNameParser", () => {
4242
should(parser("thefile.log.biscuits")).not.be.ok();
4343
should(parser("thefile.log.2019")).not.be.ok();
4444
should(parser("thefile.log.3.2")).not.be.ok();
45+
should(parser("thefile.log.04-18")).not.be.ok();
46+
should(parser("anotherfile.log.2020-04-18")).not.be.ok();
47+
should(parser("2020-05-18")).not.be.ok();
4548
});
4649
it("should take a filename and return the date", () => {
4750
parser("thefile.log.2019-07-17").should.eql({
@@ -112,7 +115,7 @@ describe("fileNameParser", () => {
112115
pattern: "mm"
113116
});
114117
it("should take a filename and return the date", () => {
115-
const expectedTimestamp = new Date(0,0);
118+
const expectedTimestamp = new Date(0, 0);
116119
expectedTimestamp.setMinutes(34);
117120
parser("thing.log.34").should.eql({
118121
filename: "thing.log.34",
@@ -123,4 +126,55 @@ describe("fileNameParser", () => {
123126
});
124127
});
125128
})
129+
130+
describe("with a four-digit date pattern", () => {
131+
const parser = require("../lib/fileNameParser")({
132+
file: {
133+
dir: "/path/to/file",
134+
base: "stuff.log",
135+
ext: ".log",
136+
name: "stuff"
137+
},
138+
pattern: "mm-ss"
139+
});
140+
it("should return null for files that do not match", () => {
141+
should(parser("stuff.log.2020-04-18")).not.be.ok();
142+
should(parser("09-18")).not.be.ok();
143+
});
144+
it("should take a filename and return the date", () => {
145+
const expectedTimestamp = new Date(0, 0);
146+
expectedTimestamp.setMinutes(34);
147+
expectedTimestamp.setSeconds(59);
148+
parser("stuff.log.34-59").should.eql({
149+
filename: "stuff.log.34-59",
150+
date: "34-59",
151+
isCompressed: false,
152+
index: 0,
153+
timestamp: expectedTimestamp.getTime()
154+
});
155+
});
156+
it("should take a filename and return both date and index", () => {
157+
const expectedTimestamp_1 = new Date(0, 0);
158+
expectedTimestamp_1.setMinutes(7);
159+
expectedTimestamp_1.setSeconds(17);
160+
parser("stuff.log.07-17.2").should.eql({
161+
filename: "stuff.log.07-17.2",
162+
index: 2,
163+
date: "07-17",
164+
timestamp: expectedTimestamp_1.getTime(),
165+
isCompressed: false
166+
});
167+
const expectedTimestamp_2 = new Date(0, 0);
168+
expectedTimestamp_2.setMinutes(17);
169+
expectedTimestamp_2.setSeconds(30);
170+
parser("stuff.log.17-30.3.gz").should.eql({
171+
filename: "stuff.log.17-30.3.gz",
172+
index: 3,
173+
date: "17-30",
174+
timestamp: expectedTimestamp_2.getTime(),
175+
isCompressed: true
176+
});
177+
});
178+
})
179+
126180
});

0 commit comments

Comments
 (0)