mybase 1.0.33 → 1.0.36

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 +46 -2
  3. package/package.json +1 -1
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
@@ -26,9 +26,16 @@ function validEmail(email) {
26
26
  // taken from https://www.w3resource.com/javascript/form/email-validation.php
27
27
  // strange looking emails might be indeed valid
28
28
  // check https://www.w3resource.com/javascript/form/example-javascript-form-validation-email-REC-2822.html
29
+ if (typeof email==='string') email=email.toLowerCase().trim()
29
30
  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
31
  }
31
32
 
33
+ function arrayRandomItem(arry,defaultValue=false) {
34
+ if (Array.isArray(arry)) {
35
+ return arry[Math.floor(Math.random()*arry.length)]
36
+ }
37
+ return defaultValue
38
+ }
32
39
 
33
40
  function validIp(str) {
34
41
  if (str && typeof str==='string') {
@@ -332,7 +339,42 @@ function softexit(message = false, seconds = 60, exitcode = 1) {
332
339
  setTimeout(() => { process.exit(exitcode) }, seconds * 1000)
333
340
  }
334
341
 
335
- function validHostname(domainName) {
342
+ function validHostname(value) {
343
+ // credit: https://github.com/miguelmota/is-valid-hostname/blob/master/index.js
344
+ if (typeof value !== 'string') return false
345
+
346
+ const validHostnameChars = /^[a-zA-Z0-9-.]{1,253}\.?$/g
347
+ if (!validHostnameChars.test(value)) {
348
+ return false
349
+ }
350
+
351
+ if (value.endsWith('.')) {
352
+ value = value.slice(0, value.length - 1)
353
+ }
354
+
355
+ if (value.length > 253) {
356
+ return false
357
+ }
358
+
359
+ const labels = value.split('.')
360
+
361
+ const isValid = labels.every(function (label) {
362
+ const validLabelChars = /^([a-zA-Z0-9-]+)$/g
363
+
364
+ const validLabel = (
365
+ validLabelChars.test(label) &&
366
+ label.length < 64 &&
367
+ !label.startsWith('-') &&
368
+ !label.endsWith('-')
369
+ )
370
+
371
+ return validLabel
372
+ })
373
+
374
+ return isValid
375
+ }
376
+
377
+ function validHostname2(domainName) {
336
378
  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
337
379
  if (!domainName || typeof domainName!=='string') return false
338
380
  // is an ip host
@@ -471,6 +513,7 @@ module.exports = {
471
513
  validHPassword,
472
514
  validEmail,
473
515
  validHostname,
516
+ validHostname2,
474
517
  validIp,validIpNative,
475
518
  validUUID4,
476
519
  getTemp,
@@ -486,5 +529,6 @@ module.exports = {
486
529
  isMochaRunning,
487
530
  portCheck_tcp,
488
531
  asJSON,
489
- ensureProperty
532
+ ensureProperty,
533
+ arrayRandomItem
490
534
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mybase",
3
- "version": "1.0.33",
3
+ "version": "1.0.36",
4
4
  "description": "",
5
5
  "main": "mybase.js",
6
6
  "scripts": {