@k03mad/ip2geo 9.7.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
  ]
package/app/cli.js CHANGED
@@ -46,7 +46,13 @@ if (isPrune) {
46
46
  log(blue(cacheFolder));
47
47
 
48
48
  try {
49
- const {duplicates, empty, entries, longLinesFiles} = await pruneCache(
49
+ const {
50
+ different,
51
+ duplicates,
52
+ empty,
53
+ entries,
54
+ longLinesFiles,
55
+ } = await pruneCache(
50
56
  cacheFolder,
51
57
  DEFAULT_CACHE_FILE_SEPARATOR,
52
58
  DEFAULT_CACHE_FILE_NEWLINE,
@@ -56,6 +62,8 @@ if (isPrune) {
56
62
  '',
57
63
  green(`Removed duplicate cache entries: ${bold(duplicates.length)}`),
58
64
  ...duplicates.map(elem => dim(`— ${elem}`)),
65
+ green(`Removed different cache entries: ${bold(different.length)}`),
66
+ ...different.map(elem => dim(`— ${elem}`)),
59
67
  green(`Removed empty cache entries: ${bold(empty.length)}`),
60
68
  ...empty.map(elem => dim(`— ${elem}`)),
61
69
  ]);
@@ -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 = [
@@ -191,6 +193,7 @@ export const pruneCache = async (cacheDir, cacheFileSeparator, cacheFileNewline)
191
193
  );
192
194
 
193
195
  const duplicates = new Set();
196
+ const different = new Set();
194
197
  const empty = [];
195
198
  const longLinesFiles = new Set();
196
199
  let entries = 0;
@@ -215,25 +218,37 @@ export const pruneCache = async (cacheDir, cacheFileSeparator, cacheFileNewline)
215
218
  empty.push(elem);
216
219
  });
217
220
 
218
- const uniq = [...new Set(dataArrRemoveEmpty)]
221
+ const uniqSorted = [...new Set(dataArrRemoveEmpty)]
219
222
  .sort((a, b) => cacheLineToNum(a) - cacheLineToNum(b));
220
223
 
221
- const fileContent = uniq.join(cacheFileNewline).trim();
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
+
235
+ const fileContent = removeDiffs.join(cacheFileNewline).trim();
222
236
 
223
237
  if (fileContent) {
224
238
  await fs.writeFile(fullFilePath, fileContent);
225
- entries += uniq.length;
239
+ entries += removeDiffs.length;
226
240
  } else {
227
241
  await fs.rm(fullFilePath);
228
242
  }
229
243
 
230
- dataArrRemoveEmpty
231
- .forEach((elem, i, arr) => arr.indexOf(elem) !== i && duplicates.add(elem));
244
+ const dups = getArrayDups(dataArrRemoveEmpty);
245
+ dups.forEach(dup => duplicates.add(dup));
232
246
  }));
233
247
 
234
248
  return {
235
249
  entries,
236
250
  duplicates: [...duplicates],
251
+ different: [...different],
237
252
  empty,
238
253
  longLinesFiles: [...longLinesFiles],
239
254
  };
@@ -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": "9.7.0",
3
+ "version": "10.1.0",
4
4
  "description": "GeoIP library",
5
5
  "maintainers": [
6
6
  "Kirill Molchanov <k03.mad@gmail.com"