Skip to content

Commit

Permalink
extract getPluginName and toCamelCase to lib-folder (#205)
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak committed Dec 1, 2022
1 parent f2be4e1 commit 5cb68c5
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 24 deletions.
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')
})

0 comments on commit 5cb68c5

Please sign in to comment.