How to use monocle-ts - 10 common examples

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 gcanti / elm-ts / examples / Http.tsx View on Github external
const decoder = flow(
  ApiPayloadSchema.decode,
  E.mapLeft(errors => failure(errors).join('\n'))
)

function getRandomGif(topic: string): cmd.Cmd {
  const url = `https://api.giphy.com/v1/gifs/random?api_key=dc6zaTOxFJmzC&tag=${topic}`

  return pipe(
    http.get(url, decoder),
    http.send(e => newGif(E.either.map(e, a => a.data.image_url)))
  )
}

// --- Update
const gifUrlLens = Lens.fromProp()('gifUrl')

export function update(msg: Msg, model: Model): [Model, cmd.Cmd] {
  switch (msg.type) {
    case 'MorePlease':
      return [gifUrlLens.set(O.none)(model), getRandomGif(model.topic)]

    case 'NewGif':
      return [gifUrlLens.set(O.some(msg.result))(model), cmd.none]
  }
  throw new Error('err')
}

// --- View
export function view(model: Model): Html {
  return dispatch => (
    <div></div>
github gcanti / newtype-ts / src / index.ts View on Github external
/**
 * @since 0.2.0
 */
export const getRing = <s>(R: Ring&gt;): Ring<s> =&gt; R

/**
 * @since 0.2.0
 */
export const getField = <s>(F: Field&gt;): Field<s> =&gt; F

//
// isos
//

const anyIso = new Iso(unsafeCoerce, unsafeCoerce)

/**
 * @since 0.2.0
 */
export function iso<s>(): Iso&gt; {
  return anyIso
}

//
// prisms
//

/**
 * @since 0.2.0
 */
export interface Concat, N2 extends Newtype&gt;&gt;</s></s></s></s></s>
github gcanti / newtype-ts / test / index.ts View on Github external
it('should allow optic management', () =&gt; {
    const person: Person = {
      name: 'Giulio',
      age: age.wrap(43)
    }
    const ageLens = Lens.fromProp()('age').compose(age.asLens())
    assert.deepEqual(ageLens.set(44)(person), {
      name: 'Giulio',
      age: 44
    })
  })
})
github unmock / unmock-js / packages / unmock-core / src / generator.ts View on Github external
t: Traversal,
  oai: OpenAPIObject,
  p: PathItem,
): Parameter[] =&gt;
  t
    .composePrism(
      new Prism(
        s =&gt;
          isReference(s)
            ? getParameterFromRef(oai, s.$ref.split("/")[3])
            : some(s),
        a =&gt; a,
      ),
    )
    .composePrism(
      new Prism(
        s =&gt;
          s.in === (header ? "header" : "query") &amp;&amp; s.required ? some(s) : none,
        a =&gt; a,
      ),
    )
    .composeGetter(new Getter(i =&gt; i))
    .getAll(p)
    .filter(a =&gt;
      a.schema ? !isNone(schemaPrism(oai).getOption(a.schema)) : false,
    )
    .map(b =&gt; ({
      ...b,
      schema: b.schema
        ? isReference(b.schema)
          ? changeRef(b.schema)
          : changeRefs(b.schema)
github unmock / unmock-js / packages / unmock-core / src / generator.ts View on Github external
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)
        : { type: "string" },
    }));
github SamHH / bukubrow-webext / src / store / browser / types.ts View on Github external
import { Lens } from 'monocle-ts';

export interface BrowserState {
	pageTitle: string;
	pageUrl: string;
}

export const pageTitle = Lens.fromProp()('pageTitle');
export const pageUrl = Lens.fromProp()('pageUrl');

export enum BrowserActionTypes {
	SyncBrowser = 'SYNC_BROWSER',
}
github SamHH / bukubrow-webext / src / modules / settings.ts View on Github external
elemC(eqString)(arg),
);

const badgeDisplayCodec = fromRefinement(
	'badgeDisplay',
	(x): x is BadgeDisplay =&gt; t.string.is(x) &amp;&amp; isBadgeDisplayOpt(x),
);

const settingsCodec = t.type({
	theme: optionFromNullable(themeCodec),
	badgeDisplay: optionFromNullable(badgeDisplayCodec),
});

export type Settings = t.TypeOf;

const theme = Lens.fromProp()('theme');
const badgeDisplay = Lens.fromProp()('badgeDisplay');

export const saveSettings = (opts: Settings): TaskEither =&gt; setSyncStorage(opts);

const getSettings: TaskEither = pipe(
	getSyncStorage(['theme', 'badgeDisplay']),
	T.map(E.chain(decode(settingsCodec))),
);

export const getActiveTheme: TaskEither&gt; = pipe(
	getSettings,
	T.map(E.map(flow(
		theme.get,
		O.chain(O.fromPredicate(isTheme)),
	))));
github SamHH / bukubrow-webext / src / store / user / types.ts View on Github external
import { Lens } from 'monocle-ts';
import { Theme } from 'Modules/settings';
export { Theme };

export interface UserState {
	hasBinaryComms: boolean;
	activeTheme: Theme;
	displayOpenAllBookmarksConfirmation: boolean;
	page: Page;
}

export const hasBinaryComms = Lens.fromProp()('hasBinaryComms');
export const activeTheme = Lens.fromProp()('activeTheme');
export const displayOpenAllBookmarksConfirmation = Lens.fromProp()('displayOpenAllBookmarksConfirmation');
export const page = Lens.fromProp()('page');

export enum UserActionTypes {
	SetHasBinaryComms = 'SET_HAS_BINARY_COMMS',
	SetActiveTheme = 'SET_ACTIVE_THEME',
	SetDisplayOpenAllBookmarksConfirmation = 'SET_OPEN_ALL_BOOKMARKS_CONFIRMATION',
	SetPage = 'SET_PAGE',
}

export enum Page {
	Search,
	AddBookmark,
	EditBookmark,
	StagedGroupsList,
	StagedGroup,
github SamHH / bukubrow-webext / src / modules / bookmarks.ts View on Github external
}

/*
 * Bookmark as stored in LocalStorage.
 */
export interface LocalBookmark extends LocalBookmarkUnsaved {
	id: number;
}


export interface LocalBookmarkWeighted extends LocalBookmark {
	weight: URLMatch;
}

export const id = Lens.fromProp()('id');
export const title = Lens.fromProp()('title');
export const weight = Lens.fromProp()('weight');

const ordTitle: Ord = contramap(title.get)(ordString);
const ordWeight: Ord = contramap(weight.get)(ordURLMatch)
export const ordLocalBookmarkWeighted = getSemigroup().concat(ordWeight, ordTitle);

/**
 * Filter out bookmarks that do not perfectly match the provided test.
 */
export const filterBookmarks = (bookmarks: Array, test: ParsedInputResult): Array =&gt;
	bookmarks.filter((bookmark) =&gt; {
		if (!includesCI(test.name)(bookmark.title)) return false;
		if (test.desc.some(d =&gt; !includesCI(d)(bookmark.desc))) return false;
		if (test.url.some(u =&gt; !includesCI(u)(bookmark.url))) return false;
		if (test.tags.some(t =&gt; !bookmark.tags.some(tag =&gt; includesCI(t)(tag)))) return false;
github SamHH / bukubrow-webext / src / modules / staged-groups.ts View on Github external
import { Lens } from 'monocle-ts';
import { LocalBookmark } from 'Modules/bookmarks';

export interface StagedBookmarksGroup {
	id: number;
	time: number;
	bookmarks: Array;
}

export const id = Lens.fromProp()('id');
export const time = Lens.fromProp()('time');
export const bookmarks = Lens.fromProp()('bookmarks');