ag-common 0.0.22 → 0.0.23

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.
@@ -0,0 +1,13 @@
1
+ /**
2
+ * run async forEach over all array items
3
+ * @param array
4
+ * @param callback
5
+ */
6
+ export declare function asyncForEach<T>(array: T[], callback: (i: T, index: number, array: T[]) => void): Promise<void>;
7
+ /**
8
+ * run async map over all array items
9
+ * @param array
10
+ * @param callback
11
+ * @returns
12
+ */
13
+ export declare function asyncMap<T, TY>(array: T[], callback: (i: T, index: number, array: T[]) => Promise<TY>): Promise<TY[]>;
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.asyncMap = exports.asyncForEach = void 0;
13
+ /**
14
+ * run async forEach over all array items
15
+ * @param array
16
+ * @param callback
17
+ */
18
+ function asyncForEach(array, callback) {
19
+ return __awaiter(this, void 0, void 0, function* () {
20
+ for (let index = 0; index < array.length; index += 1) {
21
+ yield callback(array[index], index, array);
22
+ }
23
+ });
24
+ }
25
+ exports.asyncForEach = asyncForEach;
26
+ /**
27
+ * run async map over all array items
28
+ * @param array
29
+ * @param callback
30
+ * @returns
31
+ */
32
+ function asyncMap(array, callback) {
33
+ return __awaiter(this, void 0, void 0, function* () {
34
+ const ret = [];
35
+ for (let index = 0; index < array.length; index += 1) {
36
+ ret.push(yield callback(array[index], index, array));
37
+ }
38
+ return ret;
39
+ });
40
+ }
41
+ exports.asyncMap = asyncMap;
@@ -1,4 +1,5 @@
1
1
  export * from './array';
2
+ export * from './async';
2
3
  export * from './date';
3
4
  export * from './distinctBy';
4
5
  export * from './email';
@@ -11,6 +11,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
11
11
  };
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
13
  __exportStar(require("./array"), exports);
14
+ __exportStar(require("./async"), exports);
14
15
  __exportStar(require("./date"), exports);
15
16
  __exportStar(require("./distinctBy"), exports);
16
17
  __exportStar(require("./email"), exports);
@@ -16,3 +16,10 @@ export declare const niceUrl: (siteUrl: string) => ISite | undefined;
16
16
  * @returns
17
17
  */
18
18
  export declare function toTitleCase(str: string): string;
19
+ /**
20
+ * remove all found params from str
21
+ * @param str
22
+ * @param params allows single chars and/or strings
23
+ * @returns
24
+ */
25
+ export declare function replaceRemove(str: string, ...params: string[]): string;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.toTitleCase = exports.niceUrl = exports.truncate = exports.trim = exports.trimSide = exports.csvJSON = void 0;
3
+ exports.replaceRemove = exports.toTitleCase = exports.niceUrl = exports.truncate = exports.trim = exports.trimSide = exports.csvJSON = void 0;
4
4
  const csvJSON = (csv) => {
5
5
  const lines = csv.split('\n');
6
6
  const result = [];
@@ -75,3 +75,37 @@ function toTitleCase(str) {
75
75
  return str.replace(/\w\S*/g, (txt) => txt && txt.charAt(0).toUpperCase() + txt.substring(1).toLowerCase());
76
76
  }
77
77
  exports.toTitleCase = toTitleCase;
78
+ /**
79
+ * remove all found params from str
80
+ * @param str
81
+ * @param params allows single chars and/or strings
82
+ * @returns
83
+ */
84
+ function replaceRemove(str, ...params) {
85
+ const replaceSingles = [];
86
+ const replaceStrings = [];
87
+ params.forEach((p) => {
88
+ if (typeof p !== 'string') {
89
+ throw new Error('trim only supports strings');
90
+ }
91
+ if (p.length === 1) {
92
+ replaceSingles.push(p);
93
+ }
94
+ else {
95
+ replaceStrings.push(p);
96
+ }
97
+ });
98
+ let firstLength = 0;
99
+ let changedLength = 0;
100
+ let ret = str;
101
+ const singleRegex = `[${replaceSingles.join('')}]*`;
102
+ const stringRegex = `(${replaceStrings.map((s) => `(${s})`).join('|')})*`;
103
+ do {
104
+ firstLength = ret.length;
105
+ ret = ret.replace(new RegExp(stringRegex, 'gim'), '');
106
+ ret = ret.replace(new RegExp(singleRegex, 'gim'), '');
107
+ changedLength = ret.length;
108
+ } while (changedLength < firstLength);
109
+ return ret;
110
+ }
111
+ exports.replaceRemove = replaceRemove;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ag-common",
3
- "version": "0.0.22",
3
+ "version": "0.0.23",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "author": "Andrei Gec <@andreigec> (https://gec.dev/)",