@gesslar/toolkit 0.1.2 → 0.1.4
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 +1 -1
- package/package.json +1 -1
- package/src/lib/Data.js +41 -0
- package/src/lib/DirectoryObject.js +3 -4
- package/src/types/Data.d.ts +39 -0
package/README.md
CHANGED
package/package.json
CHANGED
package/src/lib/Data.js
CHANGED
|
@@ -565,4 +565,45 @@ export default class Data {
|
|
|
565
565
|
static clamped(val, min, max) {
|
|
566
566
|
return val >= min && val <= max
|
|
567
567
|
}
|
|
568
|
+
|
|
569
|
+
/**
|
|
570
|
+
* Checks if a value is a plain object - created with object literals {},
|
|
571
|
+
* new Object(), or Object.create(null).
|
|
572
|
+
*
|
|
573
|
+
* Distinguishes plain objects from objects created by custom constructors, built-ins,
|
|
574
|
+
* or primitives. Plain objects only have Object.prototype or null in their prototype chain.
|
|
575
|
+
*
|
|
576
|
+
* @param {unknown} value - The value to check
|
|
577
|
+
* @returns {boolean} True if the value is a plain object, false otherwise
|
|
578
|
+
*
|
|
579
|
+
* @example
|
|
580
|
+
* isPlainObject({}) // true
|
|
581
|
+
* isPlainObject(new Object()) // true
|
|
582
|
+
* isPlainObject(Object.create(null)) // true
|
|
583
|
+
* isPlainObject([]) // false
|
|
584
|
+
* isPlainObject(new Date()) // false
|
|
585
|
+
* isPlainObject(null) // false
|
|
586
|
+
* isPlainObject("string") // false
|
|
587
|
+
* isPlainObject(class Person{}) // false
|
|
588
|
+
*/
|
|
589
|
+
static isPlainObject(value) {
|
|
590
|
+
// First, check if it's an object and not null
|
|
591
|
+
if(typeof value !== "object" || value === null)
|
|
592
|
+
return false
|
|
593
|
+
|
|
594
|
+
// If it has no prototype, it's plain (created with Object.create(null))
|
|
595
|
+
const proto = Object.getPrototypeOf(value)
|
|
596
|
+
|
|
597
|
+
if(proto === null)
|
|
598
|
+
return true
|
|
599
|
+
|
|
600
|
+
// Check if the prototype chain ends at Object.prototype
|
|
601
|
+
// This handles objects created with {} or new Object()
|
|
602
|
+
let current = proto
|
|
603
|
+
|
|
604
|
+
while(Object.getPrototypeOf(current) !== null)
|
|
605
|
+
current = Object.getPrototypeOf(current)
|
|
606
|
+
|
|
607
|
+
return proto === current
|
|
608
|
+
}
|
|
568
609
|
}
|
|
@@ -209,18 +209,17 @@ export default class DirectoryObject extends FS {
|
|
|
209
209
|
/**
|
|
210
210
|
* Lists the contents of a directory.
|
|
211
211
|
*
|
|
212
|
-
* @param {DirectoryObject} directory - The directory to list.
|
|
213
212
|
* @returns {Promise<{files: Array<FileObject>, directories: Array<DirectoryObject>}>} The files and directories in the directory.
|
|
214
213
|
*/
|
|
215
|
-
async read(
|
|
214
|
+
async read() {
|
|
216
215
|
const found = await fs.readdir(
|
|
217
|
-
new URL(
|
|
216
|
+
new URL(this.uri),
|
|
218
217
|
{withFileTypes: true}
|
|
219
218
|
)
|
|
220
219
|
|
|
221
220
|
const results = await Promise.all(
|
|
222
221
|
found.map(async dirent => {
|
|
223
|
-
const fullPath = path.join(
|
|
222
|
+
const fullPath = path.join(this.path, dirent.name)
|
|
224
223
|
const stat = await fs.stat(fullPath)
|
|
225
224
|
|
|
226
225
|
return {dirent, stat, fullPath}
|
package/src/types/Data.d.ts
CHANGED
|
@@ -248,4 +248,43 @@ export default class Data {
|
|
|
248
248
|
|
|
249
249
|
/** Checks if a value is within a specified range (inclusive) */
|
|
250
250
|
static clamped(val: number, min: number, max: number): boolean
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Checks if a value is a plain object - created with object literals {},
|
|
254
|
+
* new Object(), or Object.create(null).
|
|
255
|
+
*
|
|
256
|
+
* Distinguishes plain objects from objects created by custom constructors, built-ins,
|
|
257
|
+
* or primitives. Plain objects only have Object.prototype or null in their prototype chain.
|
|
258
|
+
* Useful for validating configuration objects or data structures that should be plain objects.
|
|
259
|
+
*
|
|
260
|
+
* @param value - The value to check for plain object status
|
|
261
|
+
* @returns True if the value is a plain object, false otherwise
|
|
262
|
+
*
|
|
263
|
+
* @example
|
|
264
|
+
* ```typescript
|
|
265
|
+
* import { Data } from '@gesslar/toolkit'
|
|
266
|
+
*
|
|
267
|
+
* // Plain objects return true
|
|
268
|
+
* console.log(Data.isPlainObject({})) // true
|
|
269
|
+
* console.log(Data.isPlainObject(new Object())) // true
|
|
270
|
+
* console.log(Data.isPlainObject(Object.create(null))) // true
|
|
271
|
+
*
|
|
272
|
+
* // Non-plain objects return false
|
|
273
|
+
* console.log(Data.isPlainObject([])) // false
|
|
274
|
+
* console.log(Data.isPlainObject(new Date())) // false
|
|
275
|
+
* console.log(Data.isPlainObject(/regex/)) // false
|
|
276
|
+
* console.log(Data.isPlainObject(null)) // false
|
|
277
|
+
* console.log(Data.isPlainObject('string')) // false
|
|
278
|
+
*
|
|
279
|
+
* // Useful for validating config objects
|
|
280
|
+
* function processConfig(config: unknown) {
|
|
281
|
+
* if (!Data.isPlainObject(config)) {
|
|
282
|
+
* throw new Error('Config must be a plain object')
|
|
283
|
+
* }
|
|
284
|
+
* // Safe to treat as object with string keys
|
|
285
|
+
* return Object.entries(config)
|
|
286
|
+
* }
|
|
287
|
+
* ```
|
|
288
|
+
*/
|
|
289
|
+
static isPlainObject(value: unknown): boolean
|
|
251
290
|
}
|