monocle-ts-esm
Version:
A porting of scala monocle library to TypeScript
80 lines (79 loc) • 2.31 kB
JavaScript
/**
* **This module is experimental**
*
* Experimental features are published in order to get early feedback from the community.
*
* A feature tagged as _Experimental_ is in a high state of flux, you're at risk of it changing without notice.
*
* @since 2.3.0
*/
import * as O from 'fp-ts-esm/Option';
import { pipe } from 'fp-ts-esm/pipeable';
import * as _ from './internal.js';
import * as RM from 'fp-ts-esm/ReadonlyMap';
// -------------------------------------------------------------------------------------
// constructors
// -------------------------------------------------------------------------------------
/**
* @category constructors
* @since 2.3.8
*/
export const index = _.index;
/**
* @category constructors
* @since 2.3.0
*/
export const fromAt = (at) => index((i) => _.lensComposePrism(_.prismSome())(at.at(i)));
/**
* Lift an instance of `Index` using an `Iso`.
*
* @category constructors
* @since 2.3.0
*/
export const fromIso = (iso) => (sia) => index((i) => pipe(iso, _.isoAsOptional, _.optionalComposeOptional(sia.index(i))));
/**
* @category constructors
* @since 2.3.7
*/
export const indexReadonlyArray = _.indexReadonlyArray;
/**
* @category constructors
* @since 2.3.8
*/
export const indexReadonlyNonEmptyArray = _.indexReadonlyNonEmptyArray;
/**
* @category constructors
* @since 2.3.7
*/
export const indexReadonlyRecord = _.indexReadonlyRecord;
/**
* @category constructors
* @since 2.3.7
*/
export const indexReadonlyMap = (E) => {
const lookupE = RM.lookup(E);
const insertAtE = RM.insertAt(E);
return () => index((key) => _.optional((s) => lookupE(key, s), (next) => {
const insert = insertAtE(key, next);
return (s) => pipe(lookupE(key, s), O.fold(() => s, (prev) => (next === prev ? s : insert(s))));
}));
};
// -------------------------------------------------------------------------------------
// deprecated
// -------------------------------------------------------------------------------------
/**
* Use `indexReadonlyArray` instead.
*
* @category constructors
* @since 2.3.2
* @deprecated
*/
export const indexArray = _.indexReadonlyArray;
/**
* Use `indexReadonlyRecord` instead.
*
* @category constructors
* @since 2.3.2
* @deprecated
*/
export const indexRecord = _.indexReadonlyRecord;