Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accepts lazy evaluation for prefix opt #102

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions src/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,25 @@ module.exports = function () {
}

Logdown._normalizeOpts = function (prefix, opts) {
if (typeof prefix !== 'string') {
throw new TypeError('prefix must be a string')
}

opts = opts || {}

var markdown = opts.markdown === undefined ? true : Boolean(opts.markdown)
var prefixColor = opts.prefixColor || Logdown._getNextPrefixColor()
var logger = opts.logger || console
if (typeof prefix === 'object') {
var originalPrefixVal = prefix
prefix = prefix.prefix
opts = originalPrefixVal
} else if (typeof prefix === 'string') {
opts.prefix = prefix
}

return {
logger: logger,
markdown: markdown,
prefix: prefix,
prefixColor: prefixColor
if (typeof opts.prefix !== 'string') {
throw new TypeError('prefix must be a string')
}

opts.markdown = opts.markdown === undefined ? true : Boolean(opts.markdown)
opts.prefixColor = opts.prefixColor || Logdown._getNextPrefixColor()
opts.logger = opts.logger || console

return opts
}

Logdown._getInitialState = function (opts) {
Expand Down
10 changes: 10 additions & 0 deletions test/common/constructor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,14 @@ describe('logdown()', () => {
log1.log('lorem')
expect(logger.log).toHaveBeenCalled()
})

it('accepts prefix as option argument key', () => {
const outsideVar = 'bar'
const log1 = logdown({
get prefix () {
return `foo-${outsideVar}`
}
})
expect(log1.opts.prefix).toEqual('foo-bar')
})
})