Skip to content

YounGoat/ecmascript.obtions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

obtions

Function OPTIONS Schema validator.

To normalise options argument.

Table of contents

Links

Get Started

const obtions = requrie('obtions');

const options = {
    Domain: 'www.example.com',
    Port: 8080,
    path: '/index.html',
};

const def = {
    // Whether the property names of *options* are case sensitive.
    // DEFAULT false
    caseSensitive: false,

    // Whether the property names of returned (parsed) object may keep the case 
    // as what column names look like, even *def.caseSensitive* is false.
    // DEFAULT false
    keepNameCase: false,

    // If set true, properties not explicitly declared will be ignored.
    // DEFAULT false
    explicit: true,

    // Declare properties in *options*.
    columns: [ 
        'port', 
        { name: 'Hostname', alias: 'domain' } 
    ],
};

const po = obtions(options, def);
// RETURN {
//     Hostname: 'www.example.com',
//     port: 8080
// }

API

  • Object obtions( Object options, Object def )
  • Object obtions( Object options, Array columns )
  • Object obtions( Object options )

Argument def may include following properties:

  • def.caseSensitive boolean DEFAULT false
  • def.keepNameCase boolean DEFAULT false
  • def.explicit boolean DEFAULT true
  • def.columns Array NOT NULL

columns or def.columns is an array made up of one or more column definitions. A column definition may be an object with following properties:

  • name string
  • alias string | string[]
  • default any OPTIONAL
  • parser Function OPTIONAL
  • required boolean DEFAULT false

Examples

obtions is an easy way to make your function paramemters more friendly.

function formatUrl(options) {
    
    const optionsDef = {
        caseSensitive: false, 
        explicit: true,

        // Property *columns* of definition may be an array or object.
        columns: [
            // Use object to define/declare a column.
            { name: 'protocol', default: 'http' },
            { name: 'query', alias: [ 'querystring', 'queries' ] },

            // Use description string to define/declare a column.
            'hostname required alias(domain, domainName)',

            // Only column name is also OK.
            'port',
            'path',
            'search',
            'hash'
        ]
    };

    options = obtions(options, optionsDef);

    // ...
}

ODL

OD is abbreviation of Option Definition Language which is a tiny language used to define option. It is an easy alternative for option define object. E.g.

// * The option is named "hostname" , or named with alias "domain" or "domainName". 
// * The option SHOULD exist.
'hostname required alias(domain, domainName)'

// * Set default value.
'number DEFAULT(99)'

Keywords in ODL is case-insensitive:

  • ALIAS
  • DEFAULT
  • NOT
  • REQUIRED

About

Function OPTIONS Schema validator.

Resources

Stars

Watchers

Forks

Packages

No packages published