@vbarbarosh/node-helpers 3.54.1 → 3.55.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @link https://stackoverflow.com/a/7937257
|
|
3
|
+
*/
|
|
4
|
+
function date_add_months(d, months)
|
|
5
|
+
{
|
|
6
|
+
if (!months) {
|
|
7
|
+
return d;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
// https://github.com/briannesbitt/Carbon/blob/f01cfa96468f4c38325f507ab81a4f1d2cd93cfe/src/Carbon/Traits/Units.php#L360C1-L361C1
|
|
11
|
+
// > } elseif (isset($canOverflow, $day) && $canOverflow && $day !== $date?->day) {
|
|
12
|
+
// > $date = $date?->modify('last day of previous month');
|
|
13
|
+
// > }
|
|
14
|
+
|
|
15
|
+
const date0 = d.getDate();
|
|
16
|
+
d.setMonth(d.getMonth() + months);
|
|
17
|
+
if (d.getDate() !== date0) {
|
|
18
|
+
d.setDate(0);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return d;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
module.exports = date_add_months;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
const assert = require('assert');
|
|
2
|
+
const date_add_months = require('./date_add_months');
|
|
3
|
+
const format_date_ymd = require('./format_date_ymd');
|
|
4
|
+
|
|
5
|
+
const items = [
|
|
6
|
+
['2024/12/24', '2024/12/24', 0],
|
|
7
|
+
['2024/12/24', '2025/01/24', 1],
|
|
8
|
+
['2024/12/24', '2024/11/24', -1],
|
|
9
|
+
['2012/01/14', '2011/10/14', -3],
|
|
10
|
+
['2019/03/31', '2019/02/28', -1],
|
|
11
|
+
['2023/01/15', '2023/04/15', 3, 'Basic Positive Addition'],
|
|
12
|
+
['2023/05/15', '2023/03/15', -2, 'Basic Negative Addition'],
|
|
13
|
+
['2023/10/15', '2024/03/15', 5, 'Year Crossover (Positive)'],
|
|
14
|
+
['2023/03/15', '2022/10/15', -5, 'Year Crossover (Negative)'],
|
|
15
|
+
['2024/02/29', '2025/02/28', 12, 'Leap Year - Adding Months in Leap Year'],
|
|
16
|
+
['2024/03/01', '2023/03/01', -12, 'Negative Leap Year Crossover'],
|
|
17
|
+
['2023/01/31', '2023/02/28', 1, 'End of Month (31st to Shorter Month)'],
|
|
18
|
+
['2023/03/31', '2023/02/28', -1, 'End of Month Crossover (Negative)'],
|
|
19
|
+
['2023/06/15', '2023/06/15', 0, 'Zero Months Added'],
|
|
20
|
+
['2023/01/01', '2033/01/01', 120, 'Large Positive Addition'],
|
|
21
|
+
['2023/01/01', '2013/01/01', -120, 'Large Negative Addition'],
|
|
22
|
+
['2020/02/29', '2024/02/29', 48, 'Multiple Leap Years'],
|
|
23
|
+
['2023/12/15', '2024/01/15', 1, 'Adding to December'],
|
|
24
|
+
['2023/01/15', '2022/12/15', -1, 'Subtracting from January'],
|
|
25
|
+
['2023/05/31', '2023/06/30', 1, 'Day Preservation with Shorter Month'],
|
|
26
|
+
['2023/02/28', '2024/02/28', 12, 'Adding Months to February (Non-Leap Year)'],
|
|
27
|
+
['2024/03/01', '2023/02/01', -13, 'Negative Months Across Leap Year'],
|
|
28
|
+
['2000/01/01', '2030/01/01', 360, 'Multiple Decades'],
|
|
29
|
+
['1970/01/01', '2020/01/01', 600, 'Edge Case: Epoch Date'],
|
|
30
|
+
['3000/01/01', '2900/01/01', -1200, 'Far Future Date']
|
|
31
|
+
];
|
|
32
|
+
|
|
33
|
+
describe('date_add_months', function () {
|
|
34
|
+
items.forEach(function ([input, expected, months, message]) {
|
|
35
|
+
it(`${input} → ${expected}`, function () {
|
|
36
|
+
const d = date_add_months(new Date(input), months);
|
|
37
|
+
assert.strictEqual(format_date_ymd(d), expected, message);
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @link https://github.com/moment/moment/blob/18aba135ab927ffe7f868ee09276979bed6993a6/src/lib/utils/is-leap-year.js
|
|
3
|
+
* @link https://en.wikipedia.org/wiki/Leap_year
|
|
4
|
+
* > Each leap year has 366 days instead of 365. This extra leap day
|
|
5
|
+
* > occurs in each year that is a multiple of 4, except for years
|
|
6
|
+
* > evenly divisible by 100 but not by 400.
|
|
7
|
+
*/
|
|
8
|
+
function date_is_leap_year(d)
|
|
9
|
+
{
|
|
10
|
+
const year = d.getFullYear();
|
|
11
|
+
// return (year % 4 === 0) && !(year % 100 === 0 && year % 400 !== 0);
|
|
12
|
+
return (year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
module.exports = date_is_leap_year;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
const assert = require('assert');
|
|
2
|
+
const date_is_leap_year = require('./date_is_leap_year');
|
|
3
|
+
|
|
4
|
+
describe('date_add_months', function () {
|
|
5
|
+
it('Brute force', function () {
|
|
6
|
+
for (let year = 100; year < 9999; ++year) {
|
|
7
|
+
const d = new Date(`${year}/01/01 00:00:00`);
|
|
8
|
+
const d2 = new Date(`${year}/02/29 00:00:00`);
|
|
9
|
+
assert.strictEqual(d.getFullYear(), year);
|
|
10
|
+
assert.strictEqual(d2.getFullYear(), year);
|
|
11
|
+
assert.strictEqual(date_is_leap_year(d), d2.getMonth() === 1, year);
|
|
12
|
+
}
|
|
13
|
+
});
|
|
14
|
+
});
|