Skip to content

Commit

Permalink
improve deepeq
Browse files Browse the repository at this point in the history
  • Loading branch information
slmjkdbtl committed Oct 13, 2023
1 parent ceb95b7 commit 45c502f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
3 changes: 1 addition & 2 deletions examples/doublejump.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ scene("game", () => {
anchor("center"),
pos(0, 0),
body({ jumpForce: JUMP_FORCE }),
// allow 3 jumps
doubleJump(3),
doubleJump(),
rotate(0),
spin(),
])
Expand Down
5 changes: 3 additions & 2 deletions src/kaboom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ import {
getExt,
dataURLToArrayBuffer,
EventController,
getErrorMessage,
// eslint-disable-next-line
warn,
// eslint-disable-next-line
Expand Down Expand Up @@ -1373,8 +1374,8 @@ export default (gopt: KaboomOpt = {}): KaboomCtx => {
return new Shader(ggl, vcode, fcode, VERTEX_FORMAT.map((vert) => vert.name))
} catch (e) {
const lineOffset = 14
const fmtRegex = /(?<type>^\w+) SHADER ERROR: 0:(?<line>\d+): (?<msg>.+)/
const match = (e as Error).message.match(fmtRegex)
const fmt = /(?<type>^\w+) SHADER ERROR: 0:(?<line>\d+): (?<msg>.+)/
const match = getErrorMessage(e).match(fmt)
const line = Number(match.groups.line) - lineOffset
const msg = match.groups.msg.trim()
const ty = match.groups.type.toLowerCase()
Expand Down
17 changes: 12 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,18 @@ export class EventHandler<EventMap extends Record<string, any[]>> {
}

export function deepEq(o1: any, o2: any): boolean {
if (o1 === o2) {
return true
}
const t1 = typeof o1
const t2 = typeof o2
if (t1 !== t2) {
return false
}
if (t1 === "object" && t2 === "object" && o1 !== null && o2 !== null) {
if (Array.isArray(o1) !== Array.isArray(o2)) {
return false
}
const k1 = Object.keys(o1)
const k2 = Object.keys(o2)
if (k1.length !== k2.length) {
Expand All @@ -125,15 +131,13 @@ export function deepEq(o1: any, o2: any): boolean {
for (const k of k1) {
const v1 = o1[k]
const v2 = o2[k]
if (!(typeof v1 === "function" && typeof v2 === "function")) {
if (!deepEq(v1, v2)) {
return false
}
if (!deepEq(v1, v2)) {
return false
}
}
return true
}
return o1 === o2
return false
}

export function base64ToArrayBuffer(base64: string): ArrayBuffer {
Expand Down Expand Up @@ -179,6 +183,9 @@ export const uid = (() => {
return () => id++
})()

export const getErrorMessage = (error: unknown) =>
(error instanceof Error) ? error.message : String(error)

const warned = new Set()

export function warn(msg: string) {
Expand Down

0 comments on commit 45c502f

Please sign in to comment.