Skip to content

Commit 560c462

Browse files
authored
4.0.0-beta.0: Updated date parsing and imports (#72)
* 4.0.0-beta.0: Updated date parsing and imports. See changelog for full details * Fix build out of date * Format files prettier * Fixed prettier and README * Fix function definitions * Fix rollup transform * Move to typescript and revert name change * Fix types * Change to alpha * Fixes for named imports
1 parent 8b54051 commit 560c462

23 files changed

+3632
-2450
lines changed

.babelrc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"presets": [
3+
[
4+
"@babel/preset-env"
5+
]
6+
],
7+
"plugins": ["@babel/plugin-proposal-optional-chaining"]
8+
}

.browserslistrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
defaults

.eslintrc.js

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,16 @@
11
module.exports = {
2-
"env": {
3-
"browser": true,
4-
"node": true,
5-
"amd": true
6-
},
7-
"extends": "eslint:recommended",
8-
"rules": {
9-
"indent": [
10-
"error",
11-
2
12-
],
13-
"linebreak-style": [
14-
"error",
15-
"unix"
16-
],
17-
"quotes": [
18-
"error",
19-
"single"
20-
],
21-
"semi": [
22-
"error",
23-
"always"
24-
]
25-
},
26-
"parserOptions": {
27-
"sourceType": "module"
2+
root: true,
3+
parser: '@typescript-eslint/parser',
4+
plugins: [
5+
'@typescript-eslint',
6+
],
7+
extends: [
8+
'eslint:recommended',
9+
'plugin:@typescript-eslint/eslint-recommended',
10+
'plugin:@typescript-eslint/recommended',
11+
'prettier/@typescript-eslint',
12+
],
13+
rules: {
14+
"@typescript-eslint/ban-ts-ignore": "off"
2815
}
2916
};

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
coverage
3-
.nyc_output
3+
.nyc_output
4+
ts-out

.nycrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "@istanbuljs/nyc-config-typescript"
3+
}

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
language: node_js
22
node_js:
3-
- 8.9.0
3+
- 12.14

CHANGELOG.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,39 @@
1+
## 4.0.0-alpha.0
2+
** Major Features and Breaking changes in this version**
3+
4+
#### Improvements
5+
- *Valid date parsing* - By default fecha will check validity of dates. Previously `2019-55-01` or `2019-01-42` would parse correctly, since Javascript can handle it. Now invalid dates will return `null` instead
6+
- *ES Module and Tree Shaking Support* - You can now import fecha `parse` or `format` independently
7+
```js
8+
import {format, parse} from 'fecha';
9+
10+
format(...);
11+
parse(...)
12+
13+
14+
#### Breaking changes
15+
- `parseDate` may return `null` when previously returned a `Date`. See improvements above, but invalid dates will return `null` now
16+
- Change to how to set masks and i18n
17+
Previously
18+
```js
19+
import fecha from 'fecha';
20+
21+
fecha.i18n = { ... }
22+
fecha.masks.myMask = 'DD , MM, YYYY'
23+
```
24+
25+
New
26+
```js
27+
import {parse, format, setGlobalDateI18n, setGlobalDateMasks} from 'fecha';
28+
29+
setGlobalDateI18n({
30+
// ...
31+
})
32+
setGlobalDateMasks({
33+
myMask: 'DD , MM, YYYY'
34+
});
35+
```
36+
137
### 3.0.3
238
- Fixed bug when using brackets when parsing dates
339
### 3.0.2

README.md

Lines changed: 67 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -46,58 +46,99 @@ npm install fecha --save
4646
## Use it
4747

4848
#### Formatting
49-
`fecha.format` accepts a Date object (or timestamp) and a string format and returns a formatted string. See below for
49+
`format` accepts a Date object (or timestamp) and a string format and returns a formatted string. See below for
5050
available format tokens.
5151

52-
Note: `fecha.format` will throw an error when passed invalid parameters
52+
Note: `format` will throw an error when passed invalid parameters
5353
```js
54-
fecha.format(<Date Object>, <String Format>);
54+
import { format } from 'fecha';
55+
56+
type format = (date: Date, format?: string, i18n?: I18nSettings) => str;
5557

5658
// Custom formats
57-
fecha.format(new Date(2015, 10, 20), 'dddd MMMM Do, YYYY'); // 'Friday November 20th, 2015'
58-
fecha.format(new Date(1998, 5, 3, 15, 23, 10, 350), 'YYYY-MM-DD hh:mm:ss.SSS A'); // '1998-06-03 03:23:10.350 PM'
59+
format(new Date(2015, 10, 20), 'dddd MMMM Do, YYYY'); // 'Friday November 20th, 2015'
60+
format(new Date(1998, 5, 3, 15, 23, 10, 350), 'YYYY-MM-DD hh:mm:ss.SSS A'); // '1998-06-03 03:23:10.350 PM'
5961

6062
// Named masks
61-
fecha.format(new Date(2015, 10, 20), 'mediumDate'); // 'Nov 20, 2015'
62-
fecha.format(new Date(2015, 2, 10, 5, 30, 20), 'shortTime'); // '05:30'
63+
format(new Date(2015, 10, 20), 'mediumDate'); // 'Nov 20, 2015'
64+
format(new Date(2015, 2, 10, 5, 30, 20), 'shortTime'); // '05:30'
6365

6466
// Literals
65-
fecha.format(new Date(2001, 2, 5, 6, 7, 2, 5), '[on] MM-DD-YYYY [at] HH:mm'); // 'on 03-05-2001 at 06:07'
67+
format(new Date(2001, 2, 5, 6, 7, 2, 5), '[on] MM-DD-YYYY [at] HH:mm'); // 'on 03-05-2001 at 06:07'
6668
```
6769

6870
#### Parsing
69-
`fecha.parse` accepts a Date string and a string format and returns a Date object. See below for available format tokens.
71+
`parse` accepts a Date string and a string format and returns a Date object. See below for available format tokens.
7072

71-
Note: `fecha.parse` will throw an error when passed invalid parameters
73+
*NOTE*: `parse` will throw an error when passed invalid string format or missing format. You MUST specify a format.
7274
```js
75+
import { parse } from 'fecha';
76+
77+
type parse = (dateStr: string, format: string, i18n?: I18nSettingsOptional) => Date|null;
78+
7379
// Custom formats
74-
fecha.parse('February 3rd, 2014', 'MMMM Do, YYYY'); // new Date(2014, 1, 3)
75-
fecha.parse('10-12-10 14:11:12', 'YY-MM-DD HH:mm:ss'); // new Date(2010, 11, 10, 14, 11, 12)
80+
parse('February 3rd, 2014', 'MMMM Do, YYYY'); // new Date(2014, 1, 3)
81+
parse('10-12-10 14:11:12', 'YY-MM-DD HH:mm:ss'); // new Date(2010, 11, 10, 14, 11, 12)
7682

7783
// Named masks
78-
fecha.parse('5/3/98', 'shortDate'); // new Date(1998, 4, 3)
79-
fecha.parse('November 4, 2005', 'longDate'); // new Date(2005, 10, 4)
84+
parse('5/3/98', 'shortDate'); // new Date(1998, 4, 3)
85+
parse('November 4, 2005', 'longDate'); // new Date(2005, 10, 4)
86+
87+
// Override i18n
88+
parse('4 de octubre de 2005', 'M de MMMM de YYYY', {
89+
monthNames: [
90+
'enero',
91+
'febrero',
92+
'marzo',
93+
'abril',
94+
'mayo',
95+
'junio',
96+
'julio',
97+
'agosto',
98+
'septiembre',
99+
'octubre',
100+
'noviembre',
101+
'diciembre'
102+
]
103+
});
80104
```
81105

82106
#### i18n Support
83107
```js
84-
// Override fecha.i18n to support any language
85-
fecha.i18n = {
108+
// Default Global Settings
109+
{
86110
dayNamesShort: ['Sun', 'Mon', 'Tue', 'Wed', 'Thur', 'Fri', 'Sat'],
111+
dayNames: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
112+
monthNamesShort: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
113+
monthNames: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
114+
amPm: ['am', 'pm'],
115+
// D is the day of the month, function returns something like... 3rd or 11th
116+
DoFn(dayOfMonth) {
117+
return dayOfMonth + [ 'th', 'st', 'nd', 'rd' ][ dayOfMonth % 10 > 3 ? 0 : (dayOfMonth - dayOfMonth % 10 !== 10) * dayOfMonth % 10 ];
118+
}
119+
}
120+
121+
// Override global settings
122+
import {setGlobalDateI18n} from 'fecha';
123+
124+
setGlobalDateI18n({
125+
dayNamesShort: ['Sun', 'Mon', 'Tue', 'Wed', 'Thur', 'Fri', 'Sat'],
87126
dayNames: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
88127
monthNamesShort: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
89128
monthNames: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
90129
amPm: ['am', 'pm'],
91130
// D is the day of the month, function returns something like... 3rd or 11th
92131
DoFn: function (D) {
93132
return D + [ 'th', 'st', 'nd', 'rd' ][ D % 10 > 3 ? 0 : (D - D % 10 !== 10) * D % 10 ];
94-
}
95-
}
133+
}
134+
});
135+
96136
```
97137

98138
#### Custom Named Masks
99139
```js
100-
fecha.masks = {
140+
// Default global masks
141+
{
101142
default: 'ddd MMM DD YYYY HH:mm:ss',
102143
shortDate: 'M/D/YY',
103144
mediumDate: 'MMM D, YYYY',
@@ -106,13 +147,17 @@ fecha.masks = {
106147
shortTime: 'HH:mm',
107148
mediumTime: 'HH:mm:ss',
108149
longTime: 'HH:mm:ss.SSS'
109-
};
150+
}
110151

111152
// Create a new mask
112-
fecha.masks.myMask = 'HH:mm:ss YY/MM/DD';
153+
import { format, setGlobalDateMasks } from 'fecha';
154+
155+
setGlobalDateMasks({
156+
myMask: 'HH:mm:ss YY/MM/DD';
157+
});
113158

114159
// Use it
115-
fecha.format(new Date(2014, 5, 6, 14, 10, 45), 'myMask'); // '14:10:45 14/06/06'
160+
format(new Date(2014, 5, 6, 14, 10, 45), 'myMask'); // '14:10:45 14/06/06'
116161
```
117162

118163
### Formatting Tokens

dist/fecha.min.js

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/fecha.min.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)