@rickard.antonsson/ts-utility 0.0.3 → 0.0.4

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
@@ -1,18 +1,52 @@
1
1
  # ts-utility
2
- simple utility functions
2
+ Simple utility functions
3
3
 
4
4
  ## Array functions
5
+ ### groupBy
6
+
7
+ A small implemententation of groupBy
8
+
9
+ ```typescript
10
+ type specificGroupNames = 'first' | 'second' | 'third';
11
+ const itemsWithSpecificGroups = items as Array<{
12
+ id: number;
13
+ group: specificGroupNames;
14
+ }>;
15
+
16
+ const result = groupBy(itemsWithSpecificGroups, item => item.group);
17
+ // result now contains the grouped arrays.
18
+ // typescript error occurs if you try to access result.noneexisting since its not in the group names
19
+ ```
5
20
 
6
21
  ### simpleSort
7
22
  Sorts an array using supplied functions to get values to sort by.
8
23
  Takes functions returning either strings or numbers.
9
24
 
10
25
  Alternatively functions can return a tuple of [string or number, 'asc' | 'desc'].
26
+ There are shorthand functions for a slightly cleaner look.
11
27
  Otherwise sortOrder will be ascending.
12
28
 
29
+ Example case:
30
+ Sort an array of objects primarily on sortOrder property and secondly on text property descending.
31
+
32
+ ```typescript
33
+ const list: {text:string, sortOrder:number}[] = [...];
34
+ const sortedList = simpleSort(
35
+ list,
36
+ i => i.sortOrder,
37
+ i => desc(i.text)
38
+ ); // sorts by sortOrder ascending and then by text descending
39
+ ```
40
+
41
+ Same result but more explicit:
42
+
13
43
  ```typescript
14
- const list: {text:string, sortOrder:number} = [<...>];
15
- const sortedList = simpleSort(list, i => i.sortOrder, i => [i.text, 'desc']); // sorts by sortOrder ascending and then by text descending
44
+ const list: {text:string, sortOrder:number}[] = [...];
45
+ const sortedList = simpleSort(
46
+ list,
47
+ i => asc(i.sortOrder),
48
+ i => desc(i.text)
49
+ ); // sorts by sortOrder ascending and then by text descending
16
50
  ```
17
51
 
18
52
  sortedList could after this be something like:
@@ -1,3 +1,5 @@
1
+ export declare const asc: <T>(value: T) => [T, "asc"];
2
+ export declare const desc: <T>(value: T) => [T, "desc"];
1
3
  export declare const simpleSort: <T>(list: T[], ...sortByList: (((item: T) => string | [
2
4
  string,
3
5
  'asc' | 'desc'
@@ -1,6 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.simpleSort = void 0;
3
+ exports.simpleSort = exports.desc = exports.asc = void 0;
4
+ const asc = (value) => [value, 'asc'];
5
+ exports.asc = asc;
6
+ const desc = (value) => [value, 'desc'];
7
+ exports.desc = desc;
4
8
  const simpleSort = (list, ...sortByList) => {
5
9
  const sortOrderList = sortByList.map(sortBy => {
6
10
  const value = sortBy(list[0]);
@@ -1 +1 @@
1
- {"version":3,"file":"simple-sort.js","sourceRoot":"","sources":["../../../../src/functions/array/simple-sort.ts"],"names":[],"mappings":";;;AAAO,MAAM,UAAU,GAAG,CACxB,IAAS,EACT,GAAG,UAGF,EACD,EAAE;IACF,MAAM,aAAa,GAAG,UAAU,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE;QAC5C,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;QAC9B,OAAO,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;IACjD,CAAC,CAAC,CAAC;IACH,MAAM,MAAM,GAA4C,IAAI,CAAC,GAAG,CAC9D,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC;QACf,KAAK;QACL,GAAG,UAAU;aACV,GAAG,CAAC,MAAM,CAAC,EAAE;YACZ,MAAM,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC;YAC5B,OAAO,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC;QACpD,CAAC,CAAC;aACD,OAAO,EAAE;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gBAC5D,IAAI,SAAS,KAAK,KAAK;oBAAE,OAAO,MAAM,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC;gBAC7D,OAAO,MAAM,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC;aACrC;YACD,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE;gBAC5D,IAAI,SAAS,KAAK,KAAK;oBAAE,OAAO,MAAM,GAAG,MAAM,CAAC;gBAChD,OAAO,MAAM,GAAG,MAAM,CAAC;aACxB;YACD,OAAO,CAAC,CAAC;QACX,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;IAEH,OAAO,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC;AAC9C,CAAC,CAAC;AAzCW,QAAA,UAAU,cAyCrB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rickard.antonsson/ts-utility",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
4
4
  "description": "",
5
5
  "main": "build/src/index.js",
6
6
  "types": "build/src/index.d.ts",