import { isArray, isPlainObject, isFunction } from '../../internal/base' import { looseCurryN } from '../../functional' import { isMutator } from '../particles' import { Data, Mutation, isData, isMutation, isAtom } from '../atoms' import { mutationToDataS, dataToMutationS } from './transform.helpers' import type { IsArray, IsTuple, ObjectPrettify } from '../../@types/index' import type { Mutator } from '../particles' import type { DataLike, MutationLike } from '../atoms' import type { ReplayDataMediator, ReplayMutationMediator } from '../mediators' interface BatchMakeOptions { forceWrap: boolean } const DEFAULT_BATCH_MAKE_OPTIONS: Required = { forceWrap: false } /** * Items will be wrapped in `Data`. */ function forceWrapToData (target: V): Data { return Data.of(target) } type WrapToData = Target extends Data ? Data : Target extends ReplayDataMediator ? ReplayDataMediator : Target extends Mutation ? Data : Target extends ReplayMutationMediator ? ReplayDataMediator : Data /** * Mutations will be transform to Data, Datas will not be handled, other values will be wrapped in Data. */ function wrapToData (target: Data): Data function wrapToData (target: ReplayDataMediator): ReplayDataMediator function wrapToData

(target: Mutation): Data function wrapToData

(target: ReplayMutationMediator): ReplayDataMediator function wrapToData (target: V): Data function wrapToData (target: any): any { return isData(target) ? target : (isMutation(target) ? mutationToDataS(target) : Data.of(target)) } export type WrapDataInArray = IsArray extends true // check whether the array is readonly ? T extends any[] ? (T extends Array ? Array> : never) : (T extends ReadonlyArray ? ReadonlyArray> : never) : IsTuple extends true // check whether the tuple is readonly ? T extends any[] ? T extends [infer First, ...infer Rest] ? [WrapToData, ...WrapDataInArray] : [] : T extends readonly [infer First, ...infer Rest] ? readonly [WrapToData, ...WrapDataInArray] : readonly [] : never export type ForceWrapDataInArray = IsArray extends true // check whether the array is readonly ? T extends any[] ? (T extends Array ? Array> : never) : (T extends ReadonlyArray ? ReadonlyArray> : never) : IsTuple extends true // check whether the tuple is readonly ? T extends any[] ? T extends [infer First, ...infer Rest] ? [Data, ...ForceWrapDataInArray] : [] : T extends readonly [infer First, ...infer Rest] ? readonly [Data, ...ForceWrapDataInArray] : readonly [] : never /** * forceWrap - true: items will be wrapped in Data. See {@link forceWrapToData}. * * forceWrap - false: Mutations will be transform to Data, Datas will not be handled, * other values will be wrapped in Data. See {@link wrapToData}. * @example * ``` * const tupleA: [number, Data, number] = [1, Data.of(2), 3] * const arrayA = [1, 2, 3] * // Expect: [Data, Data, Data] * const dataInTupleA = makeDataInArray(tupleA) * // Expect: Data[] * const dataInArrayA = makeDataInArray(arrayA) * ``` */ export function makeDataInArray ( targets: V, options?: BatchMakeOptions & { forceWrap: false } ): WrapDataInArray export function makeDataInArray ( targets: V, options?: BatchMakeOptions & { forceWrap: true } ): ForceWrapDataInArray export function makeDataInArray ( targets: V, options: BatchMakeOptions = DEFAULT_BATCH_MAKE_OPTIONS ): Array> { if (!isArray(targets)) { throw new TypeError('"targets" is expected to be type of "Array".') } const preparedOptions = { ...DEFAULT_BATCH_MAKE_OPTIONS, ...options } const { forceWrap } = preparedOptions const wrapFn = forceWrap ? forceWrapToData : wrapToData return targets.map(wrapFn) } /** * Curried version of {@link makeDataInArray}. */ export const makeDataInArray_ = looseCurryN(1, makeDataInArray) type WrapDataInObject> = ObjectPrettify<{ [Key in keyof T]: WrapToData }> type ForceWrapDataInObject> = ObjectPrettify<{ [Key in keyof T]: Data }> /** * forceWrap - true: items will be wrapped in Data. See {@link forceWrapToData}. * * forceWrap - false: Mutations will be transform to Data, Datas will not be handled, * other values will be wrapped in Data. See {@link wrapToData}. * @example * ``` * const indexedObject: { [key: string]: number } = { a: 1, b: 2, c: 3 } * // Expect: Record> * const dataInIndexedObject = makeDataInObject(indexedObject) * const namedObject = { a: 1, b: Data.of(2), c: 3 } * // Expect: { a: Data, b: Data, c: Data } * const dataInNamedObject = makeDataInObject(namedObject) * ``` */ export function makeDataInObject > ( targets: V, options?: BatchMakeOptions & { forceWrap: false } ): WrapDataInObject export function makeDataInObject > ( targets: V, options?: BatchMakeOptions & { forceWrap: true } ): ForceWrapDataInObject export function makeDataInObject = any> ( targets: Record, options: BatchMakeOptions = DEFAULT_BATCH_MAKE_OPTIONS ): Record> { if (!isPlainObject(targets)) { throw new TypeError('"targets" is expected to be type of "PlainObject".') } const preparedOptions = { ...DEFAULT_BATCH_MAKE_OPTIONS, ...options } const { forceWrap } = preparedOptions const result: Record = { } const wrapFn = forceWrap ? forceWrapToData : wrapToData Object.entries(targets).forEach(([key, value]) => { result[key] = wrapFn(value) }) return result } /** * Curried version of {@link makeDataInObject}. */ export const makeDataInObject_ = looseCurryN(1, makeDataInObject) /** * Datas will be transform to Mutation, Mutations will not be handled, other values will be wrapped in Mutation. */ const forceWrapToMutation = (item: V): Mutation => Mutation.of(() => item) interface IWrapToMutation { (item: Data): Mutation (item: ReplayDataMediator): ReplayMutationMediator (item: Mutation): Mutation (item: ReplayMutationMediator): ReplayMutationMediator (item: Mutator): Mutation (item: (prev?: P, cur?: C, ...args: any[]) => C): Mutation (item: V): Mutation } /** * Item will be wrapped in Mutation. */ const wrapToMutation: IWrapToMutation = (item: any): any => { if (isData(item)) { return dataToMutationS(item) } else if (isMutation(item)) { return item } else if (isMutator(item)) { return Mutation.of(item) } else if (isFunction(item)) { return Mutation.ofLiftBoth(item) } else { return Mutation.of(() => item) } } /** * forceWrap - true: items will be wrapped in Mutation. * * forceWrap - false: Datas will be transform to Mutation, Mutations will not be handled, other values will be wrapped in Mutation. */ export const createMutationInArray = ( arr: V[], options: BatchMakeOptions = DEFAULT_BATCH_MAKE_OPTIONS ): Array> => { if (!isArray(arr)) { throw new TypeError('"arr" is expected to be type of "Array".') } const { forceWrap } = { ...DEFAULT_BATCH_MAKE_OPTIONS, ...options } const wrapFn = forceWrap ? forceWrapToMutation : wrapToMutation return arr.map(wrapFn) } export const createMutationInArray_ = looseCurryN(1, createMutationInArray) /** * forceWrap - true: items will be wrapped in Mutation. * * forceWrap - false: Datas will be transform to Mutation, Mutations will not be handled, other values will be wrapped in Mutation. */ export const createMutationInObject = ( obj: Record, options: BatchMakeOptions = DEFAULT_BATCH_MAKE_OPTIONS ): Record> => { if (!isPlainObject(obj)) { throw new TypeError('"obj" is expected to be type of "PlainObjcet"') } const { forceWrap } = { ...DEFAULT_BATCH_MAKE_OPTIONS, ...options } const result: Record = {} const wrapFn = forceWrap ? forceWrapToMutation : wrapToMutation Object.entries(obj).forEach(([key, value]) => { result[key] = wrapFn(value) }) return result } export const createMutationInObject_ = looseCurryN(1, createMutationInObject) /** * functions will be wrapped in Mutation, other values will be wrapped in Data. */ const forceWrapToAtom = forceWrapToData interface IWrapToAtom { (item: V): Data (item: ReplayDataMediator): ReplayDataMediator (item: Mutation): Mutation (item: ReplayMutationMediator): ReplayMutationMediator (item: Mutator): Mutation (item: (prev?: P, cur?: C, ...args: any[]) => C): Mutation (item: V): Data } /** * Atoms will not be handled, functions will be wrapped in Mutation, other values will be wrapped in Data. */ const wrapToAtom: IWrapToAtom = (item: any): any => { if (isAtom(item)) { return item } else if (isMutator(item)) { return Mutation.of(item) } else if (isFunction(item)) { return Mutation.ofLiftBoth(item) } else { return Data.of(item) } } /** * forceWrap - true: functions will be wrapped in Mutation, other values will be wrapped in Data. * * forceWrap - false: Atoms will not be handled, functions will be wrapped in Mutation, other values will be wrapped in Data. */ export const createAtomInArray = ( arr: V[], options: BatchMakeOptions = DEFAULT_BATCH_MAKE_OPTIONS ): Array | MutationLike> => { if (!isArray(arr)) { throw new TypeError('"arr" is expected to be type of "Array".') } const { forceWrap } = { ...DEFAULT_BATCH_MAKE_OPTIONS, ...options } const wrapFn = forceWrap ? forceWrapToAtom : wrapToAtom return arr.map(wrapFn) } export const createAtomInArray_ = looseCurryN(1, createAtomInArray) /** * forceWrap - true: functions will be wrapped in Mutation, other values will be wrapped in Data. * * forceWrap - false: Atoms will not be handled, functions will be wrapped in Mutation, other values will be wrapped in Data. */ export const createAtomInObject = ( obj: Record, options: BatchMakeOptions = DEFAULT_BATCH_MAKE_OPTIONS ): Record | MutationLike> => { if (!isPlainObject(obj)) { throw new TypeError('"obj" is expected to be type of "PlainObject".') } const { forceWrap = false } = options const result: Record = {} const wrapFn = forceWrap ? forceWrapToAtom : wrapToAtom Object.entries(obj).forEach(([key, value]) => { result[key] = wrapFn(value) }) return result } export const createAtomInObject_ = looseCurryN(1, createAtomInObject)