Skip to content

Commit

Permalink
feat(*): Allow multiple .options()
Browse files Browse the repository at this point in the history
  • Loading branch information
justinvdm committed Apr 27, 2020
1 parent 9e8db55 commit 9e86cd8
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 6 deletions.
9 changes: 7 additions & 2 deletions int.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var hash = require('./hash')
var fit = require('./internal/fit')
var conj = require('./internal/conj')

function int(input, opts) {
opts = opts || 0
Expand All @@ -10,8 +11,12 @@ function int(input, opts) {
}

int.options = function intOptions(opts) {
return function intOptionsFn(input) {
return int(input, opts)
var base = this
intFn.options = int.options
return intFn

function intFn(input, overrides) {
return base(input, conj(opts, overrides))
}
}

Expand Down
24 changes: 24 additions & 0 deletions internal/conj.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var hasOwnProperty = Object.prototype.hasOwnProperty

module.exports = function conj(a, b) {
var result = {}
var k

if (a) {
for (k in a) {
if (hasOwnProperty.call(a, k)) {
result[k] = a[k]
}
}
}

if (b) {
for (k in b) {
if (hasOwnProperty.call(b, k)) {
result[k] = b[k]
}
}
}

return result
}
13 changes: 12 additions & 1 deletion tests/int.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ tap.test('min and max', t => {
})

tap.test('.options()', t => {
t.equals(int.options({ max: 2 })('foo'), int('foo', { max: 2 }))
t.equals(
int
.options({
min: 2,
max: 2
})
.options({ min: 1 })('foo'),
int('foo', {
min: 1,
max: 2
})
)
t.end()
})
5 changes: 4 additions & 1 deletion tests/word.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ tap.test('capitalize', t => {

tap.test('.options()', t => {
t.equals(
word.options({ capitalize: false })(23),
word
.options({ capitalize: false })
.options({ capitalize: true })
.options({ capitalize: false })(23),
word(23, { capitalize: false })
)

Expand Down
9 changes: 7 additions & 2 deletions word.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var hash = require('./hash')
var conj = require('./internal/conj')
var defaults = require('./internal/defaults')

var VOWELS = 'aeiou'
Expand Down Expand Up @@ -31,8 +32,12 @@ function word(input, opts) {
}

word.options = function wordOptions(opts) {
return function wordOptionsFn(input) {
return word(input, opts)
var base = this
wordFn.options = word.options
return wordFn

function wordFn(input, overrides) {
return base(input, conj(opts, overrides))
}
}

Expand Down

0 comments on commit 9e86cd8

Please sign in to comment.