UNPKG

4.92 kBJavaScriptView Raw
1/**
2 * @copyright Copyright (c) 2019 Maxim Khorin <maksimovichu@gmail.com>
3 */
4'use strict';
5
6module.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 // DELETE PROPERTIES
116
117 static deleteEmptyProperties (data, names) {
118 const check = value => value === null || value === '' || value === undefined;
119 this.deletePropertiesByValue(check, data, names);
120 }
121
122 static deleteEmptyArrayProperties (data, names) {
123 const check = value => Array.isArray(value) && !value.length;
124 this.deletePropertiesByValue(check, data, names);
125 }
126
127 static deleteEmptyObjectProperties (data, names) {
128 const check = obj => obj && obj.constructor === Object && !Object.values(obj).length;
129 this.deletePropertiesByValue(check, data, names);
130 }
131
132 static deletePropertiesByValue (value, data, names) {
133 if (!data) {
134 return;
135 }
136 if (!Array.isArray(names)) {
137 names = Object.keys(data);
138 }
139 for (const name of names) {
140 if (typeof value === 'function' ? value(data[name]) : (data[name] === value)) {
141 delete data[name];
142 }
143 }
144 }
145
146 static deleteProperties (names, data) {
147 if (Array.isArray(names) && data) {
148 for (const name of names) {
149 if (Object.prototype.hasOwnProperty.call(data, name)) {
150 delete data[name];
151 }
152 }
153 }
154 }
155
156 static deletePropertiesExcept (names, data) {
157 if (Array.isArray(names) && data) {
158 for (const name of Object.keys(data)) {
159 if (!names.includes(name)) {
160 delete data[name];
161 }
162 }
163 }
164 }
165};
\No newline at end of file