object-parse

An Object parsing script with options/settings pairing.

object-parse

Author: Robert Edward Steckroth II

Digital Persona: Surgemcgee, Bustout RobertSteckroth@gmail.com

Licence: GPLv3

Description: Parses and verifies objects with a getter/setter methodology. This module/library also incorperates a command line parser for convience.

Features:

  • Reduces errors from mis-configured cli input
  • Object oriented for project integration

**Prolouge: ** the object-parse module contains two primary built-in members: settings and options. Both of these members are idempotant object/function hybrid Objects which operate the same regardless of how it is called. Both methods of direct property setting or self function calling can/should be utilized for maximum eficiantcy.

The command line parser uses the process environment variable use within Nodejs and parses the command line input stored therein. The parser supports: String, Boolean, Number, and Object.

The cli input must be separated by a = without any non-quated space next to it.


Options are used as template data for allowable settings. An option must be an Array which can be empty or contain any number of values. An option can be set in two ways: with object-parse.options() or by directly setting a property to the object-parse.options member.

The options array values are type sensitive. When a incorrect type or value is applied with the object-parse.settings member, a default value will be used. The default value will be the first index of the corisponding options Array value. If the array is empty, undefined will be set as the default setting.

Warning: Do not do not modify any options Array values with methods, e.g. options.port.pop(), options.port.splice(1). The options must be set directly with the = operater for internal setting reasons

  • Using _object-parse.options as a function

syntax: object-parse.options({port: [8090, 8085]}, Object, etc..)

The above example shows how to specify a "port" option to the module with two availible choices (8090, 8085).

> Any number of objects can be passed into the options function for convience,
e.g. object-parse.options({port: [8090, 8085], app_name: [""]}, {url:     ["https://localhost, "http://127.0.0.1"]})
  • Using object-parse.options as an object

    syntax: object-parse.options.port = [8090, 8085]

    The above example shows how to specify a "port" option to the module with two availible choices (8090, 8085).


###settings

Settings are set with this member. They are compared against the options value Arrays for valididity. If a valid setting is set or passed in this member it will then be stored within itself. Otherwise, the current or default options value will be set to the member.

  • Using _object-parse.settings as a function

    syntax: object-parse.settings({port: 8080, app_name: "My cool app"}, Object, etc..)

    The above example shows how to specify a "port" and "app_name" settings to the module. Both of these will be validated against the options and used if appropreate.

  • Using _object-parse.settings as an object

    syntax: object-parse.settings.port = 8080, object-parse.settings.app_name = "My cool"`

    The above example shows how to specify a "port" and "app_name" settings to the module. Both of these will be validated against the options and used if appropreate.


###parse_cli

  • Using object-parse.parse_cli()

    Note: This member is to be used with the Nodejs process environment variable only.

    syntax: var obj = object-parse.parse_cli() or object-parse.settings(object-parse.parse_cli())

    The parse_cli() function takes zero arguments and returns an object from the process.argv strings.

Example: Providing this cli input string --> node Test_me.js data="{'log file': '/home/surgemcgee/log/njs_logs.js', 'another': {'name':'bob'}}" test=true name=Robert age=32 type=" Human" 'another_obj= {"cool": true}'

object-parse.parse_cli() will return the following Object -->

{
  "data": {         // Object
    "log file": "/home/surgemcgee/log/njs_logs.js",  // String
    "another": {    // Object
      "name": "bob" // String
    }
  },
  "test": true,      // Boolean
  "name": "Robert",  // String
  "age": 32,         // Number
  "type": " Human",  // String
  "another_obj": {   // Object
  "cool": true       // Boolean
  }
}
 

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 use.

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

    // example object-parse options declaration
    object-parse.options({
             debug: [true, false, "Verbose"], // Allow only _true_, _false_, or _"Ok"_ into _data.settings_ and set _true_ as default
             app_name: ["My server"], // Allow anything to be set and use String("My server") as the default value
             message: [""], // Allow anything and set and use an empty string as the default
             extra: [], // Allow anything to be set and set and use _undefined_ as default
             port: [8085, 8080], // Allow only Numbers([8085, 8080]) to be set as the port
    })
 
> Options can be appended to _the object-parse.options()_ object by passing in new objects. Any colliding qualifiers will be over-written.