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

extract getPluginName and toCamelCase to lib-folder #205

Merged
merged 1 commit into from Dec 1, 2022
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
13 changes: 12 additions & 1 deletion lib/extractPluginName.js → lib/getPluginName.js
Expand Up @@ -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
11 changes: 11 additions & 0 deletions 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
}
25 changes: 3 additions & 22 deletions 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

Expand Down Expand Up @@ -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
Expand All @@ -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
2 changes: 1 addition & 1 deletion 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)
Expand Down
24 changes: 24 additions & 0 deletions 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')
})