mybase 1.0.34 → 1.0.37

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 -1
  2. package/mybase.js +48 -3
  3. package/package.json +3 -2
package/README.md CHANGED
@@ -34,7 +34,7 @@ var { isLocal } = require('mybase')
34
34
  ### ip2int(), int2ip()
35
35
  ### ensureProperty(obj,'tier.tier',defaultValue)
36
36
 
37
-
37
+ ### arrayRandomItem(array,defaultValue=false)
38
38
  ### maxmindOpen(geoipFile) : <Promise>
39
39
  ```
40
40
  var handle = await maxmindOpen(config.geoip.country)
package/mybase.js CHANGED
@@ -6,7 +6,7 @@ const chalk = require('chalk')
6
6
  const punycode = require('punycode');
7
7
  const _validURL = require('@7c/validurl')
8
8
  const { match } = require('assert');
9
-
9
+ const validator = require('validator')
10
10
 
11
11
  Date.prototype.yyyymmdd = function() {
12
12
  var mm = this.getMonth() + 1; // getMonth() is zero-based
@@ -23,6 +23,8 @@ function validHPassword(hpassword) { return (hpassword && hpassword.length === 1
23
23
  function validEmail_old(email) { return (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)) }
24
24
 
25
25
  function validEmail(email) {
26
+ return validator.isEmail(email)
27
+ // giving up old style, was not reliable
26
28
  // taken from https://www.w3resource.com/javascript/form/email-validation.php
27
29
  // strange looking emails might be indeed valid
28
30
  // check https://www.w3resource.com/javascript/form/example-javascript-form-validation-email-REC-2822.html
@@ -30,6 +32,12 @@ function validEmail(email) {
30
32
  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)
31
33
  }
32
34
 
35
+ function arrayRandomItem(arry,defaultValue=false) {
36
+ if (Array.isArray(arry)) {
37
+ return arry[Math.floor(Math.random()*arry.length)]
38
+ }
39
+ return defaultValue
40
+ }
33
41
 
34
42
  function validIp(str) {
35
43
  if (str && typeof str==='string') {
@@ -333,7 +341,42 @@ function softexit(message = false, seconds = 60, exitcode = 1) {
333
341
  setTimeout(() => { process.exit(exitcode) }, seconds * 1000)
334
342
  }
335
343
 
336
- function validHostname(domainName) {
344
+ function validHostname(value) {
345
+ // credit: https://github.com/miguelmota/is-valid-hostname/blob/master/index.js
346
+ if (typeof value !== 'string') return false
347
+
348
+ const validHostnameChars = /^[a-zA-Z0-9-.]{1,253}\.?$/g
349
+ if (!validHostnameChars.test(value)) {
350
+ return false
351
+ }
352
+
353
+ if (value.endsWith('.')) {
354
+ value = value.slice(0, value.length - 1)
355
+ }
356
+
357
+ if (value.length > 253) {
358
+ return false
359
+ }
360
+
361
+ const labels = value.split('.')
362
+
363
+ const isValid = labels.every(function (label) {
364
+ const validLabelChars = /^([a-zA-Z0-9-]+)$/g
365
+
366
+ const validLabel = (
367
+ validLabelChars.test(label) &&
368
+ label.length < 64 &&
369
+ !label.startsWith('-') &&
370
+ !label.endsWith('-')
371
+ )
372
+
373
+ return validLabel
374
+ })
375
+
376
+ return isValid
377
+ }
378
+
379
+ function validHostname2(domainName) {
337
380
  var domainNameRegex = /^(?:[a-z0-9](?:[a-z0-9_\-]{0,61}[a-z0-9])?\.){0,126}(?:[a-z0-9](?:[a-z0-9\-]{0,61}[a-z0-9]))\.?$/i
338
381
  if (!domainName || typeof domainName!=='string') return false
339
382
  // is an ip host
@@ -472,6 +515,7 @@ module.exports = {
472
515
  validHPassword,
473
516
  validEmail,
474
517
  validHostname,
518
+ validHostname2,
475
519
  validIp,validIpNative,
476
520
  validUUID4,
477
521
  getTemp,
@@ -487,5 +531,6 @@ module.exports = {
487
531
  isMochaRunning,
488
532
  portCheck_tcp,
489
533
  asJSON,
490
- ensureProperty
534
+ ensureProperty,
535
+ arrayRandomItem
491
536
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mybase",
3
- "version": "1.0.34",
3
+ "version": "1.0.37",
4
4
  "description": "",
5
5
  "main": "mybase.js",
6
6
  "scripts": {
@@ -12,7 +12,8 @@
12
12
  "@7c/validurl": "0.0.3",
13
13
  "chalk": "^3.0.0",
14
14
  "debug": "^4.3.1",
15
- "punycode": "^2.1.1"
15
+ "punycode": "^2.1.1",
16
+ "validator": "^13.7.0"
16
17
  },
17
18
  "devDependencies": {
18
19
  "chai": "^4.2.0",