@mongez/reinforcements 2.0.5 → 2.0.7

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
@@ -498,6 +498,48 @@ import { flatten } from "@mongez/reinforcements";
498
498
  console.log(flatten(user));
499
499
  ```
500
500
 
501
+ If the object has an instance of class, all class members (except for the functions) will be included in the flattened object.
502
+
503
+ ```ts
504
+ import { Obj } from "@mongez/reinforcements";
505
+
506
+ class User {
507
+ id = 1;
508
+ name = "Hasan Zohdy";
509
+ email = "",
510
+ job = {
511
+ title: "Software Engineer",
512
+ };
513
+ address = {
514
+ country: "Egypt",
515
+ building: {
516
+ number: 12,
517
+ floor: {
518
+ number: 3,
519
+ },
520
+ },
521
+ };
522
+ }
523
+
524
+ const user = new User();
525
+
526
+ console.log(Obj.flatten(user));
527
+ ```
528
+
529
+ Output:
530
+
531
+ ```json
532
+ {
533
+ "id": 1,
534
+ "name": "Hasan Zohdy",
535
+ "email": "",
536
+ "job.title": "Software Engineer",
537
+ "address.country": "Egypt",
538
+ "address.building.number": 12,
539
+ "address.building.floor.number": 3
540
+ }
541
+ ```
542
+
501
543
  ### Sort object by its keys
502
544
 
503
545
  To sort objects based on their keys alphabets recursively use `Obj.sort(object: object, recursive: boolean = true): object` function.
package/cjs/index.d.ts CHANGED
@@ -43,6 +43,7 @@ export { default as rtrim } from "./string/rtrim";
43
43
  export { ARABIC_PATTERN, default as startsWithArabic, } from "./string/startsWithArabic";
44
44
  export { default as toCamelCase } from "./string/toCamelCase";
45
45
  export { default as toInputName } from "./string/toInputName";
46
+ export { default as toKebabCase } from "./string/toKebabCase";
46
47
  export { default as toSnakeCase } from "./string/toSnakeCase";
47
48
  export { default as toStudlyCase } from "./string/toStudlyCase";
48
49
  export { default as trim } from "./string/trim";
package/cjs/index.js CHANGED
@@ -47,6 +47,7 @@ var rtrim = require("./string/rtrim.js");
47
47
  var startsWithArabic = require("./string/startsWithArabic.js");
48
48
  var toCamelCase = require("./string/toCamelCase.js");
49
49
  var toInputName = require("./string/toInputName.js");
50
+ var toKebabCase = require("./string/toKebabCase.js");
50
51
  var toSnakeCase = require("./string/toSnakeCase.js");
51
52
  var toStudlyCase = require("./string/toStudlyCase.js");
52
53
  var trim = require("./string/trim.js");
@@ -102,6 +103,7 @@ exports.ARABIC_PATTERN = startsWithArabic.ARABIC_PATTERN;
102
103
  exports.startsWithArabic = startsWithArabic["default"];
103
104
  exports.toCamelCase = toCamelCase;
104
105
  exports.toInputName = toInputName;
106
+ exports.toKebabCase = toKebabCase;
105
107
  exports.toSnakeCase = toSnakeCase;
106
108
  exports.toStudlyCase = toStudlyCase;
107
109
  exports.trim = trim;
@@ -5,6 +5,24 @@ var tslib_es6 = require('./../__helpers/tslib/tslib.es6.js');
5
5
  function canBeFlatten(value) {
6
6
  return value !== null && typeof value === "object";
7
7
  }
8
+ function toPlainObject(object) {
9
+ if (Array.isArray(object)) {
10
+ return object.map(function (item) {
11
+ return toPlainObject(item);
12
+ });
13
+ }
14
+ if (canBeFlatten(object)) {
15
+ var clonedObject = Object.keys(object).reduce(function (acc, key) {
16
+ if (typeof object[key] === "function") return acc;
17
+ acc[key] = toPlainObject(object[key]);
18
+ return acc;
19
+ }, {});
20
+ // clone the prototype
21
+ clonedObject.__proto__ = object.__proto__;
22
+ return clonedObject;
23
+ }
24
+ return object;
25
+ }
8
26
  /**
9
27
  * Flatten the given object into one big fat object
10
28
  */
@@ -19,7 +37,10 @@ function flatten(object, separator, keepNestedOriginalObject, parent, root) {
19
37
  if (root === void 0) {
20
38
  root = {};
21
39
  }
22
- if (canBeFlatten(object) === false) return object;
40
+ if (canBeFlatten(object) === false) {
41
+ return object;
42
+ }
43
+ object = toPlainObject(object);
23
44
  try {
24
45
  for (
25
46
  var _b = tslib_es6.__values(Object.keys(object)), _c = _b.next();
@@ -31,9 +52,9 @@ function flatten(object, separator, keepNestedOriginalObject, parent, root) {
31
52
  var keyChain = parent ? parent + separator + key : key;
32
53
  if (Array.isArray(value) && value.length === 0) {
33
54
  root[keyChain] = value;
34
- } else if (canBeFlatten(value) === true) {
55
+ } else if (canBeFlatten(value)) {
35
56
  if (keepNestedOriginalObject) {
36
- root[keyChain] = value; // add the original object/array as well
57
+ root[keyChain] = value;
37
58
  }
38
59
  flatten(value, separator, keepNestedOriginalObject, keyChain, root);
39
60
  } else {
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+
3
+ var toSnakeCase = require("./toSnakeCase.js");
4
+
5
+ /**
6
+ * Convert current string to kebab case
7
+ */
8
+ function toKebabCase(string, lowerAll) {
9
+ if (lowerAll === void 0) {
10
+ lowerAll = true;
11
+ }
12
+ return toSnakeCase(string, "-", lowerAll);
13
+ }
14
+
15
+ module.exports = toKebabCase;
package/esm/index.d.ts CHANGED
@@ -43,6 +43,7 @@ export { default as rtrim } from "./string/rtrim";
43
43
  export { ARABIC_PATTERN, default as startsWithArabic, } from "./string/startsWithArabic";
44
44
  export { default as toCamelCase } from "./string/toCamelCase";
45
45
  export { default as toInputName } from "./string/toInputName";
46
+ export { default as toKebabCase } from "./string/toKebabCase";
46
47
  export { default as toSnakeCase } from "./string/toSnakeCase";
47
48
  export { default as toStudlyCase } from "./string/toStudlyCase";
48
49
  export { default as trim } from "./string/trim";
package/esm/index.js CHANGED
@@ -49,6 +49,7 @@ export {
49
49
  } from "./string/startsWithArabic.js";
50
50
  export { default as toCamelCase } from "./string/toCamelCase.js";
51
51
  export { default as toInputName } from "./string/toInputName.js";
52
+ export { default as toKebabCase } from "./string/toKebabCase.js";
52
53
  export { default as toSnakeCase } from "./string/toSnakeCase.js";
53
54
  export { default as toStudlyCase } from "./string/toStudlyCase.js";
54
55
  export { default as trim } from "./string/trim.js";
@@ -3,6 +3,24 @@ import { __values } from './../__helpers/tslib/tslib.es6.js';
3
3
  function canBeFlatten(value) {
4
4
  return value !== null && typeof value === "object";
5
5
  }
6
+ function toPlainObject(object) {
7
+ if (Array.isArray(object)) {
8
+ return object.map(function (item) {
9
+ return toPlainObject(item);
10
+ });
11
+ }
12
+ if (canBeFlatten(object)) {
13
+ var clonedObject = Object.keys(object).reduce(function (acc, key) {
14
+ if (typeof object[key] === "function") return acc;
15
+ acc[key] = toPlainObject(object[key]);
16
+ return acc;
17
+ }, {});
18
+ // clone the prototype
19
+ clonedObject.__proto__ = object.__proto__;
20
+ return clonedObject;
21
+ }
22
+ return object;
23
+ }
6
24
  /**
7
25
  * Flatten the given object into one big fat object
8
26
  */
@@ -17,7 +35,10 @@ function flatten(object, separator, keepNestedOriginalObject, parent, root) {
17
35
  if (root === void 0) {
18
36
  root = {};
19
37
  }
20
- if (canBeFlatten(object) === false) return object;
38
+ if (canBeFlatten(object) === false) {
39
+ return object;
40
+ }
41
+ object = toPlainObject(object);
21
42
  try {
22
43
  for (
23
44
  var _b = __values(Object.keys(object)), _c = _b.next();
@@ -29,9 +50,9 @@ function flatten(object, separator, keepNestedOriginalObject, parent, root) {
29
50
  var keyChain = parent ? parent + separator + key : key;
30
51
  if (Array.isArray(value) && value.length === 0) {
31
52
  root[keyChain] = value;
32
- } else if (canBeFlatten(value) === true) {
53
+ } else if (canBeFlatten(value)) {
33
54
  if (keepNestedOriginalObject) {
34
- root[keyChain] = value; // add the original object/array as well
55
+ root[keyChain] = value;
35
56
  }
36
57
  flatten(value, separator, keepNestedOriginalObject, keyChain, root);
37
58
  } else {
@@ -0,0 +1,13 @@
1
+ import toSnakeCase from "./toSnakeCase.js";
2
+
3
+ /**
4
+ * Convert current string to kebab case
5
+ */
6
+ function toKebabCase(string, lowerAll) {
7
+ if (lowerAll === void 0) {
8
+ lowerAll = true;
9
+ }
10
+ return toSnakeCase(string, "-", lowerAll);
11
+ }
12
+
13
+ export { toKebabCase as default };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mongez/reinforcements",
3
- "version": "2.0.5",
3
+ "version": "2.0.7",
4
4
  "description": "A lightweight package to give a massive reinforcements to variant types of data in Nodejs/Javascript",
5
5
  "main": "./cjs/index.js",
6
6
  "dependencies": {