tiny-essentials 1.18.0 โ†’ 1.18.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.
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Counts the number of elements in an array or the number of properties in an object.
3
+ *
4
+ * @param {Array<*>|Record<string | number | symbol, any>} obj - The array or object to count.
5
+ * @returns {number} - The count of items (array elements or object keys), or `0` if the input is neither an array nor an object.
6
+ *
7
+ * @example
8
+ * countObj([1, 2, 3]); // 3
9
+ * countObj({ a: 1, b: 2 }); // 2
10
+ * countObj('not an object'); // 0
11
+ */
12
+ export function countObj(obj: Array<any> | Record<string | number | symbol, any>): number;
13
+ /**
14
+ * Determines whether a given value is a pure JSON object (plain object).
15
+ *
16
+ * A pure object satisfies the following:
17
+ * - It is not null.
18
+ * - Its type is "object".
19
+ * - Its internal [[Class]] is "[object Object]".
20
+ * - It is not an instance of built-in types like Array, Date, Map, Set, etc.
21
+ *
22
+ * This function is useful for strict data validation when you want to ensure
23
+ * a value is a clean JSON-compatible object, free of class instances or special types.
24
+ *
25
+ * @param {unknown} value - The value to test.
26
+ * @returns {value is Record<string | number | symbol, unknown>} Returns true if the value is a pure object.
27
+ */
28
+ export function isJsonObject(value: unknown): value is Record<string | number | symbol, unknown>;
29
+ //# sourceMappingURL=objChecker.d.mts.map
@@ -0,0 +1,45 @@
1
+ /**
2
+ * Counts the number of elements in an array or the number of properties in an object.
3
+ *
4
+ * @param {Array<*>|Record<string | number | symbol, any>} obj - The array or object to count.
5
+ * @returns {number} - The count of items (array elements or object keys), or `0` if the input is neither an array nor an object.
6
+ *
7
+ * @example
8
+ * countObj([1, 2, 3]); // 3
9
+ * countObj({ a: 1, b: 2 }); // 2
10
+ * countObj('not an object'); // 0
11
+ */
12
+ export function countObj(obj) {
13
+ // Is Array
14
+ if (Array.isArray(obj))
15
+ return obj.length;
16
+ // Object
17
+ if (isJsonObject(obj))
18
+ return Object.keys(obj).length;
19
+ // Nothing
20
+ return 0;
21
+ }
22
+ /**
23
+ * Determines whether a given value is a pure JSON object (plain object).
24
+ *
25
+ * A pure object satisfies the following:
26
+ * - It is not null.
27
+ * - Its type is "object".
28
+ * - Its internal [[Class]] is "[object Object]".
29
+ * - It is not an instance of built-in types like Array, Date, Map, Set, etc.
30
+ *
31
+ * This function is useful for strict data validation when you want to ensure
32
+ * a value is a clean JSON-compatible object, free of class instances or special types.
33
+ *
34
+ * @param {unknown} value - The value to test.
35
+ * @returns {value is Record<string | number | symbol, unknown>} Returns true if the value is a pure object.
36
+ */
37
+ export function isJsonObject(value) {
38
+ if (value === null || typeof value !== 'object')
39
+ return false;
40
+ if (Array.isArray(value))
41
+ return false;
42
+ if (Object.prototype.toString.call(value) !== '[object Object]')
43
+ return false;
44
+ return true;
45
+ }
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  var buffer = require('buffer');
4
+ var objChecker = require('./objChecker.cjs');
4
5
 
5
6
  const isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';
6
7
 
@@ -194,48 +195,6 @@ function getCheckObj() {
194
195
  return Object.fromEntries(Object.entries(typeValidator.items).map(([key, fn]) => [key, fn]));
195
196
  }
196
197
 
197
- /**
198
- * Counts the number of elements in an array or the number of properties in an object.
199
- *
200
- * @param {Array<*>|Record<string|number, any>} obj - The array or object to count.
201
- * @returns {number} - The count of items (array elements or object keys), or `0` if the input is neither an array nor an object.
202
- *
203
- * @example
204
- * countObj([1, 2, 3]); // 3
205
- * countObj({ a: 1, b: 2 }); // 2
206
- * countObj('not an object'); // 0
207
- */
208
- function countObj(obj) {
209
- // Is Array
210
- if (Array.isArray(obj)) return obj.length;
211
- // Object
212
- if (objType(obj, 'object')) return Object.keys(obj).length;
213
- // Nothing
214
- return 0;
215
- }
216
-
217
- /**
218
- * Determines whether a given value is a pure JSON object (plain object).
219
- *
220
- * A pure object satisfies the following:
221
- * - It is not null.
222
- * - Its type is "object".
223
- * - Its internal [[Class]] is "[object Object]".
224
- * - It is not an instance of built-in types like Array, Date, Map, Set, etc.
225
- *
226
- * This function is useful for strict data validation when you want to ensure
227
- * a value is a clean JSON-compatible object, free of class instances or special types.
228
- *
229
- * @param {unknown} value - The value to test.
230
- * @returns {value is Record<string | number | symbol, unknown>} Returns true if the value is a pure object.
231
- */
232
- function isJsonObject(value) {
233
- if (value === null || typeof value !== 'object') return false;
234
- if (Array.isArray(value)) return false;
235
- if (Object.prototype.toString.call(value) !== '[object Object]') return false;
236
- return true;
237
- }
238
-
239
198
  // Insert obj types
240
199
 
241
200
  extendObjType([
@@ -358,15 +317,15 @@ extendObjType([
358
317
  [
359
318
  'object',
360
319
  /** @param {*} val @returns {val is Record<string | number | symbol, unknown>} */
361
- (val) => isJsonObject(val),
320
+ (val) => objChecker.isJsonObject(val),
362
321
  ],
363
322
  ]);
364
323
 
324
+ exports.countObj = objChecker.countObj;
325
+ exports.isJsonObject = objChecker.isJsonObject;
365
326
  exports.checkObj = checkObj;
366
327
  exports.cloneObjTypeOrder = cloneObjTypeOrder;
367
- exports.countObj = countObj;
368
328
  exports.extendObjType = extendObjType;
369
329
  exports.getCheckObj = getCheckObj;
370
- exports.isJsonObject = isJsonObject;
371
330
  exports.objType = objType;
372
331
  exports.reorderObjTypeOrder = reorderObjTypeOrder;
@@ -76,36 +76,11 @@ export function checkObj(obj: any): {
76
76
  export function getCheckObj(): {
77
77
  [k: string]: any;
78
78
  };
79
- /**
80
- * Counts the number of elements in an array or the number of properties in an object.
81
- *
82
- * @param {Array<*>|Record<string|number, any>} obj - The array or object to count.
83
- * @returns {number} - The count of items (array elements or object keys), or `0` if the input is neither an array nor an object.
84
- *
85
- * @example
86
- * countObj([1, 2, 3]); // 3
87
- * countObj({ a: 1, b: 2 }); // 2
88
- * countObj('not an object'); // 0
89
- */
90
- export function countObj(obj: Array<any> | Record<string | number, any>): number;
91
- /**
92
- * Determines whether a given value is a pure JSON object (plain object).
93
- *
94
- * A pure object satisfies the following:
95
- * - It is not null.
96
- * - Its type is "object".
97
- * - Its internal [[Class]] is "[object Object]".
98
- * - It is not an instance of built-in types like Array, Date, Map, Set, etc.
99
- *
100
- * This function is useful for strict data validation when you want to ensure
101
- * a value is a clean JSON-compatible object, free of class instances or special types.
102
- *
103
- * @param {unknown} value - The value to test.
104
- * @returns {value is Record<string | number | symbol, unknown>} Returns true if the value is a pure object.
105
- */
106
- export function isJsonObject(value: unknown): value is Record<string | number | symbol, unknown>;
107
79
  export type ExtendObjType = {
108
80
  [x: string]: (val: any) => any;
109
81
  };
110
82
  export type ExtendObjTypeArray = Array<[string, (val: any) => any]>;
83
+ import { countObj } from './objChecker.mjs';
84
+ import { isJsonObject } from './objChecker.mjs';
85
+ export { countObj, isJsonObject };
111
86
  //# sourceMappingURL=objFilter.d.mts.map
@@ -1,4 +1,6 @@
1
1
  import { Buffer } from 'buffer';
2
+ import { countObj, isJsonObject } from './objChecker.mjs';
3
+ export { countObj, isJsonObject };
2
4
  const isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';
3
5
  /**
4
6
  * An object containing type validation functions and their evaluation order.
@@ -176,51 +178,6 @@ export function checkObj(obj) {
176
178
  export function getCheckObj() {
177
179
  return Object.fromEntries(Object.entries(typeValidator.items).map(([key, fn]) => [key, fn]));
178
180
  }
179
- /**
180
- * Counts the number of elements in an array or the number of properties in an object.
181
- *
182
- * @param {Array<*>|Record<string|number, any>} obj - The array or object to count.
183
- * @returns {number} - The count of items (array elements or object keys), or `0` if the input is neither an array nor an object.
184
- *
185
- * @example
186
- * countObj([1, 2, 3]); // 3
187
- * countObj({ a: 1, b: 2 }); // 2
188
- * countObj('not an object'); // 0
189
- */
190
- export function countObj(obj) {
191
- // Is Array
192
- if (Array.isArray(obj))
193
- return obj.length;
194
- // Object
195
- if (objType(obj, 'object'))
196
- return Object.keys(obj).length;
197
- // Nothing
198
- return 0;
199
- }
200
- /**
201
- * Determines whether a given value is a pure JSON object (plain object).
202
- *
203
- * A pure object satisfies the following:
204
- * - It is not null.
205
- * - Its type is "object".
206
- * - Its internal [[Class]] is "[object Object]".
207
- * - It is not an instance of built-in types like Array, Date, Map, Set, etc.
208
- *
209
- * This function is useful for strict data validation when you want to ensure
210
- * a value is a clean JSON-compatible object, free of class instances or special types.
211
- *
212
- * @param {unknown} value - The value to test.
213
- * @returns {value is Record<string | number | symbol, unknown>} Returns true if the value is a pure object.
214
- */
215
- export function isJsonObject(value) {
216
- if (value === null || typeof value !== 'object')
217
- return false;
218
- if (Array.isArray(value))
219
- return false;
220
- if (Object.prototype.toString.call(value) !== '[object Object]')
221
- return false;
222
- return true;
223
- }
224
181
  // Insert obj types
225
182
  extendObjType([
226
183
  [
package/dist/v1/index.cjs CHANGED
@@ -6,6 +6,7 @@ var arraySortPositions = require('../legacy/libs/arraySortPositions.cjs');
6
6
  var array = require('./basics/array.cjs');
7
7
  var clock = require('./basics/clock.cjs');
8
8
  var objFilter = require('./basics/objFilter.cjs');
9
+ var objChecker = require('./basics/objChecker.cjs');
9
10
  var fullScreen = require('./basics/fullScreen.cjs');
10
11
  var simpleMath = require('./basics/simpleMath.cjs');
11
12
  var text = require('./basics/text.cjs');
@@ -42,11 +43,11 @@ exports.formatTimer = clock.formatTimer;
42
43
  exports.getTimeDuration = clock.getTimeDuration;
43
44
  exports.checkObj = objFilter.checkObj;
44
45
  exports.cloneObjTypeOrder = objFilter.cloneObjTypeOrder;
45
- exports.countObj = objFilter.countObj;
46
46
  exports.extendObjType = objFilter.extendObjType;
47
- exports.isJsonObject = objFilter.isJsonObject;
48
47
  exports.objType = objFilter.objType;
49
48
  exports.reorderObjTypeOrder = objFilter.reorderObjTypeOrder;
49
+ exports.countObj = objChecker.countObj;
50
+ exports.isJsonObject = objChecker.isJsonObject;
50
51
  exports.documentIsFullScreen = fullScreen.documentIsFullScreen;
51
52
  exports.exitFullScreen = fullScreen.exitFullScreen;
52
53
  exports.isFullScreenMode = fullScreen.isFullScreenMode;
@@ -82,14 +82,14 @@ import { isFullScreenMode } from './basics/fullScreen.mjs';
82
82
  import { onFullScreenChange } from './basics/fullScreen.mjs';
83
83
  import { offFullScreenChange } from './basics/fullScreen.mjs';
84
84
  import { areHtmlElsColliding } from './basics/html_deprecated.mjs';
85
- import { isJsonObject } from './basics/objFilter.mjs';
85
+ import { isJsonObject } from './basics/objChecker.mjs';
86
86
  import arraySortPositions from '../legacy/libs/arraySortPositions.mjs';
87
87
  import { formatBytes } from './basics/simpleMath.mjs';
88
88
  import { addAiMarkerShortcut } from './basics/text.mjs';
89
89
  import { extendObjType } from './basics/objFilter.mjs';
90
90
  import { reorderObjTypeOrder } from './basics/objFilter.mjs';
91
91
  import { cloneObjTypeOrder } from './basics/objFilter.mjs';
92
- import { countObj } from './basics/objFilter.mjs';
92
+ import { countObj } from './basics/objChecker.mjs';
93
93
  import { checkObj } from './basics/objFilter.mjs';
94
94
  import { objType } from './basics/objFilter.mjs';
95
95
  import { ruleOfThree } from './basics/simpleMath.mjs';
package/dist/v1/index.mjs CHANGED
@@ -3,7 +3,8 @@ import TinyLevelUp from '../legacy/libs/userLevel.mjs';
3
3
  import arraySortPositions from '../legacy/libs/arraySortPositions.mjs';
4
4
  import { shuffleArray } from './basics/array.mjs';
5
5
  import { formatCustomTimer, formatDayTimer, formatTimer, getTimeDuration, } from './basics/clock.mjs';
6
- import { countObj, extendObjType, reorderObjTypeOrder, cloneObjTypeOrder, objType, checkObj, isJsonObject, } from './basics/objFilter.mjs';
6
+ import { extendObjType, reorderObjTypeOrder, cloneObjTypeOrder, objType, checkObj, } from './basics/objFilter.mjs';
7
+ import { countObj, isJsonObject } from './basics/objChecker.mjs';
7
8
  import { documentIsFullScreen, isScreenFilled, requestFullScreen, exitFullScreen, isFullScreenMode, onFullScreenChange, offFullScreenChange, } from './basics/fullScreen.mjs';
8
9
  import { formatBytes, genFibonacciSeq, getAge, getSimplePerc, ruleOfThree, } from './basics/simpleMath.mjs';
9
10
  import { addAiMarkerShortcut, safeTextTrim, toTitleCase, toTitleCaseLowerFirst, } from './basics/text.mjs';
@@ -2,7 +2,7 @@
2
2
 
3
3
  var TinyHtml = require('./TinyHtml.cjs');
4
4
  var collision = require('../basics/collision.cjs');
5
- var objFilter = require('../basics/objFilter.cjs');
5
+ var objChecker = require('../basics/objChecker.cjs');
6
6
 
7
7
  /**
8
8
  * @typedef {Object} VibrationPatterns
@@ -97,7 +97,7 @@ class TinyDragger {
97
97
  if (
98
98
  options.vibration !== undefined &&
99
99
  options.vibration !== false &&
100
- !objFilter.isJsonObject(options.vibration)
100
+ !objChecker.isJsonObject(options.vibration)
101
101
  )
102
102
  throw new Error('The "vibration" option must be an object or false.');
103
103
 
@@ -141,7 +141,7 @@ class TinyDragger {
141
141
  const vibrationTemplate = { start: false, end: false, collide: false, move: false };
142
142
  this.#vibration = Object.assign(
143
143
  vibrationTemplate,
144
- objFilter.isJsonObject(options.vibration) ? options.vibration : {},
144
+ objChecker.isJsonObject(options.vibration) ? options.vibration : {},
145
145
  );
146
146
 
147
147
  if (typeof options.classDragging === 'string') this.#classDragging = options.classDragging;
@@ -1,6 +1,6 @@
1
1
  import TinyHtml from './TinyHtml.mjs';
2
2
  import * as TinyCollision from '../basics/collision.mjs';
3
- import { isJsonObject } from '../basics/objFilter.mjs';
3
+ import { isJsonObject } from '../basics/objChecker.mjs';
4
4
  /**
5
5
  * @typedef {Object} VibrationPatterns
6
6
  * @property {number[]|false} start - Pattern to vibrate on start
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
- var objFilter = require('../basics/objFilter.cjs');
3
+ require('../basics/objFilter.cjs');
4
+ var objChecker = require('../basics/objChecker.cjs');
4
5
  require('./TinyHtml.cjs');
5
6
  require('fs');
6
7
  require('path');
@@ -79,7 +80,7 @@ class TinyUploadClicker {
79
80
  * @throws {TypeError} If the config is invalid or required options are missing.
80
81
  */
81
82
  constructor(options) {
82
- if (!objFilter.isJsonObject(options))
83
+ if (!objChecker.isJsonObject(options))
83
84
  throw new TypeError('TinyUploadClicker: "options" must be a valid object.');
84
85
 
85
86
  this.#config = {
@@ -145,10 +146,10 @@ class TinyUploadClicker {
145
146
  )
146
147
  throw new TypeError('TinyUploadClicker: "onFileLoad" must be a function or null.');
147
148
 
148
- if (options.inputAttributes !== undefined && !objFilter.isJsonObject(options.inputAttributes))
149
+ if (options.inputAttributes !== undefined && !objChecker.isJsonObject(options.inputAttributes))
149
150
  throw new TypeError('TinyUploadClicker: "inputAttributes" must be an object.');
150
151
 
151
- if (options.inputStyles !== undefined && !objFilter.isJsonObject(options.inputStyles))
152
+ if (options.inputStyles !== undefined && !objChecker.isJsonObject(options.inputStyles))
152
153
  throw new TypeError('TinyUploadClicker: "inputStyles" must be an object.');
153
154
 
154
155
  this.#boundClick = this.#handleClick.bind(this);
package/docs/v1/README.md CHANGED
@@ -15,6 +15,7 @@ This folder contains the core scripts we have worked on so far. Each file is a m
15
15
  - ๐Ÿ“ฆ **[Array](./basics/array.md)** โ€” A tiny utility for shuffling arrays using the Fisherโ€“Yates algorithm.
16
16
  - โฐ **[Clock](./basics/clock.md)** โ€” A versatile time utility module for calculating and formatting time durations.
17
17
  - ๐Ÿง  **[ObjFilter](./basics/objFilter.md)** โ€” Type detection, extension, and analysis made easy with simple and extensible type validation.
18
+ * ๐Ÿงฎ **[objChecker](./basics/objChecker.md)** โ€” Utilities for counting keys in objects or arrays and for safely detecting plain JSON-compatible objects.
18
19
  - ๐Ÿ”ข **[SimpleMath](./basics/simpleMath.md)** โ€” A collection of simple math utilities for calculations like the Rule of Three and percentages.
19
20
  - โœ๏ธ **[Text](./basics/text.md)** โ€” A utility for transforming text into title case formats, with multiple options for capitalization.
20
21
  - ๐Ÿ”„ **[AsyncReplace](./basics/asyncReplace.md)** โ€” Asynchronously replaces matches in a string using a regex and an async function.
@@ -0,0 +1,47 @@
1
+ # ๐Ÿง  objChecker.mjs
2
+
3
+ ## ๐Ÿ“˜ Object & Type Utilities
4
+
5
+ This document introduces utility functions designed to help you safely and efficiently work with JavaScript objects and types. These helpers are particularly useful when dealing with dynamic values โ€” especially in situations where data might be coming from APIs, user input, or JSON files.
6
+
7
+ These tools are built to reduce boilerplate code and prevent common mistakes when handling unknown or loosely-typed values in JavaScript.
8
+
9
+ ---
10
+
11
+ ### ๐Ÿงฎ `countObj(obj)`
12
+
13
+ Returns the number of elements in an array or the number of keys in an object.
14
+
15
+ ```js
16
+ countObj([1, 2, 3]); // 3
17
+ countObj({ a: 1, b: 2 }); // 2
18
+ countObj('hi'); // 0
19
+ ```
20
+
21
+ ---
22
+
23
+ ### ๐Ÿงผ `isJsonObject(value)`
24
+
25
+ Check if a value is a **plain JSON-compatible object** โ€” meaning it's created via `{}` or `new Object()`, with a prototype of `Object.prototype`, and **not** a special object like `Date`, `Map`, `Array`, etc.
26
+
27
+ ```js
28
+ isJsonObject({}); // true
29
+ isJsonObject(Object.create({})); // true
30
+ isJsonObject(Object.create(Object.prototype)); // true
31
+ isJsonObject(Object.assign({}, { a: 1 })); // true
32
+ ```
33
+
34
+ ```js
35
+ isJsonObject([]); // false
36
+ isJsonObject(new Date()); // false
37
+ isJsonObject(new Map()); // false
38
+ isJsonObject(Object.create(null)); // false
39
+ ```
40
+
41
+ ๐Ÿ”’ This function ensures the object:
42
+
43
+ * is not `null`
44
+ * has `typeof === 'object'`
45
+ * is **directly** inherited from `Object.prototype`
46
+
47
+ Use this when you need to strictly validate a raw JSON object (like the output of `JSON.parse()` or manual object literals).
@@ -121,18 +121,6 @@ const currentOrder = cloneObjTypeOrder();
121
121
 
122
122
  ---
123
123
 
124
- ### ๐Ÿงฎ `countObj(obj)`
125
-
126
- Returns the number of elements in an array or the number of keys in an object.
127
-
128
- ```js
129
- countObj([1, 2, 3]); // 3
130
- countObj({ a: 1, b: 2 }); // 2
131
- countObj('hi'); // 0
132
- ```
133
-
134
- ---
135
-
136
124
  ## Supported Types
137
125
 
138
126
  Hereโ€™s a full list of supported type names (in their default order):
@@ -164,31 +152,3 @@ You can change this order or insert your own types with `extendObjType`.
164
152
  ### ๐Ÿ› ๏ธ `getCheckObj()`
165
153
 
166
154
  This function creates a clone of the functions from the `typeValidator` object. It returns a new object where the keys are the same and the values are the cloned functions.
167
-
168
- ---
169
-
170
- ### ๐Ÿงผ `isJsonObject(value)`
171
-
172
- Check if a value is a **plain JSON-compatible object** โ€” meaning it's created via `{}` or `new Object()`, with a prototype of `Object.prototype`, and **not** a special object like `Date`, `Map`, `Array`, etc.
173
-
174
- ```js
175
- isJsonObject({}); // true
176
- isJsonObject(Object.create({})); // true
177
- isJsonObject(Object.create(Object.prototype)); // true
178
- isJsonObject(Object.assign({}, { a: 1 })); // true
179
- ```
180
-
181
- ```js
182
- isJsonObject([]); // false
183
- isJsonObject(new Date()); // false
184
- isJsonObject(new Map()); // false
185
- isJsonObject(Object.create(null)); // false
186
- ```
187
-
188
- ๐Ÿ”’ This function ensures the object:
189
-
190
- * is not `null`
191
- * has `typeof === 'object'`
192
- * is **directly** inherited from `Object.prototype`
193
-
194
- Use this when you need to strictly validate a raw JSON object (like the output of `JSON.parse()` or manual object literals).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tiny-essentials",
3
- "version": "1.18.0",
3
+ "version": "1.18.1",
4
4
  "description": "Collection of small, essential scripts designed to be used across various projects. These simple utilities are crafted for speed, ease of use, and versatility.",
5
5
  "scripts": {
6
6
  "test": "npm run test:mjs && npm run test:cjs && npm run test:js",
@@ -1,8 +0,0 @@
1
- /*!
2
- * The buffer module from node.js, for the browser.
3
- *
4
- * @author Feross Aboukhadijeh <https://feross.org>
5
- * @license MIT
6
- */
7
-
8
- /*! ieee754. BSD-3-Clause License. Feross Aboukhadijeh <https://feross.org/opensource> */