import { isArray, isPlainObject, isFunction } from '../../internal/base' import { Data, Mutation, isAtomLike, DEFAULT_MUTATION_OPTIONS } from '../atoms' import { pipeAtom, binaryTweenPipeAtom } from '../helpers' import type { MutatorOriginalTransformationUnion } from '../particles' import type { MutationOptions, AtomLikeOfOutput } from '../atoms' // S -> Single, M -> Multi /****************************************************************************************************** * * SM Tache * ******************************************************************************************************/ /** * */ interface ArraySMTacheConfig

{ transformation: MutatorOriginalTransformationUnion options?: MutationOptions } export type ArraySMTache

= (source: AtomLikeOfOutput

) => Array> /** * @param configArr `[{ transformation, options }, ...]` * @return { ArraySMTache

} ArraySSTache :: `(source) => Array>` */ export const createArraySMTache =

(configArr: Array>): ArraySMTache

=> { if (!isArray(configArr)) { throw (new TypeError('"configArr" is expected to be type of "Array".')) } // format configs configArr = configArr.map(({ transformation, options = {} }) => { if (!isFunction(transformation)) { throw (new TypeError('"transformation" is expected to be type of "Function".')) } options = { ...DEFAULT_MUTATION_OPTIONS, ...options } return { transformation, options } }) /** * @param source Atom * @return { Data } Array of Data */ return source => { if (!isAtomLike(source)) { throw (new TypeError('"source" is expected to be type of "AtomLike".')) } const mutations = configArr.map(({ transformation, options }) => { return Mutation.ofLift(transformation, options) }) const outputs = Array.from({ length: configArr.length }).map(() => Data.empty()) outputs.forEach((output, index) => { pipeAtom(mutations[index], output) binaryTweenPipeAtom(source, mutations[index]) }) return outputs } } interface ObjectSMTacheConfig

{ transformation: MutatorOriginalTransformationUnion options?: MutationOptions } export type ObjectSMTache

= (source: AtomLikeOfOutput

) => Record> /** * @param configObj `{ [key: string]: { transformation, options? } }` * @return { ObjectSMTache

} ObjectSSTache :: `(source) => Record>` */ export const createObjectSMTache =

(configObj: Record>): ObjectSMTache

=> { if (!isPlainObject(configObj)) { throw (new TypeError('"configObj" is expected to be type of "PlainObject".')) } // // format configs configObj = Object.entries(configObj).reduce<(typeof configObj)>( (acc, [name, { transformation, options = {} }]) => { if (!isFunction(transformation)) { throw (new TypeError('"transformation" is expected to be type of "Function".')) } acc[name] = acc[name] ?? { transformation, options: { ...DEFAULT_MUTATION_OPTIONS, ...options } } return acc } , {}) /** * @param source Atom * @return Object of Data */ return source => { if (!isAtomLike(source)) { throw (new TypeError('"source" is expected to be type of "AtomLike".')) } const mutations = Object.entries(configObj).reduce>>( (acc, [name, { transformation, options }]) => { acc[name] = acc[name] ?? Mutation.ofLift(transformation, options) return acc } , {}) const outputs = Object.entries(configObj).reduce>>((acc, [name]) => { acc[name] = acc[name] ?? Data.empty() return acc }, {}) Object.entries(outputs).forEach(([name, output]) => { pipeAtom(mutations[name], output) binaryTweenPipeAtom(source, mutations[name]) }) return outputs } } interface ICreateSMTache {

(config: Array>): ArraySMTache

(config: Record>): ObjectSMTache

} /** * @return TacheMaker */ export const createSMTache: ICreateSMTache = (config: any): any => { if (isPlainObject(config)) { return createObjectSMTache(config) } else if (isArray(config)) { return createArraySMTache(config) } else { throw (new TypeError('"config" is expected to be type of "Array" | "PlainObject".')) } }