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 => {
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 {
transformation: MutatorOriginalTransformationUnion
options?: MutationOptions
}
export type ObjectSMTache = (source: AtomLikeOfOutput ) => Record } ObjectSSTache :: `(source) => Record (configObj: Record => {
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 (config: Array
(config: Record
}
/**
* @return TacheMaker
*/
export const createSMTache: ICreateSMTache = (config: any): any => {
if (isPlainObject(config)) {
return createObjectSMTache(config)
} else if (isArray