From cfd11fbb6d06b28eea5ff5c3faa0a396cec24aaf Mon Sep 17 00:00:00 2001 From: endiliey Date: Fri, 10 Aug 2018 15:26:37 +0800 Subject: [PATCH] test: utils for load --- lib/load/utils.js | 10 +++++----- test/load/utils.test.js | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 test/load/utils.test.js diff --git a/lib/load/utils.js b/lib/load/utils.js index 2080afbefa..38b2e5bbf9 100644 --- a/lib/load/utils.js +++ b/lib/load/utils.js @@ -31,13 +31,13 @@ function encodePath(userpath) { } function fileToComponentName(file) { - let str = file.replace(/([A-Z])/g, ' $1'); - if (str.length === 1) { - return str.toUpperCase(); - } + const ext = extRE.exec(file)[1]; + let str = file.replace(extRE, ''); + str = str.replace(/([A-Z])/g, ' $1'); str = str.replace(/^[\W_]+|[\W_]+$/g, '').toLowerCase(); str = str.charAt(0).toUpperCase() + str.slice(1); - return str.replace(/[\W_]+(\w|$)/g, (_, ch) => ch.toUpperCase()); + str = str.replace(/[\W_]+(\w|$)/g, (_, ch) => ch.toUpperCase()); + return ext ? ext.toUpperCase() + str : str; } module.exports = { diff --git a/test/load/utils.test.js b/test/load/utils.test.js new file mode 100644 index 0000000000..c292135d01 --- /dev/null +++ b/test/load/utils.test.js @@ -0,0 +1,38 @@ +import path from 'path'; +import {fileToPath, fileToComponentName} from '@lib/load/utils.js'; + +describe('load utils', () => { + test('fileToComponentName', () => { + const asserts = { + 'index.md': 'MDIndex', + 'hello/index.md': 'MDHelloIndex', + 'foo.md': 'MDFoo', + 'foo-bar.md': 'MDFooBar', + 'index.js': 'JSIndex', + 'foobar.js': 'JSFoobar', + 'docusaurus/index.js': 'JSDocusaurusIndex', + '234.md': 'MD234', + '2018-07-08-test.md': 'MD20180708Test', + '%asd.md': 'MDAsd' + }; + Object.keys(asserts).forEach(file => { + expect(fileToComponentName(file)).toBe(asserts[file]); + }); + }); + + test('fileToPath', () => { + const asserts = { + 'index.md': '/', + 'hello/index.md': '/hello/', + 'foo.md': '/foo', + 'foo/bar.md': '/foo/bar', + 'index.js': '/', + 'hello/index.js': '/hello/', + 'foo.js': '/foo', + 'foo/bar.js': '/foo/bar' + }; + Object.keys(asserts).forEach(file => { + expect(fileToPath(file)).toBe(asserts[file]); + }); + }); +});