@nka212bg/backend-utils 0.1.13 → 0.1.15

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.
Files changed (2) hide show
  1. package/misc.js +74 -1
  2. package/package.json +1 -1
package/misc.js CHANGED
@@ -481,7 +481,8 @@ exports.strToEmailsArray = (emailString) => {
481
481
  ...new Set(
482
482
  String(emailString)
483
483
  .toLowerCase()
484
- .match(/\w[\w\.-]+\w@\w+((\.\w+){1,})+\w+/gi)
484
+ // .match(/\w[\w\.-]+\w@\w+((\.\w+){1,})+\w+/gi)
485
+ .match(/\w[\w\.-]+\w@[\w\.-]+((\.\w+){1,})+\w+/g)
485
486
  ),
486
487
  ].sort();
487
488
  };
@@ -512,3 +513,75 @@ exports.iterateObject = (obj, callback) => {
512
513
  }
513
514
  }
514
515
  };
516
+
517
+ exports.flattenObject = (obj, parentKey = "", result = {}) => {
518
+ for (let key in obj) {
519
+ const newKey = parentKey ? `${parentKey}.${key}` : key;
520
+ if (typeof obj[key] === "object" && obj[key] !== null) {
521
+ this.flattenObject(obj[key], newKey, result);
522
+ } else {
523
+ result[newKey] = obj[key];
524
+ }
525
+ }
526
+ return result;
527
+ };
528
+
529
+ exports.unflattenObject = (obj) => {
530
+ let result = {};
531
+
532
+ for (const key in obj) {
533
+ if (obj.hasOwnProperty(key)) {
534
+ const keys = key.split(".");
535
+ keys.reduce((acc, part, index) => {
536
+ if (index === keys.length - 1) {
537
+ acc[part] = obj[key];
538
+ } else {
539
+ acc[part] = acc[part] || {};
540
+ }
541
+ return acc[part];
542
+ }, result);
543
+ }
544
+ }
545
+
546
+ return result;
547
+ };
548
+
549
+ exports.mergeObjects = (oldObj, newObj) => {
550
+ const tempOldObj = this.flattenObject(oldObj);
551
+ const tempNewObj = this.flattenObject(newObj);
552
+
553
+ for (const key in tempNewObj) {
554
+ if (isNaN(tempNewObj[key])) {
555
+ tempOldObj[key] = tempNewObj[key];
556
+ continue;
557
+ }
558
+ tempOldObj[key] = (Number(tempOldObj[key]) || 0) + (Number(tempNewObj[key]) || 1);
559
+ }
560
+
561
+ return this.unflattenObject(tempOldObj);
562
+ };
563
+
564
+ exports.deepJsonParse = (obj) => {
565
+ try {
566
+ obj = JSON.parse(obj);
567
+ } catch {}
568
+ if (!obj || typeof obj != "object") return obj;
569
+
570
+ for (const key in obj) {
571
+ try {
572
+ obj[key] = JSON.parse(obj[key]);
573
+ } catch {}
574
+ if (!obj[key] || typeof obj[key] != "object") continue;
575
+ obj[key] = this.deepJsonParse(obj[key]);
576
+ }
577
+
578
+ return obj;
579
+ };
580
+
581
+ exports.shortenText = (text, { maxLength = 100, filler = "[-]" } = {}) => {
582
+ if (text.length <= maxLength) {
583
+ return text;
584
+ }
585
+ const halfLength = Math.floor((maxLength - 3) / 2);
586
+ return text.slice(0, halfLength) + filler + text.slice(-halfLength);
587
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@nka212bg/backend-utils",
3
3
  "author": "nka212bg",
4
- "version": "0.1.13",
4
+ "version": "0.1.15",
5
5
  "main": "index.js",
6
6
  "dependencies": {
7
7
  "@maxmind/geoip2-node": "^5.0.0",