refactor: inherit default curl parser values (#2169)
This commit is contained in:
@@ -18,3 +18,74 @@ export const objFieldMatches =
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
(obj: T): obj is T & { [_ in K]: V } =>
|
||||
matches.findIndex((x) => isEqual(obj[fieldName], x)) !== -1
|
||||
|
||||
type JSPrimitive =
|
||||
| "undefined"
|
||||
| "object"
|
||||
| "boolean"
|
||||
| "number"
|
||||
| "bigint"
|
||||
| "string"
|
||||
| "symbol"
|
||||
| "function"
|
||||
|
||||
type TypeFromPrimitive<P extends JSPrimitive | undefined> =
|
||||
P extends "undefined"
|
||||
? undefined
|
||||
: P extends "object"
|
||||
? object | null // typeof null === "object"
|
||||
: P extends "boolean"
|
||||
? boolean
|
||||
: P extends "number"
|
||||
? number
|
||||
: P extends "bigint"
|
||||
? BigInt
|
||||
: P extends "string"
|
||||
? string
|
||||
: P extends "symbol"
|
||||
? Symbol
|
||||
: P extends "function"
|
||||
? Function
|
||||
: unknown
|
||||
|
||||
type TypeFromPrimitiveArray<P extends JSPrimitive | undefined> =
|
||||
P extends "undefined"
|
||||
? undefined
|
||||
: P extends "object"
|
||||
? object[] | null
|
||||
: P extends "boolean"
|
||||
? boolean[]
|
||||
: P extends "number"
|
||||
? number[]
|
||||
: P extends "bigint"
|
||||
? BigInt[]
|
||||
: P extends "string"
|
||||
? string[]
|
||||
: P extends "symbol"
|
||||
? Symbol[]
|
||||
: P extends "function"
|
||||
? Function[]
|
||||
: unknown[]
|
||||
|
||||
export const objHasProperty =
|
||||
<O extends object, K extends string, P extends JSPrimitive>(
|
||||
prop: K,
|
||||
type: P
|
||||
) =>
|
||||
// eslint-disable-next-line
|
||||
(obj: O): obj is O & { [_ in K]: TypeFromPrimitive<P> } =>
|
||||
// eslint-disable-next-line
|
||||
prop in obj && typeof (obj as any)[prop] === type
|
||||
|
||||
export const objHasArrayProperty =
|
||||
<O extends object, K extends string, P extends JSPrimitive>(
|
||||
prop: K,
|
||||
type: P
|
||||
) =>
|
||||
// eslint-disable-next-line
|
||||
(obj: O): obj is O & { [_ in K]: TypeFromPrimitiveArray<P> } =>
|
||||
prop in obj &&
|
||||
Array.isArray((obj as any)[prop]) &&
|
||||
(obj as any)[prop].every(
|
||||
(val: unknown) => typeof val === type // eslint-disable-line
|
||||
)
|
||||
|
||||
19
packages/hoppscotch-app/helpers/functional/option.ts
Normal file
19
packages/hoppscotch-app/helpers/functional/option.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import * as O from "fp-ts/Option"
|
||||
import * as A from "fp-ts/Array"
|
||||
import { pipe } from "fp-ts/function"
|
||||
|
||||
/**
|
||||
* Tries to match one of the given predicates.
|
||||
* If a predicate is matched, the associated value is returned in a Some.
|
||||
* Else if none of the predicates is matched, None is returned.
|
||||
* @param choice An array of tuples having a predicate function and the selected value
|
||||
* @returns A function which takes the input and returns an Option
|
||||
*/
|
||||
export const optionChoose =
|
||||
<T, V>(choice: Array<[(x: T) => boolean, V]>) =>
|
||||
(input: T): O.Option<V> =>
|
||||
pipe(
|
||||
choice,
|
||||
A.findFirst(([pred]) => pred(input)),
|
||||
O.map(([_, value]) => value)
|
||||
)
|
||||
Reference in New Issue
Block a user