quick-n-dirty-utils 0.0.14 → 0.0.15
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 +44 -0
- package/dist/util.js +68 -6
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -218,6 +218,43 @@ fetch("http://myurl.com", {
|
|
|
218
218
|
})
|
|
219
219
|
```
|
|
220
220
|
|
|
221
|
+
|
|
222
|
+
#### React State Handlers
|
|
223
|
+
|
|
224
|
+
When a React component uses a state variable to store a list of items any create/update/delete operation
|
|
225
|
+
will have to modify that state variable. To help integrate new/updated items or remove items, the
|
|
226
|
+
following 2 functions are available:
|
|
227
|
+
|
|
228
|
+
```javascript
|
|
229
|
+
import util from "quick-n-dirty-utils"
|
|
230
|
+
import React from "react"
|
|
231
|
+
|
|
232
|
+
class MyComp extends React.Component {
|
|
233
|
+
constructor(props) {
|
|
234
|
+
super(props)
|
|
235
|
+
this.state = { myList: [] }
|
|
236
|
+
}
|
|
237
|
+
saveItem(item) {
|
|
238
|
+
// doesn't matter if add or update
|
|
239
|
+
this.setState(oldState => ({
|
|
240
|
+
...oldState,
|
|
241
|
+
myList: util.integrateDbItem(oldState.myList, item),
|
|
242
|
+
}))
|
|
243
|
+
}
|
|
244
|
+
deleteItem(itemId) {
|
|
245
|
+
this.setState(oldState => ({
|
|
246
|
+
...oldState,
|
|
247
|
+
myList: util.removeDbItem(oldState.myList, itemId),
|
|
248
|
+
}))
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
Both functions will use the MongoDB convention and use the JSON key `_id` to match items. You can
|
|
254
|
+
override that by providing a 3rd parameter: `util.integrateDbItem(oldState.myList, item, "uid")`
|
|
255
|
+
and `util.removeDbItem(oldState.myList, itemId, "uid")` if for example your JSON key holding the
|
|
256
|
+
unique ID is called `uid`.
|
|
257
|
+
|
|
221
258
|
#### Login
|
|
222
259
|
|
|
223
260
|
The login functions (including `getAuthJsonHeader()` and `getAuthHeader()`) assume that you use the browser's
|
|
@@ -355,6 +392,13 @@ The colors are defaulted to blue (1.0) -> white (0.5) -> red (0.0) and are provi
|
|
|
355
392
|
|
|
356
393
|
Static field providing a `colors` setting for the `getTriColor(..)` function for red -> yellow -> green.
|
|
357
394
|
|
|
395
|
+
#### `hexToRgb(hexValue, alpha)`
|
|
396
|
+
|
|
397
|
+
Converts a colour provided as hexadecimal string into an RGB string, like `rgb(255, 255, 255)` that can
|
|
398
|
+
be used in CSS. It allows an optional `alpha` parameter (default `null`), which will create a string
|
|
399
|
+
like `rgba(255, 255, 255, 0.3)` for an `alpha = 0.3`. The `hexValue` can be provided with or without the
|
|
400
|
+
`#` prefix.
|
|
401
|
+
|
|
358
402
|
### Sorting
|
|
359
403
|
|
|
360
404
|
This library introduces a way to sort lists. It has capabilties to sort in ascending or descending order
|
package/dist/util.js
CHANGED
|
@@ -430,9 +430,14 @@ var util = {
|
|
|
430
430
|
|
|
431
431
|
/**
|
|
432
432
|
* Sums up all the values in the provided list
|
|
433
|
-
* @param {Array} list
|
|
433
|
+
* @param {Array} list - a list of numbers
|
|
434
|
+
* @returns {Number} the sum of all the items in the list or 0 if the list is empty.
|
|
434
435
|
*/
|
|
435
436
|
sum: function sum(list) {
|
|
437
|
+
if (list.length === 0) {
|
|
438
|
+
return 0;
|
|
439
|
+
}
|
|
440
|
+
|
|
436
441
|
return list.reduce(function (a, b) {
|
|
437
442
|
return a + b;
|
|
438
443
|
}, 0);
|
|
@@ -457,9 +462,9 @@ var util = {
|
|
|
457
462
|
|
|
458
463
|
/**
|
|
459
464
|
* Returns an inversed JSON object, where every value becomes the key and maps to its original key.
|
|
460
|
-
* @param {object} jsonObject - a flat JSON object, with simple keys and simple values (boolean,
|
|
465
|
+
* @param {object} jsonObject - a flat JSON object, with simple keys and simple values (boolean,
|
|
461
466
|
* string, number are supported)
|
|
462
|
-
* @param {boolean} showWarning - optional flag to print out console warnings in case any key/value
|
|
467
|
+
* @param {boolean} showWarning - optional flag to print out console warnings in case any key/value
|
|
463
468
|
* pair cannot be mapped or if the JSON object contains duplicates - default = true
|
|
464
469
|
* @returns {object} a JSON object which maps from each value of the input to the key.
|
|
465
470
|
*/
|
|
@@ -540,12 +545,13 @@ var util = {
|
|
|
540
545
|
},
|
|
541
546
|
|
|
542
547
|
/**
|
|
543
|
-
* A function that will map a list of items to a JSON object which contains keys extracted from each item
|
|
548
|
+
* A function that will map a list of items to a JSON object which contains keys extracted from each item
|
|
544
549
|
* and the value is the object from that list with that key. If multiple values map to the same keys an array
|
|
545
550
|
* of objects will be mapped to that key.
|
|
546
551
|
* @param {Array} list - a list of items
|
|
547
|
-
* @param {string|function} - a string key (can contain . for nested levels) or a lambda that does
|
|
548
|
-
* for each item.
|
|
552
|
+
* @param {string|function} keyOrFunction - a string key (can contain . for nested levels) or a lambda that does
|
|
553
|
+
* the extraction for each item.
|
|
554
|
+
* @returns {Object} a json object mapping from key to an item or list of items
|
|
549
555
|
*/
|
|
550
556
|
mapListToKeyObject: function mapListToKeyObject(list, keyOrFunction) {
|
|
551
557
|
// check if it's a simple key mapping or lambda
|
|
@@ -576,6 +582,62 @@ var util = {
|
|
|
576
582
|
}
|
|
577
583
|
});
|
|
578
584
|
return result;
|
|
585
|
+
},
|
|
586
|
+
|
|
587
|
+
/**
|
|
588
|
+
* Adds or updates an item in a list of items and returns the updated list. This is useful for React state updates.
|
|
589
|
+
* @param {Array} list - a list of items
|
|
590
|
+
* @param {Object} item - a JSON object to be added to the list or updated, if it already exists
|
|
591
|
+
* @param {string} idKey - the JSON key pointing to the element ID of the item and items in the list
|
|
592
|
+
* @returns {Array} the updated array with the provided `item` either added or updated.
|
|
593
|
+
*/
|
|
594
|
+
integrateDbItem: function integrateDbItem(list, item) {
|
|
595
|
+
var idKey = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : "_id";
|
|
596
|
+
var index = list.map(function (i) {
|
|
597
|
+
return i[idKey];
|
|
598
|
+
}).indexOf(item[idKey]);
|
|
599
|
+
|
|
600
|
+
if (index === -1) {
|
|
601
|
+
list.push(item);
|
|
602
|
+
} else {
|
|
603
|
+
list[index] = item;
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
return list;
|
|
607
|
+
},
|
|
608
|
+
|
|
609
|
+
/**
|
|
610
|
+
* Removes an item from a list by referring to the unique item id. This is a simple id filter. Useful for React state updates.
|
|
611
|
+
* @param {Array} list - a list of items
|
|
612
|
+
* @param {string} itemId - the id of the item to remove
|
|
613
|
+
* @param {string} idKey - the JSON key pointing to the element ID of the item and items in the list
|
|
614
|
+
* @returns {Array} the provided list minus the element with the id provided.
|
|
615
|
+
*/
|
|
616
|
+
removeDbItem: function removeDbItem(list, itemId) {
|
|
617
|
+
var idKey = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : "_id";
|
|
618
|
+
return list.filter(function (item) {
|
|
619
|
+
return item[idKey] !== itemId;
|
|
620
|
+
});
|
|
621
|
+
},
|
|
622
|
+
|
|
623
|
+
/**
|
|
624
|
+
* Translates a hex colour code into an RGB string. If alpha is provided, it will return an RGBA string. The string can be used in CSS styles
|
|
625
|
+
* @param {string} hexValue - the hex value of colour; can be provided with or without the # and as 3 or 6 digit color
|
|
626
|
+
* @param {Number} alpha - optional: if provided an RGBA (transparency) colour will be returned
|
|
627
|
+
* @returns {string} a colour string usable in colour definitions in CSS
|
|
628
|
+
*/
|
|
629
|
+
hexToRgb: function hexToRgb(hexValue) {
|
|
630
|
+
var alpha = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
|
|
631
|
+
var hex = hexValue.replace("#", "");
|
|
632
|
+
var r = parseInt(hex.length === 3 ? hex.slice(0, 1).repeat(2) : hex.slice(0, 2), 16);
|
|
633
|
+
var g = parseInt(hex.length === 3 ? hex.slice(1, 2).repeat(2) : hex.slice(2, 4), 16);
|
|
634
|
+
var b = parseInt(hex.length === 3 ? hex.slice(2, 3).repeat(2) : hex.slice(4, 6), 16);
|
|
635
|
+
|
|
636
|
+
if (alpha != null) {
|
|
637
|
+
return "rgba(".concat(r, ", ").concat(g, ", ").concat(b, ", ").concat(alpha, ")");
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
return "rgb(".concat(r, ", ").concat(g, ", ").concat(b, ")");
|
|
579
641
|
}
|
|
580
642
|
};
|
|
581
643
|
var _default = util;
|