/
index.js
57 lines (51 loc) · 1.31 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* anonymize-ip <https://github.com/tunnckoCore/anonymize-ip>
*
* Copyright (c) 2014-2015 Charlike Mike Reagent, contributors.
* Released under the MIT license.
*/
'use strict';
var fmt = require('util').format;
var ipRegex = require('ip-regex');
/**
* > Anonymize given ip address.
*
* **Example:**
*
* ```js
* var anonymizeIp = require('anonymize-ip');
*
* anonymizeIp('79.100.75.145');
* //=> 79.100.xx.xxx
*
* anonymizeIp('231.77.134.6', '*');
* //=> 231.77.***.*
*
* anonymizeIp('129.89.5.243', '_');
* //=> 129.89._.___
*
* anonymizeIp('92.189.231.21', [1,2,3]);
* //=> 92.189.xxx.xx
* ```
*
* @param {String} `<ip>` valid ip address
* @param {String} `[mask]` symbol to replace with
* @return {String}
* @api public
*/
module.exports = function anonymizeIp(ip, mask) {
if (!ip) {
throw new Error('[anonymize-ip] should have at least 1 argument');
}
if (typeof ip !== 'string') {
throw new TypeError('[anonymize-ip] expect `ip` be string');
}
if (!ipRegex().test(ip)) {
throw new Error('[anonymize-ip] expect `ip` be valid ip address');
}
mask = typeof mask === 'string' ? mask : 'x';
var nums = ip.split('.');
var pre = nums[2].replace(/[0-9]/g, mask);
var last = nums[3].replace(/[0-9]/g, mask);
return fmt('%s.%s.%s.%s', nums[0], nums[1], pre, last);
};