quick-n-dirty-utils 0.0.15 → 1.0.1
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 +99 -34
- package/dist/{util.js → functions.js} +134 -17
- package/index.js +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -6,6 +6,8 @@ Little useful nuggets for accelerated web development.
|
|
|
6
6
|
npm install --save quick-n-dirty-utils
|
|
7
7
|
```
|
|
8
8
|
|
|
9
|
+
*IMPORTANT: PLEASE READ THE [MIGRATION NOTES](https://github.com/ilfrich/quick-n-dirty-utils/releases/tag/1.0.0) FOR VERSION 1.0.0+*
|
|
10
|
+
|
|
9
11
|
**Table of Contents**
|
|
10
12
|
|
|
11
13
|
1. [Functions](#functions)
|
|
@@ -23,7 +25,7 @@ npm install --save quick-n-dirty-utils
|
|
|
23
25
|
|
|
24
26
|
```javascript
|
|
25
27
|
// import dependency
|
|
26
|
-
import util from "quick-n-dirty-utils"
|
|
28
|
+
import { util } from "quick-n-dirty-utils"
|
|
27
29
|
|
|
28
30
|
// call a function of it
|
|
29
31
|
const range = util.range(1, 10)
|
|
@@ -31,18 +33,18 @@ const range = util.range(1, 10)
|
|
|
31
33
|
|
|
32
34
|
### Date / Time
|
|
33
35
|
|
|
34
|
-
All date / time functions use `
|
|
36
|
+
All date / time functions use `luxon` (Luxon, the de-factor standard library for date/time) to provide the
|
|
35
37
|
functionality.
|
|
36
38
|
|
|
37
39
|
#### `formatDate(date, format)`
|
|
38
|
-
Converts a `Date` object or `
|
|
39
|
-
The default format is `
|
|
40
|
+
Converts a `Date` object, Unix timestamp or `luxon.DateTime` object into a string for quick display of dates
|
|
41
|
+
**without** a time component. The default format is `yyyy-MM-dd`.
|
|
40
42
|
|
|
41
43
|
Example:
|
|
42
44
|
|
|
43
45
|
```jsx harmony
|
|
44
46
|
import React from "react"
|
|
45
|
-
import util from "quick-n-dirty-utils"
|
|
47
|
+
import { util } from "quick-n-dirty-utils"
|
|
46
48
|
|
|
47
49
|
const reactComponent = props => (
|
|
48
50
|
<div>
|
|
@@ -50,21 +52,21 @@ const reactComponent = props => (
|
|
|
50
52
|
<div>{util.formatDate(new Date())}</div>
|
|
51
53
|
|
|
52
54
|
{/* prints out a date like 18/02/20 */}
|
|
53
|
-
<div>{util.formatDate(new Date(), "
|
|
55
|
+
<div>{util.formatDate(new Date(), "dd/MM/yy")}</div>
|
|
54
56
|
</div>
|
|
55
57
|
)
|
|
56
58
|
```
|
|
57
59
|
|
|
58
|
-
#### `formatDateTime(dateTime)`
|
|
59
|
-
Converts a `Date` object or `
|
|
60
|
-
require a custom format, you can
|
|
61
|
-
`
|
|
60
|
+
#### `formatDateTime(dateTime, format)`
|
|
61
|
+
Converts a `Date` object, Unix timestamp or `luxon.DateTime` object into a string for quick display of dates
|
|
62
|
+
**with** a time component. If you require a custom format, you can provide this. The default format is
|
|
63
|
+
`dd/MM/yy T` (e.g. 31/12/22 16:43)
|
|
62
64
|
|
|
63
65
|
Example:
|
|
64
66
|
|
|
65
67
|
```jsx harmony
|
|
66
68
|
import React from "react"
|
|
67
|
-
import util from "quick-n-dirty-utils"
|
|
69
|
+
import { util } from "quick-n-dirty-utils"
|
|
68
70
|
|
|
69
71
|
const reactComponent = props => (
|
|
70
72
|
<div>
|
|
@@ -76,8 +78,8 @@ const reactComponent = props => (
|
|
|
76
78
|
|
|
77
79
|
#### `applyTimeZoneOffset(timestamp, serverOffsetMin)`
|
|
78
80
|
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
|
|
81
|
+
but is located in a different timezone, then simply printing out those timestamps (as `luxon.DateTime`/`Date`) will print
|
|
82
|
+
the time in the clients (browser) time zone, not the server time zone. Example: Your server provides timestamps for events
|
|
81
83
|
and the event occurred at midnight, but you are located 2 hours behind the server's time zone. If you use that timestamp
|
|
82
84
|
and print out the date (e.g. `util.formatDateTime(new Date(myTimestamp))`), it will show you 10pm, rather than midnight.
|
|
83
85
|
Depending on the application, it might be useful to retain the local time.
|
|
@@ -90,7 +92,7 @@ determined by the browser. Example offsets:
|
|
|
90
92
|
Example:
|
|
91
93
|
|
|
92
94
|
```javascript
|
|
93
|
-
import util from "quick-n-dirty-utils"
|
|
95
|
+
import { util } from "quick-n-dirty-utils"
|
|
94
96
|
|
|
95
97
|
const serverTimestamp = 1580777394
|
|
96
98
|
|
|
@@ -109,7 +111,7 @@ Familiar to Python programmers, this will create a list of sequential numeric va
|
|
|
109
111
|
Example:
|
|
110
112
|
|
|
111
113
|
```javascript
|
|
112
|
-
import util from "quick-n-dirty-utils"
|
|
114
|
+
import { util } from "quick-n-dirty-utils"
|
|
113
115
|
|
|
114
116
|
let range = util.range(1, 5) // will produce [1, 2, 3, 4, 5]
|
|
115
117
|
range = util.range(5, 1, -1) // will produce [5, 4, 3, 2, 1]
|
|
@@ -123,7 +125,7 @@ currently only works for `min < max`.
|
|
|
123
125
|
Example:
|
|
124
126
|
|
|
125
127
|
```javascript
|
|
126
|
-
import util from "quick-n-dirty-utils"
|
|
128
|
+
import { util } from "quick-n-dirty-utils"
|
|
127
129
|
|
|
128
130
|
let percentage = util.normalise(3, 0, 20) // returns 0.15 or 15%
|
|
129
131
|
percentage = util.normalise(10, 0, 20) // return 0.5 or 50%
|
|
@@ -135,10 +137,20 @@ This is just a short hand for summing up numeric values in an array.
|
|
|
135
137
|
Example:
|
|
136
138
|
|
|
137
139
|
```javascript
|
|
138
|
-
import util from "quick-n-dirty-utils"
|
|
140
|
+
import { util } from "quick-n-dirty-utils"
|
|
139
141
|
|
|
140
142
|
const list = [5, 3.5, 10]
|
|
141
|
-
sum = util.sum(list) // returns 18.5
|
|
143
|
+
const sum = util.sum(list) // returns 18.5
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
#### `mean(list)`
|
|
147
|
+
This is just a short hand for summing up numeric values in an array and dividing by its length.
|
|
148
|
+
|
|
149
|
+
Example
|
|
150
|
+
```javascript
|
|
151
|
+
import { util } from "quick-n-dirty-utils"
|
|
152
|
+
const list = [3, 3, 6]
|
|
153
|
+
const avg = util.mean(list) // returns 4.0
|
|
142
154
|
```
|
|
143
155
|
|
|
144
156
|
### REST
|
|
@@ -149,7 +161,7 @@ Small helper to just provide the `Content-Type` and `Accept` header for a `fetch
|
|
|
149
161
|
Example:
|
|
150
162
|
|
|
151
163
|
```javascript
|
|
152
|
-
import util from "quick-n-dirty-utils"
|
|
164
|
+
import { util } from "quick-n-dirty-utils"
|
|
153
165
|
|
|
154
166
|
fetch("http://myurl.com", {
|
|
155
167
|
headers: util.getJsonHeader(),
|
|
@@ -167,7 +179,7 @@ authentication.
|
|
|
167
179
|
Example:
|
|
168
180
|
|
|
169
181
|
```javascript
|
|
170
|
-
import util from "quick-n-dirty-utils"
|
|
182
|
+
import { util } from "quick-n-dirty-utils"
|
|
171
183
|
|
|
172
184
|
fetch("http://myurl.com", {
|
|
173
185
|
// will use the localStorage.getItem("auth_token") as Authorization header value; provide a custom key if required
|
|
@@ -185,7 +197,7 @@ requires authentication.
|
|
|
185
197
|
Example:
|
|
186
198
|
|
|
187
199
|
```javascript
|
|
188
|
-
import util from "quick-n-dirty-utils"
|
|
200
|
+
import { util } from "quick-n-dirty-utils"
|
|
189
201
|
|
|
190
202
|
fetch("http://myurl.com", {
|
|
191
203
|
// will use the localStorage.getItem("my_token") as Authorization header value; provide a custom key if required
|
|
@@ -204,7 +216,7 @@ status code and the response body as payload.
|
|
|
204
216
|
Example:
|
|
205
217
|
|
|
206
218
|
```javascript
|
|
207
|
-
import util from "quick-n-dirty-utils"
|
|
219
|
+
import { util } from "quick-n-dirty-utils"
|
|
208
220
|
|
|
209
221
|
fetch("http://myurl.com", {
|
|
210
222
|
headers: util.getAuthJsonHeader(),
|
|
@@ -226,7 +238,7 @@ When a React component uses a state variable to store a list of items any create
|
|
|
226
238
|
following 2 functions are available:
|
|
227
239
|
|
|
228
240
|
```javascript
|
|
229
|
-
import util from "quick-n-dirty-utils"
|
|
241
|
+
import { util } from "quick-n-dirty-utils"
|
|
230
242
|
import React from "react"
|
|
231
243
|
|
|
232
244
|
class MyComp extends React.Component {
|
|
@@ -271,7 +283,7 @@ Simple access wrapper for the browser's `localStorage` to store a login token. T
|
|
|
271
283
|
Example:
|
|
272
284
|
|
|
273
285
|
```javascript
|
|
274
|
-
import util from "quick-n-dirty-utils"
|
|
286
|
+
import { util } from "quick-n-dirty-utils"
|
|
275
287
|
|
|
276
288
|
fetch("http://myurl.com/login", {
|
|
277
289
|
method: "POST",
|
|
@@ -291,7 +303,7 @@ Counterpart to `setAuthToken(token, localStorageKey)`. This one deletes the key
|
|
|
291
303
|
Example:
|
|
292
304
|
|
|
293
305
|
```javascript
|
|
294
|
-
import util from "quick-n-dirty-utils"
|
|
306
|
+
import { util } from "quick-n-dirty-utils"
|
|
295
307
|
|
|
296
308
|
fetch("http://myurl.com/logout", {
|
|
297
309
|
method: "POST",
|
|
@@ -318,7 +330,7 @@ Redux, this will be useful. Using these helpers will avoid typos and thus improv
|
|
|
318
330
|
Example:
|
|
319
331
|
|
|
320
332
|
```javascript
|
|
321
|
-
import util from "quick-n-dirty-utils"
|
|
333
|
+
import { util } from "quick-n-dirty-utils"
|
|
322
334
|
|
|
323
335
|
const reduxAction = () => ({
|
|
324
336
|
type: "myAction",
|
|
@@ -366,7 +378,7 @@ And then when the response of that request comes in either `_rejected` or `_fulf
|
|
|
366
378
|
Some smaller helpers for handling colour gradients.
|
|
367
379
|
|
|
368
380
|
```javascript
|
|
369
|
-
import util from "quick-n-dirty-utils"
|
|
381
|
+
import { util } from "quick-n-dirty-utils"
|
|
370
382
|
|
|
371
383
|
util.getTricolor(0.5) // returns white
|
|
372
384
|
util.getTricolor(0.7) // returns pale blue
|
|
@@ -437,7 +449,7 @@ Updates a React component state's sorting definition. If the current `sortKey` i
|
|
|
437
449
|
**Usage**
|
|
438
450
|
|
|
439
451
|
```javascript
|
|
440
|
-
import util from "quick-n-dirty-utils"
|
|
452
|
+
import { util } from "quick-n-dirty-utils"
|
|
441
453
|
|
|
442
454
|
...
|
|
443
455
|
// initial state
|
|
@@ -467,7 +479,7 @@ Helper function that returns an `(a, b) => ...` lambda that can be used to sort
|
|
|
467
479
|
**Usage**
|
|
468
480
|
|
|
469
481
|
```javascript
|
|
470
|
-
import util from "quick-n-dirty-utils"
|
|
482
|
+
import { util } from "quick-n-dirty-utils"
|
|
471
483
|
...
|
|
472
484
|
// initial state, set in the constructor of the react component
|
|
473
485
|
this.state = {
|
|
@@ -526,7 +538,7 @@ This function will extract a URL query part (the part after the `?`) and extract
|
|
|
526
538
|
Example:
|
|
527
539
|
|
|
528
540
|
```javascript
|
|
529
|
-
import util from "quick-n-dirty-utils"
|
|
541
|
+
import { util } from "quick-n-dirty-utils"
|
|
530
542
|
|
|
531
543
|
const queryString = "?id=abc&page=5"
|
|
532
544
|
const query = util.getQueryStringParams(queryString) // returns { id: "abc", page: "5" }
|
|
@@ -540,7 +552,7 @@ This functions creates a reverse mapping for a flat JSON object mapping from key
|
|
|
540
552
|
Example:
|
|
541
553
|
|
|
542
554
|
```javascript
|
|
543
|
-
import util from "quick-n-dirty-utils"
|
|
555
|
+
import { util } from "quick-n-dirty-utils"
|
|
544
556
|
|
|
545
557
|
const MAPPINMG = {
|
|
546
558
|
key: "value1",
|
|
@@ -562,7 +574,7 @@ This function will first call `reverseMapping` on the input JSON object and then
|
|
|
562
574
|
Example:
|
|
563
575
|
|
|
564
576
|
```javascript
|
|
565
|
-
import util from "quick-n-dirty-utils"
|
|
577
|
+
import { util } from "quick-n-dirty-utils"
|
|
566
578
|
|
|
567
579
|
const MAPPING = {
|
|
568
580
|
INVEST: "Investment and Portfolio",
|
|
@@ -580,7 +592,7 @@ This simple function has the purpose to access nested JSON objects without
|
|
|
580
592
|
Example:
|
|
581
593
|
|
|
582
594
|
```javascript
|
|
583
|
-
import util from "quick-n-dirty-utils"
|
|
595
|
+
import { util } from "quick-n-dirty-utils"
|
|
584
596
|
|
|
585
597
|
const myObject = {
|
|
586
598
|
foo: {
|
|
@@ -608,7 +620,7 @@ The result will be a JSON object. Underneath each key, it will store
|
|
|
608
620
|
Example:
|
|
609
621
|
|
|
610
622
|
```javascript
|
|
611
|
-
import util from "quick-n-dirty-utils"
|
|
623
|
+
import { util } from "quick-n-dirty-utils"
|
|
612
624
|
|
|
613
625
|
const myList = [
|
|
614
626
|
{ _id: "a", name: "foo", level: 1, order: 1 },
|
|
@@ -653,3 +665,56 @@ const result3 = util.mapListToKeyObject(myList, item => item[level] + item[order
|
|
|
653
665
|
* }
|
|
654
666
|
*/
|
|
655
667
|
```
|
|
668
|
+
|
|
669
|
+
#### `getLast(array, defaultValue = null)`
|
|
670
|
+
|
|
671
|
+
Retrieves the last element of an array or the provided `defaultValue`, in
|
|
672
|
+
case the provided array is not an array or empty.
|
|
673
|
+
|
|
674
|
+
```javascript
|
|
675
|
+
import { util } from "quick-n-dirty-utils"
|
|
676
|
+
|
|
677
|
+
const list = [0, 1, 2, 3, 4]
|
|
678
|
+
console.log(getLast(list)) // will return 4
|
|
679
|
+
```
|
|
680
|
+
|
|
681
|
+
|
|
682
|
+
#### `arrayMatch(values, matchAgainst, minMatch = 1)`
|
|
683
|
+
|
|
684
|
+
Finds out if an array of values occurs in another array of values. This is useful for filtering lists, which
|
|
685
|
+
have multiple values for an attribute and you want the user to be able to filter by some of the values.
|
|
686
|
+
|
|
687
|
+
```javascript
|
|
688
|
+
import { util } from "quick-n-dirty-utils"
|
|
689
|
+
|
|
690
|
+
const items = [
|
|
691
|
+
{ name: "Hello", tags: ["a"] },
|
|
692
|
+
{ name: "World", tags: ["c"] },
|
|
693
|
+
{ name: "Hello World", tags: ["a", "b"] },
|
|
694
|
+
]
|
|
695
|
+
let selectedTags = ["a", "b"]
|
|
696
|
+
|
|
697
|
+
const active = items.filter(i => util.arrayMatch(i.tags, selectedTags))
|
|
698
|
+
/*
|
|
699
|
+
* will return:
|
|
700
|
+
* [{ name: "Hello", tags: ["a"] }, { name: "Hello World", tags: ["a", "b"] }]
|
|
701
|
+
*/
|
|
702
|
+
const twoActive = items.filter(i => util.arrayMatch(i.tags, selectedTags, 2))
|
|
703
|
+
/* will return:
|
|
704
|
+
* [{ name: "Hello World", tags: ["a", "b"] }]
|
|
705
|
+
*/
|
|
706
|
+
```
|
|
707
|
+
|
|
708
|
+
#### `arraySearch(values, filterFunction)`
|
|
709
|
+
|
|
710
|
+
Finds the first match in a list of values that pass the `filterFunction` or `null` if no item in the list matches the
|
|
711
|
+
function.
|
|
712
|
+
|
|
713
|
+
```javascript
|
|
714
|
+
import { util } from "quick-n-dirty-utils"
|
|
715
|
+
|
|
716
|
+
const items = [0, 1, 2]
|
|
717
|
+
const result1 = util.arraySearch(items, val => val === 2) // returns 2
|
|
718
|
+
const result2 = util.arraySearch(items, val => val === 3) // returns null
|
|
719
|
+
const result3 = util.arraySearch(items, val => val > 0) // returns 1 (first match)
|
|
720
|
+
```
|
|
@@ -5,9 +5,15 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports["default"] = void 0;
|
|
7
7
|
|
|
8
|
-
var
|
|
8
|
+
var _luxon = require("luxon");
|
|
9
9
|
|
|
10
|
-
function
|
|
10
|
+
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }
|
|
11
|
+
|
|
12
|
+
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }
|
|
13
|
+
|
|
14
|
+
function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }
|
|
15
|
+
|
|
16
|
+
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }
|
|
11
17
|
|
|
12
18
|
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
19
|
|
|
@@ -26,9 +32,9 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
|
|
|
26
32
|
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
33
|
|
|
28
34
|
// default date format
|
|
29
|
-
var DATE_FORMAT = "
|
|
35
|
+
var DATE_FORMAT = "yyyy-MM-dd"; // default date/time format
|
|
30
36
|
|
|
31
|
-
var DATE_TIME_FORMAT = "
|
|
37
|
+
var DATE_TIME_FORMAT = "dd/MM/yy T"; // localStorage key uses to store the auth token
|
|
32
38
|
|
|
33
39
|
var LS_AUTH_KEY = "auth_token";
|
|
34
40
|
var SORT_DIRECTIONS = {
|
|
@@ -39,7 +45,7 @@ var SORT_DIRECTIONS = {
|
|
|
39
45
|
* Utilities used across components
|
|
40
46
|
*/
|
|
41
47
|
|
|
42
|
-
var
|
|
48
|
+
var qndUtils = {
|
|
43
49
|
/**
|
|
44
50
|
* Default date format for quick display
|
|
45
51
|
*/
|
|
@@ -61,18 +67,54 @@ var util = {
|
|
|
61
67
|
|
|
62
68
|
/**
|
|
63
69
|
* 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
|
|
70
|
+
* @param {(object|string)} date: the date to format, provided either as string or Luxon object. If a string is
|
|
71
|
+
* provided, that string needs to be parsable by Luxon
|
|
72
|
+
* @param {string} dateFormat: the date format to be used by Luxon to serialise the date, default "yyyy-MM-dd"
|
|
67
73
|
* @returns {String} the formatted string or null, if the provided date string or object is not valid or cannot be
|
|
68
74
|
* parsed.
|
|
69
75
|
*/
|
|
70
76
|
formatDate: function formatDate(date) {
|
|
71
77
|
var dateFormat = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : DATE_FORMAT;
|
|
72
|
-
var d = (0, _moment["default"])(date);
|
|
73
78
|
|
|
74
|
-
|
|
75
|
-
|
|
79
|
+
// handling JS date objects
|
|
80
|
+
if (date instanceof Date) {
|
|
81
|
+
return this.formatDate(_luxon.DateTime.fromJSDate(date));
|
|
82
|
+
} // handling unix timestamps (guessing s or ms)
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
if (typeof date === "number") {
|
|
86
|
+
if (date < 5000000000) {
|
|
87
|
+
// otherwise would be year 2128+
|
|
88
|
+
return this.formatDate(_luxon.DateTime.fromSeconds(date), dateFormat);
|
|
89
|
+
} // doesn't work for dates before 28 Feb 1970
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
return this.formatDate(_luxon.DateTime.fromMillis(date), dateFormat);
|
|
93
|
+
} // handling string date/times
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
if (typeof date === "string") {
|
|
97
|
+
// attempt to parse
|
|
98
|
+
var functions = [_luxon.DateTime.fromISO, _luxon.DateTime.fromSQL, _luxon.DateTime.fromRFC2822];
|
|
99
|
+
|
|
100
|
+
for (var i = 0; i < functions.length; i += 1) {
|
|
101
|
+
var parsedDate = functions[i](date);
|
|
102
|
+
|
|
103
|
+
if (parsedDate.isValid) {
|
|
104
|
+
return this.formatDate(parsedDate, dateFormat);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
throw Error("Provided string date could not be detected, please convert to Luxon DateTime before formatting");
|
|
109
|
+
} // handling momentjs objects
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
if (date._isAMomentObject != null && date.unix != null) {
|
|
113
|
+
return this.formatDate(date.unix(), dateFormat);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (date.isValid) {
|
|
117
|
+
return date.toFormat(dateFormat);
|
|
76
118
|
}
|
|
77
119
|
|
|
78
120
|
return null;
|
|
@@ -80,13 +122,15 @@ var util = {
|
|
|
80
122
|
|
|
81
123
|
/**
|
|
82
124
|
* 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
|
|
125
|
+
* @param {(object|string)} date: the date to format, provided either as string, Number or Luxon object. If a string
|
|
126
|
+
* is provided, that string needs to be parsable by Luxon
|
|
127
|
+
* @param {string} dateTimeFormat - the Luxon datetime format, defaults to dd/MM/yy T
|
|
85
128
|
* @returns {String} the formatted string or null, if the provided date string or object is not valid or cannot be
|
|
86
129
|
* parsed.
|
|
87
130
|
*/
|
|
88
131
|
formatDateTime: function formatDateTime(date) {
|
|
89
|
-
|
|
132
|
+
var dateTimeFormat = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : DATE_TIME_FORMAT;
|
|
133
|
+
return this.formatDate(date, dateTimeFormat);
|
|
90
134
|
},
|
|
91
135
|
|
|
92
136
|
/**
|
|
@@ -193,13 +237,13 @@ var util = {
|
|
|
193
237
|
},
|
|
194
238
|
|
|
195
239
|
/**
|
|
196
|
-
* Applies an offset to a unix timestamp to allow native JS dates and
|
|
240
|
+
* Applies an offset to a unix timestamp to allow native JS dates and Luxon to render the resulting date in the
|
|
197
241
|
* server's timezone, rather than the browsers time zone. The idea is to convert all timestamps of a time series
|
|
198
242
|
* received from a server in a different time into offset timestamps, which then allows to render the data as chart
|
|
199
243
|
* or table using the server's time and not the users time.
|
|
200
244
|
* @param {number} timestamp: the original timestamp in seconds since 1970
|
|
201
245
|
* @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
|
|
246
|
+
* @returns {number} the offset timestamp which when used by Luxon or as argument for new Date(..) will produce a
|
|
203
247
|
* date / time string in the server's timezone rather than the users/browser timezone
|
|
204
248
|
*/
|
|
205
249
|
applyTimeZoneOffset: function applyTimeZoneOffset(timestamp) {
|
|
@@ -443,6 +487,19 @@ var util = {
|
|
|
443
487
|
}, 0);
|
|
444
488
|
},
|
|
445
489
|
|
|
490
|
+
/**
|
|
491
|
+
* Provides the mean value of the provided list
|
|
492
|
+
* @param {Array} list - a list of numbers
|
|
493
|
+
* @returns {Number} the average value of the values in the list
|
|
494
|
+
*/
|
|
495
|
+
mean: function mean(list) {
|
|
496
|
+
if (list.length === 0) {
|
|
497
|
+
return 0;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
return this.sum(list) / list.length;
|
|
501
|
+
},
|
|
502
|
+
|
|
446
503
|
/**
|
|
447
504
|
* Extracts the query parameter of an URL query string and returns them as JSON object.
|
|
448
505
|
* @param {string} query - the query string starting with ?, like ?foo=bar&abc=def
|
|
@@ -584,6 +641,56 @@ var util = {
|
|
|
584
641
|
return result;
|
|
585
642
|
},
|
|
586
643
|
|
|
644
|
+
/**
|
|
645
|
+
* Retrieves the last element of an array of the defaultValue, if the array is empty or not an array.
|
|
646
|
+
* @param {Array} array - an array of items
|
|
647
|
+
* @param {Object} defaultValue - the default value that will be returned if the provided array is empty or not an array.
|
|
648
|
+
* @returns {Object} the last element of the provided array or the default value
|
|
649
|
+
*/
|
|
650
|
+
getLast: function getLast(array) {
|
|
651
|
+
var defaultValue = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
|
|
652
|
+
|
|
653
|
+
if (array == null || !Array.isArray(array) || array.length === 0) {
|
|
654
|
+
return defaultValue;
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
return array[array.length - 1];
|
|
658
|
+
},
|
|
659
|
+
|
|
660
|
+
/**
|
|
661
|
+
* Finds out if an array of values occurs in another array of values. This is useful for filtering lists, which
|
|
662
|
+
* have multiple values for an attribute and you want the user to be able to filter by some of the values.
|
|
663
|
+
* @param {Array} values - a list of values that you want to check match some items in matchAgainst. If empty,
|
|
664
|
+
* false will be returned
|
|
665
|
+
* @param {Array} matchAgainst - a list of selected filters. If the array is empty, false will be returned.
|
|
666
|
+
* @param {Number} minMatch - the number of items in matchAgainst that have to be in the values.
|
|
667
|
+
*/
|
|
668
|
+
arrayMatch: function arrayMatch() {
|
|
669
|
+
var values = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
|
|
670
|
+
var matchAgainst = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
|
|
671
|
+
var minMatch = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
|
|
672
|
+
return values.filter(function (val) {
|
|
673
|
+
return matchAgainst.includes(val);
|
|
674
|
+
}).length >= minMatch;
|
|
675
|
+
},
|
|
676
|
+
|
|
677
|
+
/**
|
|
678
|
+
* Will return the first element of an array that matches the provided filter function. If no element matches, null
|
|
679
|
+
* will be returned.
|
|
680
|
+
* @param {Array} array - an array of items
|
|
681
|
+
* @param {function} filterFunction - a function that will be applied to each item in the array.
|
|
682
|
+
* @returns {Object} the first element of the array that matches the filter function or null, if no element matches.
|
|
683
|
+
*/
|
|
684
|
+
arraySearch: function arraySearch(array, filterFunction) {
|
|
685
|
+
var filtered = array.filter(filterFunction);
|
|
686
|
+
|
|
687
|
+
if (filtered.length === 0) {
|
|
688
|
+
return null;
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
return filtered[0];
|
|
692
|
+
},
|
|
693
|
+
|
|
587
694
|
/**
|
|
588
695
|
* Adds or updates an item in a list of items and returns the updated list. This is useful for React state updates.
|
|
589
696
|
* @param {Array} list - a list of items
|
|
@@ -638,7 +745,17 @@ var util = {
|
|
|
638
745
|
}
|
|
639
746
|
|
|
640
747
|
return "rgb(".concat(r, ", ").concat(g, ", ").concat(b, ")");
|
|
748
|
+
},
|
|
749
|
+
|
|
750
|
+
/**
|
|
751
|
+
* Returns all unique values in a list of values. All duplicates will be removed. This does not modify the original
|
|
752
|
+
* list.
|
|
753
|
+
* @param {Array} values - a list of values
|
|
754
|
+
* @returns {Array} a list of unique values
|
|
755
|
+
*/
|
|
756
|
+
uniqueValues: function uniqueValues(values) {
|
|
757
|
+
return _toConsumableArray(new Set(values));
|
|
641
758
|
}
|
|
642
759
|
};
|
|
643
|
-
var _default =
|
|
760
|
+
var _default = qndUtils;
|
|
644
761
|
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": "
|
|
3
|
+
"version": "1.0.1",
|
|
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",
|