From f94ddb4699056efbac2d06886fee52449cc6ef01 Mon Sep 17 00:00:00 2001 From: Kevin Brown Date: Tue, 7 May 2019 15:20:23 +1000 Subject: [PATCH] Fixed bug where generated table functions don't match up with actual table functions at runtime if the table has a `.` in its name. --- src/contracts/contract.ts | 3 ++- src/contracts/typeGenerator.ts | 22 +--------------------- src/contracts/utils.ts | 22 ++++++++++++++++++++++ 3 files changed, 25 insertions(+), 22 deletions(-) create mode 100644 src/contracts/utils.ts diff --git a/src/contracts/contract.ts b/src/contracts/contract.ts index 07a226b..745cb64 100644 --- a/src/contracts/contract.ts +++ b/src/contracts/contract.ts @@ -4,6 +4,7 @@ import { Api } from 'eosjs'; import { Contract as EOSJSContract, Type } from 'eosjs/dist/eosjs-serialize'; import { EOSManager } from '../eosManager'; import { Abi } from 'eosjs/dist/eosjs-rpc-interfaces'; +import { camelCase } from './utils'; export interface ContractActionParameters { [key: string]: any; @@ -115,7 +116,7 @@ export class Contract implements EOSJSContract { } // And now the tables. for (const table of abi.tables) { - (this as any)[table.name] = function() { + (this as any)[camelCase(table.name)] = function() { return this.getTableRows(table.name, arguments[0]); }; } diff --git a/src/contracts/typeGenerator.ts b/src/contracts/typeGenerator.ts index b965c46..49f346f 100644 --- a/src/contracts/typeGenerator.ts +++ b/src/contracts/typeGenerator.ts @@ -4,30 +4,10 @@ import * as path from 'path'; import { promisify } from 'util'; import mapTypes from './typeMap'; import { ConfigManager } from '../configManager'; +import { pascalCase, camelCase } from './utils'; const glob = promisify(globWithCallbacks); -/** - * Transforms a string into the pascal-case format - * @author Kevin Brown - * @param value String for case transformation - */ -const pascalCase = (value: string) => { - const snakePattern = /[_.]+./g; - const upperFirst = value[0].toUpperCase() + value.slice(1); - return upperFirst.replace(snakePattern, match => match[match.length - 1].toUpperCase()); -}; - -/** - * Transforms a string into the camel-case format - * @author Kevin Brown - * @param value String for case transformation - */ -const camelCase = (value: string) => { - const snakePattern = /[_.]+./g; - return value.replace(snakePattern, match => match[match.length - 1].toUpperCase()); -}; - type IndentedGeneratorLevel = { [key: string]: Array | IndentedGeneratorLevel }; type GeneratorLevel = Array; diff --git a/src/contracts/utils.ts b/src/contracts/utils.ts new file mode 100644 index 0000000..1b3ecac --- /dev/null +++ b/src/contracts/utils.ts @@ -0,0 +1,22 @@ +/** + * Transforms a string into the pascal-case format. + * ThisIsPascalCase, while thisIsCamelCase, this-is-kebab-case, and this_is_snake_case. + * @author Kevin Brown + * @param value String for case transformation + */ +export const pascalCase = (value: string) => { + const snakePattern = /[_.]+./g; + const upperFirst = value[0].toUpperCase() + value.slice(1); + return upperFirst.replace(snakePattern, match => match[match.length - 1].toUpperCase()); +}; + +/** + * Transforms a string into the camel-case format. + * ThisIsPascalCase, while thisIsCamelCase, this-is-kebab-case, and this_is_snake_case. + * @author Kevin Brown + * @param value String for case transformation + */ +export const camelCase = (value: string) => { + const snakePattern = /[_.]+./g; + return value.replace(snakePattern, match => match[match.length - 1].toUpperCase()); +};