Skip to content

Commit

Permalink
supporting functions
Browse files Browse the repository at this point in the history
  • Loading branch information
karnthis committed Jan 5, 2024
1 parent 273d034 commit fbba4ef
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/utils/converters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
export function snakeToCamel(key: string): string {
return key.replace(/([-_][a-z])/g, ($1) => {
return $1.toUpperCase()
.replace('-', '')
.replace('_', '')
})
}

export function camelToSnake(key: string): string {
return key.replace(/([A-Z])/g, ($1) => `_${$1.toLowerCase()}`)
}

export function forAmino(msg: any) {
const sorted = Object.keys(msg)
.sort()
.reduce((acc, key) => {
const snakeKey = camelToSnake(key)

acc[snakeKey] = msg[key]
return acc
}, {} as any)
return sorted
}

export function wasAmino(msg: any) {
const sorted = Object.keys(msg)
.reduce((acc, key) => {
const camelKey = snakeToCamel(key)

acc[camelKey] = msg[key]
return acc
}, {} as any)
return sorted
}

0 comments on commit fbba4ef

Please sign in to comment.