mybase 1.0.31 → 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 +18 -1
  3. package/package.json +1 -1
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
@@ -424,6 +424,22 @@ async function portCheck_tcp (port, {timeout = 1000, host='127.0.0.1'} = {}) {
424
424
  }
425
425
  }
426
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
+
427
443
 
428
444
  function int2ip (ipInt) {
429
445
  return ( (ipInt>>>24) +'.' + (ipInt>>16 & 255) +'.' + (ipInt>>8 & 255) +'.' + (ipInt & 255) );
@@ -463,5 +479,6 @@ module.exports = {
463
479
  object_shuffle,
464
480
  isMochaRunning,
465
481
  portCheck_tcp,
466
- asJSON
482
+ asJSON,
483
+ ensureProperty
467
484
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mybase",
3
- "version": "1.0.31",
3
+ "version": "1.0.32",
4
4
  "description": "",
5
5
  "main": "mybase.js",
6
6
  "scripts": {