hola-server 0.4.4 → 0.4.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/core/array.js +33 -12
- package/core/file.js +16 -1
- package/core/number.js +150 -1
- package/core/obj.js +11 -2
- package/index.js +4 -1
- package/package.json +1 -1
package/core/array.js
CHANGED
|
@@ -97,17 +97,6 @@ const avg = function (arr) {
|
|
|
97
97
|
}
|
|
98
98
|
};
|
|
99
99
|
|
|
100
|
-
/**
|
|
101
|
-
* Remove duplicate element and keep it unique
|
|
102
|
-
* @param {array of elements} arr
|
|
103
|
-
* @returns
|
|
104
|
-
*/
|
|
105
|
-
const unique = function (arr) {
|
|
106
|
-
return arr.filter(function (elem, pos) {
|
|
107
|
-
return arr.indexOf(elem) == pos;
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
|
|
111
100
|
/**
|
|
112
101
|
*
|
|
113
102
|
* @param {array of object} arr
|
|
@@ -163,4 +152,36 @@ const sort_by_key_seq = function (arr, attr, keys) {
|
|
|
163
152
|
return arr;
|
|
164
153
|
};
|
|
165
154
|
|
|
166
|
-
|
|
155
|
+
/**
|
|
156
|
+
* Create combination of two object, the size will be arr1.length * arr2.length
|
|
157
|
+
* @param {array of object} arr1
|
|
158
|
+
* @param {array of object} arr2
|
|
159
|
+
* @returns
|
|
160
|
+
*/
|
|
161
|
+
const combine = (arr1, arr2) => {
|
|
162
|
+
const result = [];
|
|
163
|
+
for (let i = 0; i < arr1.length; i++) {
|
|
164
|
+
const obj1 = arr1[i];
|
|
165
|
+
for (let j = 0; j < arr2.length; j++) {
|
|
166
|
+
const obj2 = arr2[j];
|
|
167
|
+
result.push({ ...obj1, ...obj2 });
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return result;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Remove duplicate element and keep it unique
|
|
175
|
+
* @param {array of elements} arr
|
|
176
|
+
* @returns
|
|
177
|
+
*/
|
|
178
|
+
const unique = (array) => {
|
|
179
|
+
return array.filter((value, index) => {
|
|
180
|
+
const _value = JSON.stringify(value);
|
|
181
|
+
return index === array.findIndex(obj => {
|
|
182
|
+
return JSON.stringify(obj) === _value;
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
module.exports = { shuffle, remove_element, pop_n, shift_n, sum, avg, combine, sort_desc, sort_asc, sort_by_key_seq, unique, map_array_to_obj }
|
package/core/file.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
1
2
|
const unzipper = require('unzipper');
|
|
2
3
|
|
|
3
4
|
const file_extension = file_name => file_name ? file_name.split('.').pop() : "";
|
|
@@ -13,4 +14,18 @@ const read_from_zip_by_prefix = async (path, prefix) => {
|
|
|
13
14
|
return directory.files.filter(file => file_prefix(file.path) == prefix);
|
|
14
15
|
}
|
|
15
16
|
|
|
16
|
-
|
|
17
|
+
/**
|
|
18
|
+
* Check the file exist or not
|
|
19
|
+
* @param {the file path} path
|
|
20
|
+
* @returns
|
|
21
|
+
*/
|
|
22
|
+
const is_file_exist = (path) => {
|
|
23
|
+
try {
|
|
24
|
+
fs.accessSync(path, fs.F_OK);
|
|
25
|
+
} catch (e) {
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
module.exports = { file_extension, file_prefix, read_from_zip_by_extension, read_from_zip_by_prefix, is_file_exist }
|
package/core/number.js
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
const { has_value } = require('./validate');
|
|
2
|
+
const { is_object } = require('./obj');
|
|
3
|
+
|
|
1
4
|
/**
|
|
2
5
|
* Parse String value to number
|
|
3
6
|
* @param {String value to parse to number} str
|
|
@@ -27,4 +30,150 @@ const round_to_fixed2 = function (num) {
|
|
|
27
30
|
return Math.round(num * 100) / 100;
|
|
28
31
|
};
|
|
29
32
|
|
|
30
|
-
|
|
33
|
+
/**
|
|
34
|
+
* Generate range array, for example: range(3) = [0,1,2]
|
|
35
|
+
* @param {start index} start
|
|
36
|
+
* @param {end index} end
|
|
37
|
+
* @param {stepping} step
|
|
38
|
+
* @returns
|
|
39
|
+
*/
|
|
40
|
+
const range = (start, end, step = 1) => end ? [...Array(Math.floor((end - start) / step) + 1)].map((_, i) => start + i * step) : Array.from({ length: start }, (_, key) => key);
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Generate scale array, for example: scale(2,10) = [2,4,8]
|
|
44
|
+
* @param {start index} start
|
|
45
|
+
* @param {end index} end
|
|
46
|
+
* @param {scale ratio} scale
|
|
47
|
+
* @returns
|
|
48
|
+
*/
|
|
49
|
+
const scale = (start, end, scale = 2) => [...Array(Math.floor(Math.log(end / start) / Math.log(scale)) + 1)].map((_, i) => start * Math.pow(scale, i));
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Create a space
|
|
53
|
+
* @param {min value} min
|
|
54
|
+
* @param {max value} max
|
|
55
|
+
* @returns
|
|
56
|
+
*/
|
|
57
|
+
const space = (min, max) => {
|
|
58
|
+
if (has_value(min) && has_value(max)) {
|
|
59
|
+
return { ["min"]: min, ["max"]: max };
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
throw new Error("min and max not provide for space");
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Check this value is space object
|
|
67
|
+
* @param {value to be checked} value
|
|
68
|
+
* @returns true if it is space otherwise return false
|
|
69
|
+
*/
|
|
70
|
+
const is_space = (value) => {
|
|
71
|
+
return is_object(value) && has_value(value.min) && has_value(value.max);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* check the object is integer or not
|
|
76
|
+
* @param {string need to check} str
|
|
77
|
+
* @returns
|
|
78
|
+
*/
|
|
79
|
+
const is_integer = (value) => {
|
|
80
|
+
const regex_pattern = /^-?[0-9]+$/;
|
|
81
|
+
return regex_pattern.test(value);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Check the object attr contains space object or not
|
|
86
|
+
* @param {object need to check} obj
|
|
87
|
+
* @returns true if contains space
|
|
88
|
+
*/
|
|
89
|
+
const contains_space = (obj) => {
|
|
90
|
+
for (const key in obj) {
|
|
91
|
+
const value = obj[key];
|
|
92
|
+
if (is_space(value)) {
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Generate the random number
|
|
101
|
+
* @param {min value} min
|
|
102
|
+
* @param {max value} max
|
|
103
|
+
* @returns
|
|
104
|
+
*/
|
|
105
|
+
const random_number = (min, max) => {
|
|
106
|
+
const random = Math.random() * (max - min) + min;
|
|
107
|
+
return is_integer(min) && is_integer(max) ? Math.floor(random) : to_fixed(random);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Use LHS method to generate sample ranges
|
|
112
|
+
* @param {min value} min
|
|
113
|
+
* @param {max value} max
|
|
114
|
+
* @param {sample number} n
|
|
115
|
+
* @returns
|
|
116
|
+
*/
|
|
117
|
+
const lhs_samples = (min, max, n) => {
|
|
118
|
+
const all_int = is_integer(min + "") && is_integer(max + "");
|
|
119
|
+
const interval = (max - min) / n;
|
|
120
|
+
const ranges = [];
|
|
121
|
+
for (let i = 0; i < n; i++) {
|
|
122
|
+
const start = i * interval + min;
|
|
123
|
+
let end = start + interval;
|
|
124
|
+
if (i == n - 1) {
|
|
125
|
+
end = max;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const min_value = start < end ? start : end;
|
|
129
|
+
const max_value = start > end ? start : end;
|
|
130
|
+
|
|
131
|
+
let obj = {};
|
|
132
|
+
if (all_int) {
|
|
133
|
+
obj = { min: Math.floor(min_value), max: Math.floor(max_value) };
|
|
134
|
+
} else {
|
|
135
|
+
obj = { min: to_fixed2(min_value), max: to_fixed2(max_value) };
|
|
136
|
+
}
|
|
137
|
+
ranges.push(obj);
|
|
138
|
+
}
|
|
139
|
+
return ranges;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Create an random sample object
|
|
144
|
+
* @param {object } sample configuration
|
|
145
|
+
* @returns a sample object
|
|
146
|
+
*/
|
|
147
|
+
const random_sample = (obj) => {
|
|
148
|
+
const sample_obj = {};
|
|
149
|
+
for (const key in obj) {
|
|
150
|
+
const value = obj[key];
|
|
151
|
+
if (Array.isArray(value)) {
|
|
152
|
+
const random = Math.floor(random_number(0, value.length));
|
|
153
|
+
sample_obj[key] = value[random];
|
|
154
|
+
} else if (is_space(value)) {
|
|
155
|
+
const random = random_number(value.min, value.max);
|
|
156
|
+
sample_obj[key] = random;
|
|
157
|
+
} else {
|
|
158
|
+
sample_obj[key] = value;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return sample_obj;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Extract number from string
|
|
166
|
+
* @param {contain number value} value
|
|
167
|
+
* @returns
|
|
168
|
+
*/
|
|
169
|
+
const extract_number = (value) => {
|
|
170
|
+
const number = value.match(/[+\-0-9\\.]+/g);
|
|
171
|
+
if (!number) {
|
|
172
|
+
return 0;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const values = number.map(Number);
|
|
176
|
+
return values.length == 1 ? values[0] : 0;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
module.exports = { parse_num, extract_number, to_fixed2, round_to_fixed2, range, scale, space, is_space, contains_space, is_integer, random_number, random_sample, lhs_samples }
|
package/core/obj.js
CHANGED
|
@@ -4,10 +4,19 @@
|
|
|
4
4
|
* @param {attributes to be copied} attrs
|
|
5
5
|
* @returns
|
|
6
6
|
*/
|
|
7
|
-
const copy_obj =
|
|
7
|
+
const copy_obj = (obj, attrs) => {
|
|
8
8
|
const copied = {};
|
|
9
9
|
attrs.forEach(attr => copied[attr] = obj[attr]);
|
|
10
10
|
return copied;
|
|
11
11
|
};
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
/**
|
|
14
|
+
* Check this is object or not
|
|
15
|
+
* @param {Object to be checked} obj
|
|
16
|
+
* @returns
|
|
17
|
+
*/
|
|
18
|
+
const is_object = (obj) => {
|
|
19
|
+
return typeof obj === 'object' && obj !== null && !Array.isArray(obj);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
module.exports = { copy_obj, is_object }
|
package/index.js
CHANGED
|
@@ -7,8 +7,11 @@ const { Entity } = require('./db/entity');
|
|
|
7
7
|
|
|
8
8
|
const array = require('./core/array');
|
|
9
9
|
const date = require('./core/date');
|
|
10
|
+
const file = require('./core/file');
|
|
10
11
|
const number = require('./core/number');
|
|
11
12
|
const obj = require('./core/obj');
|
|
13
|
+
const random = require('./core/random');
|
|
14
|
+
const thread = require('./core/thread');
|
|
12
15
|
const validate = require('./core/validate');
|
|
13
16
|
const code = require('./http/code');
|
|
14
17
|
const err = require('./http/error');
|
|
@@ -21,6 +24,6 @@ const { log_debug, log_info, log_warn, log_error, is_log_debug, is_log_info, is_
|
|
|
21
24
|
|
|
22
25
|
module.exports = {
|
|
23
26
|
init_settings, init_express_server, init_router, register_type, get_type,
|
|
24
|
-
Entity, EntityMeta, get_entity_meta, array, date, number, obj, validate, code, err, params, context, gridfs, gen_i18n,
|
|
27
|
+
Entity, EntityMeta, get_entity_meta, array, date, file, number, obj, random, thread, validate, code, err, params, context, gridfs, gen_i18n,
|
|
25
28
|
log_debug, log_info, log_warn, log_error, is_log_debug, is_log_info, is_log_warn, is_log_error, get_session_userid, oid_queries, oid_query
|
|
26
29
|
};
|