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

have fp use meta name option instead of new symbol #44

Merged
merged 4 commits into from
Jul 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 8 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,31 @@
const semver = require('semver')
const console = require('console')

const DISPLAY_NAME_SYMBOL = Symbol.for('fastify.display-name')
const fpStackTracePattern = new RegExp('at\\s{1}(?:.*\\.)?plugin\\s{1}.*\\n\\s*(.*)')
const fileNamePattern = new RegExp('(?:\\/|\\\\)(\\w*(\\.\\w*)*)\\..*')

function plugin (fn, options) {
function plugin (fn, options = {}) {
if (typeof fn !== 'function') {
throw new TypeError(`fastify-plugin expects a function, instead got a '${typeof fn}'`)
}

fn[DISPLAY_NAME_SYMBOL] = checkName(fn)

fn[Symbol.for('skip-override')] = true

if (options === undefined) return fn

if (typeof options === 'string') {
checkVersion(options)
return fn
options = {}
}

if (typeof options !== 'object' || Array.isArray(options) || options === null) {
throw new TypeError('The options object should be an object')
}

if (!options.name) {
options.name = checkName(fn)
}

fn[Symbol.for('fastify.display-name')] = options.name

if (options.fastify) {
checkVersion(options.fastify)
delete options.fastify
Expand Down
3 changes: 2 additions & 1 deletion test/composite.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ const test = t.test
const fp = require('./../')

test('anonymous function should be named composite.test', t => {
t.plan(1)
t.plan(2)

const fn = fp((fastify, opts, next) => {
next()
})

t.is(fn[Symbol.for('plugin-meta')].name, 'composite.test')
t.is(fn[Symbol.for('fastify.display-name')], 'composite.test')
})
3 changes: 2 additions & 1 deletion test/composite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import * as tap from 'tap'
const test = tap.test

test('anonymous function should be named composite.test', (t: any) => {
t.plan(1)
t.plan(2)

const fn = fp((fastify, opts, next) => {
next()
})

t.is(fn[Symbol.for('plugin-meta')].name, 'composite.test')
t.is(fn[Symbol.for('fastify.display-name')], 'composite.test')
})
3 changes: 2 additions & 1 deletion test/composite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import * as tap from 'tap'
const test = tap.test

test('anonymous function should be named composite.test', (t: any) => {
t.plan(1)
t.plan(2)

const fn = fp((fastify, opts, next) => {
next()
})

t.is(fn[Symbol.for('plugin-meta')].name, 'composite')
t.is(fn[Symbol.for('fastify.display-name')], 'composite')
})
3 changes: 2 additions & 1 deletion test/mu1tip1e.composite.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ const test = t.test
const fp = require('./../')

test('anonymous function should be named mu1tip1e.composite.test', t => {
t.plan(1)
t.plan(2)

const fn = fp((fastify, opts, next) => {
next()
})

t.is(fn[Symbol.for('plugin-meta')].name, 'mu1tip1e.composite.test')
t.is(fn[Symbol.for('fastify.display-name')], 'mu1tip1e.composite.test')
})
3 changes: 2 additions & 1 deletion test/mu1tip1e.composite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import * as tap from 'tap'
const test = tap.test

test('anonymous function should be named mu1tip1e.composite.test', t => {
t.plan(1)
t.plan(2)

const fn = fp((fastify, opts, next) => {
next()
})

t.is(fn[Symbol.for('plugin-meta')].name, 'mu1tip1e.composite.test')
t.is(fn[Symbol.for('fastify.display-name')], 'mu1tip1e.composite.test')
})
16 changes: 15 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,25 @@ test('should throw if the fastify version does not satisfies the plugin requeste
})

test('should set anonymous function name to file it was called from', t => {
t.plan(1)
t.plan(2)

const fn = fp((fastify, opts, next) => {
next()
})

t.is(fn[Symbol.for('plugin-meta')].name, 'test')
t.is(fn[Symbol.for('fastify.display-name')], 'test')
})

test('should set display-name to meta name', t => {
t.plan(2)

const functionName = 'superDuperSpecialFunction'

const fn = fp((fastify, opts, next) => next(), {
name: functionName
})

t.is(fn[Symbol.for('plugin-meta')].name, functionName)
t.is(fn[Symbol.for('fastify.display-name')], functionName)
})
3 changes: 2 additions & 1 deletion test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import * as tap from 'tap'
const test = tap.test

test('should set anonymous function name to file it was called from', t => {
t.plan(1)
t.plan(2)

const fn = fp((fastify, opts, next) => {
next()
})

t.is(fn[Symbol.for('plugin-meta')].name, 'test')
t.is(fn[Symbol.for('fastify.display-name')], 'test')
})