Skip to content
Open
Show file tree
Hide file tree
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
14 changes: 10 additions & 4 deletions packages/codemode/src/stdlib/collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,18 @@ export const mapMethods = new Set(["get", "set", "has", "delete", "clear", "forE

export const setMethods = new Set(["add", "has", "delete", "clear", "forEach", "keys", "values", "entries"])

export const spreadItems = (value: unknown): Array<unknown> | undefined => {
if (Array.isArray(value)) return value
if (typeof value === "string") return Array.from(value)
const spreadSandboxItems = (value: unknown): Array<unknown> | undefined => {
if (value instanceof SandboxMap) return Array.from(value.map.entries(), ([key, item]) => [key, item])
if (value instanceof SandboxSet) return Array.from(value.set.values())
if (value instanceof SandboxURLSearchParams) return Array.from(value.params.entries(), ([key, item]) => [key, item])
if (value instanceof SandboxURLSearchParams) {
return Array.from(value.params as Iterable<[string, string]>, ([key, item]) => [key, item])
}
return undefined
}

export const spreadItems = (value: unknown): Array<unknown> | undefined => {
if (Array.isArray(value)) return value
if (typeof value === "string") return Array.from(value)
return spreadSandboxItems(value)
}
import { SandboxMap, SandboxSet, SandboxURLSearchParams } from "../values.js"
8 changes: 8 additions & 0 deletions packages/codemode/test/stdlib.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,14 @@ describe("URL and URI helpers", () => {
})
})

test("URLSearchParams spread produces entry pairs", async () => {
expect(await value(`return [...new URLSearchParams("tag=a&tag=b&q=a+b")]`)).toEqual([
["tag", "a"],
["tag", "b"],
["q", "a b"],
])
})

test("URL parsing failures are catchable and values use native JSON forms", async () => {
expect(
await value(`
Expand Down
Loading