Skip to content

Commit 274d201

Browse files
objType and countObj scripts migrated to avoid conflict of licenses
1 parent 4366d0f commit 274d201

File tree

4 files changed

+55
-19
lines changed

4 files changed

+55
-19
lines changed

package-lock.json

Lines changed: 2 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "for-promise",
3-
"version": "2.0.2",
3+
"version": "2.0.3",
44
"description": "Execute all promises inside a \"for\" script.",
55
"scripts": {
66
"test": "npm run test:mjs && npm run test:cjs && npm run test:js",
@@ -58,8 +58,5 @@
5858
"rollup-preserve-directives": "^1.1.3",
5959
"tslib": "^2.8.1",
6060
"typescript": "^5.8.3"
61-
},
62-
"dependencies": {
63-
"tiny-essentials": "^1.0.0"
6461
}
6562
}

src/index.mjs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Libs
2-
import { objType } from 'tiny-essentials';
3-
import { superValidator, validateTotal } from './utils/essentials.mjs';
2+
import { objType, superValidator, validateTotal } from './utils/essentials.mjs';
43

54
/**
65
* Runs an asynchronous iterative operation with advanced control over flow, breaks, and nested (extra) iterations.

src/utils/essentials.mjs

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { countObj, objType } from 'tiny-essentials';
2-
31
/**
42
* Validates the total value based on the type of the input.
53
*
@@ -74,3 +72,54 @@ export function superValidator(obj) {
7472
// Complete
7573
return result;
7674
}
75+
76+
/**
77+
* Checks the type of a given object or returns its type as a string.
78+
*
79+
* @param {*} obj - The object to check or identify.
80+
* @param {string} [type] - Optional. If provided, checks whether the object matches this type (e.g., "object", "array", "string").
81+
* @returns {boolean|string|null} - Returns `true` if the type matches, `false` if not,
82+
* the type string if no type is provided, or `null` if the object is `undefined`.
83+
*
84+
* @example
85+
* objType([], 'array'); // true
86+
* objType({}, 'object'); // true
87+
* objType('hello'); // "string"
88+
* objType(undefined); // null
89+
*/
90+
export function objType(obj, type) {
91+
// Is Defined
92+
if (typeof obj !== 'undefined') {
93+
// Get Obj Type
94+
const result = Object.prototype.toString.call(obj).toLowerCase();
95+
// Check Obj Type
96+
if (typeof type === 'string') {
97+
if (result === `[object ${type}]`) return true;
98+
return false;
99+
}
100+
// Send Result
101+
return result.substring(8, result.length - 1);
102+
}
103+
// Nope
104+
return null;
105+
}
106+
107+
/**
108+
* Counts the number of elements in an array or the number of properties in an object.
109+
*
110+
* @param {*} obj - The array or object to count.
111+
* @returns {number} - The count of items (array elements or object keys), or `0` if the input is neither an array nor an object.
112+
*
113+
* @example
114+
* countObj([1, 2, 3]); // 3
115+
* countObj({ a: 1, b: 2 }); // 2
116+
* countObj('not an object'); // 0
117+
*/
118+
export function countObj(obj) {
119+
// Is Array
120+
if (Array.isArray(obj)) return obj.length;
121+
// Object
122+
if (objType(obj, 'object')) return Object.keys(obj).length;
123+
// Nothing
124+
return 0;
125+
}

0 commit comments

Comments
 (0)