quick-n-dirty-utils 1.0.0 → 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 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)
@@ -42,7 +44,7 @@ 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>
@@ -64,7 +66,7 @@ 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>
@@ -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
+ ```
package/dist/functions.js CHANGED
@@ -7,6 +7,14 @@ exports["default"] = void 0;
7
7
 
8
8
  var _luxon = require("luxon");
9
9
 
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; } }
17
+
10
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); }
11
19
 
12
20
  function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
@@ -114,7 +122,7 @@ var qndUtils = {
114
122
 
115
123
  /**
116
124
  * Uses a hard-coded date/time format to format the provided date. If no valid date is provided, null is returned.
117
- * @param {(object|string)} date: the date to format, provided either as string, Number or Luxon object. If a string
125
+ * @param {(object|string)} date: the date to format, provided either as string, Number or Luxon object. If a string
118
126
  * is provided, that string needs to be parsable by Luxon
119
127
  * @param {string} dateTimeFormat - the Luxon datetime format, defaults to dd/MM/yy T
120
128
  * @returns {String} the formatted string or null, if the provided date string or object is not valid or cannot be
@@ -479,6 +487,19 @@ var qndUtils = {
479
487
  }, 0);
480
488
  },
481
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
+
482
503
  /**
483
504
  * Extracts the query parameter of an URL query string and returns them as JSON object.
484
505
  * @param {string} query - the query string starting with ?, like ?foo=bar&abc=def
@@ -620,6 +641,56 @@ var qndUtils = {
620
641
  return result;
621
642
  },
622
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
+
623
694
  /**
624
695
  * Adds or updates an item in a list of items and returns the updated list. This is useful for React state updates.
625
696
  * @param {Array} list - a list of items
@@ -674,6 +745,16 @@ var qndUtils = {
674
745
  }
675
746
 
676
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));
677
758
  }
678
759
  };
679
760
  var _default = qndUtils;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "quick-n-dirty-utils",
3
- "version": "1.0.0",
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",