@tim-code/my-util 0.6.1 → 0.6.2
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/package.json +1 -1
- package/src/object.js +17 -0
- package/src/object.test.js +33 -1
package/package.json
CHANGED
package/src/object.js
CHANGED
|
@@ -182,3 +182,20 @@ export function deepEqual(a, b) {
|
|
|
182
182
|
}
|
|
183
183
|
return true
|
|
184
184
|
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Checks if the argument is a class.
|
|
188
|
+
* Example: `isClass(class {})`
|
|
189
|
+
* Returns: true
|
|
190
|
+
* In general, this will only work for third-party or user-defined classes, not built-ins.
|
|
191
|
+
* @param {any} thing
|
|
192
|
+
* @returns {boolean}
|
|
193
|
+
*/
|
|
194
|
+
export function isClass(thing) {
|
|
195
|
+
if (typeof thing !== "function") {
|
|
196
|
+
return false
|
|
197
|
+
}
|
|
198
|
+
const stringified = Function.prototype.toString.call(thing)
|
|
199
|
+
const result = /^class\s/u.test(stringified)
|
|
200
|
+
return result
|
|
201
|
+
}
|
package/src/object.test.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/* eslint-disable no-empty-function */
|
|
1
2
|
import { jest } from "@jest/globals"
|
|
2
3
|
import {
|
|
3
4
|
deepCopy,
|
|
@@ -5,6 +6,7 @@ import {
|
|
|
5
6
|
deepMerge,
|
|
6
7
|
deepMergeCopy,
|
|
7
8
|
deleteUndefinedValues,
|
|
9
|
+
isClass,
|
|
8
10
|
isObject,
|
|
9
11
|
like,
|
|
10
12
|
mapValues,
|
|
@@ -38,7 +40,7 @@ describe("isObject", () => {
|
|
|
38
40
|
|
|
39
41
|
it("returns false for functions", () => {
|
|
40
42
|
expect(isObject(() => {})).toBe(false)
|
|
41
|
-
// eslint-disable-next-line func-names, prefer-arrow-callback
|
|
43
|
+
// eslint-disable-next-line func-names, prefer-arrow-callback
|
|
42
44
|
expect(isObject(function () {})).toBe(false)
|
|
43
45
|
})
|
|
44
46
|
})
|
|
@@ -592,3 +594,33 @@ describe("deepEqual", () => {
|
|
|
592
594
|
expect(deepEqual({}, { a: undefined })).toBe(false)
|
|
593
595
|
})
|
|
594
596
|
})
|
|
597
|
+
|
|
598
|
+
// --- isClass ---
|
|
599
|
+
describe("isClass", () => {
|
|
600
|
+
it("returns true for class declarations and expressions", () => {
|
|
601
|
+
class A {}
|
|
602
|
+
const B = class {}
|
|
603
|
+
expect(isClass(A)).toBe(true)
|
|
604
|
+
expect(isClass(B)).toBe(true)
|
|
605
|
+
})
|
|
606
|
+
|
|
607
|
+
it("returns false for non-class functions", () => {
|
|
608
|
+
function f() {}
|
|
609
|
+
const g = () => {}
|
|
610
|
+
async function af() {}
|
|
611
|
+
function* gf() {}
|
|
612
|
+
expect(isClass(f)).toBe(false)
|
|
613
|
+
expect(isClass(g)).toBe(false)
|
|
614
|
+
expect(isClass(af)).toBe(false)
|
|
615
|
+
expect(isClass(gf)).toBe(false)
|
|
616
|
+
})
|
|
617
|
+
|
|
618
|
+
it("returns false for built-in constructors and non-functions", () => {
|
|
619
|
+
// Built-ins typically stringify as 'function X() { [native code] }'
|
|
620
|
+
expect(isClass(Date)).toBe(false)
|
|
621
|
+
expect(isClass(Map)).toBe(false)
|
|
622
|
+
expect(isClass(123)).toBe(false)
|
|
623
|
+
expect(isClass({})).toBe(false)
|
|
624
|
+
expect(isClass(null)).toBe(false)
|
|
625
|
+
})
|
|
626
|
+
})
|