@k03mad/ip2geo 10.0.0 → 10.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.
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "cSpell.words": [
3
3
  "consts",
4
+ "dups",
4
5
  "geoip",
5
6
  "nvmrc"
6
7
  ]
@@ -4,6 +4,8 @@ import path from 'node:path';
4
4
  import _debug from 'debug';
5
5
  import {isIP} from 'is-ip';
6
6
 
7
+ import {getArrayDups} from './utils.js';
8
+
7
9
  const debug = _debug('mad:geoip');
8
10
 
9
11
  const outputKeys = [
@@ -216,33 +218,31 @@ export const pruneCache = async (cacheDir, cacheFileSeparator, cacheFileNewline)
216
218
  empty.push(elem);
217
219
  });
218
220
 
219
- const uniq = [...new Set(dataArrRemoveEmpty)]
220
- .sort((a, b) => cacheLineToNum(a) - cacheLineToNum(b))
221
- .filter((elem, i, arr) => {
222
- for (const [j, line] of arr.entries()) {
223
- if (
224
- i !== j
225
- && line.split(cacheFileSeparator)[0] === elem.split(cacheFileSeparator)[0]
226
- ) {
227
- different.add(elem);
228
- return false;
229
- }
230
- }
221
+ const uniqSorted = [...new Set(dataArrRemoveEmpty)]
222
+ .sort((a, b) => cacheLineToNum(a) - cacheLineToNum(b));
231
223
 
232
- return true;
233
- });
224
+ const dupsIp = getArrayDups(uniqSorted.map(elem => elem.split(cacheFileSeparator)[0]));
225
+
226
+ const removeDiffs = uniqSorted.filter(elem => {
227
+ if (dupsIp.includes(elem.split(cacheFileSeparator)[0])) {
228
+ different.add(elem);
229
+ return false;
230
+ }
231
+
232
+ return true;
233
+ });
234
234
 
235
- const fileContent = uniq.join(cacheFileNewline).trim();
235
+ const fileContent = removeDiffs.join(cacheFileNewline).trim();
236
236
 
237
237
  if (fileContent) {
238
238
  await fs.writeFile(fullFilePath, fileContent);
239
- entries += uniq.length;
239
+ entries += removeDiffs.length;
240
240
  } else {
241
241
  await fs.rm(fullFilePath);
242
242
  }
243
243
 
244
- dataArrRemoveEmpty
245
- .forEach((elem, i, arr) => arr.indexOf(elem) !== i && duplicates.add(elem));
244
+ const dups = getArrayDups(dataArrRemoveEmpty);
245
+ dups.forEach(dup => duplicates.add(dup));
246
246
  }));
247
247
 
248
248
  return {
@@ -0,0 +1,4 @@
1
+ /**
2
+ * @param {Array} arr
3
+ */
4
+ export const getArrayDups = arr => arr.filter((e, i, a) => a.indexOf(e) !== i);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@k03mad/ip2geo",
3
- "version": "10.0.0",
3
+ "version": "10.1.0",
4
4
  "description": "GeoIP library",
5
5
  "maintainers": [
6
6
  "Kirill Molchanov <k03.mad@gmail.com"