/ cabbages.test.ts
cabbages.test.ts
1 import {apply} from "./cabbages.ts" 2 import test from "node:test" 3 import assert from "node:assert" 4 5 test.test("patch", async t => { 6 await t.test("mutates", t => { 7 let path = ["deeply", "nested"] 8 let obj = {deeply: {nested: {value: 0}}} 9 apply(path, obj, "value", 10) 10 assert.equal(obj.deeply.nested.value, 10) 11 }) 12 13 await t.test("doesnt mutate path", t => { 14 let obj = {deeply: {nested: {value: 0}}} 15 let path = ["deeply", "nested"] 16 apply(path, obj, "value", 10) 17 assert.deepEqual(path, ["deeply", "nested"]) 18 }) 19 20 await t.test("fills object holes", t => { 21 let obj = {deeply: {}} 22 let path = ["deeply", "nested"] 23 apply(path, obj, "value", 10) 24 // @ts-expect-error 25 assert.equal(obj.deeply.nested.value, 10) 26 assert.deepEqual(obj, { 27 deeply: {nested: {value: 10}}, 28 }) 29 }) 30 31 await t.test("fills array path holes", t => { 32 let path = ["items", 2] 33 let obj = {} 34 apply(path, obj, "complete", true) 35 assert.deepEqual(obj, { 36 // biome-ignore lint/suspicious/noSparseArray: <explanation> 37 items: [, , {complete: true}], 38 }) 39 }) 40 41 await t.test("makes a string for a ghostly splice", t => { 42 let path = ["items", 0, "title"] 43 let obj = {} 44 apply(path, obj, [], "cool") 45 assert.deepEqual(obj, { 46 items: [{title: "cool"}], 47 }) 48 }) 49 50 await t.test("replaces range in array", t => { 51 let path = ["items"] 52 let obj = {items: [1, 2, 3, 4, 5]} 53 apply(path, obj, [1, 3], "hehe") 54 assert.deepEqual(obj, { 55 items: [1, "hehe", 4, 5], 56 }) 57 }) 58 59 await t.test("replaces range in string", t => { 60 let path = ["text"] 61 let obj = {text: "hello world"} 62 apply(path, obj, [1, 4], "aww") 63 assert.equal(obj.text, "hawwo world") 64 }) 65 66 await t.test("inserts multiple items in array", t => { 67 let path = ["items"] 68 let obj = {items: ["zero"]} 69 apply(path, obj, [], ["one", "two"]) 70 assert.deepEqual(obj.items, ["zero", "one", "two"]) 71 }) 72 73 await t.test("inserts multiple chars in string", t => { 74 let path = ["text"] 75 let obj = {text: "hello world"} 76 apply(path, obj, [6, 6], "cruel ") 77 assert.equal(obj.text, "hello cruel world") 78 }) 79 80 await t.test("appends to array", t => { 81 let path = ["items"] 82 let obj = {items: ["a", "b"]} 83 apply(path, obj, [], "c") 84 assert.deepEqual(obj.items, ["a", "b", "c"]) 85 }) 86 87 await t.test("appends multiple items to array", t => { 88 let path = ["items"] 89 let obj = {items: ["a", "b"]} 90 apply(path, obj, [], ["c", "d"]) 91 assert.deepEqual(obj.items, ["a", "b", "c", "d"]) 92 }) 93 94 await t.test("appends to string", t => { 95 let path = ["text"] 96 let obj = {text: "hello world"} 97 apply(path, obj, [], ", what's up?") 98 assert.equal(obj.text, "hello world, what's up?") 99 }) 100 101 await t.test("deletes an item from an array", t => { 102 let path = ["items"] 103 let obj = {items: [1, 2, 3]} 104 apply(path, obj, 1) 105 assert.deepEqual(obj, {items: [1, 3]}) 106 }) 107 108 await t.test("deletes an item from an object", t => { 109 let path = ["state"] 110 let obj = {state: {complete: false}} 111 apply(path, obj, "complete") 112 assert.deepEqual(obj, {state: {}}) 113 }) 114 115 await t.test("deletes multiple items in an array", t => { 116 let path = ["items"] 117 let obj = {items: [1, 2, 3, 4, 5]} 118 apply(path, obj, [2, 4]) 119 assert.deepEqual(obj, {items: [1, 2, 5]}) 120 }) 121 122 await t.test("deletes chars from a string", t => { 123 let path = ["name"] 124 let obj = {name: "chee rabbits"} 125 apply(path, obj, [2, 5]) 126 assert.deepEqual(obj.name, "chrabbits") 127 }) 128 129 await t.test("insert a string in an array", t => { 130 let path = ["items"] 131 let obj = {items: [] as string[]} 132 apply(path, obj, [], "hello") 133 assert.equal(obj.items[0], "hello") 134 }) 135 136 await t.test("insert an array in an array", t => { 137 let path = ["items"] 138 let obj = {items: [] as string[]} 139 apply(path, obj, [], "hello") 140 assert.equal(obj.items[0], "hello") 141 }) 142 143 await t.test("edit top level values", t => { 144 let obj = {text: "hello"} 145 apply([], obj, "text", "hallo") 146 assert.equal(obj.text, "hallo") 147 apply([], obj, "works", ["1", "2", "yes"]) 148 // @ts-expect-error 149 assert.deepEqual(obj.works, ["1", "2", "yes"]) 150 }) 151 152 await t.test("can really fuckin go for it", t => { 153 let obj = {} 154 apply( 155 [1, 2, 3, 4, 5, 6, "lol", "ok", "deeeeeep", 0, 1, 2, "hehe"], 156 obj, 157 "ok", 158 "computer" 159 ) 160 161 assert.equal( 162 // @ts-expect-error 163 obj[1][2][3][4][5][6].lol.ok.deeeeeep[0][1][2].hehe.ok, 164 "computer" 165 ) 166 }) 167 168 await t.test("deep", t => { 169 let obj = { 170 a: {b: {c: {d: {e: {f: {g: {h: {i: "rabbit"}}}}}}}}, 171 } 172 apply(["a", "b", "c", "d", "e", "f", "g", "h"], obj, "i", "computer") 173 174 assert.equal(obj.a.b.c.d.e.f.g.h.i, "computer") 175 }) 176 177 await t.test("uses reviver", t => { 178 let path = ["projects", "abc", "items", 2] 179 let obj = {} 180 apply( 181 path, 182 obj, 183 "count", 184 { 185 type: "special:counter", 186 value: 4, 187 }, 188 val => { 189 if (val && "type" in val && val.type == "special:counter") { 190 return val.value 191 } 192 } 193 ) 194 assert.deepEqual(obj, { 195 // biome-ignore lint/suspicious/noSparseArray: <explanation> 196 projects: {abc: {items: [, , {count: 4}]}}, 197 }) 198 }) 199 })