style(mobs): rename letter casing functions

This commit is contained in:
Alex Kaminskii 2022-08-03 12:07:16 +02:00
parent da1c645d21
commit b0d11b3216
11 changed files with 33 additions and 34 deletions

View file

@ -3,24 +3,23 @@ require 'erb'
# TODO: change method names to correct (CapitalCase and camelCase, not CamalCase and firstLower)
class String
def camel_case
return self if self !~ /_/ && self =~ /[A-Z]+.*/
split('_').map{|e| e.capitalize}.join.upperize
end
def camel_case_lower
self.split('_').inject([]){ |buffer,e| buffer.push(buffer.empty? ? e : e.capitalize) }.join.upperize
end
def upperize
def upperize_abbreviations
self.sub('Id', 'ID').sub('Url', 'URL')
end
def first_lower
# pascal_case
def pascal_case
return self if self !~ /_/ && self =~ /[A-Z]+.*/
split('_').map{|e| e.capitalize}.join.upperize_abbreviations
end
# camelCase
def camel_case
self.sub(/^[A-Z]+/) {|f| f.downcase }
end
def underscore
# snake_case
def snake_case
self.gsub(/::/, '/').
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
gsub(/([a-z\d])([A-Z])/,'\1_\2').

View file

@ -12,7 +12,7 @@ func (msg *<%= msg.name %>) Encode() []byte {
buf[0] = <%= msg.id %>
p := 1
<%= msg.attributes.map { |attr|
" p = Write#{attr.type.to_s.camel_case}(msg.#{attr.name}, buf, p)" }.join "\n" %>
" p = Write#{attr.type.to_s.pascal_case}(msg.#{attr.name}, buf, p)" }.join "\n" %>
return buf[:p]
}

View file

@ -12,7 +12,7 @@ func ReadMessage(reader io.Reader) (Message, error) {
case <%= msg.id %>:
msg := &<%= msg.name %>{}
<%= msg.attributes.map { |attr|
" if msg.#{attr.name}, err = Read#{attr.type.to_s.camel_case}(reader); err != nil {
" if msg.#{attr.name}, err = Read#{attr.type.to_s.pascal_case}(reader); err != nil {
return nil, err
}" }.join "\n" %>
return msg, nil

View file

@ -9,8 +9,8 @@ class Message(ABC):
class <%= msg.name %>(Message):
__id__ = <%= msg.id %>
def __init__(self, <%= msg.attributes.map { |attr| "#{attr.name.underscore}" }.join ", " %>):
<%= msg.attributes.map { |attr| "self.#{attr.name.underscore} = #{attr.name.underscore}" }.join "\n "
def __init__(self, <%= msg.attributes.map { |attr| "#{attr.name.snake_case}" }.join ", " %>):
<%= msg.attributes.map { |attr| "self.#{attr.name.snake_case} = #{attr.name.snake_case}" }.join "\n "
%>
<% end %>

View file

@ -22,7 +22,7 @@ class MessageCodec(Codec):
if message_id == <%= msg.id %>:
return <%= msg.name %>(
<%= msg.attributes.map { |attr|
"#{attr.name.underscore}=self.read_#{attr.type.to_s}(reader)" }
"#{attr.name.snake_case}=self.read_#{attr.type.to_s}(reader)" }
.join ",\n "
%>
)

View file

@ -19,11 +19,11 @@ export default class RawMessageReader extends PrimitiveReader {
<% $messages.select { |msg| msg.tracker || msg.replayer }.each do |msg| %>
case <%= msg.id %>: {
<%= msg.attributes.map { |attr|
" const #{attr.name.first_lower} = this.read#{attr.type.to_s.camel_case}(); if (#{attr.name.first_lower} === null) { return resetPointer() }" }.join "\n" %>
" const #{attr.name.camel_case} = this.read#{attr.type.to_s.pascal_case}(); if (#{attr.name.camel_case} === null) { return resetPointer() }" }.join "\n" %>
return {
tp: "<%= msg.name.underscore %>",
tp: "<%= msg.name.snake_case %>",
<%= msg.attributes.map { |attr|
" #{attr.name.first_lower}," }.join "\n" %>
" #{attr.name.camel_case}," }.join "\n" %>
};
}
<% end %>

View file

@ -3,11 +3,11 @@
import type { Timed } from './timed'
import type { RawMessage } from './raw'
import type {
<%= $messages.select { |msg| msg.tracker || msg.replayer }.map { |msg| " Raw#{msg.name.underscore.camel_case}," }.join "\n" %>
<%= $messages.select { |msg| msg.tracker || msg.replayer }.map { |msg| " Raw#{msg.name.snake_case.pascal_case}," }.join "\n" %>
} from './raw'
export type Message = RawMessage & Timed
<% $messages.select { |msg| msg.tracker || msg.replayer }.each do |msg| %>
export type <%= msg.name.underscore.camel_case %> = Raw<%= msg.name.underscore.camel_case %> & Timed
export type <%= msg.name.snake_case.pascal_case %> = Raw<%= msg.name.snake_case.pascal_case %> & Timed
<% end %>

View file

@ -1,14 +1,14 @@
// Auto-generated, do not edit
export const TP_MAP = {
<%= $messages.select { |msg| msg.tracker || msg.replayer }.map { |msg| " #{msg.id}: \"#{msg.name.underscore}\"," }.join "\n" %>
<%= $messages.select { |msg| msg.tracker || msg.replayer }.map { |msg| " #{msg.id}: \"#{msg.name.snake_case}\"," }.join "\n" %>
}
<% $messages.select { |msg| msg.tracker || msg.replayer }.each do |msg| %>
export interface Raw<%= msg.name.underscore.camel_case %> {
tp: "<%= msg.name.underscore %>",
<%= msg.attributes.map { |attr| " #{attr.name.first_lower}: #{attr.type_js}," }.join "\n" %>
export interface Raw<%= msg.name.snake_case.pascal_case %> {
tp: "<%= msg.name.snake_case %>",
<%= msg.attributes.map { |attr| " #{attr.name.camel_case}: #{attr.type_js}," }.join "\n" %>
}
<% end %>
export type RawMessage = <%= $messages.select { |msg| msg.tracker || msg.replayer }.map { |msg| "Raw#{msg.name.underscore.camel_case}" }.join " | " %>;
export type RawMessage = <%= $messages.select { |msg| msg.tracker || msg.replayer }.map { |msg| "Raw#{msg.name.snake_case.pascal_case}" }.join " | " %>;

View file

@ -2,15 +2,15 @@
import UIKit
enum ASMessageType: UInt64 {
<%= $messages.map { |msg| " case #{msg.name.first_lower} = #{msg.id}" }.join "\n" %>
<%= $messages.map { |msg| " case #{msg.name.camel_case} = #{msg.id}" }.join "\n" %>
}
<% $messages.each do |msg| %>
class AS<%= msg.name.to_s.camel_case %>: ASMessage {
class AS<%= msg.name.to_s.pascal_case %>: ASMessage {
<%= msg.attributes[2..-1].map { |attr| " let #{attr.property}: #{attr.type_swift}" }.join "\n" %>
init(<%= msg.attributes[2..-1].map { |attr| "#{attr.property}: #{attr.type_swift}" }.join ", " %>) {
<%= msg.attributes[2..-1].map { |attr| " self.#{attr.property} = #{attr.property}" }.join "\n" %>
super.init(messageType: .<%= "#{msg.name.first_lower}" %>)
super.init(messageType: .<%= "#{msg.name.camel_case}" %>)
}
override init?(genericMessage: GenericMessage) {
@ -30,7 +30,7 @@ class AS<%= msg.name.to_s.camel_case %>: ASMessage {
}
override var description: String {
return "-->> <%= msg.name.to_s.camel_case %>(<%= "#{msg.id}"%>): timestamp:\(timestamp) <%= msg.attributes[2..-1].map { |attr| "#{attr.property}:\\(#{attr.property})" }.join " "%>";
return "-->> <%= msg.name.to_s.pascal_case %>(<%= "#{msg.id}"%>): timestamp:\(timestamp) <%= msg.attributes[2..-1].map { |attr| "#{attr.property}:\\(#{attr.property})" }.join " "%>";
}
}
<% end %>

View file

@ -7,7 +7,7 @@ export enum Type {
<% $messages.select { |msg| msg.tracker }.each do |msg| %>
export type <%= msg.name %> = [
type: Type.<%= msg.name %>,
<%= msg.attributes.map { |attr| "#{attr.name.first_lower}: #{attr.type_js}," }.join "\n " %>
<%= msg.attributes.map { |attr| "#{attr.name.camel_case}: #{attr.type_js}," }.join "\n " %>
]
<% end %>

View file

@ -5,11 +5,11 @@ export { default } from '../../common/messages.js'
<% $messages.select { |msg| msg.tracker }.each do |msg| %>
export function <%= msg.name %>(
<%= msg.attributes.map { |attr| "#{attr.name.first_lower}: #{attr.type_js}," }.join "\n " %>
<%= msg.attributes.map { |attr| "#{attr.name.camel_case}: #{attr.type_js}," }.join "\n " %>
): Messages.<%= msg.name %> {
return [
Messages.Type.<%= msg.name %>,
<%= msg.attributes.map { |attr| "#{attr.name.first_lower}," }.join "\n " %>
<%= msg.attributes.map { |attr| "#{attr.name.camel_case}," }.join "\n " %>
]
}
<% end %>