Skip to content

Commit

Permalink
updated parser
Browse files Browse the repository at this point in the history
  • Loading branch information
nialexsan committed Sep 18, 2024
1 parent ca3f5c2 commit f1c31d3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"start": "webpack-dev-server -d source-map --config webpack.config.js",
"build": "webpack --mode=production",
"graphql:codegen": "npx graphql-codegen --config codegen.yml",
"test": "echo Tests command not implemented.",
"test": "jest",
"format:app": "prettier --write 'src'",
"format:check:app": "prettier --check 'src/**/*.{js,jsx,ts,tsx,css}' ",
"lint": "eslint --ext .js,.jsx,.ts,.tsx src",
Expand Down
27 changes: 27 additions & 0 deletions src/util/parser.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { extractSigners } from "./parser";

const oneSigner = `
transaction {
prepare(acct: auth(LoadValue, SaveValue) &Account) {}
}`


const twoSigners = `
transaction {
prepare(authorizer1: auth(Capabilities,SomethingElse) &Account, authorizer2: auth(Storage,Vaults) &Account) { }
}`

describe('parser tests', () => {
it('should extract one code signer', () => {

const signers = extractSigners(oneSigner)

expect(signers).toHaveLength(1)
})
it('should extract two code signer', () => {

const signers = extractSigners(twoSigners)

expect(signers).toHaveLength(2)
})
})
8 changes: 6 additions & 2 deletions src/util/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ export const stripNewLines = (input: string) =>

export const generateSchema = (argsDefinition: string) =>
argsDefinition
.replace(/\(([^()]+)\)/g, (match) => {
// Replace commas with placeholder inside the parentheses content
return match.replace(/,/g, '<COMMA>');
})
.split(',')
.map((item) => item.replace(/\s*/g, ''))
.map((item) => item.replace(/\s*/g, '').replace(/<COMMA>/g,','))
.filter((item) => item !== '');

export const stripComments = (code: string) => {
Expand All @@ -32,5 +36,5 @@ export const extract = (code: string, keyWord: string) => {
};

export const extractSigners = (code: string) => {
return extract(code, `(?:prepare\\s*\\(\\s*)([^\\)]*)(?:\\))`);
return extract(code, `(?:prepare\\s*\\(\\s*)([^{}]*)(?:\\))`);
};

0 comments on commit f1c31d3

Please sign in to comment.