object-parse

A universal object parser for module settings and configuratiom with easy to manage and view option settings.

npm install object-parse
1 downloads in the last day
3 downloads in the last week
25 downloads in the last month

object-parse

Cross Module Object Parser and Argument Verifier

NPM

Author: Robert Edward Steckroth II

Digital Persona: Surgemcgee, Bustout RobertSteckroth@gmail.com

Licence: GNU GENERAL PUBLIC LICENSE Version 3

Description:

Parses and verifies objects in a cross module environment. Includes a cli argument parser as well.

Features:

  • Small and unique object design
  • Simple option creation
  • Easy to manage options accross multiple modules
  • object-parse.options()

    The options are type sensitive. When a incorrect value is applied with the settings() function, a default value will be used. The default value will be the first index of the options array, eg. for the debug option below, if a string or number which is not in the array is passed to settings(), the default value will be a [boolean] true. If the array is empty, undefined will be set to the setting.

    Given the object below, the debug object will only be set to boolean true or false. All others will be rejected and the default value (true) will be used.

    When the option array length is 0 or 1 (like appname, message or extra), options will allow any setting to be set. If two or more values are supplied, only those can be set.

      --- example object-parse options declaration --
    
      object-parse.options({   debug: [true, false], // Allow only true or false into data.settings() and set true as default
               appname: ['My server'], // All anything to be set and use [String]"My server" as default
               message: [''], // Allow anthing and set an empty string as default 
               extra: [], // Allow anthing to be set and set undefined as default
               port: [8085, 8080], // Allow only Numbers[8085, 8080] to be set to settings()
      })
    
      // object-parse.options() returns a json object representing the set options as well -->
      console.dir(object-parse.options())
      /* Outputs -->
      { [Function]
          debug: [ true, false ],
          appname: [ 'My server' ],
          message: [ '' ],
          extra: [],
          port: [ 8085, 8080 ] }
      */
    
> Options can be appended to _the object-parse.options()_ object by passing in new objects. Any colliding namespace will be overwritten.
  • object-parse.parser()

    The parser() function takes zero arguments and returns a parsed object from the process.argv string in basic json format.

    Providing this cli input string --> node Test_me.js debug=false port=7002 appname='"object parse test script"'

    object-parse.parse_cli() will output the following json object --> { debug: false, port: 7002, appname: 'object parse test script' }

    Note: To force a string type, in-case a quotes in the inverse quotes. E.g. debug='"true"' or debug="'true'"

  • object-parse.settings()

    The settings() function takes zero to many argumuments as single level json objects. The passed in arguments are compared against the pre-defined options() object

Test script

var Object_parse = require('object-parse')

// Cli input --> node Test_me.js debug=false port=7002 message="'Hello object parse"' appname='"object parse test script"'

function log(message) {
    console.log('['+data.settings().appname+'] '+message)
}

var data = new Object_parse()

data.options({   debug: [true, false],
                 appname: ['My server'],
                 message: [''],
                 port: [8085, 8080],
})

log('data.settings() before argument set\n')
console.dir(data.settings())
console.log('\n\n')
log('Options are')
console.dir(data.options())
console.log('\n\n')


log('data.settings() after argument set\n')
console.dir(data.settings())
console.log('\n\n')

var cli_args = data.parser() // Get out cli arguments
// format for objects -->  name=[boolean,number]
// for strings --> name='"value"'

data.settings(cli_args) // Here we set the settings according to our options
// returns new settings object

log('data.settings().debug --> '+data.settings().debug)
log('data.settings().port --> '+data.settings().port)
log('data.settings().appname --> '+data.settings().appname)
log('data.settings().message --> '+data.settings().message)

Test script OUTPUT

// OUTPUT OF --> node Test_me.js debug="'false'" port=7002 message="'Hello object parse'" appname='"object parse test script"'

[My server] data.settings() before argument set

{ [Function] debug: true, appname: 'My server', message: '', port: 8085 }



[My server] Options are
{ [Function]
  debug: [ true, false ],
  appname: [ 'My server' ],
  message: [ '' ],
  port: [ 8085, 8080 ] }



[My server] data.settings() after argument set

{ [Function] debug: true, appname: 'My server', message: '', port: 8085 }



Warning: option: debug, does not contain a setting: string['false']. Available options are: boolean[true] boolean[false] 
Warning: option: port, does not contain a setting: number[7002]. Available options are: number[8085] number[8080] 
[object parse test script] data.settings().debug --> true
[object parse test script] data.settings().port --> 8085
[object parse test script] data.settings().appname --> object parse test script
[object parse test script] data.settings().message --> 'Hello object parse'
npm loves you