Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions src/WABinary/generic-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,32 @@ import { type BinaryNode } from './types'

// some extra useful utilities

const indexCache = new WeakMap<BinaryNode, Map<string, BinaryNode[]>>()

export const getBinaryNodeChildren = (node: BinaryNode | undefined, childTag: string) => {
if (Array.isArray(node?.content)) {
return node.content.filter(item => item.tag === childTag)
if (!node || !Array.isArray(node.content)) return []

let index = indexCache.get(node)

// Build the index once per node
if (!index) {
index = new Map<string, BinaryNode[]>()

for (const child of node.content) {
let arr = index.get(child.tag)
if (!arr) index.set(child.tag, (arr = []))
arr.push(child)
}

indexCache.set(node, index)
}

return []
// Return first matching child
return index.get(childTag) || []
}

export const getBinaryNodeChild = (node: BinaryNode | undefined, childTag: string) => {
return getBinaryNodeChildren(node, childTag)[0]
}

export const getAllBinaryNodeChildren = ({ content }: BinaryNode) => {
Expand All @@ -20,12 +40,6 @@ export const getAllBinaryNodeChildren = ({ content }: BinaryNode) => {
return []
}

export const getBinaryNodeChild = (node: BinaryNode | undefined, childTag: string) => {
if (Array.isArray(node?.content)) {
return node?.content.find(item => item.tag === childTag)
}
}

export const getBinaryNodeChildBuffer = (node: BinaryNode | undefined, childTag: string) => {
const child = getBinaryNodeChild(node, childTag)?.content
if (Buffer.isBuffer(child) || child instanceof Uint8Array) {
Expand Down
Loading