Files
hoppscotch/packages/hoppscotch-app/helpers/rawKeyValue.ts
liyasthomas b57b948107 fix: urlencoded formdata key value params ui
Co-authored-by: Andrew Bastin <andrewbastin.k@gmail.com>
2022-01-23 04:45:05 +05:30

41 lines
1.0 KiB
TypeScript

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.filter((x) => x.trim().length > 0), // Remove lines which are empty
RA.map(parseRawKeyValueEntry),
RA.toArray
)
export const rawKeyValueEntriesToString = (entries: RawKeyValueEntry[]) =>
pipe(
entries,
A.map(({ key, value, active }) =>
active ? `${key}: ${value}` : `# ${key}: ${value}`
),
stringArrayJoin("\n")
)