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.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github unmock / unmock-js / packages / unmock-core / src / generator.ts View on Github external
t: Traversal,
  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,
  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()("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 {}
export type JSONType = null | string | number | boolean | JSONArray | JSONObject

export const StringJSONPrism = new Prism(
  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(s => (typeof s === 'string' ? some(s) : none), a => a)
github gcanti / newtype-ts / src / index.ts View on Github external
export function prism<s>(predicate: Predicate&gt;): Prism, S&gt; {
  return new Prism(s =&gt; (predicate(s) ? some(s) : none), identity)
}
</s>
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(s =&gt; (typeof s === 'number' ? some(s) : none), a =&gt; a)
github gcanti / io-ts-types / src / monocle-ts / TypePrismIso.ts View on Github external
export function get(codec: t.Type): Prism {
  return new Prism(s =&gt; 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 =&gt;
  new Iso&lt;
    IExtendedObjectType,
    Array&lt;[string, ExtendedValueType | IMaybeJSONValue]&gt;
  &gt;(
    a =&gt; Object.entries(a),
    q =&gt; q.reduce((a, b) =&gt; ({ ...a, [b[0]]: b[1] }), {}),
  )
    .composeTraversal(fromTraversable(array)())
    .composePrism(
      new Prism&lt;
        [string, ExtendedValueType | IMaybeJSONValue],
        [string, ExtendedValueType]
      &gt;(
        a =&gt;
          ((
            q: string,
            r: ExtendedValueType | IMaybeJSONValue,
          ): Option&lt;[string, ExtendedValueType]&gt; =&gt;
            MaybeJSONValue.is(r) ? none : some([q, r]))(a[0], a[1]),
        a =&gt; a,
      ),
    )
    .composeGetter(identityGetter())
    .getAll(i)
    .reduce((a, b) =&gt; ({ ...a, [b[0]]: b[1] }), {});