mybase 1.0.26 → 1.0.31

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 +25 -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,10 +18,16 @@ 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
- 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)}
24
+
25
+ function validEmail(email) {
26
+ // taken from https://www.w3resource.com/javascript/form/email-validation.php
27
+ // strange looking emails might be indeed valid
28
+ // check https://www.w3resource.com/javascript/form/example-javascript-form-validation-email-REC-2822.html
29
+ 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)
30
+ }
25
31
 
26
32
 
27
33
  function validIp(str) {
@@ -29,7 +35,7 @@ function validIp(str) {
29
35
  let splitted
30
36
  if (splitted=str.match(/^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)$/)) {
31
37
  for(let octet of splitted) {
32
- if (parseInt(octet)>=0 || parseInt(octet)<=255) continue
38
+ if (parseInt(octet)>=0 && parseInt(octet)<=255) continue
33
39
  return false
34
40
  }
35
41
  return true
@@ -64,6 +70,11 @@ function validURL(host) {
64
70
  return _validURL(host)
65
71
  }
66
72
 
73
+ function asJSON(str) {
74
+ if (!str || typeof str!=='string') return false
75
+ try { return JSON.parse(str)}catch(_) {}
76
+ return false
77
+ }
67
78
 
68
79
  function isObject(a) {
69
80
  return (!!a) && (a.constructor === Object);
@@ -414,7 +425,16 @@ async function portCheck_tcp (port, {timeout = 1000, host='127.0.0.1'} = {}) {
414
425
  }
415
426
 
416
427
 
428
+ function int2ip (ipInt) {
429
+ return ( (ipInt>>>24) +'.' + (ipInt>>16 & 255) +'.' + (ipInt>>8 & 255) +'.' + (ipInt & 255) );
430
+ }
431
+
432
+ function ip2int(ip) {
433
+ return ip.split('.').reduce(function(ipInt, octet) { return (ipInt<<8) + parseInt(octet, 10)}, 0) >>> 0;
434
+ }
435
+
417
436
  module.exports = {
437
+ ip2int,int2ip,
418
438
  canReadAndWrite,
419
439
  softexit,
420
440
  randomString,
@@ -442,5 +462,6 @@ module.exports = {
442
462
  array_shuffle,
443
463
  object_shuffle,
444
464
  isMochaRunning,
445
- portCheck_tcp
465
+ portCheck_tcp,
466
+ asJSON
446
467
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mybase",
3
- "version": "1.0.26",
3
+ "version": "1.0.31",
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"