How to use the io-ts-types/lib/fromRefinement.fromRefinement function in io-ts-types

To help you get started, we’ve selected a few io-ts-types 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 SamHH / bukubrow-webext / src / modules / settings.ts View on Github external
import { elemC } from 'Modules/array';
import { values } from 'Modules/record';
import { decode } from 'Modules/io';
import { getSyncStorage, setSyncStorage } from 'Modules/sync';

export enum Theme {
	Light = 'light',
	Dark = 'dark',
}

export const isTheme: Refinement = (arg): arg is Theme => pipe(
	values(Theme),
	elemC(eqString)(arg),
);

const themeCodec = fromRefinement(
	'theme',
	(x): x is Theme => t.string.is(x) && isTheme(x),
);

export enum BadgeDisplay {
	WithCount = 'with_count',
	WithoutCount = 'without_count',
	None = 'none',
}

export const isBadgeDisplayOpt: Refinement = (arg): arg is BadgeDisplay => pipe(
	values(BadgeDisplay),
	elemC(eqString)(arg),
);

const badgeDisplayCodec = fromRefinement(
github SamHH / bukubrow-webext / src / modules / settings.ts View on Github external
'theme',
	(x): x is Theme => t.string.is(x) && isTheme(x),
);

export enum BadgeDisplay {
	WithCount = 'with_count',
	WithoutCount = 'without_count',
	None = 'none',
}

export const isBadgeDisplayOpt: Refinement = (arg): arg is BadgeDisplay => pipe(
	values(BadgeDisplay),
	elemC(eqString)(arg),
);

const badgeDisplayCodec = fromRefinement(
	'badgeDisplay',
	(x): x is BadgeDisplay => t.string.is(x) && 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 => setSyncStorage(opts);