├── README.md ├── index.html └── js └── scripts.js /README.md: -------------------------------------------------------------------------------- 1 | # testForIntegritycomua 2 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | DZ2 8 | 9 | 10 | 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /js/scripts.js: -------------------------------------------------------------------------------- 1 | class Transformer { 2 | constructor(localization = {}) { 3 | this.localization = localization; 4 | this.result = {}; 5 | } 6 | 7 | getResult() { 8 | return Object.values(this.result); 9 | } 10 | 11 | clearResult() { 12 | this.result = {}; 13 | } 14 | 15 | pushDataToResult(data) { 16 | let value = `value${data.index+1}`; 17 | if (typeof this.result[data.fullKey] === 'object') { 18 | this.result[data.fullKey][value] = data.value; 19 | } else { 20 | const localizedLabel = this.localization[data.fullKey] ? this.localization[data.fullKey] : data.shortKey; 21 | this.result[data.fullKey] = { 22 | 'name': localizedLabel, 23 | }; 24 | this.result[data.fullKey][value] = data.value 25 | } 26 | } 27 | 28 | trasnformArray(items, rules, parentKey = '') { 29 | items.forEach((item,index) => { 30 | let objectIndex = index 31 | this.transformObject(item, rules, parentKey, objectIndex); 32 | }); 33 | } 34 | 35 | transformObject(item, rules, parentKey = '', objectIndex) { 36 | Object.keys(item).forEach(property => { 37 | this.transformProperty(property, item[property], rules, parentKey, objectIndex); 38 | }); 39 | } 40 | 41 | transformProperty(property, data, rules, parentKey = '', objectIndex) { 42 | if (typeof(rules[property]) !== 'object' && !rules[property]) { 43 | return; 44 | } 45 | let transformedValue = null; 46 | const currentKey = parentKey ? parentKey + '.' + property : property; 47 | if (data instanceof Date) { 48 | transformedValue = this.formatDate(data); 49 | } else if (typeof(data) === 'boolean') { 50 | transformedValue = data ? 'Так' : 'Ні'; 51 | } else if (data instanceof Array) { 52 | transformedValue = data.join(', ') 53 | this.trasnformArray(data, rules[property], currentKey); 54 | } else if (typeof(data) === 'object') { 55 | this.transformObject(data, rules[property], currentKey, objectIndex); 56 | } else { 57 | transformedValue = data; 58 | } 59 | if (transformedValue !== null) { 60 | this.pushDataToResult({ 61 | shortKey: property, // is used when localization not found 62 | fullKey: currentKey, 63 | value: transformedValue, 64 | index: objectIndex, 65 | }); 66 | } 67 | } 68 | 69 | formatDate(date){ 70 | var dd = date.getDate(); 71 | if (dd < 10) dd = '0' + dd; 72 | 73 | var MM = date.getMonth() + 1; 74 | if (MM < 10) MM = '0' + MM; 75 | 76 | var yyyy = date.getFullYear() ; 77 | 78 | return `${dd}.${MM}.${yyyy}` 79 | } 80 | } 81 | 82 | // test data example 83 | const data = [ 84 | { 85 | fullName: {surname : 'xxx', firstName : 'yyy', middleName: 'zzz'}, 86 | age: 23, 87 | birthDate: 88 | new Date('01-31-2017'), 89 | isSmoking: false 90 | }, 91 | { 92 | fullName : {surname : 'XXX', firstName : 'YYY', middleName: 'ZZZ'} 93 | }, 94 | { 95 | testField: 'does not exist anywhere' 96 | }, 97 | { 98 | fullName: {surname : 'Smith', firstName : 'John', middleName: 'Ivanovich'}, 99 | job: { 100 | title: "Software developer", 101 | skills: ['js', 'css', 'html'], 102 | expectations: { 103 | shortTerm: "good salary", 104 | longTerm: "become a boss", 105 | } 106 | }, 107 | }, 108 | ]; 109 | 110 | const transformationRules = { 111 | fullName: { 112 | surname: true, 113 | firstName: true, 114 | middleName: false 115 | }, 116 | age: true, 117 | birthDate: true, 118 | isSmoking: true, 119 | children: true, 120 | job: { 121 | title: true, 122 | skills: true, 123 | expectations: { 124 | shortTerm: true, 125 | longTerm: true, 126 | } 127 | } 128 | }; 129 | 130 | const localization = {"fullName.surname" : "Прізвище", "fullName.middleName" : "По-батькові", "job.expectations.shortTerm": "Короткострокові очікування"} ; 131 | 132 | const dataTransformer = new Transformer(localization); 133 | dataTransformer.trasnformArray(data, transformationRules); 134 | console.log(dataTransformer.getResult()); --------------------------------------------------------------------------------