quick-n-dirty-utils 1.0.3 → 1.0.5

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
@@ -505,6 +505,40 @@ const items = [
505
505
  ...
506
506
  ```
507
507
 
508
+ #### `groupObjects(objects, key = null, count = false)`
509
+
510
+ Takes a list of objects and calls the provided key(obj) function on each obj in objects and stores the key in a JSON
511
+ object with the value being either a list of all the values matching that key (count = false) or the number of values
512
+ matching the key (if count = true).
513
+
514
+
515
+ ```javascript
516
+ import { util } from "quick-n-dirty-utils"
517
+ const objects = [{axis: "x", value: 5}, {axis: "x", value: 3}, {axis: "y", value: 17}]
518
+ const grouped = util.groupObjects(objects, (obj) => obj.axis, true)
519
+ // returns {x: 2, y: 1}
520
+ const grouped = util.groupObjects(objects, (obj) => obj.axis, false) // don't use count, but return values instead
521
+ // returns {x: [{axis: "x", value: 5}, {axis: "x", value: 3}], y: [{axis: "y", value: 17}]}
522
+ ```
523
+
524
+ #### `sortGrouping(grouping, reverse = true, countKey = "total", countExec = null)`
525
+
526
+ Takes a grouping (see `groupObjects`), which is a JSON object with values either being numbers of arrays. The function
527
+ will convert this JSON object in a list of JSON objects with keys "key", "value" and whatever is specified as `countKey`
528
+
529
+ ```javascript
530
+ import { util } from "quick-n-dirty-utils"
531
+ const objects = [{axis: "x", value: 5}, {axis: "x", value: 3}, {axis: "y", value: 17}]
532
+ const grouping = util.groupObjects(objects, (obj) => obj.axis, false) // returns { x: [..], y: [..] }
533
+
534
+ // sort grouping by the sum of "value" values in each list of items in ascending order and store the sum as "total"
535
+ const sorted = util.sortGrouping(grouping, true, "total", (items) => util.sum(items.map(i => i.value)))
536
+ // returns [
537
+ // {key: "x", value: [{axis: "x", value: 5}, {axis: "x", value: 3}], "total": 8},
538
+ // {key: "y", value: [{axis: "y", value: 17}], "total": 17},
539
+ // ]
540
+ ```
541
+
508
542
  ### Others
509
543
 
510
544
  #### `exportToJson(objectData, filename)`
package/dist/functions.js CHANGED
@@ -64,6 +64,8 @@ var qndUtils = {
64
64
  fulfilled: "_FULFILLED",
65
65
  rejected: "_REJECTED"
66
66
  },
67
+ // sort directions (ASC / DESC)
68
+ SORT_DIRECTIONS: SORT_DIRECTIONS,
67
69
 
68
70
  /**
69
71
  * Uses the hard-coded date format to format the provided date. If no valid date is provided, null is returned.
@@ -225,9 +227,21 @@ var qndUtils = {
225
227
  restHandler: function restHandler(response) {
226
228
  return new Promise(function (resolve, reject) {
227
229
  if (response.status >= 400) {
228
- reject({
229
- status: response.status,
230
- message: response.text()
230
+ response.text().then(function (responseContent) {
231
+ try {
232
+ // try to parse response into json
233
+ var responseJson = JSON.parse(responseContent);
234
+ reject({
235
+ status: response.status,
236
+ body: responseJson
237
+ });
238
+ } catch (err) {
239
+ // json parsing failed, just return as text
240
+ reject({
241
+ status: response.status,
242
+ body: responseContent
243
+ });
244
+ }
231
245
  });
232
246
  return;
233
247
  }
@@ -660,10 +674,12 @@ var qndUtils = {
660
674
  /**
661
675
  * Finds out if an array of values occurs in another array of values. This is useful for filtering lists, which
662
676
  * 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,
677
+ * @param {Array} values - a list of values that you want to check match some items in matchAgainst. If empty,
664
678
  * false will be returned
665
679
  * @param {Array} matchAgainst - a list of selected filters. If the array is empty, false will be returned.
666
680
  * @param {Number} minMatch - the number of items in matchAgainst that have to be in the values.
681
+ * @returns {boolean} a boolean indicating, whether enough items in values match against the list of matchAgainst,
682
+ * where "enough" is determined by the minMatch argument.
667
683
  */
668
684
  arrayMatch: function arrayMatch() {
669
685
  var values = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
@@ -748,13 +764,84 @@ var qndUtils = {
748
764
  },
749
765
 
750
766
  /**
751
- * Returns all unique values in a list of values. All duplicates will be removed. This does not modify the original
767
+ * Returns all unique values in a list of values. All duplicates will be removed. This does not modify the original
752
768
  * list.
753
769
  * @param {Array} values - a list of values
754
770
  * @returns {Array} a list of unique values
755
771
  */
756
772
  uniqueValues: function uniqueValues(values) {
757
773
  return _toConsumableArray(new Set(values));
774
+ },
775
+ groupObjects: function groupObjects(objects) {
776
+ var key = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
777
+ var count = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
778
+
779
+ /**
780
+ * Groups a list of items together by a key determined from the list item and supports counting items as well.
781
+ * @param {Array} objects - a list of values (either simple or complex objects)
782
+ * @param {function} key - an optional function that is used to determine the key from the item. If not
783
+ * provided, the entire item is used as key (this only works if the items contained in the list of objects are
784
+ * valid json keys)
785
+ * @param {boolean} count - a flag indicating whether to collect a list of items or the number of items (true)
786
+ * @returns {object} a json object with the keys as keys and the count (if count is true) or the list of values
787
+ * as values.
788
+ */
789
+ var result = {};
790
+ objects.forEach(function (item) {
791
+ var k = key == null ? item : key(item);
792
+
793
+ if (result[k] == null) {
794
+ result[k] = count ? 0 : [];
795
+ }
796
+
797
+ if (count === true) {
798
+ result[k] += 1;
799
+ } else {
800
+ result[k].push(item);
801
+ }
802
+ });
803
+ return result;
804
+ },
805
+ sortGrouping: function sortGrouping(grouping) {
806
+ var reverse = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
807
+ var countKey = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : "total";
808
+ var countExec = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null;
809
+
810
+ /**
811
+ * Sorts a grouping by a counter/total value determined for each value of the input.
812
+ * @param {object} grouping - a json object with key > [values]
813
+ * @param {boolean} reverse - if true, the items will be sorted from highest to lowest (default).
814
+ * @param {str} countKey - the result will contain a list of json objects, with the countKey providing the json
815
+ * key used to store the total/counter value by which the list is sorted.
816
+ * @param {function} countExec - an optional lambda/function that is used to determine the counter value for
817
+ * each key. The countExec gets one parameter, which is the list of values for each key and the result needs to
818
+ * be numeric.
819
+ * @returns {Array} a list of json objects, where each object contains keys: "key" (storing the key from the
820
+ * grouping), "value" (storing the list of values underneath the key), "total" (or whatever is provided as
821
+ * countKey - storing either the length of the list of values, if countExec is null or the counter value
822
+ * determined by the countExec function)
823
+ */
824
+ var result = [];
825
+ Object.keys(grouping).forEach(function (k) {
826
+ var v = grouping[k];
827
+ var count = v; // default case, if values are numbers
828
+
829
+ if (Array.isArray(v)) {
830
+ // list provided, either measure the length or call the count exec function on the list
831
+ count = countExec == null ? v.length : countExec(v);
832
+ }
833
+
834
+ var resultItem = {
835
+ key: k,
836
+ value: v
837
+ };
838
+ resultItem[countKey] = count;
839
+ result.push(resultItem);
840
+ }); // sorting
841
+
842
+ return result.sort(function (a, b) {
843
+ return reverse === true ? b[countKey] - a[countKey] : a[countKey] - b[countKey];
844
+ });
758
845
  }
759
846
  };
760
847
  var _default = qndUtils;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "quick-n-dirty-utils",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "Little useful nuggets for accelerated web development",
5
5
  "scripts": {
6
6
  "build": "./node_modules/.bin/babel src --out-dir ./dist",