Skip to content

Commit 6e855cc

Browse files
committed
refactor: use Array.isArray() and update dependencies
- Replace instanceof Array with Array.isArray() for better reliability - Update @types/node from 25.5.0 to 25.6.0
1 parent b3c0264 commit 6e855cc

6 files changed

Lines changed: 19 additions & 19 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ export default class Rectangle {
172172

173173
// the type annotation is technically not necessary but helps to avoid mistakes and enables code completion
174174
static fromYSON: YSONReviver<Rectangle> = x => {
175-
if (typeof x != "object" || x instanceof Array) return // reject String and Array types
175+
if (typeof x != "object" || Array.isArray(x)) return // reject String and Array types
176176

177177
if (!("width" in x)) throw new Error("Rectangles need to have a width")
178178
if (!("height" in x)) throw new Error("Rectangles need to have a height")

package-lock.json

Lines changed: 8 additions & 8 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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"homepage": "https://github.com/j0code/yson#readme",
1616
"type": "module",
1717
"devDependencies": {
18-
"@types/node": "^25.5.0",
18+
"@types/node": "^25.6.0",
1919
"typescript": "^6.0.2"
2020
}
2121
}

src/defaultRevivers.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ import { YSONReviver } from "./types.js"
33
const parseMap: YSONReviver<Map<any, any>> = x => {
44
if (typeof x != "object") throw new Error("Map must be an object or entry array")
55

6-
if (x instanceof Array) { // Map entries
6+
if (Array.isArray(x)) { // Map entries
77
return new Map(x as [any, any][])
88
}
99

1010
return new Map(Object.entries(x))
1111
}
1212

1313
const parseSet: YSONReviver<Set<any>> = x => {
14-
if (!(x instanceof Array)) throw new Error("Set must be an array")
14+
if (!(Array.isArray(x))) throw new Error("Set must be an array")
1515

1616
return new Set(x)
1717
}
@@ -29,13 +29,13 @@ const parseURL: YSONReviver<URL> = x => {
2929
}
3030

3131
const parseArrayBuffer: YSONReviver<ArrayBuffer> = x => {
32-
if (!(x instanceof Array)) throw new Error("ArrayBuffer must be an array")
32+
if (!(Array.isArray(x))) throw new Error("ArrayBuffer must be an array")
3333

3434
return new Uint8Array(x as []).buffer
3535
}
3636

3737
const parseDataView: YSONReviver<DataView> = x => {
38-
if (!(x instanceof Array)) throw new Error("DataView must be an array")
38+
if (!(Array.isArray(x))) throw new Error("DataView must be an array")
3939

4040
return new DataView(new Uint8Array(x as []).buffer)
4141
}
@@ -44,7 +44,7 @@ const typedArrays = [Int8Array, Uint8Array, Uint8ClampedArray, Int16Array, Uint1
4444
type TypedArray = Int8Array| Uint8Array| Uint8ClampedArray| Int16Array| Uint16Array| Int32Array| Uint32Array| BigInt64Array| BigUint64Array| Float32Array| Float64Array
4545

4646
const parseTypedArray: YSONReviver<TypedArray> = (x, { name }) => {
47-
if (!(x instanceof Array)) throw new Error("TypedArray must be an array")
47+
if (!Array.isArray(x)) throw new Error("TypedArray must be an array")
4848

4949
const typedArray = typedArrays.find(typedArray => typedArray.name == name)
5050
if (!typedArray) throw new Error("Unknown TypedArray")

src/stringify.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export function stringifyValue(value: unknown, options: StringifyOptions, depth:
3131
}
3232
}
3333

34-
if (value instanceof Array) return stringifyArray(value, options, depth)
34+
if (Array.isArray(value)) return stringifyArray(value, options, depth)
3535
if (value instanceof Map) {
3636
const newValue = Object.fromEntries(value.entries())
3737
const raw = stringifyObject(newValue, options, depth)

src/test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ function equals(a: unknown, b: unknown) {
4141
if (typeof a != "object" || typeof b != "object") return a == b
4242

4343
if (a == null) return b == null
44-
if (a instanceof Array != b instanceof Array) return false
45-
if (a instanceof Array && b instanceof Array) {
44+
if (Array.isArray(a) != Array.isArray(b)) return false
45+
if (Array.isArray(a) && Array.isArray(b)) {
4646
if (a.length != b.length) return false
4747
}
4848
for (let key in a) {
@@ -95,7 +95,7 @@ class StringClass {
9595
class Tuple<T> {
9696

9797
static fromYSON: YSONReviver<Tuple<any>> = x => {
98-
if (!(x instanceof Array)) return
98+
if (!Array.isArray(x)) return
9999
return new Tuple(...x)
100100
}
101101

0 commit comments

Comments
 (0)