UNPKG

5.43 kBJavaScriptView Raw
1var ornellemberBundle=(function(exports){'use strict';var getDaysBeforeCurrentMonth = function (currentMonthIndex, isLeapYear) {
2 // then calculate the days of all the months before.
3 var daysInMonths = [
4 31,
5 isLeapYear ? 29 : 28,
6 31,
7 30,
8 31,
9 30,
10 31,
11 31,
12 30,
13 31,
14 30,
15 31 // December
16 ];
17 // how many days before the current month?
18 var daysBeforeCurrentMonth = 0;
19 for (var i = 0; i < currentMonthIndex; i++) {
20 daysBeforeCurrentMonth += daysInMonths[i];
21 }
22 return daysBeforeCurrentMonth;
23};
24var isLeapYear = function (date) {
25 return (date.getFullYear() % 4) === 0;
26};
27var getDayNumber = function (date) {
28 // first determine if it is a leap year
29 var isCurrentLeapYear = isLeapYear(date);
30 var currentMonth = date.getMonth();
31 // then determine how many days happened before this current month started
32 var daysBeforeCurrentMonth = getDaysBeforeCurrentMonth(currentMonth, isCurrentLeapYear);
33 var dayOfMonth = date.getDate();
34 return daysBeforeCurrentMonth + dayOfMonth;
35};
36var convertDay = function (dayNumber) {
37 var monthLength = 28;
38 var monthIndex = Math.floor((dayNumber - 1) / monthLength);
39 // make sure there are only 13 months (additional days can be added to month 12)
40 if (monthIndex > 12) {
41 monthIndex = 12;
42 }
43 var dayOfMonth = dayNumber - (monthIndex * monthLength);
44 var monthLetter = String.fromCharCode(97 + monthIndex).toUpperCase();
45 return {
46 day: dayOfMonth,
47 month: monthLetter
48 };
49};
50/*
51 This returns the date a new format that I invented, largely based on the International Fixed Calendar
52 https://en.wikipedia.org/wiki/International_Fixed_Calendar
53 It's mostly a joke but also I think it is logical and fun
54*/
55var getOrnellemberDay = function (date) {
56 var dayNumber = getDayNumber(date);
57 return convertDay(dayNumber);
58};/*
59 This function adds a certain quantity of a certain Ornellember unit to a Date.
60 Two things are important to remember here.
61 1. The time units we are adding are Ornellember time units, as they are input by the user.
62 2. The original date and the final date are NOT in Ornellember format:
63 they are regular Dates from the [Date Constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date)
64*/
65var addToDate = function (originalDate, quantity, unit) {
66 var valueToAdd;
67 switch (unit) {
68 case 'seconds': {
69 valueToAdd = quantity * 1000;
70 break;
71 }
72 case 'minutes': {
73 valueToAdd = quantity * 1000 * 60;
74 break;
75 }
76 case 'hours': {
77 valueToAdd = quantity * 1000 * 60 * 60;
78 break;
79 }
80 case 'days': {
81 valueToAdd = quantity * 1000 * 60 * 60 * 24;
82 break;
83 }
84 case 'months': {
85 // if we are in the last month of the year
86 if (getDayNumber(originalDate) >= (365 - 29)) {
87 if (isLeapYear(originalDate)) {
88 valueToAdd = quantity * 1000 * 60 * 60 * 24 * 30;
89 }
90 else {
91 valueToAdd = quantity * 1000 * 60 * 60 * 24 * 29;
92 }
93 }
94 else {
95 valueToAdd = quantity * 1000 * 60 * 60 * 24 * 28;
96 }
97 break;
98 }
99 default: {
100 valueToAdd = 0;
101 break;
102 }
103 }
104 var newDateValue = originalDate.valueOf() + valueToAdd;
105 var newDate = new Date(newDateValue);
106 if (unit === 'years') {
107 newDate.setFullYear(originalDate.getFullYear() + quantity);
108 }
109 return newDate;
110};var Ornellember = /** @class */ (function () {
111 function Ornellember(date) {
112 var rawDate = date;
113 var converted = getOrnellemberDay(rawDate);
114 this.day = converted.day;
115 this.month = converted.month;
116 this.rawDate = rawDate;
117 this.year = rawDate.getFullYear();
118 this.hours = rawDate.getHours();
119 this.minutes = rawDate.getMinutes();
120 this.milliseconds = rawDate.getMilliseconds();
121 }
122 Ornellember.prototype.clone = function () {
123 return new Ornellember(this.rawDate);
124 };
125 Ornellember.prototype.add = function (quantity, unit) {
126 var newDate = addToDate(this.rawDate, quantity, unit);
127 return new Ornellember(newDate);
128 };
129 Ornellember.prototype.format = function () {
130 return this.day + " " + this.month + ", " + this.year;
131 };
132 Ornellember.prototype.holiday = function () {
133 if (this.day === 29)
134 return 'N';
135 if (this.day === 30)
136 return 'O';
137 return null;
138 };
139 Ornellember.prototype.subtract = function (quantity, unit) {
140 return this.add(-quantity, unit);
141 };
142 return Ornellember;
143}());
144var index = (function (date) {
145 if (date instanceof Date) {
146 return new Ornellember(date);
147 }
148 if (date instanceof Ornellember) {
149 return date.clone();
150 }
151 if (!date) {
152 return new Ornellember(new Date());
153 }
154 throw new Error('Unexpected input. Accepted types are a Date, an Ornellember instance, or undefined');
155});exports.Ornellember=Ornellember;exports["default"]=index;Object.defineProperty(exports,'__esModule',{value:true});return exports;})({});
\No newline at end of file