1 |
|
2 |
|
3 |
|
4 | 'use strict';
|
5 |
|
6 | module.exports = class ObjectHelper {
|
7 |
|
8 | static push (value, key, data) {
|
9 | if (Array.isArray(data[key])) {
|
10 | data[key].push(value);
|
11 | } else if (data) {
|
12 | data[key] = [value];
|
13 | }
|
14 | }
|
15 |
|
16 | static getValue (keys, data, defaults) {
|
17 | if (!Array.isArray(keys)) {
|
18 | return data && Object.prototype.hasOwnProperty.call(data, keys) ? data[keys] : defaults;
|
19 | }
|
20 | const values = [];
|
21 | for (const key of keys) {
|
22 | values.push(data && Object.prototype.hasOwnProperty.call(data, key) ? data[key] : defaults);
|
23 | }
|
24 | return values;
|
25 | }
|
26 |
|
27 | static getValueOrKey (keys, data) {
|
28 | if (!Array.isArray(keys)) {
|
29 | return data && Object.prototype.hasOwnProperty.call(data, keys) ? data[keys] : keys;
|
30 | }
|
31 | const result = [];
|
32 | for (const key of keys) {
|
33 | result.push(data && Object.prototype.hasOwnProperty.call(data, key) ? data[key] : key);
|
34 | }
|
35 | return result;
|
36 | }
|
37 |
|
38 | static getKeyByValue (value, data) {
|
39 | if (data) {
|
40 | for (const key of Object.keys(data)) {
|
41 | if (data[key] === value) {
|
42 | return key;
|
43 | }
|
44 | }
|
45 | }
|
46 | }
|
47 |
|
48 | static getKeysByValue (value, data) {
|
49 | const keys = [];
|
50 | if (data) {
|
51 | for (const key of Object.keys(data)) {
|
52 | if (data[key] === value) {
|
53 | keys.push(key);
|
54 | }
|
55 | }
|
56 | }
|
57 | return keys;
|
58 | }
|
59 |
|
60 | static getAllPropertyNames (data) {
|
61 | if (!data) {
|
62 | return [];
|
63 | }
|
64 | const names = Object.getOwnPropertyNames(data);
|
65 | for (const name of this.getAllPropertyNames(Object.getPrototypeOf(data))) {
|
66 | if (!names.includes(name)) {
|
67 | names.push(name);
|
68 | }
|
69 | }
|
70 | return names;
|
71 | }
|
72 |
|
73 | static getAllFunctionNames (data) {
|
74 | const result = [];
|
75 | for (const item of this.getAllPropertyNames(data)) {
|
76 | if (typeof data[item] === 'function') {
|
77 | result.push(item);
|
78 | }
|
79 | }
|
80 | return result;
|
81 | }
|
82 |
|
83 | static filterByKeys (keys, data) {
|
84 | const result = {};
|
85 | if (Array.isArray(keys) && data) {
|
86 | for (const key of keys) {
|
87 | if (Object.prototype.hasOwnProperty.call(data, key)) {
|
88 | result[key] = data[key];
|
89 | }
|
90 | }
|
91 | }
|
92 | return result;
|
93 | }
|
94 |
|
95 | static sortByKeys (keys, data) {
|
96 | return Object.assign(this.filterByKeys(keys, data), data);
|
97 | }
|
98 |
|
99 | static hasCircularLinks (target, key, source = target) {
|
100 | return target && target[key]
|
101 | ? target[key] === source || this.hasCircularLinks(target[key], key, source)
|
102 | : false;
|
103 | }
|
104 |
|
105 | static addKeyAsNestedValue (valueKey, data) {
|
106 | if (data) {
|
107 | for (const key of Object.keys(data)) {
|
108 | if (data[key]) {
|
109 | data[key][valueKey] = key;
|
110 | }
|
111 | }
|
112 | }
|
113 | }
|
114 |
|
115 | static replaceKeys (keyMap, data) {
|
116 | if (keyMap && data) {
|
117 | for (const key of Object.keys(data)) {
|
118 | if (Object.prototype.hasOwnProperty.call(keyMap, key) && key !== keyMap[key]) {
|
119 | data[keyMap[key]] = data[key];
|
120 | delete data[key];
|
121 | }
|
122 | }
|
123 | }
|
124 | }
|
125 |
|
126 |
|
127 |
|
128 | static deleteEmptyProperties (data, names) {
|
129 | const check = value => value === null || value === '' || value === undefined;
|
130 | this.deletePropertiesByValue(check, data, names);
|
131 | }
|
132 |
|
133 | static deleteEmptyArrayProperties (data, names) {
|
134 | const check = value => Array.isArray(value) && !value.length;
|
135 | this.deletePropertiesByValue(check, data, names);
|
136 | }
|
137 |
|
138 | static deleteEmptyObjectProperties (data, names) {
|
139 | const check = obj => obj && obj.constructor === Object && !Object.values(obj).length;
|
140 | this.deletePropertiesByValue(check, data, names);
|
141 | }
|
142 |
|
143 | static deletePropertiesByValue (value, data, names) {
|
144 | if (!data) {
|
145 | return;
|
146 | }
|
147 | if (!Array.isArray(names)) {
|
148 | names = Object.keys(data);
|
149 | }
|
150 | for (const name of names) {
|
151 | if (typeof value === 'function' ? value(data[name]) : (data[name] === value)) {
|
152 | delete data[name];
|
153 | }
|
154 | }
|
155 | }
|
156 |
|
157 | static deleteProperties (names, data) {
|
158 | if (Array.isArray(names) && data) {
|
159 | for (const name of names) {
|
160 | if (Object.prototype.hasOwnProperty.call(data, name)) {
|
161 | delete data[name];
|
162 | }
|
163 | }
|
164 | }
|
165 | }
|
166 |
|
167 | static deletePropertiesExcept (names, data) {
|
168 | if (Array.isArray(names) && data) {
|
169 | for (const name of Object.keys(data)) {
|
170 | if (!names.includes(name)) {
|
171 | delete data[name];
|
172 | }
|
173 | }
|
174 | }
|
175 | }
|
176 | }; |
\ | No newline at end of file |