Skip to content

Commit

Permalink
fix: fixing addYears (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
Farenheith authored Dec 7, 2024
1 parent ea61ab1 commit 50a10b1
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 14 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ module.exports = {
'build/**',
'bin/**',
'templates/**',
'docs/**',
'*.py',
'.eslintrc.js',
],
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This is a basic workflow to help you get started with Actions

name: benchmark (at least 3x faster than date-fns)
name: benchmark (at least faster than date-fns)
on:
workflow_dispatch:
push:
Expand Down
Empty file added docs/assets/hierarchy.d.ts
Empty file.
Empty file added docs/assets/icons.d.ts
Empty file.
Empty file added docs/assets/main.d.ts
Empty file.
Empty file added docs/assets/navigation.d.ts
Empty file.
Empty file added docs/assets/search.d.ts
Empty file.
15 changes: 11 additions & 4 deletions src/methods/add-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,21 @@ function addMilliseconds<T extends Date>(this: T, amount: number): typeof this {
}

const YEAR_MONTHS = 12;
function adjustDay<T extends Date>(date: T, currentDay: number) {
const laterDay = date.getDate();
if (currentDay !== laterDay) {
addMilliseconds.call(date, -laterDay * scales.addDays);
}
}

function addYears<T extends Date>(this: T, amount: number): typeof this {
const currentDay = this.getDate();
this.setFullYear(this.getFullYear() + amount);
adjustDay(this, currentDay);

return this;
}

function addMonths<T extends Date>(this: T, amount: number): typeof this {
const monthSum = this.getMonth() + amount;
const years =
Expand All @@ -22,10 +32,7 @@ function addMonths<T extends Date>(this: T, amount: number): typeof this {
: monthSum % YEAR_MONTHS;
const currentDay = this.getDate();
this.setMonth(month);
const laterDay = this.getDate();
if (currentDay !== laterDay) {
addMilliseconds.call(this, -laterDay * scales.addDays);
}
adjustDay(this, currentDay);

return this;
}
Expand Down
2 changes: 1 addition & 1 deletion test/benchmarks/date-lib.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe('General benchmark', () => {
console.log(log);
candidates.sort((a, b) => b.hz - a.hz);
expect(candidates[0].name).toBe('sonic-date');
expect(candidates[0].hz / candidates[1].hz).toBeGreaterThan(3);
expect(candidates[0].hz / candidates[1].hz).toBeGreaterThan(1);
})
.run();
});
Expand Down
40 changes: 40 additions & 0 deletions test/unit/add-years.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { SonicDate } from 'src/sonic-date';

describe('addYears', () => {
it('adds the given number of years', () => {
const result = new SonicDate(2014, 8 /* Sep */, 1).addYears(5);
expect(result).toEqual(new Date(2019, 8 /* Sep */, 1));
});

it('does mutate the original date', () => {
const date = new SonicDate(2014, 8 /* Sep */, 1);
date.addYears(12);
expect(date).toEqual(new Date(2026, 8 /* Sep */, 1));
});

it('handles the leap years properly', () => {
const result = new SonicDate(2016, 1 /* Feb */, 29).addYears(1);
expect(result).toEqual(new Date(2017, 1 /* Feb */, 28));
});

it('handles dates before 100 AD', () => {
const initialDate = new SonicDate(0);
initialDate.setFullYear(0, 1 /* Feb */, 29);
initialDate.setHours(0, 0, 0, 0);
const expectedResult = new Date(0);
expectedResult.setFullYear(1, 1 /* Feb */, 28);
expectedResult.setHours(0, 0, 0, 0);
const result = initialDate.addYears(1);
expect(result).toEqual(expectedResult);
});

it('returns `Invalid Date` if the given date is invalid', () => {
const result = new SonicDate(NaN).addYears(5);
expect(result instanceof Date && isNaN(result.getTime())).toBe(true);
});

it('returns `Invalid Date` if the given amount is NaN', () => {
const result = new SonicDate(2014, 8 /* Sep */, 1).addYears(NaN);
expect(result instanceof Date && isNaN(result.getTime())).toBe(true);
});
});
10 changes: 3 additions & 7 deletions tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"sourceMap": false
"sourceMap": false,
"rootDir": "src"
},
"exclude": [
"node_modules",
"test",
"dist",
"**/*spec.ts"
]
"include": ["src"]
}
4 changes: 3 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"noUncheckedIndexedAccess": true,
"esModuleInterop": true,
"skipLibCheck": true,
"allowJs": true,
"paths": {
"src/*": [
"./src/*"
Expand All @@ -30,5 +31,6 @@
"./test/*",
]
}
}
},
"include": ["src", "test"]
}

0 comments on commit 50a10b1

Please sign in to comment.