├── .editorconfig ├── .eslintrc.json ├── .gitattributes ├── .gitignore ├── .travis.yml ├── .verb.md ├── LICENSE ├── README.md ├── gulpfile.js ├── index.js ├── package.json ├── test.js └── utils.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | end_of_line = lf 6 | charset = utf-8 7 | indent_size = 2 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | 11 | [{**/{actual,fixtures,expected,templates}/**,*.md}] 12 | trim_trailing_whitespace = false 13 | insert_final_newline = false -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "ecmaFeatures": { 3 | "modules": true, 4 | "experimentalObjectRestSpread": true 5 | }, 6 | 7 | "env": { 8 | "browser": false, 9 | "es6": true, 10 | "node": true, 11 | "mocha": true 12 | }, 13 | 14 | "globals": { 15 | "document": false, 16 | "navigator": false, 17 | "window": false 18 | }, 19 | 20 | "rules": { 21 | "accessor-pairs": 2, 22 | "arrow-spacing": [2, { "before": true, "after": true }], 23 | "block-spacing": [2, "always"], 24 | "brace-style": [2, "1tbs", { "allowSingleLine": true }], 25 | "comma-dangle": [2, "never"], 26 | "comma-spacing": [2, { "before": false, "after": true }], 27 | "comma-style": [2, "last"], 28 | "constructor-super": 2, 29 | "curly": [2, "multi-line"], 30 | "dot-location": [2, "property"], 31 | "eol-last": 2, 32 | "eqeqeq": [2, "allow-null"], 33 | "generator-star-spacing": [2, { "before": true, "after": true }], 34 | "handle-callback-err": [2, "^(err|error)$" ], 35 | "indent": [2, 2, { "SwitchCase": 1 }], 36 | "key-spacing": [2, { "beforeColon": false, "afterColon": true }], 37 | "keyword-spacing": [2, { "before": true, "after": true }], 38 | "new-cap": [2, { "newIsCap": true, "capIsNew": false }], 39 | "new-parens": 2, 40 | "no-array-constructor": 2, 41 | "no-caller": 2, 42 | "no-class-assign": 2, 43 | "no-cond-assign": 2, 44 | "no-const-assign": 2, 45 | "no-control-regex": 2, 46 | "no-debugger": 2, 47 | "no-delete-var": 2, 48 | "no-dupe-args": 2, 49 | "no-dupe-class-members": 2, 50 | "no-dupe-keys": 2, 51 | "no-duplicate-case": 2, 52 | "no-empty-character-class": 2, 53 | "no-eval": 2, 54 | "no-ex-assign": 2, 55 | "no-extend-native": 2, 56 | "no-extra-bind": 2, 57 | "no-extra-boolean-cast": 2, 58 | "no-extra-parens": [2, "functions"], 59 | "no-fallthrough": 2, 60 | "no-floating-decimal": 2, 61 | "no-func-assign": 2, 62 | "no-implied-eval": 2, 63 | "no-inner-declarations": [2, "functions"], 64 | "no-invalid-regexp": 2, 65 | "no-irregular-whitespace": 2, 66 | "no-iterator": 2, 67 | "no-label-var": 2, 68 | "no-labels": 2, 69 | "no-lone-blocks": 2, 70 | "no-mixed-spaces-and-tabs": 2, 71 | "no-multi-spaces": 2, 72 | "no-multi-str": 2, 73 | "no-multiple-empty-lines": [2, { "max": 1 }], 74 | "no-native-reassign": 0, 75 | "no-negated-in-lhs": 2, 76 | "no-new": 2, 77 | "no-new-func": 2, 78 | "no-new-object": 2, 79 | "no-new-require": 2, 80 | "no-new-wrappers": 2, 81 | "no-obj-calls": 2, 82 | "no-octal": 2, 83 | "no-octal-escape": 2, 84 | "no-proto": 0, 85 | "no-redeclare": 2, 86 | "no-regex-spaces": 2, 87 | "no-return-assign": 2, 88 | "no-self-compare": 2, 89 | "no-sequences": 2, 90 | "no-shadow-restricted-names": 2, 91 | "no-spaced-func": 2, 92 | "no-sparse-arrays": 2, 93 | "no-this-before-super": 2, 94 | "no-throw-literal": 2, 95 | "no-trailing-spaces": 0, 96 | "no-undef": 2, 97 | "no-undef-init": 2, 98 | "no-unexpected-multiline": 2, 99 | "no-unneeded-ternary": [2, { "defaultAssignment": false }], 100 | "no-unreachable": 2, 101 | "no-unused-vars": [2, { "vars": "all", "args": "none" }], 102 | "no-useless-call": 0, 103 | "no-with": 2, 104 | "one-var": [0, { "initialized": "never" }], 105 | "operator-linebreak": [0, "after", { "overrides": { "?": "before", ":": "before" } }], 106 | "padded-blocks": [0, "never"], 107 | "quotes": [2, "single", "avoid-escape"], 108 | "radix": 2, 109 | "semi": [2, "always"], 110 | "semi-spacing": [2, { "before": false, "after": true }], 111 | "space-before-blocks": [2, "always"], 112 | "space-before-function-paren": [2, "never"], 113 | "space-in-parens": [2, "never"], 114 | "space-infix-ops": 2, 115 | "space-unary-ops": [2, { "words": true, "nonwords": false }], 116 | "spaced-comment": [0, "always", { "markers": ["global", "globals", "eslint", "eslint-disable", "*package", "!", ","] }], 117 | "use-isnan": 2, 118 | "valid-typeof": 2, 119 | "wrap-iife": [2, "any"], 120 | "yoda": [2, "never"] 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Enforce Unix newlines 2 | * text eol=lf 3 | 4 | # binaries 5 | *.ai binary 6 | *.psd binary 7 | *.jpg binary 8 | *.gif binary 9 | *.png binary 10 | *.jpeg binary 11 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # always ignore files 2 | *.DS_Store 3 | *.sublime-* 4 | 5 | # test related, or directories generated by tests 6 | test/actual 7 | actual 8 | coverage 9 | .nyc* 10 | 11 | # npm 12 | node_modules 13 | npm-debug.log 14 | 15 | # yarn 16 | yarn.lock 17 | yarn-error.log 18 | 19 | # misc 20 | _gh_pages 21 | _draft 22 | _drafts 23 | bower_components 24 | vendor 25 | temp 26 | tmp 27 | TODO.md 28 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | os: 3 | - linux 4 | - osx 5 | language: node_js 6 | node_js: 7 | - node 8 | - '7' 9 | - '6' 10 | - '5' 11 | - '4' 12 | - '0.12' 13 | - '0.10' 14 | matrix: 15 | allow_failures: [] 16 | fast_finish: true 17 | -------------------------------------------------------------------------------- /.verb.md: -------------------------------------------------------------------------------- 1 | ## Usage 2 | 3 | Use as a plugin with with your `base` application: 4 | 5 | ```js 6 | var Base = require('base'); 7 | var options = require('{%= name %}'); 8 | 9 | var base = new Base(); 10 | base.use(options()); // plugin 11 | 12 | // set an option 13 | app.option('a', 'b'); 14 | 15 | // set a nested property 16 | app.option('x.y', 'z'); 17 | 18 | // get an option 19 | console.log(app.option('x')); 20 | //=> {y: 'z'} 21 | ``` 22 | 23 | ## API 24 | 25 | ### .option 26 | 27 | Set or get an option. 28 | 29 | **Params** 30 | 31 | * `key` **{String}**: The option name. 32 | * `value` **{*}**: The value to set. 33 | * `returns` **{*}**: Returns a `value` when only `key` is defined. 34 | 35 | **Example** 36 | 37 | ```js 38 | app.option('a', true); 39 | app.option('a'); 40 | //=> true 41 | ``` 42 | 43 | ### .hasOption 44 | 45 | Return true if `options.hasOwnProperty(key)` 46 | 47 | **Params** 48 | 49 | * `prop` **{String}** 50 | * `returns` **{Boolean}**: True if `prop` exists. 51 | 52 | **Example** 53 | 54 | ```js 55 | app.hasOption('a'); 56 | //=> false 57 | app.option('a', 'b'); 58 | app.hasOption('a'); 59 | //=> true 60 | ``` 61 | 62 | ### .enable 63 | 64 | Enable `key`. 65 | 66 | **Params** 67 | 68 | * `key` **{String}** 69 | * `returns` **{Object}** `Options`: to enable chaining 70 | 71 | **Example** 72 | 73 | ```js 74 | app.enable('a'); 75 | ``` 76 | 77 | ### .disable 78 | 79 | Disable `key`. 80 | 81 | **Params** 82 | 83 | * `key` **{String}**: The option to disable. 84 | * `returns` **{Object}** `Options`: to enable chaining 85 | 86 | **Example** 87 | 88 | ```js 89 | app.disable('a'); 90 | ``` 91 | 92 | ### .enabled 93 | 94 | Check if `prop` is enabled (truthy). 95 | 96 | **Params** 97 | 98 | * `prop` **{String}** 99 | * `returns` **{Boolean}** 100 | 101 | **Example** 102 | 103 | ```js 104 | app.enabled('a'); 105 | //=> false 106 | 107 | app.enable('a'); 108 | app.enabled('a'); 109 | //=> true 110 | ``` 111 | 112 | ### .disabled 113 | 114 | Check if `prop` is disabled (falsey). 115 | 116 | **Params** 117 | 118 | * `prop` **{String}** 119 | * `returns` **{Boolean}**: Returns true if `prop` is disabled. 120 | 121 | **Example** 122 | 123 | ```js 124 | app.disabled('a'); 125 | //=> true 126 | 127 | app.enable('a'); 128 | app.disabled('a'); 129 | //=> false 130 | ``` 131 | 132 | ### .isTrue 133 | 134 | Returns true if the value of `prop` is strictly `true`. 135 | 136 | **Params** 137 | 138 | * `prop` **{String}** 139 | * `returns` **{Boolean}**: Uses strict equality for comparison. 140 | 141 | **Example** 142 | 143 | ```js 144 | app.option('a', 'b'); 145 | app.isTrue('a'); 146 | //=> false 147 | 148 | app.option('c', true); 149 | app.isTrue('c'); 150 | //=> true 151 | 152 | app.option({a: {b: {c: true}}}); 153 | app.isTrue('a.b.c'); 154 | //=> true 155 | ``` 156 | 157 | ### .isFalse 158 | 159 | Returns true if the value of `key` is strictly `false`. 160 | 161 | **Params** 162 | 163 | * `prop` **{String}** 164 | * `returns` **{Boolean}**: Uses strict equality for comparison. 165 | 166 | **Example** 167 | 168 | ```js 169 | app.option('a', null); 170 | app.isFalse('a'); 171 | //=> false 172 | 173 | app.option('c', false); 174 | app.isFalse('c'); 175 | //=> true 176 | 177 | app.option({a: {b: {c: false}}}); 178 | app.isFalse('a.b.c'); 179 | //=> true 180 | ``` 181 | 182 | ### .isBoolean 183 | 184 | Return true if the value of key is either `true` or `false`. 185 | 186 | **Params** 187 | 188 | * `key` **{String}** 189 | * `returns` **{Boolean}**: True if `true` or `false`. 190 | 191 | **Example** 192 | 193 | ```js 194 | app.option('a', 'b'); 195 | app.isBoolean('a'); 196 | //=> false 197 | 198 | app.option('c', true); 199 | app.isBoolean('c'); 200 | //=> true 201 | ``` 202 | 203 | {%= apidocs('index.js') %} 204 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015-2017, Jon Schlinkert 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # base-option [![NPM version](https://img.shields.io/npm/v/base-option.svg?style=flat)](https://www.npmjs.com/package/base-option) [![NPM monthly downloads](https://img.shields.io/npm/dm/base-option.svg?style=flat)](https://npmjs.org/package/base-option) [![NPM total downloads](https://img.shields.io/npm/dt/base-option.svg?style=flat)](https://npmjs.org/package/base-option) [![Linux Build Status](https://img.shields.io/travis/node-base/base-option.svg?style=flat&label=Travis)](https://travis-ci.org/node-base/base-option) 2 | 3 | > Adds a few options methods to base, like `option`, `enable` and `disable`. See the readme for the full API. 4 | 5 | ## Install 6 | 7 | Install with [npm](https://www.npmjs.com/): 8 | 9 | ```sh 10 | $ npm install --save base-option 11 | ``` 12 | 13 | ## Usage 14 | 15 | Use as a plugin with with your `base` application: 16 | 17 | ```js 18 | var Base = require('base'); 19 | var options = require('base-option'); 20 | 21 | var base = new Base(); 22 | base.use(options()); // plugin 23 | 24 | // set an option 25 | app.option('a', 'b'); 26 | 27 | // set a nested property 28 | app.option('x.y', 'z'); 29 | 30 | // get an option 31 | console.log(app.option('x')); 32 | //=> {y: 'z'} 33 | ``` 34 | 35 | ## API 36 | 37 | ### .option 38 | 39 | Set or get an option. 40 | 41 | **Params** 42 | 43 | * `key` **{String}**: The option name. 44 | * `value` **{any}**: The value to set. 45 | * `returns` **{any}**: Returns a `value` when only `key` is defined. 46 | 47 | **Example** 48 | 49 | ```js 50 | app.option('a', true); 51 | app.option('a'); 52 | //=> true 53 | ``` 54 | 55 | ### .hasOption 56 | 57 | Return true if `options.hasOwnProperty(key)` 58 | 59 | **Params** 60 | 61 | * `prop` **{String}** 62 | * `returns` **{Boolean}**: True if `prop` exists. 63 | 64 | **Example** 65 | 66 | ```js 67 | app.hasOption('a'); 68 | //=> false 69 | app.option('a', 'b'); 70 | app.hasOption('a'); 71 | //=> true 72 | ``` 73 | 74 | ### .enable 75 | 76 | Enable `key`. 77 | 78 | **Params** 79 | 80 | * `key` **{String}** 81 | * `returns` **{Object}** `Options`: to enable chaining 82 | 83 | **Example** 84 | 85 | ```js 86 | app.enable('a'); 87 | ``` 88 | 89 | ### .disable 90 | 91 | Disable `key`. 92 | 93 | **Params** 94 | 95 | * `key` **{String}**: The option to disable. 96 | * `returns` **{Object}** `Options`: to enable chaining 97 | 98 | **Example** 99 | 100 | ```js 101 | app.disable('a'); 102 | ``` 103 | 104 | ### .enabled 105 | 106 | Check if `prop` is enabled (truthy). 107 | 108 | **Params** 109 | 110 | * `prop` **{String}** 111 | * `returns` **{Boolean}** 112 | 113 | **Example** 114 | 115 | ```js 116 | app.enabled('a'); 117 | //=> false 118 | 119 | app.enable('a'); 120 | app.enabled('a'); 121 | //=> true 122 | ``` 123 | 124 | ### .disabled 125 | 126 | Check if `prop` is disabled (falsey). 127 | 128 | **Params** 129 | 130 | * `prop` **{String}** 131 | * `returns` **{Boolean}**: Returns true if `prop` is disabled. 132 | 133 | **Example** 134 | 135 | ```js 136 | app.disabled('a'); 137 | //=> true 138 | 139 | app.enable('a'); 140 | app.disabled('a'); 141 | //=> false 142 | ``` 143 | 144 | ### .isTrue 145 | 146 | Returns true if the value of `prop` is strictly `true`. 147 | 148 | **Params** 149 | 150 | * `prop` **{String}** 151 | * `returns` **{Boolean}**: Uses strict equality for comparison. 152 | 153 | **Example** 154 | 155 | ```js 156 | app.option('a', 'b'); 157 | app.isTrue('a'); 158 | //=> false 159 | 160 | app.option('c', true); 161 | app.isTrue('c'); 162 | //=> true 163 | 164 | app.option({a: {b: {c: true}}}); 165 | app.isTrue('a.b.c'); 166 | //=> true 167 | ``` 168 | 169 | ### .isFalse 170 | 171 | Returns true if the value of `key` is strictly `false`. 172 | 173 | **Params** 174 | 175 | * `prop` **{String}** 176 | * `returns` **{Boolean}**: Uses strict equality for comparison. 177 | 178 | **Example** 179 | 180 | ```js 181 | app.option('a', null); 182 | app.isFalse('a'); 183 | //=> false 184 | 185 | app.option('c', false); 186 | app.isFalse('c'); 187 | //=> true 188 | 189 | app.option({a: {b: {c: false}}}); 190 | app.isFalse('a.b.c'); 191 | //=> true 192 | ``` 193 | 194 | ### .isBoolean 195 | 196 | Return true if the value of key is either `true` or `false`. 197 | 198 | **Params** 199 | 200 | * `key` **{String}** 201 | * `returns` **{Boolean}**: True if `true` or `false`. 202 | 203 | **Example** 204 | 205 | ```js 206 | app.option('a', 'b'); 207 | app.isBoolean('a'); 208 | //=> false 209 | 210 | app.option('c', true); 211 | app.isBoolean('c'); 212 | //=> true 213 | ``` 214 | 215 | ### [.option.set](index.js#L45) 216 | 217 | Set option `key` on `app.options` with the given `value` 218 | 219 | **Params** 220 | 221 | * `key` **{String}**: Option key, dot-notation may be used. 222 | * `value` **{any}** 223 | 224 | **Example** 225 | 226 | ```js 227 | app.option.set('a', 'b'); 228 | console.log(app.option.get('a')); 229 | //=> 'b' 230 | ``` 231 | 232 | ### [.option.get](index.js#L64) 233 | 234 | Get option `key` from `app.options` 235 | 236 | **Params** 237 | 238 | * `key` **{String}**: Option key, dot-notation may be used. 239 | * `returns` **{any}** 240 | 241 | **Example** 242 | 243 | ```js 244 | app.option({a: 'b'}); 245 | console.log(app.option.get('a')); 246 | //=> 'b' 247 | ``` 248 | 249 | ### [.option.create](index.js#L82) 250 | 251 | Returns a shallow clone of `app.options` with all of the options methods, as well as a `.merge` method for merging options onto the cloned object. 252 | 253 | **Params** 254 | 255 | * `options` **{Options}**: Object to merge onto the returned options object. 256 | * `returns` **{Object}** 257 | 258 | **Example** 259 | 260 | ```js 261 | var opts = app.option.create(); 262 | opts.merge({foo: 'bar'}); 263 | ``` 264 | 265 | ## About 266 | 267 | ### Related projects 268 | 269 | * [base-data](https://www.npmjs.com/package/base-data): adds a `data` method to base-methods. | [homepage](https://github.com/node-base/base-data "adds a `data` method to base-methods.") 270 | * [base-task](https://www.npmjs.com/package/base-task): base plugin that provides a very thin wrapper around [https://github.com/doowb/composer](https://github.com/doowb/composer) for adding task methods to… [more](https://github.com/node-base/base-task) | [homepage](https://github.com/node-base/base-task "base plugin that provides a very thin wrapper around for adding task methods to your application.") 271 | * [base](https://www.npmjs.com/package/base): Framework for rapidly creating high quality node.js applications, using plugins like building blocks | [homepage](https://github.com/node-base/base "Framework for rapidly creating high quality node.js applications, using plugins like building blocks") 272 | 273 | ### Contributing 274 | 275 | Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new). 276 | 277 | ### Contributors 278 | 279 | | **Commits** | **Contributor** | 280 | | --- | --- | 281 | | 64 | [jonschlinkert](https://github.com/jonschlinkert) | 282 | | 4 | [doowb](https://github.com/doowb) | 283 | 284 | ### Building docs 285 | 286 | _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_ 287 | 288 | To generate the readme, run the following command: 289 | 290 | ```sh 291 | $ npm install -g verbose/verb#dev verb-generate-readme && verb 292 | ``` 293 | 294 | ### Running tests 295 | 296 | Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command: 297 | 298 | ```sh 299 | $ npm install && npm test 300 | ``` 301 | 302 | ### Author 303 | 304 | **Jon Schlinkert** 305 | 306 | * [github/jonschlinkert](https://github.com/jonschlinkert) 307 | * [twitter/jonschlinkert](https://twitter.com/jonschlinkert) 308 | 309 | ### License 310 | 311 | Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert). 312 | Released under the [MIT License](LICENSE). 313 | 314 | *** 315 | 316 | _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on May 30, 2017._ -------------------------------------------------------------------------------- /gulpfile.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var gulp = require('gulp'); 4 | var mocha = require('gulp-mocha'); 5 | var istanbul = require('gulp-istanbul'); 6 | var eslint = require('gulp-eslint'); 7 | 8 | var lint = ['index.js', 'utils.js', 'gulpfile.js']; 9 | 10 | gulp.task('coverage', function() { 11 | return gulp.src(lint) 12 | .pipe(istanbul()) 13 | .pipe(istanbul.hookRequire()); 14 | }); 15 | 16 | gulp.task('test', ['coverage'], function() { 17 | return gulp.src('test.js') 18 | .pipe(mocha({reporter: 'spec'})) 19 | .pipe(istanbul.writeReports()); 20 | }); 21 | 22 | gulp.task('lint', function() { 23 | return gulp.src(lint.concat('test.js')) 24 | .pipe(eslint()) 25 | .pipe(eslint.format()); 26 | }); 27 | 28 | gulp.task('default', ['test', 'lint']); 29 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * base-option 3 | * 4 | * Copyright (c) 2015-2017, Jon Schlinkert. 5 | * Released under the MIT License. 6 | */ 7 | 8 | 'use strict'; 9 | 10 | var utils = require('./utils'); 11 | 12 | module.exports = function(options) { 13 | return function fn(app) { 14 | if (!utils.isValid(app, 'base-option', '*')) return; 15 | 16 | var Options = utils.Options; 17 | var define = utils.define; 18 | var set = utils.set; 19 | var get = utils.get; 20 | 21 | // original constructor reference 22 | var ctor = this.constructor; 23 | Options.call(this, utils.merge(this.options, options)); 24 | 25 | /** 26 | * Mixin `Options.prototype` methods 27 | */ 28 | 29 | this.visit('define', Options.prototype); 30 | var opts = this.options; 31 | 32 | /** 33 | * Set option `key` on `app.options` with the given `value` 34 | * ```js 35 | * app.option.set('a', 'b'); 36 | * console.log(app.option.get('a')); 37 | * //=> 'b' 38 | * ``` 39 | * @name .option.set 40 | * @param {String} `key` Option key, dot-notation may be used. 41 | * @param {any} `value` 42 | * @api public 43 | */ 44 | 45 | define(this.option, 'set', function(key, val) { 46 | set(opts, key, val); 47 | return opts; 48 | }); 49 | 50 | /** 51 | * Get option `key` from `app.options` 52 | * 53 | * ```js 54 | * app.option({a: 'b'}); 55 | * console.log(app.option.get('a')); 56 | * //=> 'b' 57 | * ``` 58 | * @name .option.get 59 | * @param {String} `key` Option key, dot-notation may be used. 60 | * @return {any} 61 | * @api public 62 | */ 63 | 64 | define(this.option, 'get', function(key) { 65 | return get(opts, key); 66 | }); 67 | 68 | /** 69 | * Returns a shallow clone of `app.options` with all of the options methods, as 70 | * well as a `.merge` method for merging options onto the cloned object. 71 | * 72 | * ```js 73 | * var opts = app.option.create(); 74 | * opts.merge({foo: 'bar'}); 75 | * ``` 76 | * @name .option.create 77 | * @param {Options} `options` Object to merge onto the returned options object. 78 | * @return {Object} 79 | * @api public 80 | */ 81 | 82 | define(this.option, 'create', function(options) { 83 | var inst = new Options(utils.merge({}, opts)); 84 | if (options) { 85 | inst.option.apply(inst, arguments); 86 | } 87 | 88 | define(inst.options, 'set', function(key, val) { 89 | set(this, key, val); 90 | return this; 91 | }); 92 | 93 | define(inst.options, 'get', function(key) { 94 | return get(this, key); 95 | }); 96 | 97 | define(inst.options, 'merge', function() { 98 | var args = [].concat.apply([], [].slice.call(arguments)); 99 | args.unshift(this); 100 | return utils.merge.apply(utils.merge, args); 101 | }); 102 | 103 | define(inst, '_callbacks', inst._callbacks); 104 | return inst.options; 105 | }); 106 | 107 | // restore original constructor 108 | define(this, 'constructor', ctor); 109 | return fn; 110 | }; 111 | }; 112 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "base-option", 3 | "description": "Adds a few options methods to base, like `option`, `enable` and `disable`. See the readme for the full API.", 4 | "version": "1.0.0", 5 | "homepage": "https://github.com/node-base/base-option", 6 | "author": "Jon Schlinkert (https://github.com/jonschlinkert)", 7 | "contributors": [ 8 | "Brian Woodward (https://twitter.com/doowb)", 9 | "Jon Schlinkert (http://twitter.com/jonschlinkert)" 10 | ], 11 | "repository": "node-base/base-option", 12 | "bugs": { 13 | "url": "https://github.com/node-base/base-option/issues" 14 | }, 15 | "license": "MIT", 16 | "files": [ 17 | "index.js", 18 | "utils.js" 19 | ], 20 | "main": "index.js", 21 | "engines": { 22 | "node": ">=0.10.0" 23 | }, 24 | "scripts": { 25 | "test": "mocha" 26 | }, 27 | "dependencies": { 28 | "define-property": "^1.0.0", 29 | "get-value": "^2.0.6", 30 | "is-valid-app": "^0.3.0", 31 | "isobject": "^3.0.0", 32 | "lazy-cache": "^2.0.2", 33 | "mixin-deep": "^1.2.0", 34 | "option-cache": "^4.0.0", 35 | "set-value": "^1.0.0" 36 | }, 37 | "devDependencies": { 38 | "base": "^0.13.0", 39 | "base-plugins": "^1.0.0", 40 | "gulp": "^3.9.1", 41 | "gulp-eslint": "^3.0.1", 42 | "gulp-format-md": "^0.1.12", 43 | "gulp-istanbul": "^1.1.1", 44 | "gulp-mocha": "^3.0.1", 45 | "mocha": "^3.4.2" 46 | }, 47 | "keywords": [ 48 | "api", 49 | "app", 50 | "application", 51 | "base", 52 | "base-plugin", 53 | "baseplugin", 54 | "building-blocks", 55 | "config", 56 | "configuration", 57 | "create", 58 | "framework", 59 | "option", 60 | "options", 61 | "plugin", 62 | "plugins", 63 | "settings", 64 | "tool", 65 | "toolkit", 66 | "tools" 67 | ], 68 | "verb": { 69 | "run": true, 70 | "toc": false, 71 | "layout": "default", 72 | "tasks": [ 73 | "readme" 74 | ], 75 | "plugins": [ 76 | "gulp-format-md" 77 | ], 78 | "related": { 79 | "list": [ 80 | "base", 81 | "base-data", 82 | "base-task" 83 | ] 84 | }, 85 | "reflinks": [ 86 | "verb", 87 | "verb-readme-generator" 88 | ], 89 | "lint": { 90 | "reflinks": true 91 | } 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | require('mocha'); 4 | var Base = require('base'); 5 | var assert = require('assert'); 6 | var plugins = require('base-plugins'); 7 | var options = require('./'); 8 | var app; 9 | 10 | Base.use(function() { 11 | this.isApp = true; 12 | }); 13 | 14 | describe('option', function() { 15 | beforeEach(function() { 16 | app = new Base(); 17 | app.use(options()); 18 | }); 19 | 20 | describe('method', function() { 21 | it('should add the option method to the `app` instance:', function() { 22 | assert.equal(typeof app.option, 'function'); 23 | }); 24 | 25 | it('should not add the option method to the `Base` prototype:', function() { 26 | assert.notEqual(typeof Base.prototype.option, 'function'); 27 | }); 28 | }); 29 | 30 | describe('plugin', function() { 31 | it('should add options passed to the plugin', function() { 32 | app = new Base(); 33 | app.use(options({z: 'y'})); 34 | assert.equal(app.options.z, 'y'); 35 | }); 36 | 37 | it('should pass the plugin to "run" when the `plugins` plugin is used', function() { 38 | app = new Base(); 39 | app.use(plugins()); 40 | app.use(options()); 41 | assert.equal(app.fns.length, 1); 42 | }); 43 | 44 | it('should pass the plugin to "run" when {run:false} is defined', function() { 45 | app.use(plugins()); 46 | app.use(options({run: false})); 47 | assert.equal(app.fns.length, 0); 48 | }); 49 | 50 | it('should not overwrite existing options', function() { 51 | app.options.foo = 'bar'; 52 | app.use(options()); 53 | app.option('a', 'b'); 54 | assert.equal(app.options.foo, 'bar'); 55 | assert.equal(app.options.a, 'b'); 56 | }); 57 | 58 | it('should set options as key-value pairs', function() { 59 | app.option('a', 'b'); 60 | assert.equal(app.options.a, 'b'); 61 | }); 62 | 63 | it('should set an object', function() { 64 | app.option({c: 'd'}); 65 | var c = app.option('c'); 66 | assert.equal(c, 'd'); 67 | }); 68 | 69 | it('should set multiple values', function() { 70 | app.option({a: 'b'}); 71 | app.option({c: 'd'}); 72 | assert.equal(app.options.a, 'b'); 73 | assert.equal(app.options.c, 'd'); 74 | }); 75 | 76 | it('should set multiple values using key-value', function() { 77 | app.option('a', 'b'); 78 | app.option('c', 'd'); 79 | assert.equal(app.options.a, 'b'); 80 | assert.equal(app.options.c, 'd'); 81 | }); 82 | 83 | it('should set multiple nested values using dot-notation', function() { 84 | app.option('a.b', 'c'); 85 | app.option('a.c', 'd'); 86 | assert.equal(app.options.a.b, 'c'); 87 | assert.equal(app.options.a.c, 'd'); 88 | }); 89 | 90 | it('should get options', function() { 91 | app.option('a', 'b'); 92 | var a = app.option('a'); 93 | assert.equal(a, 'b'); 94 | }); 95 | 96 | it('should return true if an option exists', function() { 97 | app.option('a', 'b'); 98 | assert(app.hasOption('a')); 99 | }); 100 | 101 | it('should enable an option', function() { 102 | assert(!app.options.a); 103 | app.enable('a'); 104 | assert(app.options.a); 105 | }); 106 | 107 | it('should disable an option', function() { 108 | app.enable('a'); 109 | assert(app.options.a); 110 | app.disable('a'); 111 | assert(!app.options.a); 112 | }); 113 | 114 | it('should set nested options', function() { 115 | app.option('a.b.c', 'd'); 116 | assert.deepEqual(app.options.a, {b: {c: 'd'}}); 117 | }); 118 | 119 | it('should get nested options', function() { 120 | app.option('a.b.c', 'd'); 121 | assert.deepEqual(app.option('a'), {b: {c: 'd'}}); 122 | assert.deepEqual(app.option('a.b'), {c: 'd'}); 123 | assert.deepEqual(app.option('a.b.c'), 'd'); 124 | }); 125 | }); 126 | 127 | describe('option.set', function() { 128 | beforeEach(function() { 129 | app = new Base(); 130 | app.use(options()); 131 | }); 132 | 133 | it('should set a key-value pair on app.options', function() { 134 | app.option.set('a', 'b'); 135 | app.option.set('c', 'd'); 136 | assert.equal(app.options.a, 'b'); 137 | assert.equal(app.options.c, 'd'); 138 | }); 139 | }); 140 | 141 | describe('option.get', function() { 142 | beforeEach(function() { 143 | app = new Base(); 144 | app.use(options()); 145 | }); 146 | 147 | it('should get a key-value pair from app.options', function() { 148 | app.option.set('a', 'b'); 149 | app.option.set('c', 'd'); 150 | assert.equal(app.option.get('a'), 'b'); 151 | assert.equal(app.option.get('c'), 'd'); 152 | }); 153 | }); 154 | 155 | describe('option.create', function() { 156 | beforeEach(function() { 157 | app = new Base(); 158 | app.use(options()); 159 | }); 160 | 161 | it('should clone options', function() { 162 | app.option({a: 'b', c: 'd'}); 163 | var opts = app.option.create(); 164 | app.option({e: 'f'}); 165 | assert.equal(opts.a, 'b'); 166 | assert.equal(opts.c, 'd'); 167 | assert.equal(typeof opts.e, 'undefined'); 168 | }); 169 | 170 | it('should expose a set method', function() { 171 | app.option({a: 'b', c: 'd'}); 172 | var opts = app.option.create(); 173 | 174 | opts.set('e', 'f'); 175 | opts.set('g', 'h'); 176 | assert.equal(opts.a, 'b'); 177 | assert.equal(opts.c, 'd'); 178 | assert.equal(opts.e, 'f'); 179 | assert.equal(opts.g, 'h'); 180 | }); 181 | 182 | it('should expose a get method', function() { 183 | app.option({a: 'b', c: 'd'}); 184 | var opts = app.option.create(); 185 | 186 | opts.set('e', 'f'); 187 | opts.set('g', 'h'); 188 | 189 | assert.equal(opts.get('a'), 'b'); 190 | assert.equal(opts.get('c'), 'd'); 191 | assert.equal(opts.get('e'), 'f'); 192 | assert.equal(opts.get('g'), 'h'); 193 | }); 194 | 195 | it('should expose a merge method', function() { 196 | app.option({a: 'b', c: 'd'}); 197 | var opts = app.option.create(); 198 | 199 | opts.merge({e: 'f'}, {g: 'h'}); 200 | assert.equal(opts.a, 'b'); 201 | assert.equal(opts.c, 'd'); 202 | assert.equal(opts.e, 'f'); 203 | assert.equal(opts.g, 'h'); 204 | }); 205 | }); 206 | }); 207 | -------------------------------------------------------------------------------- /utils.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var utils = require('lazy-cache')(require); 4 | var fn = require; 5 | require = utils; 6 | 7 | /** 8 | * Module dependencies 9 | */ 10 | 11 | require('define-property', 'define'); 12 | require('get-value', 'get'); 13 | require('is-valid-app', 'isValid'); 14 | require('isobject', 'isObject'); 15 | require('mixin-deep', 'merge'); 16 | require('option-cache', 'Options'); 17 | require('set-value', 'set'); 18 | require = fn; 19 | 20 | /** 21 | * Expose `utils` modules 22 | */ 23 | 24 | module.exports = utils; 25 | --------------------------------------------------------------------------------