From fb6aa97bc25e00abeb32494209e30fe88bfe51ec Mon Sep 17 00:00:00 2001 From: John Otander Date: Thu, 11 Jul 2019 10:03:05 -0600 Subject: [PATCH] Add param case tests, follow up for #624 --- packages/mdx/mdx-hast-to-jsx.js | 8 +------- packages/mdx/test/util.test.js | 14 ++++++++++++++ packages/mdx/util.js | 7 +++++++ 3 files changed, 22 insertions(+), 7 deletions(-) create mode 100644 packages/mdx/test/util.test.js diff --git a/packages/mdx/mdx-hast-to-jsx.js b/packages/mdx/mdx-hast-to-jsx.js index de8ccaa80..edfc7e7e5 100644 --- a/packages/mdx/mdx-hast-to-jsx.js +++ b/packages/mdx/mdx-hast-to-jsx.js @@ -3,16 +3,10 @@ const declare = require('@babel/helper-plugin-utils').declare const {types: t} = require('@babel/core') const toStyleObject = require('to-style').object const uniq = require('lodash.uniq') -const {toTemplateLiteral} = require('./util') +const {paramCase, toTemplateLiteral} = require('./util') const STARTS_WITH_CAPITAL_LETTER = /^[A-Z]/ -const paramCase = string => - string - .replace(/([a-z0-9])([A-Z])/g, '$1-$2') - .replace(/([a-z])([0-9])/g, '$1-$2') - .toLowerCase() - class BabelPluginExtractJsxNames { constructor() { const names = [] diff --git a/packages/mdx/test/util.test.js b/packages/mdx/test/util.test.js new file mode 100644 index 000000000..e83504f89 --- /dev/null +++ b/packages/mdx/test/util.test.js @@ -0,0 +1,14 @@ +const {paramCase} = require('../util') + +const PARAM_CASE_FIXTURES = [ + ['ariaHidden', 'aria-hidden'], + ['data123', 'data-123'] +] + +PARAM_CASE_FIXTURES.forEach(([input, expected]) => { + test(`it turns ${input} into ${expected}`, () => { + const result = paramCase(input) + + expect(result).toEqual(expected) + }) +}) diff --git a/packages/mdx/util.js b/packages/mdx/util.js index f78974d0d..70b767803 100644 --- a/packages/mdx/util.js +++ b/packages/mdx/util.js @@ -4,6 +4,12 @@ const EXPORT_DEFAULT_REGEX = /^export default/ const BLOCKS_REGEX = '[a-z\\.]+(\\.){0,1}[a-z\\.]' const EMPTY_NEWLINE = '\n\n' +const paramCase = string => + string + .replace(/([a-z0-9])([A-Z])/g, '$1-$2') + .replace(/([a-z])([0-9])/g, '$1-$2') + .toLowerCase() + const toTemplateLiteral = text => { const escaped = text .replace(/\\/g, '\\\\') // Escape all "\" to avoid unwanted escaping in text nodes @@ -18,4 +24,5 @@ module.exports.BLOCKS_REGEX = BLOCKS_REGEX module.exports.isImport = text => IMPORT_REGEX.test(text) module.exports.isExport = text => EXPORT_REGEX.test(text) module.exports.isExportDefault = text => EXPORT_DEFAULT_REGEX.test(text) +module.exports.paramCase = paramCase module.exports.toTemplateLiteral = toTemplateLiteral