UNPKG

dtl-js

Version:

Data Transformation Language - JSON templates and data transformation

44 lines (34 loc) 2 kB
## `chain()` Function ### Description The `chain()` function in DTL is used to apply a sequence of data transformations in a specific order. It is particularly useful within transform libraries, where it can be employed to execute multiple named transformations in succession. ### Usage ```dtl chain( $data, $transform_chain ) ``` - `$data`: The initial input data to be transformed. - `$transform_chain`: An array that represents the sequence of transformations to apply to the data. ### Details - Each element in the `$transform_chain` represents a step in the transformation process. - The `$data` is passed through each transform or expression in the `$transform_chain` one after the other. - This method enables the composition of complex data transformations in a modular and readable manner. ### Example Consider a scenario where you have a string `$greeting` with the value 'hello'. You want to capitalize it and then reverse it. You can define a `transform_chain` like so: ```dtl { "out": "(: chain($greeting, 'transform_chain') :)", "transform_chain": [ "(: capitalize($.) :)", "(: reverse($.) :)" ] } ``` The `chain()` will apply the transformations in order, resulting in 'olleH' from the initial `$greeting` value 'hello'. ### Considerations - The sequence in which transforms are placed in the `$transform_chain` is critical, as each transform's output will be the input for the next. - It is important to ensure that each transform is compatible with the output of the previous one to prevent errors or unintended results. ### Performance Implications - When using `chain()`, the performance should be considered if individual transforms are resource-intensive or if the data set is large. ### Note - `chain()` enhances the readability and maintainability of complex data transformation sequences. - While it abstracts away complexity, developers should still be mindful of the logical flow and compatibility of transformations within the chain. ```