@punks/backend-core 0.0.71 → 0.0.74

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/dist/cjs/index.js CHANGED
@@ -121,28 +121,28 @@ const notNull = (x) => {
121
121
  return x !== null;
122
122
  };
123
123
  const selectMany = (array, selector) => {
124
- return array.reduce(function (a, b) {
124
+ return (array?.reduce(function (a, b) {
125
125
  return a.concat(selector(b));
126
- }, []);
126
+ }, []) ?? []);
127
127
  };
128
128
  const flatten = (array) => selectMany(array, (x) => x);
129
129
  const distinct = (values) => {
130
- return values.filter((n, i) => values.indexOf(n) === i);
130
+ return values?.filter((n, i) => values.indexOf(n) === i) ?? [];
131
131
  };
132
132
  const distinctElements = (values, comparer) => {
133
- return values.filter((other, i, arr) => arr.findIndex((t) => comparer(t) === comparer(other)) === i);
133
+ return (values?.filter((other, i, arr) => arr.findIndex((t) => comparer(t) === comparer(other)) === i) ?? []);
134
134
  };
135
135
  const jsonDistinct = (values) => {
136
136
  return distinctElements(values, (x) => JSON.stringify(x));
137
137
  };
138
138
  const iterate = (values, action) => {
139
- values.forEach((value, index) => {
139
+ values?.forEach((value, index) => {
140
140
  action(value, values?.[index + 1], values?.[index - 1]);
141
141
  });
142
142
  };
143
143
  const groupBy = (values, keySelector) => {
144
144
  const map = new Map();
145
- values.forEach((value) => {
145
+ values?.forEach((value) => {
146
146
  const key = keySelector(value);
147
147
  const items = map.get(key) || [];
148
148
  items.push(value);
@@ -154,6 +154,53 @@ const subArrays = (array1, array2, eqFn) => {
154
154
  return array1.filter((a1) => !array2.some((a2) => eqFn(a1, a2)));
155
155
  };
156
156
 
157
+ const testConnection = async (url) => {
158
+ try {
159
+ const controller = new AbortController();
160
+ const timeoutId = setTimeout(() => controller.abort(), 30 * 1000);
161
+ const response = await fetch(url, { signal: controller.signal });
162
+ clearTimeout(timeoutId);
163
+ if (response.ok) {
164
+ return {
165
+ status: "success",
166
+ };
167
+ }
168
+ else {
169
+ return {
170
+ status: "error",
171
+ message: `HTTP error: ${response.status}`,
172
+ };
173
+ }
174
+ }
175
+ catch (e) {
176
+ return {
177
+ status: "error",
178
+ message: e.name === "AbortError" ? "Request timed out" : e.message,
179
+ };
180
+ }
181
+ };
182
+ const DEFAULT_HEALTH_CHECK_URLS = [
183
+ "https://www.google.com",
184
+ "https://www.cloudflare.com",
185
+ "https://www.microsoft.com",
186
+ ];
187
+ const testInternetConnection = async (options) => {
188
+ const websitesToCheck = options?.healthCheckUrls ?? DEFAULT_HEALTH_CHECK_URLS;
189
+ const results = await Promise.all(websitesToCheck.map(testConnection));
190
+ const allSuccess = results.every((result) => result.status === "success");
191
+ if (allSuccess) {
192
+ return {
193
+ status: "success",
194
+ };
195
+ }
196
+ else {
197
+ return {
198
+ status: "error",
199
+ details: results,
200
+ };
201
+ }
202
+ };
203
+
157
204
  const addTime = (date, offset) => {
158
205
  const { value, unit } = offset;
159
206
  const newDate = new Date(date);
@@ -277,6 +324,15 @@ const toCamelCase = (str) => `${str[0].toLowerCase()}${str.slice(1)}`;
277
324
  const toTitleCase = (str) => `${str[0].toUpperCase()}${str.slice(1)}`;
278
325
  const ensureTailingSlash = (value) => value?.endsWith("/") ? value : value + "/";
279
326
  const ensureStartSlash = (value) => value?.startsWith("/") ? value : "/" + value;
327
+ const multipleSplit = (str, separators) => {
328
+ const escapeRegExp = (separator) => {
329
+ return separator.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
330
+ };
331
+ const regexPattern = separators
332
+ .map((separator) => escapeRegExp(separator))
333
+ .join("|");
334
+ return str.split(new RegExp(regexPattern));
335
+ };
280
336
 
281
337
  const removeUndefinedProps = (obj, options = { recursive: false }) => {
282
338
  const { recursive } = options;
@@ -384,6 +440,12 @@ const jsonSerialize = (obj, options) => {
384
440
  return JSON.stringify(obj, replacer, spaces);
385
441
  };
386
442
 
443
+ const maskVariable = (value, unmaskedLength = 4) => value
444
+ ? value.substring(0, unmaskedLength).concat(Array.from(value.substring(unmaskedLength))
445
+ .map(() => "*")
446
+ .join(""))
447
+ : "";
448
+
387
449
  const pluralize = (word) => {
388
450
  return word.endsWith("s") ? `${word}es` : `${word}s`;
389
451
  };
@@ -30873,7 +30935,7 @@ const excelBuild = (...sheets) => {
30873
30935
  const sheetContents = [];
30874
30936
  for (const sheet of sheets) {
30875
30937
  sheetContents.push({
30876
- name: sheet.sheetName,
30938
+ name: sheet.sheetName.substring(0, 31),
30877
30939
  data: [
30878
30940
  sheet.columns.map((x) => x.header),
30879
30941
  ...sheet.data.map((d) => Array.from(buildRow(d, sheet))),
@@ -31252,7 +31314,9 @@ exports.last = last;
31252
31314
  exports.logMemoryUsage = logMemoryUsage;
31253
31315
  exports.mapAsync = mapAsync;
31254
31316
  exports.mapOrThrow = mapOrThrow;
31317
+ exports.maskVariable = maskVariable;
31255
31318
  exports.mergeDeep = mergeDeep;
31319
+ exports.multipleSplit = multipleSplit;
31256
31320
  exports.newUuid = newUuid;
31257
31321
  exports.notNull = notNull;
31258
31322
  exports.notUndefined = notUndefined;
@@ -31268,6 +31332,7 @@ exports.sort = sort;
31268
31332
  exports.splitPath = splitPath;
31269
31333
  exports.subArrays = subArrays;
31270
31334
  exports.subtractTime = subtractTime;
31335
+ exports.testInternetConnection = testInternetConnection;
31271
31336
  exports.toArrayDict = toArrayDict;
31272
31337
  exports.toCamelCase = toCamelCase;
31273
31338
  exports.toDict = toDict;