Skip to content

Commit

Permalink
feat: add utility function
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasKruckenberg committed Mar 8, 2021
1 parent edf1c2e commit 29ec3e5
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { DirectiveOptions } from "./types"

export const cartesian = (...a: any[]) => a.reduce((a, b) => a.flatMap(d => b.map(e => [d, e].flat())))

export function buildDirectiveOptions(src: URL): DirectiveOptions[] {
return Array.from(src.searchParams.entries())
.map<[string, string[]]>(([key, value]) => [key, value.split(',')])
.map(([key, values]) => values.map(v => ({ [key]: v })))
.reduce((prev, cur) => prev.length ? cartesian(prev, cur) : cur, [])
.map((options: Record<string, any>[]) => Array.isArray(options) ? Object.assign({}, ...options) : options)
}

0 comments on commit 29ec3e5

Please sign in to comment.