refactor: urlencoded key value pair system
This commit is contained in:
39
packages/hoppscotch-app/helpers/rawKeyValue.ts
Normal file
39
packages/hoppscotch-app/helpers/rawKeyValue.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import * as A from "fp-ts/Array"
|
||||
import * as RA from "fp-ts/ReadonlyArray"
|
||||
import * as S from "fp-ts/string"
|
||||
import { pipe, flow } from "fp-ts/function"
|
||||
import { stringArrayJoin } from "./functional/array"
|
||||
|
||||
export type RawKeyValueEntry = {
|
||||
key: string
|
||||
value: string
|
||||
active: boolean
|
||||
}
|
||||
|
||||
const parseRawKeyValueEntry = (str: string): RawKeyValueEntry => {
|
||||
const trimmed = str.trim()
|
||||
const inactive = trimmed.startsWith("#")
|
||||
|
||||
const [key, value] = trimmed.split(":").map(S.trim)
|
||||
|
||||
return {
|
||||
key: inactive ? key.replaceAll(/^#+\s*/g, "") : key, // Remove comment hash and early space
|
||||
value,
|
||||
active: !inactive,
|
||||
}
|
||||
}
|
||||
|
||||
export const parseRawKeyValueEntries = flow(
|
||||
S.split("\n"),
|
||||
RA.map(parseRawKeyValueEntry),
|
||||
RA.toArray
|
||||
)
|
||||
|
||||
export const rawKeyValueEntriesToString = (entries: RawKeyValueEntry[]) =>
|
||||
pipe(
|
||||
entries,
|
||||
A.map(({ key, value, active }) =>
|
||||
active ? `${key}: ${value}` : `# ${key}: ${value}`
|
||||
),
|
||||
stringArrayJoin("\n")
|
||||
)
|
||||
Reference in New Issue
Block a user