Skip to content

Commit 4925e37

Browse files
committed
🐛 Replace 'capitalize' function with 'toPascalCase' for better string formatting
1 parent 7621ec5 commit 4925e37

File tree

4 files changed

+36
-7
lines changed

4 files changed

+36
-7
lines changed

src/buildPath.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
const {loadYaml, loadConfig, capitalize} = require("./common");
1+
const {loadConfig, toPascalCase} = require("./common");
22

33
function buildPath(url, configFile, opts) {
44
const path = _buildPath(url, configFile)
55
const pathArr = path.replacedUrl.split("/")
66
const pkg = pathArr[pathArr.length - 2].replace(/\./g, '')
77
const last = pathArr[pathArr.length - 1] || "index"
8-
const struct = capitalize(last)
8+
const struct = toPascalCase(last)
99
pathArr.pop()
1010
const dir = pathArr.join("/")
1111
let method = opts?.method.toLowerCase()

src/common.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,13 @@ exports.loadFile = file => {
2020
return fs.readFileSync(file, 'utf8');
2121
};
2222

23-
exports.capitalize = str => {
24-
const lower = str.toLowerCase();
25-
return str.charAt(0).toUpperCase() + lower.slice(1);
23+
exports.toPascalCase = str => {
24+
return str
25+
.replace(/[\s_\-]+/g, ' ')
26+
.replace(/([a-z])([A-Z])/g, '$1 $2')
27+
.split(' ')
28+
.map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
29+
.join('');
2630
}
2731

2832
exports.isJsonString = str => {

src/common.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const {toPascalCase} = require('./common');
2+
3+
test('converts normal string to pascal case', () => {
4+
expect(toPascalCase('hello world')).toBe('HelloWorld');
5+
});
6+
7+
test('converts snake_case string to pascal case', () => {
8+
expect(toPascalCase('some_demo_string')).toBe('SomeDemoString');
9+
});
10+
11+
test('converts kebab-case string to pascal case', () => {
12+
expect(toPascalCase('another-example string')).toBe('AnotherExampleString');
13+
});
14+
15+
test('converts all uppercase string to pascal case', () => {
16+
expect(toPascalCase('ALLUPPERCASE')).toBe('Alluppercase');
17+
});
18+
19+
test('converts all uppercase string to pascal case', () => {
20+
expect(toPascalCase('GET')).toBe('Get');
21+
});
22+
23+
test('keeps camelCase string in pascal case', () => {
24+
expect(toPascalCase('camelCaseInput')).toBe('CamelCaseInput');
25+
});

src/run.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const fetch = require('node-fetch');
22
const fs = require('fs');
33
const jsonToGo = require('../vendor/json-to-go.js');
44
const buildPath = require('./buildPath');
5-
const {isJsonString, loadConfig, loadFile, loadJson, capitalize} = require("./common");
5+
const {isJsonString, loadConfig, loadFile, loadJson, toPascalCase} = require("./common");
66

77
let cliOpts
88

@@ -35,7 +35,7 @@ function run(urlStr, body, options) {
3535
return res.json()
3636
})
3737
.then(json => {
38-
let method = capitalize(opts?.method)
38+
let method = toPascalCase(opts?.method)
3939
const struct = jsonToGo(JSON.stringify(json), path.struct + method);
4040
const content = buildContent(struct.go, path, comment,"")
4141
write(json, path, content)

0 commit comments

Comments
 (0)