UNPKG

1.35 kBJavaScriptView Raw
1const debug = require('debug')('Transforms');
2const Storage = require('./Storage');
3const transformsFactory = require('./transforms/transformsFactory');
4
5class Transforms {
6 constructor(options) {
7 this._storage = options.storage || new Storage();
8 }
9
10 /**
11 * Perform transformations to result value
12 * @param {Array.<TransformOptions>} transforms
13 * @param {*} value
14 * @returns {*}
15 */
16 produce(transforms, value) {
17 transforms = transforms || [];
18 value = typeof value === 'undefined' ? '' : value;
19 debug('transforms are producing for %o on %o', transforms, value);
20 return transforms.reduce((value, options) => {
21 value = typeof value === 'undefined' ? '' : value;
22 const transform = transformsFactory.createTransform({
23 options,
24 value,
25 storage: this._storage
26 });
27
28 if (!transform) {
29 throw new Error('Unsupported transform type: ' + options.type);
30 }
31
32 return transform.transform();
33 }, value);
34 }
35
36 /**
37 * Add custom transform
38 * @param {string} type
39 * @param {Function} transform
40 */
41 addTransform(type, transform) {
42 transformsFactory.addTransform(type, transform);
43 }
44}
45
46module.exports = Transforms;