mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-02-13 04:00:56 -05:00
chore(utils): migrate block-nameify to TS and add unit tests (#48086)
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -166,6 +166,8 @@ config/i18n/all-langs.js
|
||||
config/certification-settings.js
|
||||
|
||||
### Generated utils files ###
|
||||
utils/block-nameify.js
|
||||
utils/block-nameify.test.js
|
||||
utils/slugs.js
|
||||
utils/slugs.test.js
|
||||
utils/index.js
|
||||
|
||||
@@ -8,9 +8,11 @@ config/**/*.json
|
||||
config/i18n/all-langs.js
|
||||
config/certification-settings.js
|
||||
docs/i18n
|
||||
utils/block-nameify.js
|
||||
utils/block-nameify.test.js
|
||||
utils/slugs.js
|
||||
utils/slugs.test.js
|
||||
utils/index.js
|
||||
**/package-lock.json
|
||||
web/.next
|
||||
curriculum-server/data/curriculum.json
|
||||
curriculum-server/data/curriculum.json
|
||||
|
||||
1
utils/block-nameify.d.ts
vendored
1
utils/block-nameify.d.ts
vendored
@@ -1 +0,0 @@
|
||||
export declare function blockNameify(phrase: string): string;
|
||||
23
utils/block-nameify.test.ts
Normal file
23
utils/block-nameify.test.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { blockNameify } from './block-nameify';
|
||||
|
||||
describe('blockNameify', () => {
|
||||
it('should return a preformatted name when it exists', () => {
|
||||
const result = blockNameify('back-end-development-and-apis');
|
||||
expect(result).toBe('Back End Development and APIs');
|
||||
});
|
||||
|
||||
it('should not format prepositions', () => {
|
||||
const result = blockNameify('and-for-of-the-up-with');
|
||||
expect(result).toBe('and for of the up with');
|
||||
});
|
||||
|
||||
it('should format javascript to JavaScript', () => {
|
||||
const result = blockNameify('javascript');
|
||||
expect(result).toBe('JavaScript');
|
||||
});
|
||||
|
||||
it('should transform "-" to " " and uppercase each word', () => {
|
||||
const result = blockNameify('hello-world');
|
||||
expect(result).toBe('Hello World');
|
||||
});
|
||||
});
|
||||
@@ -1,9 +1,10 @@
|
||||
const preFormattedBlockNames = require('./preformatted-block-names.json');
|
||||
import preFormattedBlockNames from './preformatted-block-names.json';
|
||||
|
||||
const noFormatting = ['and', 'for', 'of', 'the', 'up', 'with'];
|
||||
|
||||
exports.blockNameify = function blockNameify(phrase) {
|
||||
const preFormatted = preFormattedBlockNames[phrase] || '';
|
||||
export function blockNameify(phrase: string): string {
|
||||
const preFormatted =
|
||||
(preFormattedBlockNames as Record<string, string>)[phrase] || '';
|
||||
if (preFormatted) {
|
||||
return preFormatted;
|
||||
}
|
||||
@@ -19,4 +20,4 @@ exports.blockNameify = function blockNameify(phrase) {
|
||||
return word.charAt(0).toUpperCase() + word.slice(1);
|
||||
})
|
||||
.join(' ');
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user