limu
Version:
A fast js lib of immutable data, based on shallow copy on read and mark modified on write mechanism
126 lines (125 loc) • 4.94 kB
TypeScript
import { deepFreeze as deepFreezeFn } from './core/freeze';
import { getDraftMeta, isDiff as isDiffFn, isDraft as isDraftFn, shallowCompare as shallowCompareFn } from './core/meta';
import { current as currentFn, markRaw as markRawFn, original as originalFn } from './core/user-util';
import type { ICreateDraftOptions, IOperateParams, ObjectLike, Op } from './inner-types';
import { canBeNum, getDataType, has, isFn, isMap, isMardedRaw, isObject, isPrimitive, isPromiseFn, isPromiseResult, isSet, isSymbol, noop } from './support/util';
export type { ICreateDraftOptions, IOperateParams, ObjectLike, Op };
/**
* 判断是否是草稿
* @see https://tnfe.github.io/limu/docs/api/basic/limu-utils#isdraft
*/
export declare const isDraft: typeof isDraftFn;
/**
* 判断两个值是否相同
* @see https://tnfe.github.io/limu/docs/api/basic/limu-utils#isdiff
*/
export declare const isDiff: typeof isDiffFn;
/**
* 浅比较两个对象是否相同
* @see https://tnfe.github.io/limu/docs/api/basic/limu-utils#shallowcompare
*/
export declare const shallowCompare: typeof shallowCompareFn;
/**
* 内部工具函数集合,写为如下格式会降低覆盖率,故导入后再导出
* ```txt
* import * as limuUtils; from './support/util';
* const { isFn, isPromiseFn, isPromiseResult } = limuUtils;;
* export { limuUtils; };
* ```
*/
export declare const limuUtils: {
has: typeof has;
noop: typeof noop;
isObject: typeof isObject;
isMap: typeof isMap;
isSet: typeof isSet;
isFn: typeof isFn;
isPrimitive: typeof isPrimitive;
isPromiseFn: typeof isPromiseFn;
isPromiseResult: typeof isPromiseResult;
isSymbol: typeof isSymbol;
isMardedRaw: typeof isMardedRaw;
canBeNum: typeof canBeNum;
isDraft: typeof isDraftFn;
isDiff: typeof isDiffFn;
shallowCompare: typeof shallowCompareFn;
getDraftMeta: typeof getDraftMeta;
getDataType: typeof getDataType;
};
export declare type LimuUtils = typeof limuUtils;
export declare type Draft<T> = T;
export declare type CreateDraft = <T = ObjectLike>(base: T, options?: ICreateDraftOptions) => Draft<T>;
export declare type FinishDraft = <T = ObjectLike>(draft: T) => T;
export declare type ProduceCb<T> = (draft: Draft<T>) => void;
export declare type GenNewStateCb<T> = (state: T) => T;
/**
* 同步完成生成草稿、修改草稿、结束草稿3个步骤的函数
*/
export interface IProduce {
<T = ObjectLike>(baseState: T, recipe: ProduceCb<T>, options?: ICreateDraftOptions): T;
/**
* use in react:
* setState(produce(draft=>{
* draft.name = 2;
* }));
*/
<T = ObjectLike>(recipe: ProduceCb<T>, options?: ICreateDraftOptions): GenNewStateCb<T>;
}
/** limu 的版本号 */
export declare const VER = "3.13.1";
/**
* 创建草稿函数,可对返回的草稿对象直接修改,此修改不会影响原始对象
* @see https://tnfe.github.io/limu/docs/api/basic/create-draft
*/
export declare function createDraft<T = ObjectLike>(base: T, options?: ICreateDraftOptions): Draft<T>;
/**
* 结束草稿的函数,生成的新对象和原始对象会结构共享
* @see https://tnfe.github.io/limu/docs/api/basic/create-draft
*/
export declare function finishDraft<T = ObjectLike>(draft: Draft<T>): T;
/**
* ```ts
* // normal use:
* const next = produce(draft=>draft.a=1);
*
* // use in react:
* setState(produce(draft=>{
* draft.name = 2;
* }));
* ```
*/
export declare const produce: IProduce;
/**
* 深冻结传入的值,返回的是冻结后的新值,如传入原始值则不做任何操作,原样返回
*/
export declare const deepFreeze: typeof deepFreezeFn;
/**
* 对传入值做深克隆,返回的是克隆后的新值,如传入原始值则不做任何操作,原样返回
*/
export declare function deepCopy<T = ObjectLike>(obj: T): T;
/**
* 生成一个不可修改的对象im,但原始对象的修改将同步会影响到im
* immut 采用了读时浅代理的机制,相比deepFreeze会拥有更好性能,适用于不暴露原始对象出去,只暴露生成的不可变对象出去的场景
* @see: https://tnfe.github.io/limu/docs/api/basic/immut
*/
export declare function immut<T = ObjectLike>(base: T, options?: Omit<ICreateDraftOptions, 'readOnly'>): T;
/**
* 设置全局冻结配置,可在 createDraft, produce 时二次覆盖此全局配置
*/
export declare function setAutoFreeze(autoFreeze: boolean): void;
/**
* 获取全局设置的 autoFreeze 值
*/
export declare function getAutoFreeze(): boolean;
/**
* 查看草稿对应的原始值
*/
export declare const original: typeof originalFn;
/**
* 标记对象为 raw,此对象再次被复用时不会被代理,需注意标为 raw 后此对象会失去结构共享特性
*/
export declare const markRaw: typeof markRawFn;
/**
* 导出草稿的副本数据( 即深克隆当前草稿 )
*/
export declare const current: typeof currentFn;