@mongez/reinforcements 2.2.8 → 2.3.1

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
@@ -384,9 +384,11 @@ const user = {
384
384
  },
385
385
  };
386
386
 
387
- const simpleUserData = unset(user, ["id", "address", "email"]); // { name: 'Hasan Zohdy', job: {title: 'Software Engineer'}}
387
+ const unset(user, ["id", "address", "email"]); // { name: 'Hasan Zohdy', job: {title: 'Software Engineer'}}
388
388
  ```
389
389
 
390
+ > The `unset` removes the keys from the object, but it doesn't return a new object, it modifies the original object and returns it, if you want to return a new object, use `except` method.
391
+
390
392
  ### Flatten objects
391
393
 
392
394
  We can flatten any big fat objects into one object, with only one dimension.
@@ -629,7 +631,8 @@ Now let's move to arrays utilities.
629
631
  - [Median](#median): Get the median of all values in an array or by given key.
630
632
  - [Unique](#unique): Get unique values from an array.
631
633
  - [Push Unique](#push-unique): Push a value or more to an array if it doesn't exist.
632
- - [Unshift Unique](#unshift-unique): Add a value or more to the beginning of an array if it doesn't exist.
634
+ - [Unshift Unique](#unshift-unique): Add a value or more to the beginning of an array if it
635
+ doesn't exist.
633
636
 
634
637
  ### Pluck
635
638
 
@@ -674,7 +677,7 @@ Group by a single key:
674
677
  ```ts
675
678
  import { groupBy } from "@mongez/reinforcements";
676
679
 
677
- const studentsClasses = [
680
+ const studentsClasses = [
678
681
  {
679
682
  id: 1,
680
683
  class: "A",
@@ -902,7 +905,7 @@ const studentsClasses = [
902
905
  },
903
906
  ];
904
907
 
905
- console.log(groupBy(studentsClasses, ["class", "grade"], 'students'));
908
+ console.log(groupBy(studentsClasses, ["class", "grade"], "students"));
906
909
  ```
907
910
 
908
911
  Output:
@@ -1013,7 +1016,7 @@ const array = [
1013
1016
  { id: 5, value: 5 },
1014
1017
  ];
1015
1018
 
1016
- console.log(count(array, (item) => item.value > 2)); // 3
1019
+ console.log(count(array, item => item.value > 2)); // 3
1017
1020
  ```
1018
1021
 
1019
1022
  ### Count By
@@ -1026,14 +1029,14 @@ Count total occurrence of values for the given key.
1026
1029
  import { countBy } from "@mongez/reinforcements";
1027
1030
 
1028
1031
  const array = [
1029
- { id: 1, animal: 'dog' },
1030
- { id: 2, animal: 'cat' },
1031
- { id: 3, animal: 'dog' },
1032
- { id: 4, animal: 'cat' },
1033
- { id: 5, animal: 'dog' },
1032
+ { id: 1, animal: "dog" },
1033
+ { id: 2, animal: "cat" },
1034
+ { id: 3, animal: "dog" },
1035
+ { id: 4, animal: "cat" },
1036
+ { id: 5, animal: "dog" },
1034
1037
  ];
1035
1038
 
1036
- console.log(countBy(array, 'animal')); // { dog: 3, cat: 2 }
1039
+ console.log(countBy(array, "animal")); // { dog: 3, cat: 2 }
1037
1040
  ```
1038
1041
 
1039
1042
  ### Even
@@ -1337,6 +1340,26 @@ const array = [1, 2, 3, 4, 5];
1337
1340
  console.log(unshiftUnique(array, 6, 7, 5, 6, 1, 2, 4, 3)); // [7, 6, 1, 2, 3, 4, 5]
1338
1341
  ```
1339
1342
 
1343
+ ### Range
1344
+
1345
+ > Added in v2.3.0
1346
+
1347
+ Generates an array of numbers starting from the given min value to the given max value.
1348
+
1349
+ `range(min: number, max: number): number[]`
1350
+
1351
+ ```ts
1352
+ import { range } from "@mongez/reinforcements";
1353
+
1354
+ console.log(range(1, 6)); // [1, 2, 3, 4, 5, 6]
1355
+
1356
+ console.log(range(3, 5)); // [3, 4, 5]
1357
+ ```
1358
+
1359
+ If the given `min` or `max` parameter is not a number, an error **will be thrown**.
1360
+
1361
+ > Min value must be higher than max value.
1362
+
1340
1363
  ## Working With Strings
1341
1364
 
1342
1365
  The following list defines all available string utilities
@@ -1410,7 +1433,7 @@ import { toSnakeCase } from "@mongez/reinforcements";
1410
1433
 
1411
1434
  const words = "hello world";
1412
1435
 
1413
- console.log(toSnakeCase(words, '-')); // hello-world
1436
+ console.log(toSnakeCase(words, "-")); // hello-world
1414
1437
  ```
1415
1438
 
1416
1439
  Also setting the third argument to false will not convert letters to lower case, will keep each letter as its own.
@@ -1420,7 +1443,7 @@ import { toSnakeCase } from "@mongez/reinforcements";
1420
1443
 
1421
1444
  const words = "Hello World";
1422
1445
 
1423
- console.log(toSnakeCase(words, '-', false)); // Hello_World
1446
+ console.log(toSnakeCase(words, "-", false)); // Hello_World
1424
1447
  ```
1425
1448
 
1426
1449
  ### Kebab Case
@@ -1758,7 +1781,7 @@ import { initials } from "@mongez/reinforcements";
1758
1781
 
1759
1782
  const name = "John Doe";
1760
1783
 
1761
- console.log(initials(name, '.')); // J.D
1784
+ console.log(initials(name, ".")); // J.D
1762
1785
  ```
1763
1786
 
1764
1787
  If the given parameter is not a string it will throw an error.
@@ -1862,11 +1885,7 @@ The section covers general utilities that can be used in any project.
1862
1885
 
1863
1886
  You can debounce your functions using `debounce` to prevent multiple calls.
1864
1887
 
1865
- > This debounce function will be called instantly and will not return a callback function.
1866
-
1867
1888
  ```tsx
1868
- import { debounce } from "@mongez/reinforcements";
1869
-
1870
1889
  function sendEmail(e: any) {
1871
1890
  sendEmailApi(e.target);
1872
1891
  }
@@ -1882,14 +1901,14 @@ Now when using `debounce`
1882
1901
  import { debounce } from "@mongez/reinforcements";
1883
1902
 
1884
1903
  function sendEmail(e: any) {
1885
- debounce(() => {
1886
- sendEmailApi(e.target);
1887
- });
1904
+ sendEmailApi(e.target);
1888
1905
  }
1889
1906
 
1907
+ const debouncedSendEmail = debounce(sendEmail);
1908
+
1890
1909
  // If user clicked 5 times, it will make only one ajax call
1891
1910
 
1892
- <button click={sendEmail}>Send Email</button>;
1911
+ <button click={debouncedSendEmail}>Send Email</button>;
1893
1912
  ```
1894
1913
 
1895
1914
  You can also set a timer when to trigger the function
@@ -1898,15 +1917,14 @@ You can also set a timer when to trigger the function
1898
1917
  import { debounce } from "@mongez/reinforcements";
1899
1918
 
1900
1919
  function sendEmail(e: any) {
1901
- // wait 3 seconds before calling the function
1902
- debounce(() => {
1903
- sendEmailApi(e.target);
1904
- }, 150);
1920
+ sendEmailApi(e.target);
1905
1921
  }
1906
1922
 
1923
+ const debouncedSendEmail = debounce(sendEmail, 1000);
1924
+
1907
1925
  // If user clicked 5 times, it will make only one ajax call
1908
1926
 
1909
- <button click={sendEmail}>Send Email</button>;
1927
+ <button click={debouncedSendEmail}>Send Email</button>;
1910
1928
  ```
1911
1929
 
1912
1930
  ### Escape Regex
@@ -1918,7 +1936,8 @@ Escape regex special characters in the given string.
1918
1936
  ```ts
1919
1937
  import { escapeRegex } from "@mongez/reinforcements";
1920
1938
 
1921
- const string = "This is a string with special characters like: . * + ? ^ $ { } ( ) | [ ] / \\";
1939
+ const string =
1940
+ "This is a string with special characters like: . * + ? ^ $ { } ( ) | [ ] / \\";
1922
1941
 
1923
1942
  console.log(escapeRegex(string)); // This is a string with special characters like: \\. \\* \\+ \\? \\^ \\$ \\{ \\} \\( \\) \\| \\[ \\] / \\\\
1924
1943
  ```
@@ -2038,6 +2057,10 @@ To run tests run `npm run test` or `yarn test`
2038
2057
 
2039
2058
  ## Change Log
2040
2059
 
2060
+ - 2.3.0 (25 Mar 2023)
2061
+ - Added [Range](#range) function.
2062
+ - Exposed typings.
2063
+ - Enhanced tests for `areEqual` function,
2041
2064
  - 2.2.7 (22 Feb 2023)
2042
2065
  - Added [Name Initials](#name-initials) function.
2043
2066
  - 2.2.0 (08 Nov 2022)
@@ -9,7 +9,7 @@ require("../object/merge.js");
9
9
  * The key(s) support dot notation.
10
10
  */
11
11
  function groupBy(array, groupByKey, listAs = "data") {
12
- const newData = {};
12
+ const groupedData = {};
13
13
  const groupByKeys =
14
14
  typeof groupByKey === "string" ? [groupByKey] : groupByKey;
15
15
  for (const item of array) {
@@ -23,20 +23,20 @@ function groupBy(array, groupByKey, listAs = "data") {
23
23
  });
24
24
  }
25
25
  const dataKey = JSON.stringify(baseKeys);
26
- if (!newData[dataKey]) {
26
+ if (!groupedData[dataKey]) {
27
27
  const groupedDataList = {
28
28
  data: [],
29
29
  };
30
30
  for (const groupByKeyData of baseKeys) {
31
31
  groupedDataList[groupByKeyData.key] = groupByKeyData.value;
32
32
  }
33
- newData[dataKey] = groupedDataList;
33
+ groupedData[dataKey] = groupedDataList;
34
34
  }
35
- newData[dataKey]?.data?.push(item);
35
+ groupedData[dataKey]?.data?.push(item);
36
36
  }
37
37
  const finalData = [];
38
- for (const groupedKeyWithValue in newData) {
39
- const { data, ...otherGroupedKeys } = newData[groupedKeyWithValue];
38
+ for (const groupedKeyWithValue in groupedData) {
39
+ const { data, ...otherGroupedKeys } = groupedData[groupedKeyWithValue];
40
40
  finalData.push({
41
41
  ...otherGroupedKeys,
42
42
  [listAs]: data,
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Generate array of numbers starting from the" given min value to the given max value
3
+ */
4
+ export default function range(min: number, max: number): number[];
5
+ //# sourceMappingURL=range.d.ts.map
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+
3
+ /**
4
+ * Generate array of numbers starting from the" given min value to the given max value
5
+ */
6
+ function range(min, max) {
7
+ if (typeof min !== "number") {
8
+ throw new Error(`min parameter should be number, "${typeof min}" given.`);
9
+ }
10
+ if (typeof max !== "number") {
11
+ throw new Error(`max parameter should be number, "${typeof max}" given.`);
12
+ }
13
+ if (min >= max) {
14
+ throw new Error("max parameter should be higher than min parameter");
15
+ }
16
+ const array = [];
17
+ for (let i = min; i <= max; i++) {
18
+ array.push(i);
19
+ }
20
+ return array;
21
+ }
22
+
23
+ module.exports = range;
package/cjs/index.d.ts CHANGED
@@ -12,6 +12,7 @@ export { default as odd } from "./array/odd";
12
12
  export { default as oddIndexes } from "./array/oddIndexes";
13
13
  export { default as pluck } from "./array/pluck";
14
14
  export { default as pushUnique } from "./array/pushUnique";
15
+ export { default as range } from "./array/range";
15
16
  export { default as sum } from "./array/sum";
16
17
  export { default as unique } from "./array/unique";
17
18
  export { default as unshiftUnique } from "./array/unshiftUnique";
@@ -50,6 +51,7 @@ export { default as toSnakeCase } from "./string/toSnakeCase";
50
51
  export { default as toStudlyCase } from "./string/toStudlyCase";
51
52
  export { default as trim } from "./string/trim";
52
53
  export { default as ucfirst } from "./string/ucfirst";
54
+ export * from "./types";
53
55
  export { default as debounce } from "./utils/debounce";
54
56
  export { default as escapeRegex } from "./utils/escapeRegex";
55
57
  //# sourceMappingURL=index.d.ts.map
package/cjs/index.js CHANGED
@@ -16,6 +16,7 @@ var odd = require("./array/odd.js");
16
16
  var oddIndexes = require("./array/oddIndexes.js");
17
17
  var pluck = require("./array/pluck.js");
18
18
  var pushUnique = require("./array/pushUnique.js");
19
+ var range = require("./array/range.js");
19
20
  var sum = require("./array/sum.js");
20
21
  var unique = require("./array/unique.js");
21
22
  var unshiftUnique = require("./array/unshiftUnique.js");
@@ -72,6 +73,7 @@ exports.odd = odd;
72
73
  exports.oddIndexes = oddIndexes;
73
74
  exports.pluck = pluck;
74
75
  exports.pushUnique = pushUnique;
76
+ exports.range = range;
75
77
  exports.sum = sum;
76
78
  exports.unique = unique;
77
79
  exports.unshiftUnique = unshiftUnique;
@@ -1,9 +1,6 @@
1
1
  /**
2
2
  * Convert the current a dot notation string to compound input name
3
3
  * my.input.name >> my[input][name]
4
- *
5
- * @param {string} string
6
- * @returns {string}
7
4
  */
8
5
  export default function toInputName(string: string): string;
9
6
  //# sourceMappingURL=toInputName.d.ts.map
@@ -5,13 +5,9 @@ var rtrim = require("./rtrim.js");
5
5
  /**
6
6
  * Convert the current a dot notation string to compound input name
7
7
  * my.input.name >> my[input][name]
8
- *
9
- * @param {string} string
10
- * @returns {string}
11
8
  */
12
9
  function toInputName(string) {
13
10
  if (!string) return "";
14
- if (!string.includes(".")) return string;
15
11
  const namesList = string.split(".");
16
12
  let mainName = namesList.shift() || "";
17
13
  for (let name of namesList) {
@@ -1,2 +1,9 @@
1
- export default function debounce(callback: () => void, wait?: number): void;
1
+ declare type DebounceFn<T extends (...args: any[]) => any> = (this: ThisParameterType<T>, ...args: Parameters<T>) => void;
2
+ /**
3
+ * Debounce the callback function and return a new function.
4
+ * Example of usage:
5
+ * @example const debounced = debounce(() => console.log('Hello'), 1000);
6
+ */
7
+ export default function debounce<T extends (...args: any[]) => any>(callback: T, time: number): DebounceFn<T>;
8
+ export {};
2
9
  //# sourceMappingURL=debounce.d.ts.map
@@ -1,17 +1,18 @@
1
1
  "use strict";
2
2
 
3
- let timeoutId;
4
- function debounce(callback, wait = 0) {
5
- // Clear previous delayed action, if existent
6
- if (timeoutId !== undefined) {
3
+ /**
4
+ * Debounce the callback function and return a new function.
5
+ * Example of usage:
6
+ * @example const debounced = debounce(() => console.log('Hello'), 1000);
7
+ */
8
+ function debounce(callback, time) {
9
+ let timeoutId;
10
+ return function (...args) {
7
11
  clearTimeout(timeoutId);
8
- timeoutId = undefined;
9
- }
10
- // Start new delayed action for latest call
11
- timeoutId = setTimeout(() => {
12
- callback();
13
- timeoutId = undefined; // Clear timeout
14
- }, wait);
12
+ timeoutId = setTimeout(() => {
13
+ callback.apply(this, args);
14
+ }, time);
15
+ };
15
16
  }
16
17
 
17
18
  module.exports = debounce;
@@ -7,7 +7,7 @@ import "../object/merge.js";
7
7
  * The key(s) support dot notation.
8
8
  */
9
9
  function groupBy(array, groupByKey, listAs = "data") {
10
- const newData = {};
10
+ const groupedData = {};
11
11
  const groupByKeys =
12
12
  typeof groupByKey === "string" ? [groupByKey] : groupByKey;
13
13
  for (const item of array) {
@@ -21,20 +21,20 @@ function groupBy(array, groupByKey, listAs = "data") {
21
21
  });
22
22
  }
23
23
  const dataKey = JSON.stringify(baseKeys);
24
- if (!newData[dataKey]) {
24
+ if (!groupedData[dataKey]) {
25
25
  const groupedDataList = {
26
26
  data: [],
27
27
  };
28
28
  for (const groupByKeyData of baseKeys) {
29
29
  groupedDataList[groupByKeyData.key] = groupByKeyData.value;
30
30
  }
31
- newData[dataKey] = groupedDataList;
31
+ groupedData[dataKey] = groupedDataList;
32
32
  }
33
- newData[dataKey]?.data?.push(item);
33
+ groupedData[dataKey]?.data?.push(item);
34
34
  }
35
35
  const finalData = [];
36
- for (const groupedKeyWithValue in newData) {
37
- const { data, ...otherGroupedKeys } = newData[groupedKeyWithValue];
36
+ for (const groupedKeyWithValue in groupedData) {
37
+ const { data, ...otherGroupedKeys } = groupedData[groupedKeyWithValue];
38
38
  finalData.push({
39
39
  ...otherGroupedKeys,
40
40
  [listAs]: data,
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Generate array of numbers starting from the" given min value to the given max value
3
+ */
4
+ export default function range(min: number, max: number): number[];
5
+ //# sourceMappingURL=range.d.ts.map
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Generate array of numbers starting from the" given min value to the given max value
3
+ */
4
+ function range(min, max) {
5
+ if (typeof min !== "number") {
6
+ throw new Error(`min parameter should be number, "${typeof min}" given.`);
7
+ }
8
+ if (typeof max !== "number") {
9
+ throw new Error(`max parameter should be number, "${typeof max}" given.`);
10
+ }
11
+ if (min >= max) {
12
+ throw new Error("max parameter should be higher than min parameter");
13
+ }
14
+ const array = [];
15
+ for (let i = min; i <= max; i++) {
16
+ array.push(i);
17
+ }
18
+ return array;
19
+ }
20
+
21
+ export { range as default };
package/esm/index.d.ts CHANGED
@@ -12,6 +12,7 @@ export { default as odd } from "./array/odd";
12
12
  export { default as oddIndexes } from "./array/oddIndexes";
13
13
  export { default as pluck } from "./array/pluck";
14
14
  export { default as pushUnique } from "./array/pushUnique";
15
+ export { default as range } from "./array/range";
15
16
  export { default as sum } from "./array/sum";
16
17
  export { default as unique } from "./array/unique";
17
18
  export { default as unshiftUnique } from "./array/unshiftUnique";
@@ -50,6 +51,7 @@ export { default as toSnakeCase } from "./string/toSnakeCase";
50
51
  export { default as toStudlyCase } from "./string/toStudlyCase";
51
52
  export { default as trim } from "./string/trim";
52
53
  export { default as ucfirst } from "./string/ucfirst";
54
+ export * from "./types";
53
55
  export { default as debounce } from "./utils/debounce";
54
56
  export { default as escapeRegex } from "./utils/escapeRegex";
55
57
  //# sourceMappingURL=index.d.ts.map
package/esm/index.js CHANGED
@@ -12,6 +12,7 @@ export { default as odd } from "./array/odd.js";
12
12
  export { default as oddIndexes } from "./array/oddIndexes.js";
13
13
  export { default as pluck } from "./array/pluck.js";
14
14
  export { default as pushUnique } from "./array/pushUnique.js";
15
+ export { default as range } from "./array/range.js";
15
16
  export { default as sum } from "./array/sum.js";
16
17
  export { default as unique } from "./array/unique.js";
17
18
  export { default as unshiftUnique } from "./array/unshiftUnique.js";
@@ -1,9 +1,6 @@
1
1
  /**
2
2
  * Convert the current a dot notation string to compound input name
3
3
  * my.input.name >> my[input][name]
4
- *
5
- * @param {string} string
6
- * @returns {string}
7
4
  */
8
5
  export default function toInputName(string: string): string;
9
6
  //# sourceMappingURL=toInputName.d.ts.map
@@ -3,13 +3,9 @@ import rtrim from "./rtrim.js";
3
3
  /**
4
4
  * Convert the current a dot notation string to compound input name
5
5
  * my.input.name >> my[input][name]
6
- *
7
- * @param {string} string
8
- * @returns {string}
9
6
  */
10
7
  function toInputName(string) {
11
8
  if (!string) return "";
12
- if (!string.includes(".")) return string;
13
9
  const namesList = string.split(".");
14
10
  let mainName = namesList.shift() || "";
15
11
  for (let name of namesList) {
@@ -1,2 +1,9 @@
1
- export default function debounce(callback: () => void, wait?: number): void;
1
+ declare type DebounceFn<T extends (...args: any[]) => any> = (this: ThisParameterType<T>, ...args: Parameters<T>) => void;
2
+ /**
3
+ * Debounce the callback function and return a new function.
4
+ * Example of usage:
5
+ * @example const debounced = debounce(() => console.log('Hello'), 1000);
6
+ */
7
+ export default function debounce<T extends (...args: any[]) => any>(callback: T, time: number): DebounceFn<T>;
8
+ export {};
2
9
  //# sourceMappingURL=debounce.d.ts.map
@@ -1,15 +1,16 @@
1
- let timeoutId;
2
- function debounce(callback, wait = 0) {
3
- // Clear previous delayed action, if existent
4
- if (timeoutId !== undefined) {
1
+ /**
2
+ * Debounce the callback function and return a new function.
3
+ * Example of usage:
4
+ * @example const debounced = debounce(() => console.log('Hello'), 1000);
5
+ */
6
+ function debounce(callback, time) {
7
+ let timeoutId;
8
+ return function (...args) {
5
9
  clearTimeout(timeoutId);
6
- timeoutId = undefined;
7
- }
8
- // Start new delayed action for latest call
9
- timeoutId = setTimeout(() => {
10
- callback();
11
- timeoutId = undefined; // Clear timeout
12
- }, wait);
10
+ timeoutId = setTimeout(() => {
11
+ callback.apply(this, args);
12
+ }, time);
13
+ };
13
14
  }
14
15
 
15
16
  export { debounce as default };
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@mongez/reinforcements",
3
- "version": "2.2.8",
3
+ "version": "2.3.1",
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": {
7
- "@mongez/supportive-is": "^1.0.11"
7
+ "@mongez/supportive-is": "^1.0.12"
8
8
  },
9
9
  "scripts": {
10
10
  "test": "jest ./src",
@@ -17,11 +17,11 @@
17
17
  "url": "https://github.com/hassanzohdy/reinforcements"
18
18
  },
19
19
  "devDependencies": {
20
- "@types/jest": "^28.1.6",
21
- "jest": "^28.1.3",
20
+ "@types/jest": "^29.5.0",
21
+ "jest": "^29.5.0",
22
22
  "jest-esm-jsx-transform": "^1.0.0",
23
- "ts-jest": "^28.0.7",
24
- "typescript": "^4.7.4"
23
+ "ts-jest": "^29.0.5",
24
+ "typescript": "^5.0.2"
25
25
  },
26
26
  "keywords": [
27
27
  "array",