mybase 1.0.27 → 1.0.32

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 +27 -4
  3. package/package.json +2 -2
package/README.md CHANGED
@@ -32,6 +32,7 @@ var { isLocal } = require('mybase')
32
32
  ### array_shuffle(array)
33
33
  ### isMochaRunning
34
34
  ### ip2int(), int2ip()
35
+ ### ensureProperty(obj,'tier.tier',defaultValue)
35
36
 
36
37
 
37
38
  ### 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
@@ -418,6 +424,22 @@ async function portCheck_tcp (port, {timeout = 1000, host='127.0.0.1'} = {}) {
418
424
  }
419
425
  }
420
426
 
427
+ function ensureProperty(target, propsString, defaultValue = false, debug = false) {
428
+ function iterate(o, parts) {
429
+ if (debug) console.log(parts)
430
+ let pname = parts.shift()
431
+ if (debug) console.log(`iterate`, o, pname)
432
+ if (o.hasOwnProperty(pname) && typeof o[pname] === 'object' && parts.length > 0) return iterate(o[pname], parts)
433
+ if (debug) console.log(`--`, o, pname, o[pname], parts)
434
+ if (o.hasOwnProperty(pname) && parts.length === 0) return o[pname]
435
+ if (debug) console.log(`+++`)
436
+ return defaultValue
437
+ }
438
+ var parts = propsString.split('.')
439
+ return iterate(target, parts)
440
+ }
441
+
442
+
421
443
 
422
444
  function int2ip (ipInt) {
423
445
  return ( (ipInt>>>24) +'.' + (ipInt>>16 & 255) +'.' + (ipInt>>8 & 255) +'.' + (ipInt & 255) );
@@ -457,5 +479,6 @@ module.exports = {
457
479
  object_shuffle,
458
480
  isMochaRunning,
459
481
  portCheck_tcp,
460
- asJSON
482
+ asJSON,
483
+ ensureProperty
461
484
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mybase",
3
- "version": "1.0.27",
3
+ "version": "1.0.32",
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"