second commit
This commit is contained in:
58
node_modules/flowbite-datepicker/extras/twoDigitToFullYear/test/twoDigitToFullYear.js
generated
vendored
Normal file
58
node_modules/flowbite-datepicker/extras/twoDigitToFullYear/test/twoDigitToFullYear.js
generated
vendored
Normal file
@ -0,0 +1,58 @@
|
||||
import {twoDigitToFullYear} from '../twoDigitToFullYear.js';
|
||||
|
||||
const expect = require('unexpected');
|
||||
const sinon = require('sinon');
|
||||
|
||||
describe('twoDigitToFullYear()', function () {
|
||||
it('converts 2-digit year to full year using 50 for the future years range by default', function () {
|
||||
let clock = sinon.useFakeTimers({now: new Date().setFullYear(2020)});
|
||||
|
||||
expect(twoDigitToFullYear(0), 'to be', 2000);
|
||||
expect(twoDigitToFullYear(20), 'to be', 2020);
|
||||
expect(twoDigitToFullYear(70), 'to be', 2070);
|
||||
expect(twoDigitToFullYear(71), 'to be', 1971);
|
||||
|
||||
clock.restore();
|
||||
clock = sinon.useFakeTimers({now: new Date().setFullYear(2080)});
|
||||
|
||||
expect(twoDigitToFullYear(0), 'to be', 2100);
|
||||
expect(twoDigitToFullYear(30), 'to be', 2130);
|
||||
expect(twoDigitToFullYear(31), 'to be', 2031);
|
||||
expect(twoDigitToFullYear(80), 'to be', 2080);
|
||||
|
||||
clock.restore();
|
||||
});
|
||||
|
||||
it('uses sepecified future year range if it is given', function () {
|
||||
let clock = sinon.useFakeTimers({now: new Date().setFullYear(2020)});
|
||||
|
||||
expect(twoDigitToFullYear(40, 20), 'to be', 2040);
|
||||
expect(twoDigitToFullYear(40, 19), 'to be', 1940);
|
||||
expect(twoDigitToFullYear(95, 75), 'to be', 2095);
|
||||
expect(twoDigitToFullYear(95, 74), 'to be', 1995);
|
||||
|
||||
clock.restore();
|
||||
clock = sinon.useFakeTimers({now: new Date().setFullYear(2080)});
|
||||
|
||||
expect(twoDigitToFullYear(0, 20), 'to be', 2100);
|
||||
expect(twoDigitToFullYear(0, 19), 'to be', 2000);
|
||||
expect(twoDigitToFullYear(55, 75), 'to be', 2155);
|
||||
expect(twoDigitToFullYear(55, 74), 'to be', 2055);
|
||||
|
||||
clock.restore();
|
||||
});
|
||||
|
||||
it('uses the century of baseDate for conversion if it is given', function () {
|
||||
const year2020 = new Date(2020, 0, 1);
|
||||
expect(twoDigitToFullYear(40, 20, year2020), 'to be', 2040);
|
||||
expect(twoDigitToFullYear(40, 19, year2020), 'to be', 1940);
|
||||
expect(twoDigitToFullYear(95, 75, year2020), 'to be', 2095);
|
||||
expect(twoDigitToFullYear(95, 74, year2020), 'to be', 1995);
|
||||
|
||||
const year2280 = new Date(2280, 0, 1);
|
||||
expect(twoDigitToFullYear(0, 20, year2280), 'to be', 2300);
|
||||
expect(twoDigitToFullYear(0, 19, year2280), 'to be', 2200);
|
||||
expect(twoDigitToFullYear(55, 75, year2280), 'to be', 2355);
|
||||
expect(twoDigitToFullYear(55, 74, year2280), 'to be', 2255);
|
||||
});
|
||||
});
|
26
node_modules/flowbite-datepicker/extras/twoDigitToFullYear/twoDigitToFullYear.js
generated
vendored
Normal file
26
node_modules/flowbite-datepicker/extras/twoDigitToFullYear/twoDigitToFullYear.js
generated
vendored
Normal file
@ -0,0 +1,26 @@
|
||||
/**
|
||||
* Convert 2-digit year to 4-digit year
|
||||
* By default, 2-digit year will be converted to a year in the 100-year
|
||||
* period ending 50 years after the current year. The target 100-year period
|
||||
* can be changed by specifying base date and the length of the future side.
|
||||
* @param {Number} year - 2-digit year
|
||||
* @param {Number} [futureSideSpan] - number of yeas in the future side of
|
||||
* the target 100-year period. Default: 50
|
||||
* @param {Date} [baseDate] - base date to caliculate the target 100-year
|
||||
* period. if omitted, the current date is used.
|
||||
* @return {Number} 4-digit year
|
||||
*/
|
||||
export function twoDigitToFullYear(year, futureSideSpan = 50, baseDate = undefined) {
|
||||
if (isNaN(year) || year > 99 || year < 0) {
|
||||
return year;
|
||||
}
|
||||
|
||||
const date = baseDate instanceof Date && !isNaN(baseDate) ? baseDate : new Date();
|
||||
const currentYear = date.getFullYear();
|
||||
const endOfCurrent = currentYear + futureSideSpan;
|
||||
let century = Math.floor(endOfCurrent / 100) * 100;
|
||||
if (year > endOfCurrent % 100) {
|
||||
century -= 100;
|
||||
}
|
||||
return year + century;
|
||||
}
|
Reference in New Issue
Block a user