How to use the monocle-ts.Prism function in monocle-ts

To help you get started, we’ve selected a few monocle-ts examples, based on popular ways it is used in public projects.

github unmock / unmock-js / packages / unmock-core / src / generator.ts View on Github external
t: Traversal<PathItem, Reference | Parameter>,
  oai: OpenAPIObject,
  p: PathItem,
): Parameter[] =>
  t
    .composePrism(
      new Prism(
        s =>
          isReference(s)
            ? getParameterFromRef(oai, s.$ref.split("/")[3])
            : some(s),
        a => a,
      ),
    )
    .composePrism(
      new Prism(
        s =>
          s.in === (header ? "header" : "query") && s.required ? some(s) : none,
        a => a,
      ),
    )
    .composeGetter(new Getter(i => i))
    .getAll(p)
    .filter(a =>
      a.schema ? !isNone(schemaPrism(oai).getOption(a.schema)) : false,
    )
    .map(b => ({
      ...b,
      schema: b.schema
        ? isReference(b.schema)
          ? changeRef(b.schema)
          : changeRefs(b.schema)
github unmock / unmock-js / packages / unmock-core / src / generator-experimental.ts View on Github external
const internalGetParameter = (
  t: Traversal<PathItem, Reference | Parameter>,
  vname: string,
  pathItem: PathItem,
  oas: OpenAPIObject,
) =>
  t
    .composePrism(new Prism(
      i => isReference(i)
        ? discernName(getParameterFromRef(oas, refName(i)), vname)
        : discernName(some(i), vname),
      a => a,
    ))
    .composeOptional(Optional.fromNullableProp<Parameter>()("schema"))
    .composeGetter(identityGetter())
    .getAll(pathItem);
github gcanti / io-ts-types / src / monocle-ts / StringJSONPrism.ts View on Github external
import { Prism } from 'monocle-ts'
import { tryCatch } from 'fp-ts/lib/Either'

export type JSONObject = { [key: string]: JSONType }
export interface JSONArray extends Array<JSONType> {}
export type JSONType = null | string | number | boolean | JSONArray | JSONObject

export const StringJSONPrism = new Prism<string, JSONType>(
  s => tryCatch(() => JSON.parse(s)).toOption(),
  a => JSON.stringify(a)
)
github gcanti / io-ts-types / src / monocle-ts / AnyStringPrism.ts View on Github external
import { Prism } from 'monocle-ts'
import { some, none } from 'fp-ts/lib/Option'

export const AnyStringPrism = new Prism<any, string>(s => (typeof s === 'string' ? some(s) : none), a => a)
github gcanti / newtype-ts / src / index.ts View on Github external
export function prism<S extends AnyNewtype>(predicate: Predicate<CarrierOf<S>>): Prism<CarrierOf<S>, S> {
  return new Prism(s => (predicate(s) ? some(s) : none), identity)
}
github gcanti / io-ts-types / src / monocle-ts / AnyNumberPrism.ts View on Github external
import { Prism } from 'monocle-ts'
import { some, none } from 'fp-ts/lib/Option'

export const AnyNumberPrism = new Prism<any, number>(s => (typeof s === 'number' ? some(s) : none), a => a)
github gcanti / io-ts-types / src / monocle-ts / TypePrismIso.ts View on Github external
export function get<A, O extends I, I>(codec: t.Type<A, O, I>): Prism<O, A> {
  return new Prism<O, A>(s => fromEither(codec.decode(s)), codec.encode)
}
github unmock / unmock-js / packages / unmock-core / src / nock.ts View on Github external
const rejectOptionals = (i: IExtendedObjectType): ITameExtendedObjectType =>
  new Iso<
    IExtendedObjectType,
    Array<[string, ExtendedValueType | IMaybeJSONValue]>
  >(
    a => Object.entries(a),
    q => q.reduce((a, b) => ({ ...a, [b[0]]: b[1] }), {}),
  )
    .composeTraversal(fromTraversable(array)())
    .composePrism(
      new Prism<
        [string, ExtendedValueType | IMaybeJSONValue],
        [string, ExtendedValueType]
      >(
        a =>
          ((
            q: string,
            r: ExtendedValueType | IMaybeJSONValue,
          ): Option<[string, ExtendedValueType]> =>
            MaybeJSONValue.is(r) ? none : some([q, r]))(a[0], a[1]),
        a => a,
      ),
    )
    .composeGetter(identityGetter())
    .getAll(i)
    .reduce((a, b) => ({ ...a, [b[0]]: b[1] }), {});

monocle-ts

A porting of scala monocle library to TypeScript

MIT
Latest version published 6 months ago

Package Health Score

72 / 100
Full package analysis

Similar packages