mybase 1.0.25 → 1.0.30

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.
Files changed (3) hide show
  1. package/README.md +1 -0
  2. package/mybase.js +33 -4
  3. package/package.json +2 -2
package/README.md CHANGED
@@ -31,6 +31,7 @@ var { isLocal } = require('mybase')
31
31
  ### object_shuffle(object)
32
32
  ### array_shuffle(array)
33
33
  ### isMochaRunning
34
+ ### ip2int(), int2ip()
34
35
 
35
36
 
36
37
  ### maxmindOpen(geoipFile) : <Promise>
package/mybase.js CHANGED
@@ -18,12 +18,27 @@ Date.prototype.yyyymmdd = function() {
18
18
  ].join('');
19
19
  };
20
20
 
21
- function validHPassword(hpassword) { return (hpassword && hpassword.length === 128 && hpassword.search(/^[a-z0-9]+$/) == 0) }
21
+ function validHPassword(hpassword) { return (hpassword && hpassword.length === 128 && hpassword.search(/^[a-f0-9]+$/) == 0) }
22
22
 
23
23
  function validEmail_old(email) { return (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)) }
24
24
  function validEmail(email) { return (/(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/).test(email)}
25
25
 
26
+
26
27
  function validIp(str) {
28
+ if (str && typeof str==='string') {
29
+ let splitted
30
+ if (splitted=str.match(/^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)$/)) {
31
+ for(let octet of splitted) {
32
+ if (parseInt(octet)>=0 || parseInt(octet)<=255) continue
33
+ return false
34
+ }
35
+ return true
36
+ }
37
+ }
38
+ return false
39
+ }
40
+
41
+ function validIpNative(str) {
27
42
  return (net.isIP(str)!==0)
28
43
  // const octet = '(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]?|0)';
29
44
  // const regex = new RegExp(`^${octet}\\.${octet}\\.${octet}\\.${octet}$`);
@@ -34,7 +49,6 @@ function randomIP() {
34
49
  function int2ip (ipInt) {
35
50
  return ( (ipInt>>>24) +'.' + (ipInt>>16 & 255) +'.' + (ipInt>>8 & 255) +'.' + (ipInt & 255) );
36
51
  }
37
-
38
52
  return int2ip(Math.random()*Math.pow(2,32))
39
53
  }
40
54
 
@@ -50,6 +64,11 @@ function validURL(host) {
50
64
  return _validURL(host)
51
65
  }
52
66
 
67
+ function asJSON(str) {
68
+ if (!str || typeof str!=='string') return false
69
+ try { return JSON.parse(str)}catch(_) {}
70
+ return false
71
+ }
53
72
 
54
73
  function isObject(a) {
55
74
  return (!!a) && (a.constructor === Object);
@@ -400,7 +419,16 @@ async function portCheck_tcp (port, {timeout = 1000, host='127.0.0.1'} = {}) {
400
419
  }
401
420
 
402
421
 
422
+ function int2ip (ipInt) {
423
+ return ( (ipInt>>>24) +'.' + (ipInt>>16 & 255) +'.' + (ipInt>>8 & 255) +'.' + (ipInt & 255) );
424
+ }
425
+
426
+ function ip2int(ip) {
427
+ return ip.split('.').reduce(function(ipInt, octet) { return (ipInt<<8) + parseInt(octet, 10)}, 0) >>> 0;
428
+ }
429
+
403
430
  module.exports = {
431
+ ip2int,int2ip,
404
432
  canReadAndWrite,
405
433
  softexit,
406
434
  randomString,
@@ -415,7 +443,7 @@ module.exports = {
415
443
  validHPassword,
416
444
  validEmail,
417
445
  validHostname,
418
- validIp,
446
+ validIp,validIpNative,
419
447
  validUUID4,
420
448
  getTemp,
421
449
  getMysql,
@@ -428,5 +456,6 @@ module.exports = {
428
456
  array_shuffle,
429
457
  object_shuffle,
430
458
  isMochaRunning,
431
- portCheck_tcp
459
+ portCheck_tcp,
460
+ asJSON
432
461
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mybase",
3
- "version": "1.0.25",
3
+ "version": "1.0.30",
4
4
  "description": "",
5
5
  "main": "mybase.js",
6
6
  "scripts": {
@@ -9,7 +9,7 @@
9
9
  "author": "",
10
10
  "license": "ISC",
11
11
  "dependencies": {
12
- "@7c/validurl": "0.0.2",
12
+ "@7c/validurl": "0.0.3",
13
13
  "chalk": "^3.0.0",
14
14
  "debug": "^4.3.1",
15
15
  "punycode": "^2.1.1"