Skip to content

Commit

Permalink
Merge pull request #9 from nkmr-jp/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
nkmr-jp authored Dec 14, 2023
2 parents ff258f9 + 4925e37 commit 5667959
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 13 deletions.
4 changes: 2 additions & 2 deletions .semver.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ branches: [ master, main ]
semver:
# minor:
# - lipstick
# patch:
# - art
patch:
- memo
# none: # gitmoji.json "semver": null is convert to none
# - pencil2
ignore: # not add in release-template.hbs
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ package user
import "time"
// ReposGet is the structure of the HTTP Response Body.
// ReposGet is the struct of the HTTP Response Body.
//
// Status: 200 OK
// Request: GET https://api.github.com/users/github/repos
Expand Down
4 changes: 2 additions & 2 deletions src/buildPath.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const {loadYaml, loadConfig, capitalize} = require("./common");
const {loadConfig, toPascalCase} = require("./common");

function buildPath(url, configFile, opts) {
const path = _buildPath(url, configFile)
const pathArr = path.replacedUrl.split("/")
const pkg = pathArr[pathArr.length - 2].replace(/\./g, '')
const last = pathArr[pathArr.length - 1] || "index"
const struct = capitalize(last)
const struct = toPascalCase(last)
pathArr.pop()
const dir = pathArr.join("/")
let method = opts?.method.toLowerCase()
Expand Down
10 changes: 7 additions & 3 deletions src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ exports.loadFile = file => {
return fs.readFileSync(file, 'utf8');
};

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

exports.isJsonString = str => {
Expand Down
25 changes: 25 additions & 0 deletions src/common.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const {toPascalCase} = require('./common');

test('converts normal string to pascal case', () => {
expect(toPascalCase('hello world')).toBe('HelloWorld');
});

test('converts snake_case string to pascal case', () => {
expect(toPascalCase('some_demo_string')).toBe('SomeDemoString');
});

test('converts kebab-case string to pascal case', () => {
expect(toPascalCase('another-example string')).toBe('AnotherExampleString');
});

test('converts all uppercase string to pascal case', () => {
expect(toPascalCase('ALLUPPERCASE')).toBe('Alluppercase');
});

test('converts all uppercase string to pascal case', () => {
expect(toPascalCase('GET')).toBe('Get');
});

test('keeps camelCase string in pascal case', () => {
expect(toPascalCase('camelCaseInput')).toBe('CamelCaseInput');
});
10 changes: 5 additions & 5 deletions src/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const fetch = require('node-fetch');
const fs = require('fs');
const jsonToGo = require('../vendor/json-to-go.js');
const buildPath = require('./buildPath');
const {isJsonString, loadConfig, loadFile, loadJson, capitalize} = require("./common");
const {isJsonString, loadConfig, loadFile, loadJson, toPascalCase} = require("./common");

let cliOpts

Expand Down Expand Up @@ -35,7 +35,7 @@ function run(urlStr, body, options) {
return res.json()
})
.then(json => {
let method = capitalize(opts?.method)
let method = toPascalCase(opts?.method)
const struct = jsonToGo(JSON.stringify(json), path.struct + method);
const content = buildContent(struct.go, path, comment,"")
write(json, path, content)
Expand Down Expand Up @@ -151,11 +151,11 @@ function buildContent(go, path, comment, paramType) {
content += `import "time"\n\n`
}
if (paramType === "body") {
content += `// ${go.split(" ")[1]} is the structure of the the HTTP Request Body Parameter.\n//`
content += `// ${go.split(" ")[1]} is the struct of the the HTTP Request Body Parameter.\n//`
} else if (paramType === "query") {
content += `// ${go.split(" ")[1]} is the structure of the HTTP Request Query Parameter.\n//`
content += `// ${go.split(" ")[1]} is the struct of the HTTP Request Query Parameter.\n//`
}else{
content += `// ${go.split(" ")[1]} is the structure of the HTTP Response Body.\n//`
content += `// ${go.split(" ")[1]} is the struct of the HTTP Response Body.\n//`
}
content += comment
content += go
Expand Down

0 comments on commit 5667959

Please sign in to comment.