{"id":21158257,"url":"https://github.com/simbo/defined-options","last_synced_at":"2025-06-13T12:09:09.737Z","repository":{"id":57154686,"uuid":"42372393","full_name":"simbo/defined-options","owner":"simbo","description":"Define option properties with optional validation, filter and default value. Read, write, validate and merge option values as simple as possible.","archived":false,"fork":false,"pushed_at":"2015-09-16T13:25:45.000Z","size":172,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-13T12:09:00.015Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/simbo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-09-12T20:44:04.000Z","updated_at":"2015-09-14T07:14:13.000Z","dependencies_parsed_at":"2022-09-06T20:50:53.403Z","dependency_job_id":null,"html_url":"https://github.com/simbo/defined-options","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/simbo/defined-options","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simbo%2Fdefined-options","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simbo%2Fdefined-options/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simbo%2Fdefined-options/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simbo%2Fdefined-options/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/simbo","download_url":"https://codeload.github.com/simbo/defined-options/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simbo%2Fdefined-options/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259642351,"owners_count":22888995,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-20T12:19:05.059Z","updated_at":"2025-06-13T12:09:04.723Z","avatar_url":"https://github.com/simbo.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"defined-options\n===============\n\n  \u003e Define option properties with optional validation, filter and default value.\n  \u003e Read, write, validate and merge option values as simple as possible.\n\n[![npm Package Version](https://img.shields.io/npm/v/defined-options.svg?style=flat-square)](https://www.npmjs.com/package/defined-options)\n[![MIT License](http://img.shields.io/:license-mit-blue.svg?style=flat-square)](http://simbo.mit-license.org)\n[![Travis Build Status](https://img.shields.io/travis/simbo/defined-options/master.svg?style=flat-square)](https://travis-ci.org/simbo/defined-options)\n\n[![Dependencies Status](https://img.shields.io/david/simbo/defined-options.svg?style=flat-square)](https://david-dm.org/simbo/defined-options)\n[![devDependencies Status](https://img.shields.io/david/dev/simbo/defined-options.svg?style=flat-square)](https://david-dm.org/simbo/defined-options#info=devDependencies)\n[![Code Climate GPA](https://img.shields.io/codeclimate/github/simbo/defined-options.svg?style=flat-square)](https://codeclimate.com/github/simbo/defined-options)\n[![Code Climate Test Coverage](https://img.shields.io/codeclimate/coverage/github/simbo/defined-options.svg?style=flat-square)](https://codeclimate.com/github/simbo/defined-options)\n\n---\n\n# README IN PROGRESS\n\n\u003c!-- MarkdownTOC depth=5 bracket=round autolink=true --\u003e\n\n- [API](#api)\n    - [Options()](#options)\n        - [.defineOption()](#defineoption)\n            - [Option definition](#option-definition)\n        - [.defineOptions()](#defineoptions)\n        - [.removeOption()](#removeoption)\n        - [.hasOption()](#hasoption)\n        - [.getPlainObject()](#getplainobject)\n        - [.merge()](#merge)\n        - [.mergeOptionValues()](#mergeoptionvalues)\n        - [.mergeOptions()](#mergeoptions)\n        - [.getOptionDefinition()](#getoptiondefinition)\n        - [.getOptionDefinitions()](#getoptiondefinitions)\n        - [.default()](#default)\n        - [.setDefaultOptionValue()](#setdefaultoptionvalue)\n        - [.setDefaultOptionValues()](#setdefaultoptionvalues)\n        - [.validate()](#validate)\n        - [.validateOptionValue()](#validateoptionvalue)\n        - [.validateOptionValues()](#validateoptionvalues)\n    - [Option()](#option)\n- [License](#license)\n\n\u003c!-- /MarkdownTOC --\u003e\n\n\n\n## API\n\nSee also [tests](https://github.com/simbo/gulpplug/blob/master/test/index.js)\nand [examples](https://github.com/simbo/gulpplug/blob/master/example/index.js).\n\n\n### Options()\n\nSee [`lib/options.js`](https://github.com/simbo/gulpplug/blob/master/lib/options.js).\n\nCreates a new `Options` instance. Accepts an object with option definitions as\noptional argument.\n\n``` javascript\nvar Options = require('defined-options');\n\nvar options = new Options({\n        text: {\n            validate: 'string!empty',\n            default: 'foo'\n        },\n        answer: {\n            validate: 'number\u003e0',\n            default: 42\n        }\n    });\n\nconsole.log(options); // { text: [Getter/Setter], answer: [Getter/Setter] }\nconsole.log(options.getPlainObject()); // { text: 'foo', answer: 42 }\nconsole.log(options.text); // foo\nconsole.log(options.answer); // 42\n```\n\nOptions' properties have getters and setters defined via their descriptors. \nThis way validation and filtering is done automagically:\n\n``` javascript\n// option 'text' only accepts non-empty strings\noptions.text = '';\nconsole.log(options.text); // foo\noptions.text = 'bar';\nconsole.log(options.text); // bar\n\n// option 'answer' only accepts numbers \u003e 0\noptions.answer = -7;\nconsole.log(options.answer); // 42\noptions.answer = 5;\nconsole.log(options.answer); // 5\n```\n\n\n#### .defineOption()\n\nCreates a new option property or replaces an existing one with same name.\n\nAccepts option definition as single argument or option name as first and\noption definition as second argument.\n\nReturns current Options instance.\n\n``` javascript\noptions.defineOption({name: 'text', validate: 'string'});\n// or\noptions.defineOption('text', {validate: 'string'});\n```\n\n\n##### Option definition\n\nAn option definition object can have the following properties:\n\n  - `name`  \n    *required*  \n    a non-empty string defining the option name\n\n  - `validate`  \n    default: `'any'`  \n    defines how to validate an options value; accepts \n    [`validate-by-shorthand`](https://github.com/simbo/validate-by-shorthand)\n    arguments:\n      * a string defining a [shorthand string](https://github.com/simbo/validate-by-shorthand#shorthands)\n      * a regular rexpression for a match test\n      * a function, receiving a value to test, returning a boolean result\n      * an array of shorthand strings, regular expressions and/or \n        functions; validating an option value if any of these tests returns true\n\n  - `filter`  \n    default: `function(value) {return value;}`  \n    defines a filter function, receiving the validated value, returning the \n    filtered value\n\n  - `default`  \n    default: `undefined`  \n    defines an option's default value; if set to a function, it will be called\n    to set the default value\n\nExample with all properties:\n\n``` javascript\noptions.defineProperty({\n    name: 'shout',\n    default: 'HELLO!',\n    validate: 'string!empty',\n    filter: function(value) {\n        return value.toLowerCase();\n    }\n});\n\nconsole.log(options.shout); // HELLO!\noptions.shout = 'bye!';\noptions.shout = 1;\nconsole.log(options.shout); // BYE!\n```\n\n\n#### .defineOptions()\n\nCreates new option properties or replaces existing ones with same name using \n[`defineOption()`](#defineoption).\n\nExpects and object with option names as keys and option definitions as values.\n\nReturns current Options instance.\n\n``` javascript\noptions.defineOptions({\n    name: {\n        validate: 'string'\n    },\n    age: {\n        validate: 'number\u003e0'\n    }\n});\n```\n\n\n#### .removeOption()\n\nRemoves an option. Expects and option name. Returns current Options instance.\n\n``` javascript\noptions.removeOption('foo');\n```\n\n\n#### .hasOption()\n\nTests is a option is defined. Expects an option name. Returns a boolean result.\n\n``` javascript\noptions.hasOption('foo');\n```\n\n\n#### .getPlainObject()\n\nReturns a plain object with option name as keys and option values as values, \nwithout described getters and setters.\n\n``` javascript\nconsole.log(options); // { text: [Getter/Setter], answer: [Getter/Setter] }\nconsole.log(options.getPlainObject()); // { text: 'foo', answer: 42 }\n```\n\n\n#### .merge()\n\nAlias for [`mergeOptionValues()`](#optionsprototypemergeoptionvalues).\n\n\n#### .mergeOptionValues()\n\nMerges an new values into current Options instance and updates an option values \nif given value is valid.\n\nExpects on or more objects containing option names as keys and option values as \nvalues.\n\nReturns current Options instance.\n\n``` javascript\nvar options = new Options({\n        text: {\n            validate: 'string!empty',\n            default: 'foo'\n        },\n        answer: {\n            validate: 'number\u003e0',\n            default: 42\n        }\n    });\n\nconsole.log(options.getPlainObject()); // { text: 'foo', answer: 42 }\n\noptions.merge({\n    text: bar,\n    answer: -7,\n    name: 'Han'\n});\n\nconsole.log(options.getPlainObject()); // { text: 'bar', answer: 42 }\n```\n\n\n#### .mergeOptions()\n\nMerges one Options instance into another. Replaces options with same name.\n\nExpects one or more Options instances.\n\nReturns current Options instance.\n\n``` javascript\nconsole.log(options.getPlainObject()); // { text: 'foo', answer: 42 }\n\noptions.mergeOptions(new Options({\n    name: {\n        validate: 'string',\n        default: 'Han'\n    }\n}));\n\nconsole.log(options.getPlainObject()); // { text: 'foo', answer: 42, name: 'Han' }\n```\n\n#### .getOptionDefinition()\n\nReturns an option definition as [Option](#option) instance. Expects an \noption name.\n\n``` javascript\n```\n\n\n#### .getOptionDefinitions()\n\nReturns all option definitions as an object with option names as keys and the\nrespective Option instance as values.\n\n``` javascript\n```\n\n\n#### .default()\n\n``` javascript\n```\n\n\n#### .setDefaultOptionValue()\n\n``` javascript\n```\n\n\n#### .setDefaultOptionValues()\n\n``` javascript\n```\n\n\n#### .validate()\n\n``` javascript\n```\n\n\n#### .validateOptionValue()\n\n``` javascript\n```\n\n\n#### .validateOptionValues()\n\n``` javascript\n```\n\n\n### Option()\n\n``` javascript\n```\n\n\n## License\n\n[MIT \u0026copy; 2015 Simon Lepel](http://simbo.mit-license.org/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimbo%2Fdefined-options","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsimbo%2Fdefined-options","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimbo%2Fdefined-options/lists"}