diff --git a/lib/extractPluginName.js b/lib/getPluginName.js similarity index 52% rename from lib/extractPluginName.js rename to lib/getPluginName.js index d99db16..764ec13 100644 --- a/lib/extractPluginName.js +++ b/lib/getPluginName.js @@ -3,9 +3,20 @@ const fpStackTracePattern = /at\s{1}(?:.*\.)?plugin\s{1}.*\n\s*(.*)/ const fileNamePattern = /(\w*(\.\w*)*)\..*/ -module.exports = function extractPluginName (stack) { +module.exports = function getPluginName (fn) { + if (fn.name.length > 0) return fn.name + + try { + throw new Error('anonymous function') + } catch (e) { + return extractPluginName(e.stack) + } +} + +function extractPluginName (stack) { const m = stack.match(fpStackTracePattern) // get last section of path and match for filename return m ? m[1].split(/[/\\]/).slice(-1)[0].match(fileNamePattern)[1] : 'anonymous' } +module.exports.extractPluginName = extractPluginName diff --git a/lib/toCamelCase.js b/lib/toCamelCase.js new file mode 100644 index 0000000..93e2fd2 --- /dev/null +++ b/lib/toCamelCase.js @@ -0,0 +1,11 @@ +'use strict' + +module.exports = function toCamelCase (name) { + if (name[0] === '@') { + name = name.slice(1).replace('/', '-') + } + const newName = name.replace(/-(.)/g, function (match, g1) { + return g1.toUpperCase() + }) + return newName +} diff --git a/plugin.js b/plugin.js index fd4ba1b..3939b4d 100644 --- a/plugin.js +++ b/plugin.js @@ -1,6 +1,7 @@ 'use strict' -const extractPluginName = require('./lib/extractPluginName') +const getPluginName = require('./lib/getPluginName') +const toCamelCase = require('./lib/toCamelCase') let count = 0 @@ -38,7 +39,7 @@ function plugin (fn, options = {}) { if (!options.name) { autoName = true - options.name = checkName(fn) + '-auto-' + count++ + options.name = getPluginName(fn) + '-auto-' + count++ } fn[Symbol.for('skip-override')] = options.encapsulate !== true @@ -61,25 +62,5 @@ function plugin (fn, options = {}) { return fn } -function checkName (fn) { - if (fn.name.length > 0) return fn.name - - try { - throw new Error('anonymous function') - } catch (e) { - return extractPluginName(e.stack) - } -} - -function toCamelCase (name) { - if (name[0] === '@') { - name = name.slice(1).replace('/', '-') - } - const newName = name.replace(/-(.)/g, function (match, g1) { - return g1.toUpperCase() - }) - return newName -} - plugin.default = plugin module.exports = plugin diff --git a/test/extractPluginName.test.js b/test/extractPluginName.test.js index ba11ccd..e056c8a 100644 --- a/test/extractPluginName.test.js +++ b/test/extractPluginName.test.js @@ -1,7 +1,7 @@ 'use strict' const t = require('tap') -const extractPluginName = require('../lib/extractPluginName') +const extractPluginName = require('../lib/getPluginName').extractPluginName const winStack = `Error: anonymous function at checkName (C:\\Users\\leonardo.davinci\\Desktop\\fastify-plugin\\index.js:43:11) diff --git a/test/toCamelCase.test.js b/test/toCamelCase.test.js new file mode 100644 index 0000000..9bb6e25 --- /dev/null +++ b/test/toCamelCase.test.js @@ -0,0 +1,24 @@ +'use strict' + +const { test } = require('tap') +const toCamelCase = require('../lib/toCamelCase') + +test('from kebab-case to camelCase', (t) => { + t.plan(1) + t.equal(toCamelCase('hello-world'), 'helloWorld') +}) + +test('from @-prefixed named imports', (t) => { + t.plan(1) + t.equal(toCamelCase('@hello/world'), 'helloWorld') +}) + +test('from @-prefixed named kebab-case to camelCase', (t) => { + t.plan(1) + t.equal(toCamelCase('@hello/my-world'), 'helloMyWorld') +}) + +test('from kebab-case to camelCase multiple words', (t) => { + t.plan(1) + t.equal(toCamelCase('hello-long-world'), 'helloLongWorld') +})