Skip to content

Commit 9e86cd8

Browse files
committed
feat(*): Allow multiple .options()
1 parent 9e8db55 commit 9e86cd8

File tree

5 files changed

+54
-6
lines changed

5 files changed

+54
-6
lines changed

int.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var hash = require('./hash')
22
var fit = require('./internal/fit')
3+
var conj = require('./internal/conj')
34

45
function int(input, opts) {
56
opts = opts || 0
@@ -10,8 +11,12 @@ function int(input, opts) {
1011
}
1112

1213
int.options = function intOptions(opts) {
13-
return function intOptionsFn(input) {
14-
return int(input, opts)
14+
var base = this
15+
intFn.options = int.options
16+
return intFn
17+
18+
function intFn(input, overrides) {
19+
return base(input, conj(opts, overrides))
1520
}
1621
}
1722

internal/conj.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
var hasOwnProperty = Object.prototype.hasOwnProperty
2+
3+
module.exports = function conj(a, b) {
4+
var result = {}
5+
var k
6+
7+
if (a) {
8+
for (k in a) {
9+
if (hasOwnProperty.call(a, k)) {
10+
result[k] = a[k]
11+
}
12+
}
13+
}
14+
15+
if (b) {
16+
for (k in b) {
17+
if (hasOwnProperty.call(b, k)) {
18+
result[k] = b[k]
19+
}
20+
}
21+
}
22+
23+
return result
24+
}

tests/int.test.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@ tap.test('min and max', t => {
99
})
1010

1111
tap.test('.options()', t => {
12-
t.equals(int.options({ max: 2 })('foo'), int('foo', { max: 2 }))
12+
t.equals(
13+
int
14+
.options({
15+
min: 2,
16+
max: 2
17+
})
18+
.options({ min: 1 })('foo'),
19+
int('foo', {
20+
min: 1,
21+
max: 2
22+
})
23+
)
1324
t.end()
1425
})

tests/word.test.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ tap.test('capitalize', t => {
99

1010
tap.test('.options()', t => {
1111
t.equals(
12-
word.options({ capitalize: false })(23),
12+
word
13+
.options({ capitalize: false })
14+
.options({ capitalize: true })
15+
.options({ capitalize: false })(23),
1316
word(23, { capitalize: false })
1417
)
1518

word.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var hash = require('./hash')
2+
var conj = require('./internal/conj')
23
var defaults = require('./internal/defaults')
34

45
var VOWELS = 'aeiou'
@@ -31,8 +32,12 @@ function word(input, opts) {
3132
}
3233

3334
word.options = function wordOptions(opts) {
34-
return function wordOptionsFn(input) {
35-
return word(input, opts)
35+
var base = this
36+
wordFn.options = word.options
37+
return wordFn
38+
39+
function wordFn(input, overrides) {
40+
return base(input, conj(opts, overrides))
3641
}
3742
}
3843

0 commit comments

Comments
 (0)