@etsoo/shared 1.1.68 → 1.1.69

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
@@ -218,6 +218,8 @@ String and other related utilities
218
218
  |charsToNumber|Base64 chars to number|
219
219
  |correctTypes|Correct object's property value type|
220
220
  |equals|Two values equal|
221
+ |exclude|Exclude specific items|
222
+ |excludeAsync|Async exclude specific items|
221
223
  |formatInitial|Format inital character to lower case or upper case|
222
224
  |formatString|Format string with parameters|
223
225
  |getDataChanges|Get data changed fields with input data updated|
@@ -67,6 +67,16 @@ test('Tests for getDataChanges', () => {
67
67
  expect(input.amount).toBeUndefined();
68
68
  });
69
69
 
70
+ test('Tests for exclude', () => {
71
+ const options = [
72
+ { id1: 1, name: 'a' },
73
+ { id1: 2, name: 'b' }
74
+ ];
75
+ const result = Utils.exclude(options, 'id1', 1);
76
+ expect(result.length).toBe(1);
77
+ expect(result[0].id1).toBe(2);
78
+ });
79
+
70
80
  test('Tests for formatInitial', () => {
71
81
  expect(Utils.formatInitial('HelloWorld')).toBe('helloWorld');
72
82
  expect('HelloWorld'.formatInitial(false)).toBe('helloWorld');
@@ -75,6 +75,26 @@ export declare namespace Utils {
75
75
  * @param strict Strict level, 0 with ==, 1 === but null equal undefined, 2 ===
76
76
  */
77
77
  function equals(v1: unknown, v2: unknown, strict?: number): boolean;
78
+ /**
79
+ * Exclude specific items
80
+ * @param items Items
81
+ * @param field Filter field
82
+ * @param excludedValues Excluded values
83
+ * @returns Result
84
+ */
85
+ function exclude<T extends {
86
+ [P in D]: DataTypes.IdType;
87
+ }, D extends string = 'id'>(items: T[], field: D, ...excludedValues: T[D][]): T[];
88
+ /**
89
+ * Async exclude specific items
90
+ * @param items Items
91
+ * @param field Filter field
92
+ * @param excludedValues Excluded values
93
+ * @returns Result
94
+ */
95
+ function excludeAsync<T extends {
96
+ [P in D]: DataTypes.IdType;
97
+ }, D extends string = 'id'>(items: Promise<T[] | undefined>, field: D, ...excludedValues: T[D][]): Promise<T[] | undefined>;
78
98
  /**
79
99
  * Format inital character to lower case or upper case
80
100
  * @param input Input string
package/lib/cjs/Utils.js CHANGED
@@ -1,4 +1,13 @@
1
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
+ };
2
11
  Object.defineProperty(exports, "__esModule", { value: true });
3
12
  exports.Utils = void 0;
4
13
  const DataTypes_1 = require("./DataTypes");
@@ -127,6 +136,33 @@ var Utils;
127
136
  return v1 === v2;
128
137
  }
129
138
  Utils.equals = equals;
139
+ /**
140
+ * Exclude specific items
141
+ * @param items Items
142
+ * @param field Filter field
143
+ * @param excludedValues Excluded values
144
+ * @returns Result
145
+ */
146
+ function exclude(items, field, ...excludedValues) {
147
+ return items.filter((item) => !excludedValues.includes(item[field]));
148
+ }
149
+ Utils.exclude = exclude;
150
+ /**
151
+ * Async exclude specific items
152
+ * @param items Items
153
+ * @param field Filter field
154
+ * @param excludedValues Excluded values
155
+ * @returns Result
156
+ */
157
+ function excludeAsync(items, field, ...excludedValues) {
158
+ return __awaiter(this, void 0, void 0, function* () {
159
+ const result = yield items;
160
+ if (result == null)
161
+ return result;
162
+ return exclude(result, field, ...excludedValues);
163
+ });
164
+ }
165
+ Utils.excludeAsync = excludeAsync;
130
166
  /**
131
167
  * Format inital character to lower case or upper case
132
168
  * @param input Input string
@@ -75,6 +75,26 @@ export declare namespace Utils {
75
75
  * @param strict Strict level, 0 with ==, 1 === but null equal undefined, 2 ===
76
76
  */
77
77
  function equals(v1: unknown, v2: unknown, strict?: number): boolean;
78
+ /**
79
+ * Exclude specific items
80
+ * @param items Items
81
+ * @param field Filter field
82
+ * @param excludedValues Excluded values
83
+ * @returns Result
84
+ */
85
+ function exclude<T extends {
86
+ [P in D]: DataTypes.IdType;
87
+ }, D extends string = 'id'>(items: T[], field: D, ...excludedValues: T[D][]): T[];
88
+ /**
89
+ * Async exclude specific items
90
+ * @param items Items
91
+ * @param field Filter field
92
+ * @param excludedValues Excluded values
93
+ * @returns Result
94
+ */
95
+ function excludeAsync<T extends {
96
+ [P in D]: DataTypes.IdType;
97
+ }, D extends string = 'id'>(items: Promise<T[] | undefined>, field: D, ...excludedValues: T[D][]): Promise<T[] | undefined>;
78
98
  /**
79
99
  * Format inital character to lower case or upper case
80
100
  * @param input Input string
package/lib/mjs/Utils.js CHANGED
@@ -1,3 +1,12 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
1
10
  import { DataTypes } from './DataTypes';
2
11
  String.prototype.format = function (...parameters) {
3
12
  let template = this;
@@ -124,6 +133,33 @@ export var Utils;
124
133
  return v1 === v2;
125
134
  }
126
135
  Utils.equals = equals;
136
+ /**
137
+ * Exclude specific items
138
+ * @param items Items
139
+ * @param field Filter field
140
+ * @param excludedValues Excluded values
141
+ * @returns Result
142
+ */
143
+ function exclude(items, field, ...excludedValues) {
144
+ return items.filter((item) => !excludedValues.includes(item[field]));
145
+ }
146
+ Utils.exclude = exclude;
147
+ /**
148
+ * Async exclude specific items
149
+ * @param items Items
150
+ * @param field Filter field
151
+ * @param excludedValues Excluded values
152
+ * @returns Result
153
+ */
154
+ function excludeAsync(items, field, ...excludedValues) {
155
+ return __awaiter(this, void 0, void 0, function* () {
156
+ const result = yield items;
157
+ if (result == null)
158
+ return result;
159
+ return exclude(result, field, ...excludedValues);
160
+ });
161
+ }
162
+ Utils.excludeAsync = excludeAsync;
127
163
  /**
128
164
  * Format inital character to lower case or upper case
129
165
  * @param input Input string
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.1.68",
3
+ "version": "1.1.69",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
package/src/Utils.ts CHANGED
@@ -199,6 +199,36 @@ export namespace Utils {
199
199
  return v1 === v2;
200
200
  }
201
201
 
202
+ /**
203
+ * Exclude specific items
204
+ * @param items Items
205
+ * @param field Filter field
206
+ * @param excludedValues Excluded values
207
+ * @returns Result
208
+ */
209
+ export function exclude<
210
+ T extends { [P in D]: DataTypes.IdType },
211
+ D extends string = 'id'
212
+ >(items: T[], field: D, ...excludedValues: T[D][]) {
213
+ return items.filter((item) => !excludedValues.includes(item[field]));
214
+ }
215
+
216
+ /**
217
+ * Async exclude specific items
218
+ * @param items Items
219
+ * @param field Filter field
220
+ * @param excludedValues Excluded values
221
+ * @returns Result
222
+ */
223
+ export async function excludeAsync<
224
+ T extends { [P in D]: DataTypes.IdType },
225
+ D extends string = 'id'
226
+ >(items: Promise<T[] | undefined>, field: D, ...excludedValues: T[D][]) {
227
+ const result = await items;
228
+ if (result == null) return result;
229
+ return exclude(result, field, ...excludedValues);
230
+ }
231
+
202
232
  /**
203
233
  * Format inital character to lower case or upper case
204
234
  * @param input Input string