quick-n-dirty-utils 0.0.15 → 1.0.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/README.md +10 -10
- package/dist/{util.js → functions.js} +54 -18
- package/index.js +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -31,12 +31,12 @@ const range = util.range(1, 10)
|
|
|
31
31
|
|
|
32
32
|
### Date / Time
|
|
33
33
|
|
|
34
|
-
All date / time functions use `
|
|
34
|
+
All date / time functions use `luxon` (Luxon, the de-factor standard library for date/time) to provide the
|
|
35
35
|
functionality.
|
|
36
36
|
|
|
37
37
|
#### `formatDate(date, format)`
|
|
38
|
-
Converts a `Date` object or `
|
|
39
|
-
The default format is `
|
|
38
|
+
Converts a `Date` object, Unix timestamp or `luxon.DateTime` object into a string for quick display of dates
|
|
39
|
+
**without** a time component. The default format is `yyyy-MM-dd`.
|
|
40
40
|
|
|
41
41
|
Example:
|
|
42
42
|
|
|
@@ -50,15 +50,15 @@ const reactComponent = props => (
|
|
|
50
50
|
<div>{util.formatDate(new Date())}</div>
|
|
51
51
|
|
|
52
52
|
{/* prints out a date like 18/02/20 */}
|
|
53
|
-
<div>{util.formatDate(new Date(), "
|
|
53
|
+
<div>{util.formatDate(new Date(), "dd/MM/yy")}</div>
|
|
54
54
|
</div>
|
|
55
55
|
)
|
|
56
56
|
```
|
|
57
57
|
|
|
58
|
-
#### `formatDateTime(dateTime)`
|
|
59
|
-
Converts a `Date` object or `
|
|
60
|
-
require a custom format, you can
|
|
61
|
-
`
|
|
58
|
+
#### `formatDateTime(dateTime, format)`
|
|
59
|
+
Converts a `Date` object, Unix timestamp or `luxon.DateTime` object into a string for quick display of dates
|
|
60
|
+
**with** a time component. If you require a custom format, you can provide this. The default format is
|
|
61
|
+
`dd/MM/yy T` (e.g. 31/12/22 16:43)
|
|
62
62
|
|
|
63
63
|
Example:
|
|
64
64
|
|
|
@@ -76,8 +76,8 @@ const reactComponent = props => (
|
|
|
76
76
|
|
|
77
77
|
#### `applyTimeZoneOffset(timestamp, serverOffsetMin)`
|
|
78
78
|
Used to offset mismatching server/client time zones in regards to timestamps. If your server provides Unix timestamps,
|
|
79
|
-
but is located in a different timezone, then simply printing out those timestamps (as `
|
|
80
|
-
time in the clients (browser) time zone, not the server time zone. Example: Your server provides timestamps for events
|
|
79
|
+
but is located in a different timezone, then simply printing out those timestamps (as `luxon.DateTime`/`Date`) will print
|
|
80
|
+
the time in the clients (browser) time zone, not the server time zone. Example: Your server provides timestamps for events
|
|
81
81
|
and the event occurred at midnight, but you are located 2 hours behind the server's time zone. If you use that timestamp
|
|
82
82
|
and print out the date (e.g. `util.formatDateTime(new Date(myTimestamp))`), it will show you 10pm, rather than midnight.
|
|
83
83
|
Depending on the application, it might be useful to retain the local time.
|
|
@@ -5,9 +5,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports["default"] = void 0;
|
|
7
7
|
|
|
8
|
-
var
|
|
9
|
-
|
|
10
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
8
|
+
var _luxon = require("luxon");
|
|
11
9
|
|
|
12
10
|
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
|
|
13
11
|
|
|
@@ -26,9 +24,9 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
|
|
|
26
24
|
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
27
25
|
|
|
28
26
|
// default date format
|
|
29
|
-
var DATE_FORMAT = "
|
|
27
|
+
var DATE_FORMAT = "yyyy-MM-dd"; // default date/time format
|
|
30
28
|
|
|
31
|
-
var DATE_TIME_FORMAT = "
|
|
29
|
+
var DATE_TIME_FORMAT = "dd/MM/yy T"; // localStorage key uses to store the auth token
|
|
32
30
|
|
|
33
31
|
var LS_AUTH_KEY = "auth_token";
|
|
34
32
|
var SORT_DIRECTIONS = {
|
|
@@ -39,7 +37,7 @@ var SORT_DIRECTIONS = {
|
|
|
39
37
|
* Utilities used across components
|
|
40
38
|
*/
|
|
41
39
|
|
|
42
|
-
var
|
|
40
|
+
var qndUtils = {
|
|
43
41
|
/**
|
|
44
42
|
* Default date format for quick display
|
|
45
43
|
*/
|
|
@@ -61,18 +59,54 @@ var util = {
|
|
|
61
59
|
|
|
62
60
|
/**
|
|
63
61
|
* Uses the hard-coded date format to format the provided date. If no valid date is provided, null is returned.
|
|
64
|
-
* @param {(object|string)} date: the date to format, provided either as string or
|
|
65
|
-
* provided, that string needs to be parsable by
|
|
66
|
-
* @param {string} dateFormat: the date format to be used by
|
|
62
|
+
* @param {(object|string)} date: the date to format, provided either as string or Luxon object. If a string is
|
|
63
|
+
* provided, that string needs to be parsable by Luxon
|
|
64
|
+
* @param {string} dateFormat: the date format to be used by Luxon to serialise the date, default "yyyy-MM-dd"
|
|
67
65
|
* @returns {String} the formatted string or null, if the provided date string or object is not valid or cannot be
|
|
68
66
|
* parsed.
|
|
69
67
|
*/
|
|
70
68
|
formatDate: function formatDate(date) {
|
|
71
69
|
var dateFormat = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : DATE_FORMAT;
|
|
72
|
-
var d = (0, _moment["default"])(date);
|
|
73
70
|
|
|
74
|
-
|
|
75
|
-
|
|
71
|
+
// handling JS date objects
|
|
72
|
+
if (date instanceof Date) {
|
|
73
|
+
return this.formatDate(_luxon.DateTime.fromJSDate(date));
|
|
74
|
+
} // handling unix timestamps (guessing s or ms)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
if (typeof date === "number") {
|
|
78
|
+
if (date < 5000000000) {
|
|
79
|
+
// otherwise would be year 2128+
|
|
80
|
+
return this.formatDate(_luxon.DateTime.fromSeconds(date), dateFormat);
|
|
81
|
+
} // doesn't work for dates before 28 Feb 1970
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
return this.formatDate(_luxon.DateTime.fromMillis(date), dateFormat);
|
|
85
|
+
} // handling string date/times
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
if (typeof date === "string") {
|
|
89
|
+
// attempt to parse
|
|
90
|
+
var functions = [_luxon.DateTime.fromISO, _luxon.DateTime.fromSQL, _luxon.DateTime.fromRFC2822];
|
|
91
|
+
|
|
92
|
+
for (var i = 0; i < functions.length; i += 1) {
|
|
93
|
+
var parsedDate = functions[i](date);
|
|
94
|
+
|
|
95
|
+
if (parsedDate.isValid) {
|
|
96
|
+
return this.formatDate(parsedDate, dateFormat);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
throw Error("Provided string date could not be detected, please convert to Luxon DateTime before formatting");
|
|
101
|
+
} // handling momentjs objects
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
if (date._isAMomentObject != null && date.unix != null) {
|
|
105
|
+
return this.formatDate(date.unix(), dateFormat);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (date.isValid) {
|
|
109
|
+
return date.toFormat(dateFormat);
|
|
76
110
|
}
|
|
77
111
|
|
|
78
112
|
return null;
|
|
@@ -80,13 +114,15 @@ var util = {
|
|
|
80
114
|
|
|
81
115
|
/**
|
|
82
116
|
* Uses a hard-coded date/time format to format the provided date. If no valid date is provided, null is returned.
|
|
83
|
-
* @param {(object|string)} date: the date to format, provided either as string or
|
|
84
|
-
* provided, that string needs to be parsable by
|
|
117
|
+
* @param {(object|string)} date: the date to format, provided either as string, Number or Luxon object. If a string
|
|
118
|
+
* is provided, that string needs to be parsable by Luxon
|
|
119
|
+
* @param {string} dateTimeFormat - the Luxon datetime format, defaults to dd/MM/yy T
|
|
85
120
|
* @returns {String} the formatted string or null, if the provided date string or object is not valid or cannot be
|
|
86
121
|
* parsed.
|
|
87
122
|
*/
|
|
88
123
|
formatDateTime: function formatDateTime(date) {
|
|
89
|
-
|
|
124
|
+
var dateTimeFormat = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : DATE_TIME_FORMAT;
|
|
125
|
+
return this.formatDate(date, dateTimeFormat);
|
|
90
126
|
},
|
|
91
127
|
|
|
92
128
|
/**
|
|
@@ -193,13 +229,13 @@ var util = {
|
|
|
193
229
|
},
|
|
194
230
|
|
|
195
231
|
/**
|
|
196
|
-
* Applies an offset to a unix timestamp to allow native JS dates and
|
|
232
|
+
* Applies an offset to a unix timestamp to allow native JS dates and Luxon to render the resulting date in the
|
|
197
233
|
* server's timezone, rather than the browsers time zone. The idea is to convert all timestamps of a time series
|
|
198
234
|
* received from a server in a different time into offset timestamps, which then allows to render the data as chart
|
|
199
235
|
* or table using the server's time and not the users time.
|
|
200
236
|
* @param {number} timestamp: the original timestamp in seconds since 1970
|
|
201
237
|
* @param {number} serverOffsetMin: the number of minutes behind UTC (e.g. +10:00 is 600 minutes after UTC)
|
|
202
|
-
* @returns {number} the offset timestamp which when used by
|
|
238
|
+
* @returns {number} the offset timestamp which when used by Luxon or as argument for new Date(..) will produce a
|
|
203
239
|
* date / time string in the server's timezone rather than the users/browser timezone
|
|
204
240
|
*/
|
|
205
241
|
applyTimeZoneOffset: function applyTimeZoneOffset(timestamp) {
|
|
@@ -640,5 +676,5 @@ var util = {
|
|
|
640
676
|
return "rgb(".concat(r, ", ").concat(g, ", ").concat(b, ")");
|
|
641
677
|
}
|
|
642
678
|
};
|
|
643
|
-
var _default =
|
|
679
|
+
var _default = qndUtils;
|
|
644
680
|
exports["default"] = _default;
|
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
module.exports = require("./dist/
|
|
1
|
+
module.exports = { util: require("./dist/functions").default }
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "quick-n-dirty-utils",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "Little useful nuggets for accelerated web development",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"build": "./node_modules/.bin/babel src --out-dir ./dist",
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
},
|
|
23
23
|
"homepage": "https://github.com/ilfrich/quick-n-dirty-utils#readme",
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"
|
|
25
|
+
"luxon": "^3.0.1"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
28
|
"@babel/cli": "^7.2.2",
|