#JSON tree utility library for TS safe mutations

8 messages · Page 1 of 1 (latest)

hearty ivy
#

Hey guys, i'm looking for a suite of TS supporting object traversal functions. An example, given a JSON tree of unknown depth and a discriminated union of shapes. Given a key value pair:

  • Remove the parent object that key value pair are properties of
  • Update the object where that key value pair is present with a new object (additional param on function)

I've TS definition for the tree shape, and I'd like to have this inferred through.

For a clearer example - I'm looking to satisfy the following tests : https://tsplay.dev/mZ10oN

Any tips on libraries, or approaches, i'm coming up blank - many thanks!

uncut apex
#

Types are kind of nasty, not sure how much smartness you want...

dusty narwhalBOT
#
Gerrit0#7591

Preview:```ts
console.clear()
function test(name: string, cb: () => void) {
console.log(name)
cb()
}

function expect(x: any) {
return {
toStrictEqual(y: any) {
if (JSON.stringify(x) === JSON.stringify(y)) {
console.log("pass")
} else {
console.log("fail", x)
...```

uncut apex
#

(If you wanted deep matching you'll have to change v[key] === value, could use JSON.stringify like I do in the expect helper, or if on Node you could use util.isDeepStrictEqual

#

... or any number of deep equal libraries

opal wing
#

Hi @hearty ivy and Gerrit0, I’m Alexander Lill (a real person), a researcher at the University of Zurich. I’m trying to help people receive answers faster on Discord. If you are not ok with giving me some feedback, click the 🚪 symbol, and I will delete this message and apologize for the inconvenience.

#

Would any of the provided links have helped to answer the OPs question? Please click the number-emoji for each link that would have been helpful, and on the ❌ if none of the links would have been helpful to answer the OPs question.

1️⃣ yawny - Help working with json object and type aliases
https://discord.com/channels/508357248330760243/984463603971264522

2️⃣ Map Datastructure in TS
https://discord.com/channels/508357248330760243/961889261760704562

3️⃣ How to Parse OrgNode Arrays
https://discord.com/channels/508357248330760243/1007271299690795088

Let me know if you have any questions! More info: #1063200184315674706

hearty ivy
#

That's super helpful - thank you @uncut apex