-
Notifications
You must be signed in to change notification settings - Fork 1
/
evolve.js
77 lines (74 loc) · 2.05 KB
/
evolve.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
* Returns a new object according to the transformation functions.
*
* @param {Object} transforms The object specifying transformation functions to apply.
* @param {Object} obj The object to be transformed.
* @return {Object} The transformed object.
* @example
*
* const product = {
* "id": 66443,
* "image": "aceb.png",
* "width": 965,
* "height": 1040,
* "description": "Description goes here!",
* "categories": ["4114", "4232"],
* "brand": "Brand goes here!",
* "price": 9.99,
* "allergens": {
* "a": 'Allergen A',
* "b": 'Allergen B'
* }
* }
*
* const renameCategory = a => `000_${ a }`
* const discount20 = t => '20% off!! ' + t
* const restoreRetina = h => h * 2
* const applyDiscount20 = p => p - percent(p, 20)
* const renameAllergen = t => `- ${ t }`
*
* const transformations = {
* description: discount20,
* title: discount20,
* categories: [renameCategory, renameCategory],
* height: restoreRetina,
* price: applyDiscount20,
* allergens: {
* a: renameAllergen,
* b:renameAllergen
* }
* }
*
* evolve(transformations, product)
* // {
* // "allergens": Object {
* // "a": "- Allergen A",
* // "b": "- Allergen B"
* // },
* // "brand": "Brand goes here!",
* // "categories": Object {
* // "0": "000_4114",
* // "1": "000_4232"
* // },
* // "description": "20% off!! Description goes here!",
* // "height": 2080,
* // "id": 66443,
* // "image": "aceb.png",
* // "price": 7.992,
* // "width": 965
* // }
*
**/
const evolve = (transforms, obj) => {
const result = {}
Object.keys(obj).forEach(key => {
const transform = transforms[key]
const type = typeof transform
result[key] = type === 'function' ? transform(obj[key])
: transform && type === 'object'
? evolve(transform, obj[key])
: obj[key]
})
return result
}
export default evolve