Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update parse() to handle args that are tuples #6

Merged
merged 2 commits into from
Apr 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,24 @@ class MethodRegistry {
}

parse (signature) {
let name = signature.match(/^.+(?=\()/)

if (name) {
name = name[0].charAt(0).toUpperCase() + name[0].slice(1).split(/(?=[A-Z])/).join(' ')
const rawName = signature.match(new RegExp("^([^)(]*)\\((.*)\\)([^)(]*)$"))
let parsedName

if (rawName) {
parsedName = rawName[1].charAt(0).toUpperCase() + rawName[1].slice(1).split(/(?=[A-Z])/).join(' ')
} else {
name = ''
parsedName = ''
}

const match = signature.match(/\(.+\)/)
const match = signature.match(new RegExp(rawName[1] + '\\(+([a-z1-9,()]+)\\)'))

let args = [];
if (match) {
args = match[0].slice(1, -1).split(',').map((arg) => { return {type: arg}})
args = match[1].match(/[A-z1-9]+/g).map((arg) => { return {type: arg}})
}

return {
name,
name: parsedName,
args
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eth-method-registry",
"version": "1.1.0",
"version": "1.2.0",
"description": "A module for getting method signature info from an ethereum method signature.",
"main": "index.js",
"scripts": {
Expand Down
54 changes: 54 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,57 @@ test('parsing adds spaces to multi words', function (t) {
t.end()
})

test('parse signature that includes a tuple as the first param', function (t) {
const sig = 'method((address,uint256,bytes),uint256,bytes)'
const parsed = registry.parse(sig)

t.equal(parsed.name, 'Method')
t.equal(parsed.args.length, 5)
t.equal(parsed.args[0].type, 'address')
t.equal(parsed.args[1].type, 'uint256')
t.equal(parsed.args[2].type, 'bytes')
t.equal(parsed.args[3].type, 'uint256')
t.equal(parsed.args[4].type, 'bytes')
t.end()
})

test('parse signature that includes a tuple of tuples', function (t) {
const sig = 'method(((address,uint256),(address,uint256)))'
const parsed = registry.parse(sig)

t.equal(parsed.name, 'Method')
t.equal(parsed.args.length, 4)
t.equal(parsed.args[0].type, 'address')
t.equal(parsed.args[1].type, 'uint256')
t.equal(parsed.args[2].type, 'address')
t.equal(parsed.args[3].type, 'uint256')
t.end()
})

test('parse signature that includes a tuple as a middle param', function (t) {
const sig = 'method(uint256,(address,uint256,bytes),bytes)'
const parsed = registry.parse(sig)

t.equal(parsed.name, 'Method')
t.equal(parsed.args.length, 5)
t.equal(parsed.args[0].type, 'uint256')
t.equal(parsed.args[1].type, 'address')
t.equal(parsed.args[2].type, 'uint256')
t.equal(parsed.args[3].type, 'bytes')
t.equal(parsed.args[4].type, 'bytes')
t.end()
})

test('parse signature that includes a tuple as the last param', function (t) {
const sig = 'method(uint256,bytes,(address,uint256,bytes))'
const parsed = registry.parse(sig)

t.equal(parsed.name, 'Method')
t.equal(parsed.args.length, 5)
t.equal(parsed.args[0].type, 'uint256')
t.equal(parsed.args[1].type, 'bytes')
t.equal(parsed.args[2].type, 'address')
t.equal(parsed.args[3].type, 'uint256')
t.equal(parsed.args[4].type, 'bytes')
t.end()
})