Skip to content

Commit

Permalink
fix: datetime month mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
izatop committed May 6, 2020
1 parent b5b58d0 commit 9ccb835
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 17 deletions.
13 changes: 9 additions & 4 deletions packages/test/src/util/date/DateTime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ describe("DateTime", () => {
["ms", +100],
["sec", +10],
["min", -10],
["week", -1],
["week", +2],
["month", +3],
["hour", +2],
["day", -1],
["year", -5],
];

const result = [];
for (const value of mutations) {
result.push(date.mutate(value).getDate());
for (const [type, value] of mutations) {
result.push([type, value, date.mutate([type, value]).getDate()]);
}

expect(result).toMatchSnapshot();
Expand All @@ -26,13 +29,14 @@ describe("DateTime", () => {
["sec", 22],
["min", 11],
["hour", 0],
["month", 2],
["day", 11],
["year", 2022],
];

const result = [];
for (const value of setters) {
result.push(date.set(value).getDate());
for (const [type, value] of setters) {
result.push([type, value, date.set([type, value]).getDate()]);
}

expect(result).toMatchSnapshot();
Expand All @@ -43,6 +47,7 @@ describe("DateTime", () => {
const begins: Exclude<DateTimeKind, "ms" | "week">[] = ["sec", "min", "hour", "day", "month", "year"];
for (const kind of begins) {
result.push({
kind,
begins: date.begins(kind).getDate(),
ends: date.ends(kind).getDate(),
});
Expand Down
98 changes: 86 additions & 12 deletions packages/test/src/util/date/__snapshots__/DateTime.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,122 @@ Array [
Object {
"begins": 2020-02-12T12:35:13.000Z,
"ends": 2020-02-12T12:35:13.999Z,
"kind": "sec",
},
Object {
"begins": 2020-02-12T12:35:00.000Z,
"ends": 2020-02-12T12:35:59.999Z,
"kind": "min",
},
Object {
"begins": 2020-02-12T12:00:00.000Z,
"ends": 2020-02-12T12:59:59.999Z,
"kind": "hour",
},
Object {
"begins": 2020-02-11T21:00:00.000Z,
"ends": 2020-02-12T20:59:59.999Z,
"kind": "day",
},
Object {
"begins": 2020-01-30T21:00:00.000Z,
"ends": 2020-02-29T20:59:59.999Z,
"kind": "month",
},
Object {
"begins": 2019-12-30T21:00:00.000Z,
"ends": 2020-12-31T20:59:59.999Z,
"kind": "year",
},
]
`;

exports[`DateTime mutate() 1`] = `
Array [
2020-02-12T12:35:13.223Z,
2020-02-12T12:35:23.123Z,
2020-02-12T12:25:13.123Z,
2020-02-12T14:35:13.123Z,
2020-02-11T12:35:13.123Z,
2015-02-12T12:35:13.123Z,
Array [
"ms",
100,
2020-02-12T12:35:13.223Z,
],
Array [
"sec",
10,
2020-02-12T12:35:23.123Z,
],
Array [
"min",
-10,
2020-02-12T12:25:13.123Z,
],
Array [
"week",
-1,
2020-02-05T12:35:13.123Z,
],
Array [
"week",
2,
2020-02-26T12:35:13.123Z,
],
Array [
"month",
3,
2020-05-12T12:35:13.123Z,
],
Array [
"hour",
2,
2020-02-12T14:35:13.123Z,
],
Array [
"day",
-1,
2020-02-11T12:35:13.123Z,
],
Array [
"year",
-5,
2015-02-12T12:35:13.123Z,
],
]
`;

exports[`DateTime set() 1`] = `
Array [
2020-02-12T12:35:13.333Z,
2020-02-12T12:35:22.123Z,
2020-02-12T12:11:13.123Z,
2020-02-11T21:35:13.123Z,
2020-02-11T12:35:13.123Z,
2022-02-12T12:35:13.123Z,
Array [
"ms",
333,
2020-02-12T12:35:13.333Z,
],
Array [
"sec",
22,
2020-02-12T12:35:22.123Z,
],
Array [
"min",
11,
2020-02-12T12:11:13.123Z,
],
Array [
"hour",
0,
2020-02-11T21:35:13.123Z,
],
Array [
"month",
2,
2020-02-02T12:35:13.123Z,
],
Array [
"day",
11,
2020-02-11T12:35:13.123Z,
],
Array [
"year",
2022,
2022-02-12T12:35:13.123Z,
],
]
`;
2 changes: 1 addition & 1 deletion packages/util/src/Date/mutators/mutators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const week: TimeMutateFn = (time, value) => {

const month: TimeMutateFn = (time, value) => {
const date = new Date(time);
return date.setDate(date.getDate() + value);
return date.setMonth(date.getMonth() + value);
};

const year: TimeMutateFn = (time, value) => {
Expand Down

0 comments on commit 9ccb835

Please sign in to comment.