@nka212bg/backend-utils 0.1.12 → 0.1.14

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 +86 -0
  2. package/package.json +1 -1
package/misc.js CHANGED
@@ -498,3 +498,89 @@ exports.handlebars = ({ template, ...params } = {}) => {
498
498
 
499
499
  return template;
500
500
  };
501
+
502
+ exports.iterateObject = (obj, callback) => {
503
+ for (let key in obj) {
504
+ if (Array.isArray(obj[key])) {
505
+ callback({ key, value: obj[key], dataType: "array" });
506
+ this.iterateObject(obj[key], callback);
507
+ } else if (typeof obj[key] === "object" && obj[key] !== null) {
508
+ callback({ key, value: obj[key], dataType: typeof obj[key] });
509
+ this.iterateObject(obj[key], callback);
510
+ } else {
511
+ callback({ key, value: obj[key], dataType: typeof obj[key] });
512
+ }
513
+ }
514
+ };
515
+
516
+ exports.flattenObject = (obj, parentKey = "", result = {}) => {
517
+ for (let key in obj) {
518
+ const newKey = parentKey ? `${parentKey}.${key}` : key;
519
+ if (typeof obj[key] === "object" && obj[key] !== null) {
520
+ this.flattenObject(obj[key], newKey, result);
521
+ } else {
522
+ result[newKey] = obj[key];
523
+ }
524
+ }
525
+ return result;
526
+ };
527
+
528
+ exports.unflattenObject = (obj) => {
529
+ let result = {};
530
+
531
+ for (const key in obj) {
532
+ if (obj.hasOwnProperty(key)) {
533
+ const keys = key.split(".");
534
+ keys.reduce((acc, part, index) => {
535
+ if (index === keys.length - 1) {
536
+ acc[part] = obj[key];
537
+ } else {
538
+ acc[part] = acc[part] || {};
539
+ }
540
+ return acc[part];
541
+ }, result);
542
+ }
543
+ }
544
+
545
+ return result;
546
+ };
547
+
548
+ exports.mergeObjects = (oldObj, newObj) => {
549
+ const tempOldObj = this.flattenObject(oldObj);
550
+ const tempNewObj = this.flattenObject(newObj);
551
+
552
+ for (const key in tempNewObj) {
553
+ if (isNaN(tempNewObj[key])) {
554
+ tempOldObj[key] = tempNewObj[key];
555
+ continue;
556
+ }
557
+ tempOldObj[key] = (Number(tempOldObj[key]) || 0) + (Number(tempNewObj[key]) || 1);
558
+ }
559
+
560
+ return this.unflattenObject(tempOldObj);
561
+ };
562
+
563
+ exports.deepJsonParse = (obj) => {
564
+ try {
565
+ obj = JSON.parse(obj);
566
+ } catch {}
567
+ if (!obj || typeof obj != "object") return obj;
568
+
569
+ for (const key in obj) {
570
+ try {
571
+ obj[key] = JSON.parse(obj[key]);
572
+ } catch {}
573
+ if (!obj[key] || typeof obj[key] != "object") continue;
574
+ obj[key] = this.deepJsonParse(obj[key]);
575
+ }
576
+
577
+ return obj;
578
+ };
579
+
580
+ exports.shortenText = (text, { maxLength = 100, filler = "[-]" } = {}) => {
581
+ if (text.length <= maxLength) {
582
+ return text;
583
+ }
584
+ const halfLength = Math.floor((maxLength - 3) / 2);
585
+ return text.slice(0, halfLength) + filler + text.slice(-halfLength);
586
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@nka212bg/backend-utils",
3
3
  "author": "nka212bg",
4
- "version": "0.1.12",
4
+ "version": "0.1.14",
5
5
  "main": "index.js",
6
6
  "dependencies": {
7
7
  "@maxmind/geoip2-node": "^5.0.0",