phane-js-utils 1.0.0

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/index.js ADDED
@@ -0,0 +1 @@
1
+ export * from "./js/index.js";
@@ -0,0 +1,228 @@
1
+ /**
2
+ * Data Check helpers.
3
+ * @module DataTypeCheck
4
+ */
5
+
6
+ /**
7
+ * Check if the user input is a number or not.
8
+ *
9
+ * @function checkIsNumber
10
+ * @memberof module:DataTypeCheck
11
+ *
12
+ * @param {*} userInput - Any value to check
13
+ * @returns {boolean|undefined} Returns true if number, false if not, undefined if input is null/undefined
14
+ *
15
+ * @example
16
+ * checkIsNumber(37) => true
17
+ *
18
+ * checkIsNumber("hello") => false
19
+ *
20
+ * checkIsNumber() => undefined
21
+ *
22
+ * checkIsNumber(null) => undefined
23
+ *
24
+ */
25
+ export function checkIsNumber(userInput) {
26
+ if (userInput === null || userInput === undefined) return undefined;
27
+ return typeof userInput === "number";
28
+ }
29
+
30
+ /**
31
+ * Check if the user input is a string or not.
32
+ *
33
+ * @function checkIsString
34
+ * @memberof module:DataTypeCheck
35
+ *
36
+ * @param {*} userInput - Any value to check
37
+ * @returns {boolean|undefined} Returns true if string, false if not, undefined if input is null/undefined
38
+ *
39
+ * @example
40
+ * checkIsString(37) => false
41
+ *
42
+ * checkIsString("hello") => true
43
+ *
44
+ * checkIsString() => undefined
45
+ *
46
+ * checkIsString(null) => undefined
47
+ *
48
+ */
49
+ export function checkIsString(userInput) {
50
+ if (userInput === null || userInput === undefined) return undefined;
51
+ return typeof userInput === "string";
52
+ }
53
+
54
+ /**
55
+ * Check if the user input is a boolean or not.
56
+ *
57
+ * @function checkIsBoolean
58
+ * @memberof module:DataTypeCheck
59
+ *
60
+ * @param {*} userInput - Any value to check
61
+ * @returns {boolean|undefined} Returns true if boolean, false if not, undefined if input is null/undefined
62
+ *
63
+ * @example
64
+ * checkIsBoolean(37) => false
65
+ *
66
+ * checkIsBoolean("hello") => false
67
+ *
68
+ * checkIsBoolean(false) => true
69
+ *
70
+ * checkIsBoolean() => undefined
71
+ *
72
+ * checkIsBoolean(null) => undefined
73
+ *
74
+ */
75
+ export function checkIsBoolean(userInput) {
76
+ if (userInput === null || userInput === undefined) return undefined;
77
+ return typeof userInput === "boolean";
78
+ }
79
+
80
+ /**
81
+ * Check if the user input is a Bigint or not.
82
+ *
83
+ * @function checkIsBigint
84
+ * @memberof module:DataTypeCheck
85
+ *
86
+ * @param {*} userInput - Any value to check
87
+ * @returns {boolean|undefined} Returns true if bigint, false if not, undefined if input is null/undefined
88
+ *
89
+ * @example
90
+ * checkIsBigint(37n) => true
91
+ *
92
+ * checkIsBigint("hello") => false
93
+ *
94
+ * checkIsBigint(37) => false
95
+ *
96
+ * checkIsBigint() => undefined
97
+ *
98
+ * checkIsBigint(null) => undefined
99
+ */
100
+ export function checkIsBigint(userInput) {
101
+ if (userInput === null || userInput === undefined) return undefined;
102
+ return typeof userInput === "bigint";
103
+ }
104
+
105
+ /**
106
+ * Check if the user input is a function.
107
+ *
108
+ * @function checkIsFunction
109
+ * @memberof module:DataTypeCheck
110
+ *
111
+ * @param {*} userInput - Any value to check
112
+ * @returns {boolean|undefined} Returns true if function, false if not, undefined if input is null/undefined
113
+ *
114
+ * @example
115
+ * checkIsFunction(function() {}) => true
116
+ *
117
+ * checkIsFunction(() => {}) => true
118
+ *
119
+ * checkIsFunction(42) => false
120
+ *
121
+ * checkIsFunction(null) => undefined
122
+ *
123
+ * checkIsFunction() => undefined
124
+ */
125
+ export function checkIsFunction(userInput) {
126
+ if (userInput === null || userInput === undefined) return undefined;
127
+ return typeof userInput === "function";
128
+ }
129
+
130
+ /**
131
+ * Check if the user input is an object.
132
+ *
133
+ * @function checkIsObject
134
+ * @memberof module:DataTypeCheck
135
+ *
136
+ * @param {*} userInput - Any value to check
137
+ * @returns {boolean|undefined} Returns true if object, false if not, undefined if input is null/undefined
138
+ *
139
+ * @example
140
+ * checkIsObject({}) => true
141
+ *
142
+ * checkIsObject({ a: 1 }) => true
143
+ *
144
+ * checkIsObject([]) => true (arrays are objects in JavaScript)
145
+ *
146
+ * checkIsObject(null) => undefined
147
+ *
148
+ * checkIsObject(42) => false
149
+ */
150
+ export function checkIsObject(userInput) {
151
+ if (userInput === null || userInput === undefined) return undefined;
152
+ return typeof userInput === "object";
153
+ }
154
+
155
+ /**
156
+ * Get the number of key-value pairs in an object.
157
+ *
158
+ * @function checkObjectLength
159
+ * @memberof module:DataTypeCheck
160
+ *
161
+ * @param {*} userInput - Any value to check
162
+ * @returns {number|undefined} Number of entries if object, undefined otherwise
163
+ *
164
+ * @example
165
+ * checkObjectLength({ a: 1, b: 2 }) => 2
166
+ *
167
+ * checkObjectLength({}) => 0
168
+ *
169
+ * checkObjectLength([]) => 0 (arrays are objects in JavaScript)
170
+ *
171
+ * checkObjectLength(null) => undefined
172
+ *
173
+ * checkObjectLength(42) => undefined
174
+ */
175
+ export function checkObjectLength(userInput) {
176
+ if (checkIsObject(userInput)) return Object.entries(userInput)?.length;
177
+ return undefined;
178
+ }
179
+
180
+ /**
181
+ * Check if the user input is an array.
182
+ *
183
+ * @function checkIsArray
184
+ * @memberof module:DataTypeCheck
185
+ *
186
+ * @param {*} userInput - Any value to check
187
+ * @returns {boolean|undefined} Returns true if array, false if not, undefined if input is null/undefined
188
+ *
189
+ * @example
190
+ * checkIsArray([1, 2, 3]) => true
191
+ *
192
+ * checkIsArray([]) => true
193
+ *
194
+ * checkIsArray({}) => false
195
+ *
196
+ * checkIsArray(null) => undefined
197
+ *
198
+ * checkIsArray(42) => false
199
+ */
200
+ export function checkIsArray(userInput) {
201
+ if (userInput === null || userInput === undefined) return undefined;
202
+ return Array.isArray(userInput);
203
+ }
204
+
205
+ /**
206
+ * Get the length of an array.
207
+ *
208
+ * @function checkArrayLength
209
+ * @memberof module:DataTypeCheck
210
+ *
211
+ * @param {*} userInput - Any value to check
212
+ * @returns {number|undefined} Length of the array if input is an array, undefined otherwise
213
+ *
214
+ * @example
215
+ * checkArrayLength([1, 2, 3]) => 3
216
+ *
217
+ * checkArrayLength([]) => 0
218
+ *
219
+ * checkArrayLength({}) => undefined
220
+ *
221
+ * checkArrayLength(null) => undefined
222
+ *
223
+ * checkArrayLength(42) => undefined
224
+ */
225
+ export function checkArrayLength(userInput) {
226
+ if (checkIsArray(userInput)) return userInput.length;
227
+ return undefined;
228
+ }
@@ -0,0 +1,163 @@
1
+ import {
2
+ checkIsNumber,
3
+ checkIsString,
4
+ checkIsBoolean,
5
+ checkIsBigint,
6
+ checkIsFunction,
7
+ checkIsObject,
8
+ checkObjectLength,
9
+ checkIsArray,
10
+ checkArrayLength
11
+ } from './dataTypeCheck.js';
12
+
13
+
14
+ describe("DataTypeCheck Helpers - Comprehensive Tests", () => {
15
+
16
+ describe("checkIsNumber", () => {
17
+ // Positive
18
+ test('42 should return true', () => expect(checkIsNumber(42)).toBe(true));
19
+ test('0 should return true', () => expect(checkIsNumber(0)).toBe(true));
20
+ test('-1 should return true', () => expect(checkIsNumber(-1)).toBe(true));
21
+ test('NaN should return true', () => expect(checkIsNumber(NaN)).toBe(true));
22
+ test('Infinity should return true', () => expect(checkIsNumber(Infinity)).toBe(true));
23
+ // Negative
24
+ test('"hello" should return false', () => expect(checkIsNumber("hello")).toBe(false));
25
+ test('true should return false', () => expect(checkIsNumber(true)).toBe(false));
26
+ test('false should return false', () => expect(checkIsNumber(false)).toBe(false));
27
+ test('[] should return false', () => expect(checkIsNumber([])).toBe(false));
28
+ test('{} should return false', () => expect(checkIsNumber({})).toBe(false));
29
+ test('() => {} should return false', () => expect(checkIsNumber(() => {})).toBe(false));
30
+ test('123n should return false', () => expect(checkIsNumber(123n)).toBe(false));
31
+ // Null / Undefined
32
+ test('null should return undefined', () => expect(checkIsNumber(null)).toBeUndefined());
33
+ test('undefined should return undefined', () => expect(checkIsNumber(undefined)).toBeUndefined());
34
+ });
35
+
36
+ describe("checkIsString", () => {
37
+ // Positive
38
+ test('"hello" should return true', () => expect(checkIsString("hello")).toBe(true));
39
+ test('"" should return true', () => expect(checkIsString("")).toBe(true));
40
+ test('"123" should return true', () => expect(checkIsString("123")).toBe(true));
41
+ // Negative
42
+ test('42 should return false', () => expect(checkIsString(42)).toBe(false));
43
+ test('true should return false', () => expect(checkIsString(true)).toBe(false));
44
+ test('false should return false', () => expect(checkIsString(false)).toBe(false));
45
+ test('[] should return false', () => expect(checkIsString([])).toBe(false));
46
+ test('{} should return false', () => expect(checkIsString({})).toBe(false));
47
+ test('() => {} should return false', () => expect(checkIsString(() => {})).toBe(false));
48
+ test('123n should return false', () => expect(checkIsString(123n)).toBe(false));
49
+ // Null / Undefined
50
+ test('null should return undefined', () => expect(checkIsString(null)).toBeUndefined());
51
+ test('undefined should return undefined', () => expect(checkIsString(undefined)).toBeUndefined());
52
+ });
53
+
54
+ describe("checkIsBoolean", () => {
55
+ // Positive
56
+ test('true should return true', () => expect(checkIsBoolean(true)).toBe(true));
57
+ test('false should return true', () => expect(checkIsBoolean(false)).toBe(true));
58
+ // Negative
59
+ test('0 should return false', () => expect(checkIsBoolean(0)).toBe(false));
60
+ test('1 should return false', () => expect(checkIsBoolean(1)).toBe(false));
61
+ test('"true" should return false', () => expect(checkIsBoolean("true")).toBe(false));
62
+ test('[] should return false', () => expect(checkIsBoolean([])).toBe(false));
63
+ test('{} should return false', () => expect(checkIsBoolean({})).toBe(false));
64
+ test('() => {} should return false', () => expect(checkIsBoolean(() => {})).toBe(false));
65
+ test('123n should return false', () => expect(checkIsBoolean(123n)).toBe(false));
66
+ // Null / Undefined
67
+ test('null should return undefined', () => expect(checkIsBoolean(null)).toBeUndefined());
68
+ test('undefined should return undefined', () => expect(checkIsBoolean(undefined)).toBeUndefined());
69
+ });
70
+
71
+ describe("checkIsBigint", () => {
72
+ // Positive
73
+ test('0n should return true', () => expect(checkIsBigint(0n)).toBe(true));
74
+ test('123n should return true', () => expect(checkIsBigint(123n)).toBe(true));
75
+ test('-999n should return true', () => expect(checkIsBigint(-999n)).toBe(true));
76
+ // Negative
77
+ test('42 should return false', () => expect(checkIsBigint(42)).toBe(false));
78
+ test('"123"' , () => expect(checkIsBigint("123")).toBe(false));
79
+ test('true should return false', () => expect(checkIsBigint(true)).toBe(false));
80
+ test('[] should return false', () => expect(checkIsBigint([])).toBe(false));
81
+ test('{} should return false', () => expect(checkIsBigint({})).toBe(false));
82
+ test('() => {} should return false', () => expect(checkIsBigint(() => {})).toBe(false));
83
+ // Null / Undefined
84
+ test('null should return undefined', () => expect(checkIsBigint(null)).toBeUndefined());
85
+ test('undefined should return undefined', () => expect(checkIsBigint(undefined)).toBeUndefined());
86
+ });
87
+
88
+ describe("checkIsFunction", () => {
89
+ // Positive
90
+ test('function declaration should return true', () => expect(checkIsFunction(function() {})).toBe(true));
91
+ test('arrow function should return true', () => expect(checkIsFunction(() => {})).toBe(true));
92
+ test('async function should return true', () => expect(checkIsFunction(async function() {})).toBe(true));
93
+ // Negative
94
+ test('42 should return false', () => expect(checkIsFunction(42)).toBe(false));
95
+ test('"hello" should return false', () => expect(checkIsFunction("hello")).toBe(false));
96
+ test('true should return false', () => expect(checkIsFunction(true)).toBe(false));
97
+ test('[] should return false', () => expect(checkIsFunction([])).toBe(false));
98
+ test('{} should return false', () => expect(checkIsFunction({})).toBe(false));
99
+ test('123n should return false', () => expect(checkIsFunction(123n)).toBe(false));
100
+ // Null / Undefined
101
+ test('null should return undefined', () => expect(checkIsFunction(null)).toBeUndefined());
102
+ test('undefined should return undefined', () => expect(checkIsFunction(undefined)).toBeUndefined());
103
+ });
104
+
105
+ describe("checkIsObject", () => {
106
+ // Positive
107
+ test('{} should return true', () => expect(checkIsObject({})).toBe(true));
108
+ test('{ a: 1 } should return true', () => expect(checkIsObject({ a: 1 })).toBe(true));
109
+ test('[] should return true (arrays are objects)', () => expect(checkIsObject([])).toBe(true));
110
+ // Negative
111
+ test('42 should return false', () => expect(checkIsObject(42)).toBe(false));
112
+ test('"hello" should return false', () => expect(checkIsObject("hello")).toBe(false));
113
+ test('true should return false', () => expect(checkIsObject(true)).toBe(false));
114
+ test('() => {} should return false', () => expect(checkIsObject(() => {})).toBe(false));
115
+ test('123n should return false', () => expect(checkIsObject(123n)).toBe(false));
116
+ // Null / Undefined
117
+ test('null should return undefined', () => expect(checkIsObject(null)).toBeUndefined());
118
+ test('undefined should return undefined', () => expect(checkIsObject(undefined)).toBeUndefined());
119
+ });
120
+
121
+ describe("checkObjectLength", () => {
122
+ // Positive
123
+ test('{} should return 0', () => expect(checkObjectLength({})).toBe(0));
124
+ test('{ a: 1, b: 2 } should return 2', () => expect(checkObjectLength({ a: 1, b: 2 })).toBe(2));
125
+ test('[] should return 0 (array is object)', () => expect(checkObjectLength([])).toBe(0));
126
+ // Negative / Non-object
127
+ test('42 should return undefined', () => expect(checkObjectLength(42)).toBeUndefined());
128
+ test('"hello" should return undefined', () => expect(checkObjectLength("hello")).toBeUndefined());
129
+ test('null should return undefined', () => expect(checkObjectLength(null)).toBeUndefined());
130
+ test('undefined should return undefined', () => expect(checkObjectLength(undefined)).toBeUndefined());
131
+ test('() => {} should return undefined', () => expect(checkObjectLength(() => {})).toBeUndefined());
132
+ });
133
+
134
+ describe("checkIsArray", () => {
135
+ // Positive
136
+ test('[] should return true', () => expect(checkIsArray([])).toBe(true));
137
+ test('[1,2,3] should return true', () => expect(checkIsArray([1,2,3])).toBe(true));
138
+ // Negative
139
+ test('{} should return false', () => expect(checkIsArray({})).toBe(false));
140
+ test('42 should return false', () => expect(checkIsArray(42)).toBe(false));
141
+ test('"hello" should return false', () => expect(checkIsArray("hello")).toBe(false));
142
+ test('true should return false', () => expect(checkIsArray(true)).toBe(false));
143
+ test('123n should return false', () => expect(checkIsArray(123n)).toBe(false));
144
+ // Null / Undefined
145
+ test('null should return undefined', () => expect(checkIsArray(null)).toBeUndefined());
146
+ test('undefined should return undefined', () => expect(checkIsArray(undefined)).toBeUndefined());
147
+ });
148
+
149
+ describe("checkArrayLength", () => {
150
+ // Positive
151
+ test('[] should return 0', () => expect(checkArrayLength([])).toBe(0));
152
+ test('[1,2,3] should return 3', () => expect(checkArrayLength([1,2,3])).toBe(3));
153
+ test('["a", "b"] should return 2', () => expect(checkArrayLength(["a","b"])).toBe(2));
154
+ // Negative / Non-array
155
+ test('{} should return undefined', () => expect(checkArrayLength({})).toBeUndefined());
156
+ test('42 should return undefined', () => expect(checkArrayLength(42)).toBeUndefined());
157
+ test('"hello" should return undefined', () => expect(checkArrayLength("hello")).toBeUndefined());
158
+ test('true should return undefined', () => expect(checkArrayLength(true)).toBeUndefined());
159
+ test('null should return undefined', () => expect(checkArrayLength(null)).toBeUndefined());
160
+ test('undefined should return undefined', () => expect(checkArrayLength(undefined)).toBeUndefined());
161
+ });
162
+
163
+ });
package/js/index.js ADDED
@@ -0,0 +1 @@
1
+ export * from "./js/dataTypeCheck.js";
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "phane-js-utils",
3
+ "version": "1.0.0",
4
+ "description": "Pure JavaScript utility functions",
5
+ "main": "index.js",
6
+ "type": "module",
7
+ "files": [
8
+ "js",
9
+ "doc"
10
+ ],
11
+ "keywords": [
12
+ "javascript",
13
+ "utils",
14
+ "library"
15
+ ],
16
+ "author": "Kotipalli Phaneendra Kumar",
17
+ "license": "MIT",
18
+ "scripts": {
19
+ "docs": "jsdoc -c jsdoc.json"
20
+ },
21
+ "devDependencies": {
22
+ "docdash": "^2.0.2",
23
+ "jest": "^30.2.0",
24
+ "jest-environment-jsdom": "^30.2.0",
25
+ "jsdoc": "^4.0.5",
26
+ "rimraf": "^6.1.2"
27
+ }
28
+ }