@wikicasa-dev/node-common 1.0.0 → 1.1.0

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/dist/utils.js ADDED
@@ -0,0 +1,209 @@
1
+ import fs from "fs";
2
+ import hash from "object-hash";
3
+ export function arrayRotate(arr, n) {
4
+ Array.from(Array(n)).forEach((x, i) => {
5
+ arr.push(arr.shift());
6
+ });
7
+ return arr;
8
+ }
9
+ export function cleanASCII(str) {
10
+ return str.replace(/[\uA78C\uA78B]/g, "'").replace(/[“”]/g, "\"").replace(/[^\x00-\x7F]/g, "");
11
+ }
12
+ export function cleanUpSpecialChars(str) {
13
+ return str.replace(/[\uA78C\uA78B]/g, "'").replace(/[“”]/g, "\"");
14
+ }
15
+ export function normalizeString(str) {
16
+ return cleanUpSpecialChars(str).replace(/\W/g, "");
17
+ }
18
+ export function trimAll(str) {
19
+ return str?.replace(/ +|\t/g, " ")?.replace(/\n\n+/g, "\n")?.trim();
20
+ }
21
+ export function cleanUpSpecialChars2(str) {
22
+ return str
23
+ .replace(/[ÀÁÂÃÄÅ]/g, "A")
24
+ .replace(/[àáâãäå]/g, "a")
25
+ .replace(/[ÈÉÊË]/g, "E")
26
+ .replace(/[èéêëėē]/g, "e")
27
+ .replace(/[ÌÍÎÏĪ]/g, "I")
28
+ .replace(/[ìíîïī]/g, "i")
29
+ .replace(/[ÒÓÔÖÕŌ]/g, "O")
30
+ .replace(/[òóôöõō]/g, "o")
31
+ .replace(/[ÙÚÛÜŪ]/g, "U")
32
+ .replace(/[ùúûüū]/g, "u")
33
+ .replace(/[ç]/g, "c");
34
+ }
35
+ export function capitalizeWords(str) {
36
+ return str?.split(" ")?.map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()).join(" ");
37
+ }
38
+ export function capitalizeFirstLetter(str) {
39
+ if (!str)
40
+ return;
41
+ return str?.charAt(0).toUpperCase() + str?.slice(1);
42
+ }
43
+ export function readJson(path) {
44
+ if (!fs.existsSync(path))
45
+ return null;
46
+ let json;
47
+ try {
48
+ json = JSON.parse(fs.readFileSync(path, "utf8"));
49
+ }
50
+ catch (e) {
51
+ json = null;
52
+ }
53
+ return json;
54
+ }
55
+ export function orderBy(arr, prop) {
56
+ arr.sort((a, b) => {
57
+ if (a[prop] > b[prop]) {
58
+ return 1;
59
+ }
60
+ else
61
+ return -1;
62
+ });
63
+ return arr;
64
+ }
65
+ export function pan(number, digits) {
66
+ return ("0" + number).slice(-digits);
67
+ }
68
+ export function uniqWith(array, compareFn) {
69
+ const unique = [];
70
+ for (const current of array) {
71
+ const isDuplicate = unique.some(other => compareFn(current, other));
72
+ if (!isDuplicate)
73
+ unique.push(current);
74
+ }
75
+ return unique;
76
+ }
77
+ export function pop(array, n) {
78
+ const out = [];
79
+ for (let i = 0; i < Math.min(array.length, n); i++) {
80
+ out.push(array[i]);
81
+ }
82
+ return out;
83
+ }
84
+ export function splitChunks(array, chunkSize) {
85
+ const chunks = [];
86
+ for (let i = 0; i < array.length; i += chunkSize) {
87
+ chunks.push(array.slice(i, i + chunkSize));
88
+ }
89
+ return chunks;
90
+ }
91
+ export function newpopList(list, fn) {
92
+ const index = list.findIndex(n => {
93
+ if (!n)
94
+ return false;
95
+ return fn(n);
96
+ });
97
+ if (index >= 0) {
98
+ list[index] = null;
99
+ list = list.filter(a => !!a);
100
+ }
101
+ return list.filter(a => !!a);
102
+ }
103
+ export function popList(list1, list2, fn) {
104
+ const pop = [];
105
+ list1.forEach((l, i) => {
106
+ if (!l)
107
+ return;
108
+ const hash1 = fn ? fn(l) : hash(l);
109
+ const index = list2.findIndex(n => {
110
+ if (!n)
111
+ return false;
112
+ const hash2 = fn ? fn(n) : hash(n);
113
+ return hash1 === hash2;
114
+ });
115
+ if (index >= 0) {
116
+ pop.push(list1[i]);
117
+ list2[index] = null;
118
+ list1[i] = null;
119
+ }
120
+ });
121
+ list1 = list1.filter(f => !!f);
122
+ list2 = list2.filter(f => !!f);
123
+ return pop.filter(a => !!a);
124
+ }
125
+ export function diffList(list1, list2, fn) {
126
+ const l1 = [...list1];
127
+ const l2 = [...list2];
128
+ l1.forEach((l, i) => {
129
+ const hash1 = fn ? fn(l) : hash(l);
130
+ const index = l2.findIndex(n => {
131
+ if (!n)
132
+ return false;
133
+ const hash2 = fn ? fn(n) : hash(n);
134
+ return hash1 === hash2;
135
+ });
136
+ if (index >= 0) {
137
+ l2[index] = null;
138
+ l1[i] = null;
139
+ }
140
+ });
141
+ return [...l1, ...l2].filter(a => !!a);
142
+ }
143
+ export function truncator(num, digits) {
144
+ const numS = num.toString(), decPos = numS.indexOf("."), substrLength = decPos == -1 ? numS.length : 1 + decPos + digits, trimmedResult = numS.substr(0, substrLength), finalResult = isNaN(trimmedResult) ? 0 : trimmedResult;
145
+ return parseFloat(finalResult);
146
+ }
147
+ /*function truncator(num: number | string, digits: number): number {
148
+ const numS: string = Number(num).toFixed(digits);
149
+ return parseFloat(numS);
150
+ }
151
+ */
152
+ export function parseNumber(num, thousand = ".") {
153
+ return parseFloat(num?.replace(/^\D+/g, "")?.replaceAll(thousand, ""));
154
+ }
155
+ export function getRandom(arr) {
156
+ return arr?.at(Math.floor(Math.random() * arr.length));
157
+ }
158
+ export function shuffleArray(array) {
159
+ let currentIndex = array.length;
160
+ let temporaryValue;
161
+ let randomIndex;
162
+ // While there remain elements to shuffle...
163
+ while (0 !== currentIndex) {
164
+ // Pick a remaining element...
165
+ randomIndex = Math.floor(Math.random() * currentIndex);
166
+ currentIndex -= 1;
167
+ // And swap it with the current element.
168
+ temporaryValue = array[currentIndex];
169
+ array[currentIndex] = array[randomIndex];
170
+ array[randomIndex] = temporaryValue;
171
+ }
172
+ return array;
173
+ }
174
+ export function extractNumbers(str) {
175
+ if (!str)
176
+ return;
177
+ // Regex per identificare i numeri con separatore delle migliaia e parte decimale
178
+ const regex = /(\d{1,3}(?:[.,]\d{3})*(?:[.,]\d+)?)/g;
179
+ const matches = str.match(regex);
180
+ return matches?.at(0);
181
+ }
182
+ export function parseFloatFromString(str) {
183
+ if (!str)
184
+ return;
185
+ if (typeof str !== "string")
186
+ throw new Error(`${parseFloatFromString.name} error: The input is not a string`);
187
+ if (str?.includes(","))
188
+ str = str?.replace(",", ".");
189
+ if ((/\.\d{3}/).test(str))
190
+ str = str?.replace(".", "");
191
+ return parseFloat(str);
192
+ }
193
+ /**
194
+ * Convert MySQL POLYGON string into coordinates array
195
+ * @param {string} polygonString - The POLYGON string from MySQL
196
+ * @returns {Array<Array<number>>} - Array of coordinates in [longitude, latitude] format
197
+ */
198
+ export function convertPolygonStringToArray(polygonString) {
199
+ // Remove 'POLYGON((' and '))'
200
+ const coordinatesString = polygonString
201
+ ?.replace("POLYGON((", "")
202
+ ?.replace("))", "");
203
+ // Split the string into individual coordinate pairs
204
+ return coordinatesString?.split(",")?.map(coord => {
205
+ // Split each pair by space and convert to numbers
206
+ const [lon, lat] = coord.trim().split(" ").map(Number);
207
+ return [lon, lat]; // Return [longitude, latitude]
208
+ });
209
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wikicasa-dev/node-common",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "Wikicasa node common",
5
5
  "type": "module",
6
6
  "files": [
@@ -34,8 +34,13 @@
34
34
  "redis": "^4.7.0",
35
35
  "selenium-webdriver": "^4.25.0",
36
36
  "throttled-queue": "^2.1.4",
37
+ "ts-node": "^10.9.2",
37
38
  "zlib": "^1.0.5"
38
39
  },
40
+ "devDependencies": {
41
+ "@types/lodash": "^4.17.10",
42
+ "@types/random-useragent": "^0.3.3"
43
+ },
39
44
  "scripts": {
40
45
  "lint": "eslint --quiet --fix ./",
41
46
  "compile": "tsc -p ./",