Skip to content
This repository was archived by the owner on Aug 8, 2020. It is now read-only.

Commit bc21e42

Browse files
committed
init commit
0 parents  commit bc21e42

9 files changed

+207
-0
lines changed

.editorconfig

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
root = true
2+
3+
[*]
4+
indent_style = tab
5+
end_of_line = lf
6+
charset = utf-8
7+
trim_trailing_whitespace = true
8+
insert_final_newline = true
9+
10+
[{package.json,*.yml}]
11+
indent_style = space
12+
indent_size = 2
13+
14+
[*.md]
15+
trim_trailing_whitespace = false

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

.travis.yml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
language: node_js
2+
node_js:
3+
- 'stable'
4+
- '0.12'
5+
- '0.10'

index.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict';
2+
var objectAssign = require('object-assign');
3+
4+
module.exports = function (options) {
5+
var chainables = options.chainables || {};
6+
7+
return function (target, fn) {
8+
fn = fn || target;
9+
10+
function wrap(createOpts, extensionOpts) {
11+
function wrappedOpts() {
12+
return objectAssign(createOpts(), extensionOpts);
13+
}
14+
15+
function wrappedFn() {
16+
var args = Array.prototype.slice.call(arguments);
17+
fn(wrappedOpts(), args);
18+
}
19+
20+
Object.keys(chainables).forEach(function (key) {
21+
Object.defineProperty(wrappedFn, key, {
22+
get: function () {
23+
return wrap(wrappedOpts, chainables[key]);
24+
}
25+
});
26+
});
27+
28+
return wrappedFn;
29+
}
30+
31+
return wrap(function () {
32+
return objectAssign({}, options.defaults);
33+
});
34+
};
35+
};

license

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) James Talmage <james@talmage.io> (github.com/jamestalmage)
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.

package.json

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "option-chain",
3+
"version": "0.0.0",
4+
"description": "My supreme module",
5+
"license": "MIT",
6+
"repository": "jamestalmage/option-chain",
7+
"author": {
8+
"name": "James Talmage",
9+
"email": "james@talmage.io",
10+
"url": "github.com/jamestalmage"
11+
},
12+
"engines": {
13+
"node": ">=0.10.0"
14+
},
15+
"scripts": {
16+
"test": "xo && ava"
17+
},
18+
"files": [
19+
"index.js"
20+
],
21+
"keywords": [
22+
""
23+
],
24+
"devDependencies": {
25+
"ava": "^0.10.0",
26+
"xo": "^0.12.1"
27+
},
28+
"dependencies": {
29+
"object-assign": "^4.0.1"
30+
}
31+
}

readme.md

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# option-chain [![Build Status](https://travis-ci.org/jamestalmage/option-chain.svg?branch=master)](https://travis-ci.org/jamestalmage/option-chain)
2+
3+
> My supreme module
4+
5+
6+
## Install
7+
8+
```
9+
$ npm install --save option-chain
10+
```
11+
12+
13+
## Usage
14+
15+
```js
16+
const optionChain = require('option-chain');
17+
18+
optionChain('unicorns');
19+
//=> 'unicorns & rainbows'
20+
```
21+
22+
23+
## API
24+
25+
### optionChain(input, [options])
26+
27+
#### input
28+
29+
Type: `string`
30+
31+
Lorem ipsum.
32+
33+
#### options
34+
35+
##### foo
36+
37+
Type: `boolean`
38+
Default: `false`
39+
40+
Lorem ipsum.
41+
42+
43+
## License
44+
45+
MIT © [James Talmage](http://github.com/jamestalmage)

test.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import test from 'ava';
2+
import fn from './';
3+
4+
test('defaults and args are passed', t => {
5+
t.plan(2);
6+
fn({
7+
defaults: {foo: 'bar'}
8+
})(function (opts, args) {
9+
t.same(opts, {foo: 'bar'});
10+
t.same(args, ['uni', 'corn']);
11+
})('uni', 'corn');
12+
});
13+
14+
test('chainables extend the options passed', t => {
15+
t.plan(2);
16+
fn({
17+
defaults: {foo: 'bar'},
18+
chainables: {
19+
moo: {cow: true}
20+
}
21+
})(function (opts, args) {
22+
t.same(opts, {foo: 'bar', cow: true});
23+
t.same(args, ['duck', 'goose']);
24+
}).moo('duck', 'goose');
25+
});
26+
27+
test('last item in the chain takes precedence', t => {
28+
t.plan(4);
29+
30+
const config = fn({
31+
chainables: {
32+
foo: {foo: true},
33+
notFoo: {foo: false}
34+
}
35+
});
36+
37+
let expected = true;
38+
39+
function isExpected(opts) {
40+
t.is(opts.foo, expected);
41+
}
42+
43+
const notFoo = config(isExpected).notFoo;
44+
const foo = config(isExpected).foo;
45+
46+
foo();
47+
notFoo.foo();
48+
49+
expected = false;
50+
51+
notFoo();
52+
foo.notFoo();
53+
});

0 commit comments

Comments
 (0)