npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

defined-options

v0.2.3

Published

Define option properties with optional validation, filter and default value. Read, write, validate and merge option values as simple as possible.

Downloads

16

Readme

defined-options

Define option properties with optional validation, filter and default value. Read, write, validate and merge option values as simple as possible.

npm Package Version MIT License Travis Build Status

Dependencies Status devDependencies Status Code Climate GPA Code Climate Test Coverage


README IN PROGRESS

API

See also tests and examples.

Options()

See lib/options.js.

Creates a new Options instance. Accepts an object with option definitions as optional argument.

var Options = require('defined-options');

var options = new Options({
        text: {
            validate: 'string!empty',
            default: 'foo'
        },
        answer: {
            validate: 'number>0',
            default: 42
        }
    });

console.log(options); // { text: [Getter/Setter], answer: [Getter/Setter] }
console.log(options.getPlainObject()); // { text: 'foo', answer: 42 }
console.log(options.text); // foo
console.log(options.answer); // 42

Options' properties have getters and setters defined via their descriptors. This way validation and filtering is done automagically:

// option 'text' only accepts non-empty strings
options.text = '';
console.log(options.text); // foo
options.text = 'bar';
console.log(options.text); // bar

// option 'answer' only accepts numbers > 0
options.answer = -7;
console.log(options.answer); // 42
options.answer = 5;
console.log(options.answer); // 5

.defineOption()

Creates a new option property or replaces an existing one with same name.

Accepts option definition as single argument or option name as first and option definition as second argument.

Returns current Options instance.

options.defineOption({name: 'text', validate: 'string'});
// or
options.defineOption('text', {validate: 'string'});
Option definition

An option definition object can have the following properties:

  • name
    required
    a non-empty string defining the option name

  • validate
    default: 'any'
    defines how to validate an options value; accepts validate-by-shorthand arguments:

    • a string defining a shorthand string
    • a regular rexpression for a match test
    • a function, receiving a value to test, returning a boolean result
    • an array of shorthand strings, regular expressions and/or functions; validating an option value if any of these tests returns true
  • filter
    default: function(value) {return value;}
    defines a filter function, receiving the validated value, returning the filtered value

  • default
    default: undefined
    defines an option's default value; if set to a function, it will be called to set the default value

Example with all properties:

options.defineProperty({
    name: 'shout',
    default: 'HELLO!',
    validate: 'string!empty',
    filter: function(value) {
        return value.toLowerCase();
    }
});

console.log(options.shout); // HELLO!
options.shout = 'bye!';
options.shout = 1;
console.log(options.shout); // BYE!

.defineOptions()

Creates new option properties or replaces existing ones with same name using defineOption().

Expects and object with option names as keys and option definitions as values.

Returns current Options instance.

options.defineOptions({
    name: {
        validate: 'string'
    },
    age: {
        validate: 'number>0'
    }
});

.removeOption()

Removes an option. Expects and option name. Returns current Options instance.

options.removeOption('foo');

.hasOption()

Tests is a option is defined. Expects an option name. Returns a boolean result.

options.hasOption('foo');

.getPlainObject()

Returns a plain object with option name as keys and option values as values, without described getters and setters.

console.log(options); // { text: [Getter/Setter], answer: [Getter/Setter] }
console.log(options.getPlainObject()); // { text: 'foo', answer: 42 }

.merge()

Alias for mergeOptionValues().

.mergeOptionValues()

Merges an new values into current Options instance and updates an option values if given value is valid.

Expects on or more objects containing option names as keys and option values as values.

Returns current Options instance.

var options = new Options({
        text: {
            validate: 'string!empty',
            default: 'foo'
        },
        answer: {
            validate: 'number>0',
            default: 42
        }
    });

console.log(options.getPlainObject()); // { text: 'foo', answer: 42 }

options.merge({
    text: bar,
    answer: -7,
    name: 'Han'
});

console.log(options.getPlainObject()); // { text: 'bar', answer: 42 }

.mergeOptions()

Merges one Options instance into another. Replaces options with same name.

Expects one or more Options instances.

Returns current Options instance.

console.log(options.getPlainObject()); // { text: 'foo', answer: 42 }

options.mergeOptions(new Options({
    name: {
        validate: 'string',
        default: 'Han'
    }
}));

console.log(options.getPlainObject()); // { text: 'foo', answer: 42, name: 'Han' }

.getOptionDefinition()

Returns an option definition as Option instance. Expects an option name.

.getOptionDefinitions()

Returns all option definitions as an object with option names as keys and the respective Option instance as values.

.default()

.setDefaultOptionValue()

.setDefaultOptionValues()

.validate()

.validateOptionValue()

.validateOptionValues()

Option()

License

MIT © 2015 Simon Lepel