@k03mad/ip2geo 10.0.0 → 10.1.1

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,29 @@ 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
+ getArrayDups(dataArrRemoveEmpty).forEach(dup => duplicates.add(dup));
225
+ const dupsIp = getArrayDups(uniqSorted.map(elem => elem.split(cacheFileSeparator)[0]));
226
+
227
+ const removeDiffs = uniqSorted.filter(elem => {
228
+ if (dupsIp.includes(elem.split(cacheFileSeparator)[0])) {
229
+ different.add(elem);
230
+ return false;
231
+ }
232
+
233
+ return true;
234
+ });
234
235
 
235
- const fileContent = uniq.join(cacheFileNewline).trim();
236
+ const fileContent = removeDiffs.join(cacheFileNewline).trim();
236
237
 
237
238
  if (fileContent) {
238
239
  await fs.writeFile(fullFilePath, fileContent);
239
- entries += uniq.length;
240
+ entries += removeDiffs.length;
240
241
  } else {
241
242
  await fs.rm(fullFilePath);
242
243
  }
243
-
244
- dataArrRemoveEmpty
245
- .forEach((elem, i, arr) => arr.indexOf(elem) !== i && duplicates.add(elem));
246
244
  }));
247
245
 
248
246
  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.1",
4
4
  "description": "GeoIP library",
5
5
  "maintainers": [
6
6
  "Kirill Molchanov <k03.mad@gmail.com"