zet-lib 1.4.13 → 1.4.14

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.
Files changed (2) hide show
  1. package/lib/Util.js +94 -191
  2. package/package.json +1 -1
package/lib/Util.js CHANGED
@@ -7,241 +7,144 @@ const sha256 = require("js-sha256");
7
7
 
8
8
  const Util = {};
9
9
 
10
+ // Constants
10
11
  Util.tab = "\t";
11
- Util.tabs = (n) => {
12
- let ret = "";
13
- for (var i = 0; i < n; i++) {
14
- ret += Util.tab;
15
- }
16
- return ret;
17
- };
18
-
19
12
  Util.NEW_LINE = "\n";
20
13
  Util.newLine = "\r\n";
21
- Util.newLines = (n) => {
22
- let ret = "";
23
- for (var i = 0; i < n; i++) {
24
- ret += Util.newLine;
25
- }
26
- return ret;
27
- };
28
14
 
29
- //sha256
30
- Util.hash = (string) => {
31
- return sha256(string);
32
- };
15
+ // Optimized string generation functions
16
+ Util.tabs = (n) => Util.tab.repeat(n);
17
+ Util.newLines = (n) => Util.newLine.repeat(n);
33
18
 
34
- Util.hashCompare = (myPlaintextPassword, hash) => {
35
- return Util.hash(myPlaintextPassword) == hash;
36
- };
19
+ // Hashing functions
20
+ Util.hash = (string) => sha256(string);
21
+ Util.hashCompare = (myPlaintextPassword, hash) =>
22
+ Util.hash(myPlaintextPassword) === hash;
37
23
 
24
+ // Excel sequence generator - optimized with memoization
25
+ const excelSequenceCache = new Map();
38
26
  Util.excelSequence = function () {
39
- let abjads = [
40
- "A",
41
- "B",
42
- "C",
43
- "D",
44
- "E",
45
- "F",
46
- "G",
47
- "H",
48
- "I",
49
- "J",
50
- "K",
51
- "L",
52
- "M",
53
- "N",
54
- "O",
55
- "P",
56
- "Q",
57
- "R",
58
- "S",
59
- "T",
60
- "U",
61
- "V",
62
- "W",
63
- "X",
64
- "Y",
65
- "Z",
66
- ];
67
- let arr = abjads;
68
- let char = 0;
27
+ if (excelSequenceCache.has("sequence")) {
28
+ return excelSequenceCache.get("sequence");
29
+ }
30
+
31
+ const abjads = Array.from({ length: 26 }, (_, i) =>
32
+ String.fromCharCode(65 + i)
33
+ );
34
+ const arr = [...abjads];
69
35
  let num = 26;
70
- for (let x = 2; x < 15; x++) {
71
- let idx = 0;
72
- for (let i = 1; i <= 26; i++) {
73
- arr[num] = abjads[char] + abjads[idx];
74
- idx++;
75
- num++;
36
+
37
+ for (let char = 0; char < 13; char++) {
38
+ for (let idx = 0; idx < 26; idx++) {
39
+ arr[num++] = abjads[char] + abjads[idx];
76
40
  }
77
- char++;
78
41
  }
79
42
 
43
+ excelSequenceCache.set("sequence", arr);
80
44
  return arr;
81
45
  };
82
46
 
83
- Util.now = function () {
84
- return moment(new Date()).format("YYYY-MM-DD HH:mm:ss");
85
- };
47
+ // Date utilities - optimized with consistent formatting
48
+ Util.now = () => moment().format("YYYY-MM-DD HH:mm:ss");
49
+ Util.nowShort = () => moment().format("YYYY-MM-DD");
50
+ Util.ago = (date) => moment(date).fromNow();
86
51
 
87
- Util.nowShort = function () {
88
- return moment(new Date()).format("YYYY-MM-DD");
89
- };
52
+ // Optimized date formatting functions
53
+ const defaultDateFormat = "YYYY-MM-DD";
54
+ const defaultTimeFormat = "YYYY-MM-DD HH:mm:ss";
90
55
 
91
- Util.ago = (data) => {
92
- return moment(data).fromNow();
93
- };
94
- /*
95
- moment get one month ago from current
96
- var monthago = moment().subtract(1, 'months').format('YYYY-MM-DD');
97
-
98
- */
56
+ Util.dateSql = (date, format = defaultDateFormat) =>
57
+ date && date !== "0000-00-00" ? moment(date).format(format) : "";
99
58
 
100
- Util.dateSql = function (date, format) {
101
- format = format || "YYYY-MM-DD";
102
- if (date && date != "0000-00-00") return moment(date).format(format);
103
- else return "";
104
- };
59
+ Util.dateOriginal = (date) =>
60
+ date && date !== "0000-00-00" ? moment(date).utc(false) : "";
105
61
 
106
- Util.dateOriginal = function (date) {
107
- if (date && date != "0000-00-00") {
108
- let value = moment(date).utc(false);
109
- return value;
110
- } else {
111
- return "";
112
- }
113
- };
62
+ Util.dateFormat = (date, format = defaultDateFormat) =>
63
+ date && date !== "0000-00-00" ? moment(date).format(format) : "";
114
64
 
115
- Util.dateFormat = function (date, format) {
116
- format = format || "YYYY-MM-DD";
117
- if (date && date != "0000-00-00") return moment(date).format(format);
118
- else return "";
119
- };
65
+ Util.timePublic = (date) => (date ? moment(date).format("DD MMM YYYY") : "");
120
66
 
121
- Util.timePublic = function (date) {
122
- if (date) return moment(date).format("DD MMM YYYY");
123
- else return "";
124
- };
67
+ Util.timeSql = (date, format = defaultTimeFormat) =>
68
+ date && date !== "0000-00-00 00:00:00" ? moment(date).format(format) : "";
125
69
 
126
- Util.timeSql = function (date, format) {
127
- if (date && date != "0000-00-00 00:00:00") {
128
- format = format || "YYYY-MM-DD HH:mm:ss";
129
- return moment(date).format(format);
130
- } else return "";
131
- };
70
+ // Optimized date difference calculation
71
+ Util.dateDiff = (start, end, format = "months") =>
72
+ start && end ? moment(end).diff(moment(start), format, true) : 0;
132
73
 
133
- //moment('2017-11-30T15:00:00-07:00').diff(moment('2017-05-31T15:00:00-07:00'), 'months')
134
- Util.dateDiff = (start, end, format = "months") => {
135
- let count = 0;
136
- if (start && end) {
137
- count = moment(end).diff(moment(start), format, true);
138
- }
139
- return count;
140
- };
74
+ // Optimized date parsing
75
+ Util.getDate = (date = "") => {
76
+ if (!date) return { year: 0, month: 0, date: 0 };
141
77
 
142
- Util.getDate = function (date) {
143
- date = date + "" || "";
144
- if (date != "") {
145
- let explode = date.split("-");
146
- return {
147
- year: parseInt(explode[0]),
148
- month: parseInt(explode[1]),
149
- date: parseInt(explode[2]),
150
- };
151
- } else {
152
- return {
153
- year: 0,
154
- month: 0,
155
- date: 0,
156
- };
157
- }
78
+ const [year, month, day] = date.split("-").map(Number);
79
+ return { year, month, date: day };
158
80
  };
159
81
 
82
+ // Optimized date range check
160
83
  Util.dateIsBetween = (compare, start, end) => {
161
- if (compare == "" || compare == "0000-00-00") {
162
- return false;
163
- }
164
- compare = moment(compare).format("YYYY-MM-DD");
165
- start = moment(start).format("YYYY-MM-DD");
166
- end = moment(end).format("YYYY-MM-DD");
167
- let today = moment(compare);
168
- let startDate = moment(start);
169
- let endDate = moment(end);
170
-
171
- if (compare == start) {
172
- return true;
173
- } else if (compare == end) {
174
- return true;
175
- } else {
176
- return today.isBetween(startDate, endDate);
177
- }
178
- };
84
+ if (!compare || compare === "0000-00-00") return false;
179
85
 
180
- Util.getMonth = function (date) {
181
- if (date.length > 5) {
182
- let n = new Date(date);
183
- let m = n.getMonth();
184
- return parseInt(m) + 1;
185
- }
186
- return 0;
187
- };
86
+ const compareDate = moment(compare);
87
+ const startDate = moment(start);
88
+ const endDate = moment(end);
188
89
 
189
- Util.getYear = function (date) {
190
- date = Util.dateSql(date) || "";
191
- return date.slice(0, 4);
90
+ return (
91
+ compareDate.isSame(startDate) ||
92
+ compareDate.isSame(endDate) ||
93
+ compareDate.isBetween(startDate, endDate)
94
+ );
192
95
  };
193
96
 
194
- //first is smaller than second
97
+ // Optimized month and year extraction
98
+ Util.getMonth = (date) =>
99
+ date?.length > 5 ? new Date(date).getMonth() + 1 : 0;
100
+
101
+ Util.getYear = (date) => Util.dateSql(date)?.slice(0, 4) || "";
102
+
103
+ // Optimized day calculation with memoization
104
+ const dayCalculationCache = new Map();
195
105
  Util.calculateDay = function (from, to, holidayInWeek = 0) {
106
+ const cacheKey = `${from}-${to}-${holidayInWeek}`;
107
+ if (dayCalculationCache.has(cacheKey)) {
108
+ return dayCalculationCache.get(cacheKey);
109
+ }
110
+
196
111
  holidayInWeek = parseInt(holidayInWeek) || 0;
197
- let count = 0;
198
- if (holidayInWeek == 1) {
199
- let days = Util.enumerateDaysBetweenDates(
112
+ let count;
113
+
114
+ if (holidayInWeek === 1) {
115
+ const days = Util.enumerateDaysBetweenDates(
200
116
  moment(from).format("YYYY-MM-DD"),
201
117
  moment(to).format("YYYY-MM-DD")
202
118
  );
203
- let countdays = days.filter(
204
- (item) => parseInt(moment(item).format("d")) != 0
205
- );
206
- count = countdays.length;
207
- } else if (holidayInWeek == 2) {
208
- let days = Util.enumerateDaysBetweenDates(
119
+ count = days.filter((item) => moment(item).day() !== 0).length;
120
+ } else if (holidayInWeek === 2) {
121
+ const days = Util.enumerateDaysBetweenDates(
209
122
  moment(from).format("YYYY-MM-DD"),
210
123
  moment(to).format("YYYY-MM-DD")
211
124
  );
212
- let countdays = days.filter(
213
- (item) =>
214
- parseInt(moment(item).format("d")) != 0 &&
215
- parseInt(moment(item).format("d")) != 6
216
- );
217
- count = countdays.length;
125
+ count = days.filter((item) => {
126
+ const day = moment(item).day();
127
+ return day !== 0 && day !== 6;
128
+ }).length;
218
129
  } else {
219
- let a = moment(from);
220
- let b = moment(to);
221
- count = b.diff(a, "days") + 1;
130
+ count = moment(to).diff(moment(from), "days") + 1;
222
131
  }
223
132
 
133
+ dayCalculationCache.set(cacheKey, count);
224
134
  return count;
225
135
  };
226
136
 
227
- var getDaysBetweenDates = function (startDate, endDate) {
228
- let now = startDate.clone(),
229
- dates = [];
230
- while (now.isSameOrBefore(endDate)) {
231
- dates.push(now.format("MM/DD/YYYY"));
232
- now.add(1, "days");
233
- }
234
- return dates;
235
- };
236
-
237
- //itterate days in array
137
+ // Optimized date enumeration
238
138
  Util.enumerateDaysBetweenDates = function (startDate, endDate) {
239
- let now = moment(startDate).clone(),
240
- dates = [];
241
- while (now.isSameOrBefore(endDate)) {
242
- dates.push(now.format("MM/DD/YYYY"));
243
- now.add(1, "days");
139
+ const dates = [];
140
+ const current = moment(startDate);
141
+ const end = moment(endDate);
142
+
143
+ while (current.isSameOrBefore(end)) {
144
+ dates.push(current.format("MM/DD/YYYY"));
145
+ current.add(1, "days");
244
146
  }
147
+
245
148
  return dates;
246
149
  };
247
150
 
@@ -1542,8 +1445,8 @@ Util.tableShowInGrid = (arr, qey, MYMODEL, myCache, companyId) => {
1542
1445
  var keyFields = key + "Fields";
1543
1446
  var keyObject = key + "Object";
1544
1447
  let value = item[key];
1545
- if(mywidget[key].name == "select"){
1546
- value = mywidget[key].fields[value] || ""
1448
+ if (mywidget[key].name == "select") {
1449
+ value = mywidget[key].fields[value] || "";
1547
1450
  } else if (mywidget[key].name == "relation") {
1548
1451
  value = value
1549
1452
  ? Util.array_id_to_zname(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zet-lib",
3
- "version": "1.4.13",
3
+ "version": "1.4.14",
4
4
  "description": "zet is a library that part of zet generator.",
5
5
  "engines": {
6
6
  "node": ">=18"