Skip to content

Commit

Permalink
chore: Rename camelCase to uncapitalize. (#817)
Browse files Browse the repository at this point in the history
Because that is what it does. Not sure how it got this way.
  • Loading branch information
stephenh committed Apr 15, 2023
1 parent e122610 commit 9709225
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 26 deletions.
13 changes: 6 additions & 7 deletions src/case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,15 @@ export function capitalize(s: string): string {
return s.substring(0, 1).toUpperCase() + s.substring(1);
}

export function camelCase(s: string): string {
export function uncapitalize(s: string): string {
return s.substring(0, 1).toLowerCase() + s.substring(1);
}

/* This function uses the exact same semantics found inside the grpc
* nodejs library. Camel case splitting must be done by word i.e
* GetAPIValue must become getApiValue (notice the API becomes Api).
* This needs to be followed otherwise it will not succeed in the grpc nodejs module.
*/
export function camelCaseGrpc(s: string): string {
/* This function uses the exact same semantics found inside the grpc
* nodejs library. Camel case splitting must be done by word i.e
* GetAPIValue must become getApiValue (notice the API becomes Api).
* This needs to be followed otherwise it will not succeed in the grpc nodejs module.
*/

return camelCaseAnything(s);
}
8 changes: 4 additions & 4 deletions src/enums.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { code, def, Code, joinCode } from "ts-poet";
import { EnumDescriptorProto, EnumValueDescriptorProto } from "ts-proto-descriptors";
import { maybeAddComment } from "./utils";
import { camelCase, camelToSnake } from "./case";
import { uncapitalize, camelToSnake } from "./case";
import SourceInfo, { Fields } from "./sourceInfo";
import { Context } from "./context";

Expand Down Expand Up @@ -73,7 +73,7 @@ export function generateEnumFromJson(ctx: Context, fullName: string, enumDesc: E
const { options, utils } = ctx;
const chunks: Code[] = [];

const functionName = camelCase(fullName) + "FromJSON";
const functionName = uncapitalize(fullName) + "FromJSON";
chunks.push(code`export function ${def(functionName)}(object: any): ${fullName} {`);
chunks.push(code`switch (object) {`);

Expand Down Expand Up @@ -113,7 +113,7 @@ export function generateEnumToJson(ctx: Context, fullName: string, enumDesc: Enu

const chunks: Code[] = [];

const functionName = camelCase(fullName) + "ToJSON";
const functionName = uncapitalize(fullName) + "ToJSON";
chunks.push(
code`export function ${def(functionName)}(object: ${fullName}): ${
ctx.options.useNumericEnumForJson ? "number" : "string"
Expand Down Expand Up @@ -166,7 +166,7 @@ export function generateEnumToNumber(ctx: Context, fullName: string, enumDesc: E

const chunks: Code[] = [];

const functionName = camelCase(fullName) + "ToNumber";
const functionName = uncapitalize(fullName) + "ToNumber";
chunks.push(code`export function ${def(functionName)}(object: ${fullName}): number {`);
chunks.push(code`switch (object) {`);
for (const valueDesc of enumDesc.value) {
Expand Down
4 changes: 2 additions & 2 deletions src/generate-generic-service-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
MethodOptions_IdempotencyLevel,
ServiceDescriptorProto,
} from "ts-proto-descriptors";
import { camelCase } from "./case";
import { uncapitalize } from "./case";
import { Context } from "./context";
import SourceInfo, { Fields } from "./sourceInfo";
import { messageToTypeName } from "./types";
Expand Down Expand Up @@ -48,7 +48,7 @@ export function generateGenericServiceDefinition(
maybeAddComment(info, chunks, methodDesc.options?.deprecated);

chunks.push(code`
${camelCase(methodDesc.name)}: ${generateMethodDefinition(ctx, methodDesc)},
${uncapitalize(methodDesc.name)}: ${generateMethodDefinition(ctx, methodDesc)},
`);
}

Expand Down
2 changes: 1 addition & 1 deletion src/generate-nestjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import SourceInfo, { Fields } from "./sourceInfo";
import { contextTypeVar } from "./main";
import { assertInstanceOf, FormattedMethodDescriptor, maybeAddComment, singular } from "./utils";
import { camelCase } from "./case";
import { uncapitalize } from "./case";
import { Context } from "./context";
import { ServiceOption } from "./options";

Expand Down
18 changes: 9 additions & 9 deletions src/generate-nice-grpc.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Code, code, def, imp, joinCode } from "ts-poet";
import { FileDescriptorProto, ServiceDescriptorProto } from "ts-proto-descriptors";
import { camelCase } from "./case";
import { uncapitalize } from "./case";
import { Context } from "./context";
import SourceInfo, { Fields } from "./sourceInfo";
import { messageToTypeName } from "./types";
Expand Down Expand Up @@ -50,15 +50,15 @@ function generateServerStub(ctx: Context, sourceInfo: SourceInfo, serviceDesc: S
if (methodDesc.serverStreaming) {
// bidi streaming
chunks.push(code`
${camelCase(methodDesc.name)}(
${uncapitalize(methodDesc.name)}(
request: AsyncIterable<${inputType}>,
context: ${CallContext} & CallContextExt,
): ${ServerStreamingMethodResult}<${outputType}>;
`);
} else {
// client streaming
chunks.push(code`
${camelCase(methodDesc.name)}(
${uncapitalize(methodDesc.name)}(
request: AsyncIterable<${inputType}>,
context: ${CallContext} & CallContextExt,
): Promise<${outputType}>;
Expand All @@ -68,15 +68,15 @@ function generateServerStub(ctx: Context, sourceInfo: SourceInfo, serviceDesc: S
if (methodDesc.serverStreaming) {
// server streaming
chunks.push(code`
${camelCase(methodDesc.name)}(
${uncapitalize(methodDesc.name)}(
request: ${inputType},
context: ${CallContext} & CallContextExt,
): ${ServerStreamingMethodResult}<${outputType}>;
`);
} else {
// unary
chunks.push(code`
${camelCase(methodDesc.name)}(
${uncapitalize(methodDesc.name)}(
request: ${inputType},
context: ${CallContext} & CallContextExt,
): Promise<${outputType}>;
Expand Down Expand Up @@ -113,15 +113,15 @@ function generateClientStub(ctx: Context, sourceInfo: SourceInfo, serviceDesc: S
if (methodDesc.serverStreaming) {
// bidi streaming
chunks.push(code`
${camelCase(methodDesc.name)}(
${uncapitalize(methodDesc.name)}(
request: AsyncIterable<${inputType}>,
options?: ${CallOptions} & CallOptionsExt,
): AsyncIterable<${outputType}>;
`);
} else {
// client streaming
chunks.push(code`
${camelCase(methodDesc.name)}(
${uncapitalize(methodDesc.name)}(
request: AsyncIterable<${inputType}>,
options?: ${CallOptions} & CallOptionsExt,
): Promise<${outputType}>;
Expand All @@ -131,15 +131,15 @@ function generateClientStub(ctx: Context, sourceInfo: SourceInfo, serviceDesc: S
if (methodDesc.serverStreaming) {
// server streaming
chunks.push(code`
${camelCase(methodDesc.name)}(
${uncapitalize(methodDesc.name)}(
request: ${inputType},
options?: ${CallOptions} & CallOptionsExt,
): AsyncIterable<${outputType}>;
`);
} else {
// unary
chunks.push(code`
${camelCase(methodDesc.name)}(
${uncapitalize(methodDesc.name)}(
request: ${inputType},
options?: ${CallOptions} & CallOptionsExt,
): Promise<${outputType}>;
Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { DateOption, EnvOption, LongOption, OneofOption, Options } from "./optio
import { visit } from "./visit";
import { fail, FormattedMethodDescriptor, impProto, maybePrefixPackage } from "./utils";
import SourceInfo from "./sourceInfo";
import { camelCase } from "./case";
import { uncapitalize } from "./case";
import { Context } from "./context";

/** Based on https://github.com/dcodeIO/protobuf.js/blob/master/src/types.js#L37. */
Expand Down Expand Up @@ -601,7 +601,7 @@ function toModuleAndType(typeMap: TypeMap, protoType: string): [string, string,

export function getEnumMethod(ctx: Context, enumProtoType: string, methodSuffix: string): Import {
const [module, type] = toModuleAndType(ctx.typeMap, enumProtoType);
return impProto(ctx.options, module, `${camelCase(type)}${methodSuffix}`);
return impProto(ctx.options, module, `${uncapitalize(type)}${methodSuffix}`);
}

/** Return the TypeName for any field (primitive/message/etc.) as exposed in the interface. */
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export class FormattedMethodDescriptor implements MethodDescriptorProto {
let result = methodName;

if (options.lowerCaseServiceMethods || options.outputServices.includes(ServiceOption.GRPC)) {
result = camelCaseGrpc(result);
if (options.snakeToCamel) result = camelCaseGrpc(result);
}

return result;
Expand Down

0 comments on commit 9709225

Please sign in to comment.