Skip to content

Commit a3af273

Browse files
committed
chore: add internal comma separated parsing method
1 parent 5d1ae3f commit a3af273

File tree

3 files changed

+16
-17
lines changed

3 files changed

+16
-17
lines changed

packages/cli/src/cli.ts

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ import {
9898
import { gaiaAuth, gaiaConnect, gaiaUploadProfileAll, getGaiaAddressFromProfile } from './data';
9999

100100
import { defaultUrlFromNetwork, STACKS_TESTNET } from '@stacks/network';
101+
import { internal_parseCommaSeparated } from '@stacks/transactions';
101102
import {
102103
generateNewAccount,
103104
generateWallet,
@@ -793,23 +794,7 @@ async function contractDeploy(_network: CLINetworkAdapter, args: string[]): Prom
793794

794795
/** @internal */
795796
export function parseDirectFunctionArgs(functionArgsStr: string): ClarityValue[] {
796-
return functionArgsStr
797-
.split('')
798-
.reduce(
799-
(acc, char) => {
800-
if (char === '(' || char === '{') acc.p++;
801-
if (char === ')' || char === '}') acc.p--;
802-
if (char === ',' && !acc.p) {
803-
acc.segs.push('');
804-
} else {
805-
acc.segs[acc.segs.length - 1] += char;
806-
}
807-
return acc;
808-
},
809-
{ p: 0, segs: [''] }
810-
)
811-
.segs.filter(arg => arg.trim())
812-
.map(arg => Cl.parse(arg.trim()));
797+
return internal_parseCommaSeparated(functionArgsStr);
813798
}
814799

815800
// Get function arguments via interactive prompts

packages/transactions/src/clarity/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,6 @@ export * from './types';
2424

2525
export * from './deserialize';
2626
export * from './serialize';
27+
28+
/** @ignore Meant for internal use by other Stacks.js packages. Not stable. */
29+
export { internal_parseCommaSeparated } from './parser';

packages/transactions/src/clarity/parser.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,3 +327,14 @@ export function parse(clarityValueString: string): ClarityValue {
327327
if (!result.success || !result.capture) throw 'Parse error'; // todo: we can add better error messages and add position tracking
328328
return result.capture as ClarityValue;
329329
}
330+
331+
/** @ignore Meant for internal use by other Stacks.js packages. Not stable. */
332+
export function internal_parseCommaSeparated(clarityValueString: string): ClarityValue[] {
333+
const combinator = entire(
334+
greedy(1, clValue(), c => Cl.list(c as ClarityValue[]), regex(/\s*,\s*/))
335+
);
336+
const result = combinator(clarityValueString);
337+
if (!result.success || !result.capture)
338+
throw `Error trying to parse string: ${clarityValueString}`;
339+
return (result.capture as ListCV<ClarityValue>).value;
340+
}

0 commit comments

Comments
 (0)