Skip to content

Commit 02a5b52

Browse files
authored
TypeScript rewrite (#6)
Maintains the same signature and tests, just rewriting using TypeScript, as well as using [tsdx](https://github.com/jaredpalmer/tsdx) for modern asset generation. The goal is to be compatible with [unpkg](https://unpkg.com) and [Pika](https://www.pika.dev).
1 parent 4e036c3 commit 02a5b52

File tree

15 files changed

+1293
-603
lines changed

15 files changed

+1293
-603
lines changed

.github/workflows/tests.yml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
name: CI Tests
2+
3+
on:
4+
pull_request:
5+
types: [opened, synchronize, reopened]
6+
push:
7+
branches:
8+
- master
9+
10+
jobs:
11+
lint:
12+
runs-on: ubuntu-latest
13+
14+
name: Lint on node 12.x and ubuntu-latest
15+
16+
steps:
17+
- uses: actions/checkout@v1
18+
- name: Use Node.js 12.x
19+
uses: actions/setup-node@v1
20+
with:
21+
node-version: 12.x
22+
23+
- name: Install deps and build
24+
run: yarn install
25+
26+
- name: Lint codebase
27+
run: yarn lint
28+
29+
test:
30+
runs-on: ${{ matrix.os }}
31+
32+
strategy:
33+
matrix:
34+
node: ['10.x', '12.x', '13.x']
35+
os: [ubuntu-latest, windows-latest, macOS-latest]
36+
37+
name: Test on node ${{ matrix.node }} and ${{ matrix.os }}
38+
39+
steps:
40+
- uses: actions/checkout@v1
41+
- name: Use Node.js ${{ matrix.node }}
42+
uses: actions/setup-node@v1
43+
with:
44+
node-version: ${{ matrix.node }}
45+
46+
- name: Install deps and build
47+
run: yarn install
48+
49+
- name: Test package
50+
run: yarn test --runInBand

.gitignore

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
node_modules
1+
*.log
2+
.DS_Store
3+
node_modules
4+
dist
5+
package-lock.json
6+
yarn.lock

.travis.yml

Lines changed: 0 additions & 7 deletions
This file was deleted.

LICENSE

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Copyright © Wes Souza <[email protected]>
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of
4+
this software and associated documentation files (the “Software”), to deal in
5+
the Software without restriction, including without limitation the rights to
6+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7+
of the Software, and to permit persons to whom the Software is furnished to do
8+
so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
15+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
16+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 62 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,85 @@
1-
# Calendar Base [![Build Status](https://travis-ci.org/WesleydeSouza/calendar-base.svg?branch=master)](https://travis-ci.org/WesleydeSouza/calendar-base)
1+
# Calendar Base
22

3-
[![NPM](https://nodei.co/npm/calendar-base.png)](https://nodei.co/npm/calendar-base/)
3+
<a href="https://github.com/WesSouza/calendar-base/actions?query=branch%3Amaster+workflow%3A%22CI%20Tests"><img src="https://github.com/WesSouza/calendar-base/workflows/CI%20Tests/badge.svg" alt="Install, lint, test and build status badge"></a>
44

5-
Base methods for generating calendars using JavaScript.
5+
[![npm](https://nodei.co/npm/calendar-base.png)](https://nodei.co/npm/calendar-base/)
66

7-
Supports IE 6+, Chrome 1+, Firefox 3+, Safari 4+.
7+
Base methods for generating calendars using JavaScript.
88

9+
Output is ES5 compatible.
910

1011
## Installation
1112

1213
```bash
14+
# using npm
1315
npm install calendar-base
14-
```
15-
16-
Or manually copy `dist/calendar-base.min.js` to your project.
1716

17+
# or yarn
18+
yarn add calendar-base
19+
```
1820

1921
## Usage
20-
```js
21-
var Calendar = require('calendar-base').Calendar,
22-
cal = new Calendar();
2322

24-
cal.getCalendar(2015, 0);
25-
// Returns an Array with the calendar for January 2015.
23+
```js
24+
const Calendar = require('calendar-base').Calendar;
25+
const cal = new Calendar();
26+
27+
cal.getCalendar(2020, 0);
28+
/*
29+
Returns an Array with the calendar for January 2020, including empty spaces for
30+
days from the previous and next months:
31+
32+
[
33+
false,
34+
false,
35+
{ day: 1, weekDay: 3, month: 0, year: 2020 },
36+
{ day: 2, weekDay: 4, month: 0, year: 2020 },
37+
{ day: 3, weekDay: 5, month: 0, year: 2020 },
38+
{ day: 4, weekDay: 6, month: 0, year: 2020 },
39+
{ day: 5, weekDay: 0, month: 0, year: 2020 },
40+
...
41+
]
42+
*/
2643
```
2744

28-
Usage with AMD and global variables is available through `dist/calendar-base.min.js`.
29-
30-
[Check an online example](https://tonicdev.com/npm/calendar-base) or browse the `examples` folder for some simple use cases.
31-
45+
[Check an online example](https://npm.runkit.com/calendar-base) or browse the
46+
[`examples`](./examples/) folder for some simple use cases.
3247

3348
### Date object notation
3449

3550
Every returned day or date argument follows this notation:
51+
3652
```js
3753
{
38-
day: 14,
39-
month: 9,
40-
year: 1986,
41-
weekDay: 4,
42-
selected: false,
43-
siblingMonth: false,
44-
weekNumber: 42
54+
day: 14,
55+
month: 9,
56+
year: 1986,
57+
weekDay: 4,
58+
selected: false,
59+
siblingMonth: false,
60+
weekNumber: 42
4561
}
4662
```
4763

48-
Properties `month` and `weekDay` respect JavaScript’s [`Date.prototype`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/prototype).
49-
50-
Only `day`, `month`, and `year` are necessary as input parameters for methods that require a date.
64+
Properties `month` and `weekDay` respect JavaScript’s
65+
[`Date.prototype`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/prototype).
5166

67+
Only `day`, `month`, and `year` are necessary as input parameters for methods
68+
that require a date.
5269

5370
#### `Calendar(options)`
5471

5572
Constructor for a new calendar generator.
5673

5774
The object `options` may have the following properties:
5875

59-
* `startDate`: current selected starting date (default `undefined`)
60-
* `endDate`: current selected ending date (default `undefined`)
61-
* `siblingMonths`: whether to include the previous and next months’ days before and after the current month when generating a calendar (default `false`)
62-
* `weekNumbers`: whether to include the week number on each day
63-
* `weekStart`: day of the week, respects [`Date.prototype.getDay`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDay) (default `0`, Sunday)
64-
76+
- `startDate`: current selected starting date (default `undefined`)
77+
- `endDate`: current selected ending date (default `undefined`)
78+
- `siblingMonths`: whether to include the previous and next months’ days before
79+
and after the current month when generating a calendar (default `false`)
80+
- `weekNumbers`: whether to include the week number on each day
81+
- `weekStart`: day of the week, respects
82+
[`Date.prototype.getDay`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDay) (default `0`, Sunday)
6583

6684
#### `Calendar.diff(dateOne, dateTwo)`
6785

@@ -72,7 +90,6 @@ Returns the difference in days between `dateOne` and `dateTwo` as a `Number`.
7290
-9
7391
```
7492

75-
7693
#### `Calendar.interval(dateOne, dateTwo)`
7794

7895
Returns the amount of days between `dateOne` and `dateTwo` as a `Number`.
@@ -82,13 +99,12 @@ Returns the amount of days between `dateOne` and `dateTwo` as a `Number`.
8299
10
83100
```
84101

85-
86102
#### `Calendar.compare(leftDate, rightDate)`
87103

88104
Compares two date objects, returns:
89105

90106
- `-1` if `leftDate` < `rightDate`
91-
- `0` if `leftDate` == `rightDate`
107+
- `0` if `leftDate` == `rightDate`
92108
- `1` if `leftDate` > `rightDate`
93109

94110
Useful for quick comparisons such as sorting an array of dates.
@@ -98,7 +114,6 @@ Useful for quick comparisons such as sorting an array of dates.
98114
1
99115
```
100116

101-
102117
#### `Calendar.daysInMonth(year, month)`
103118

104119
Returns the amount of days in the given month as a `Number`.
@@ -108,7 +123,6 @@ Returns the amount of days in the given month as a `Number`.
108123
31
109124
```
110125

111-
112126
#### `Calendar.isLeapYear(year)`
113127

114128
Returns whether the given year is a leap year, as a `Boolean`.
@@ -118,7 +132,6 @@ Returns whether the given year is a leap year, as a `Boolean`.
118132
false
119133
```
120134

121-
122135
#### `Calendar.calculateWeekNumber(date)`
123136

124137
Returns the week number for the specified date.
@@ -128,12 +141,14 @@ Returns the week number for the specified date.
128141
42
129142
```
130143

131-
132144
#### `Calendar.prototype.getCalendar(year, month)`
133145

134-
Returns an `Array` of dates with the days from the given month, always starting at the configured week day.
146+
Returns an `Array` of dates with the days from the given month, always starting
147+
at the configured week day.
135148

136-
If sibling months is disabled, paddings are added as `false` to align the week days, otherwise the respective days from the previous or next months are included.
149+
If sibling months is disabled, paddings are added as `false` to align the week
150+
days, otherwise the respective days from the previous or next months are
151+
included.
137152

138153
```js
139154
> var cal = new Calendar({ siblingMonths: true });
@@ -145,12 +160,10 @@ If sibling months is disabled, paddings are added as `false` to align the week d
145160
{ day: 4, weekDay: 6, month: 6, year: 2015, siblingMonth: true } ]
146161
```
147162

148-
149163
#### `Calendar.prototype.setDate(date)`
150164

151165
Alias to `Calendar.prototype.setStartDate`.
152166

153-
154167
#### `Calendar.prototype.setStartDate(date)`
155168

156169
Sets the current selected starting date.
@@ -159,7 +172,6 @@ Sets the current selected starting date.
159172
> cal.setStartDate({ year: 2015, month: 0, day: 1 });
160173
```
161174

162-
163175
#### `Calendar.prototype.setEndDate(date)`
164176

165177
Sets the current selected ending date.
@@ -168,22 +180,22 @@ Sets the current selected ending date.
168180
> cal.setEndDate({ year: 2015, month: 0, day: 31 });
169181
```
170182

171-
172183
#### `Calendar.prototype.isDateSelected(date)`
173184

174-
Checks wheter the given date is inside the selected dates interval, returns a `Boolean`.
185+
Checks whether the given date is inside the selected dates interval, returns a
186+
`Boolean`.
175187

176188
```js
177189
> cal.isDateSelected({ year: 2015, month: 0, day: 10 });
178190
true
179191
```
180192

181-
182193
## Important note on week numbers
183194

184-
Week numbers are calculated based on the ISO 8601 standard, which assumes calculations based on weeks starting on Mondays. Be extra careful displaying the week number if your calendar doesn't start on a Monday.
185-
195+
Week numbers are calculated based on the ISO 8601 standard, which assumes
196+
calculations based on weeks starting on Mondays. Be extra careful displaying the
197+
week number if your calendar doesn't start on a Monday.
186198

187199
## License
188200

189-
MIT, http://wesleydesouza.mit-license.org/
201+
MIT, https://wes.dev/LICENSE.txt

dist/calendar-base.min.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

examples/browser/index.html

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
11
<!DOCTYPE html>
22
<html>
3-
<head>
4-
<title>Simple Calendar</title>
5-
<meta charset="utf-8">
6-
<link rel="stylesheet" href="style.css">
7-
</head>
8-
<body>
3+
<head>
4+
<title>Simple Calendar</title>
5+
<meta charset="utf-8" />
6+
<link rel="stylesheet" href="./style.css" />
7+
</head>
8+
<body>
9+
<h1 class="calendar js-calendar-month"></h1>
910

10-
<h1 class="calendar js-calendar-month"></h1>
11+
<ul class="calendar js-calendar">
12+
<li class="calendar-day -weekday">Mo</li>
13+
<li class="calendar-day -weekday">Tu</li>
14+
<li class="calendar-day -weekday">We</li>
15+
<li class="calendar-day -weekday">Th</li>
16+
<li class="calendar-day -weekday">Fr</li>
17+
<li class="calendar-day -weekday">Sa</li>
18+
<li class="calendar-day -weekday">Su</li>
19+
</ul>
1120

12-
<ul class="calendar js-calendar">
13-
<li class="calendar-day -weekday">Mo</li>
14-
<li class="calendar-day -weekday">Tu</li>
15-
<li class="calendar-day -weekday">We</li>
16-
<li class="calendar-day -weekday">Th</li>
17-
<li class="calendar-day -weekday">Fr</li>
18-
<li class="calendar-day -weekday">Sa</li>
19-
<li class="calendar-day -weekday">Su</li>
20-
</ul>
21-
22-
<script src="../../dist/calendar-base.min.js"></script>
23-
<script src="my-calendar.js"></script>
24-
25-
</body>
26-
</html>
21+
<script src="https://cdn.pika.dev/calendar-base@^1.0.0"></script>
22+
<script src="./my-calendar.js"></script>
23+
</body>
24+
</html>

0 commit comments

Comments
 (0)