Skip to content

Commit 613fb4b

Browse files
committed
feat: replaceInlineContext commands: split, index
1 parent a9f4247 commit 613fb4b

File tree

5 files changed

+73
-3
lines changed

5 files changed

+73
-3
lines changed

jest.config.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import type { Config } from 'jest';
2+
3+
const jestConfig: Config = {
4+
testEnvironment: 'node',
5+
moduleNameMapper: {
6+
'^~/(.*)$': '<rootDir>/src/$1',
7+
'^(.+)_generated.js$': '$1_generated', // Support flatc generated files
8+
},
9+
coveragePathIgnorePatterns: ['<rootDir>/build/', '<rootDir>/node_modules/'],
10+
testPathIgnorePatterns: ['<rootDir>/build', '<rootDir>/node_modules'],
11+
extensionsToTreatAsEsm: ['.ts'],
12+
/**
13+
* For high performance with minimal configuration transform with TS with swc.
14+
* @see https://github.com/farcasterxyz/hubble/issues/314
15+
*/
16+
transform: {
17+
'^.+\\.(t|j)sx?$': '@swc/jest',
18+
},
19+
};
20+
21+
export default jestConfig;

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
"devDependencies": {
1212
"@changesets/cli": "2.26.2",
1313
"@turbo/gen": "^1.9.7",
14+
"@types/jest": "^29.5.11",
1415
"eslint": "^8.46.0",
1516
"eslint-config-custom": "*",
17+
"jest": "^29.7.0",
1618
"prettier": "^2.5.1",
1719
"react": "^18.2.0",
1820
"tsup": "^7.2.0",
@@ -28,4 +30,4 @@
2830
"packages/*",
2931
"docs"
3032
]
31-
}
33+
}

packages/core/src/renderer.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { replaceInlineContext } from "./renderer"; // Adjust the import path as needed
2+
3+
describe("replaceInlineContext", () => {
4+
it("replaces simple path without operation", () => {
5+
const context = { user: { name: "Jane Doe" } };
6+
const template = "Hello {{user.name}}";
7+
expect(replaceInlineContext(template, context)).toBe("Hello Jane Doe");
8+
});
9+
10+
it("applies split and index operations to extract part of a string", () => {
11+
const context = { refs: { example: { url: "ipfs://exampleCID" } } };
12+
const template = "{{refs.example.url | split ipfs:// | index 1}}";
13+
expect(replaceInlineContext(template, context)).toBe("exampleCID");
14+
});
15+
});

packages/core/src/renderer.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,32 @@ export interface ExitActionResolver {
314314
(): void;
315315
}
316316

317-
function replaceInlineContext(target: string, context: any): string {
318-
return target.replace(/{{([^{{}}]+)}}/g, (_, key) => get(context, key, ``));
317+
function executeCommand(input: string, command: string) {
318+
const [cmd, ...args] = command.split(" ");
319+
switch (cmd) {
320+
case "split":
321+
return input.split(args[0]);
322+
case "index":
323+
return input[parseInt(args[0], 10)];
324+
default:
325+
return input;
326+
}
327+
}
328+
329+
export function replaceInlineContext(target: string, context: any): string {
330+
return target.replace(/{{([^{{}}]+)}}/g, (_, key) => {
331+
const parts = key.split("|").map((part: string) => part.trim());
332+
let value = get(context, parts[0], "");
333+
334+
// Process additional commands if they exist
335+
if (parts.length > 1) {
336+
for (let i = 1; i < parts.length; i++) {
337+
value = executeCommand(value, parts[i]);
338+
}
339+
}
340+
341+
return value;
342+
});
319343
}
320344

321345
function matchesOp(value: string, op: Op, context: any): boolean {

yarn.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4121,6 +4121,14 @@
41214121
expect "^29.0.0"
41224122
pretty-format "^29.0.0"
41234123

4124+
"@types/jest@^29.5.11":
4125+
version "29.5.11"
4126+
resolved "https://registry.yarnpkg.com/@types/jest/-/jest-29.5.11.tgz#0c13aa0da7d0929f078ab080ae5d4ced80fa2f2c"
4127+
integrity sha512-S2mHmYIVe13vrm6q4kN6fLYYAka15ALQki/vgDC3mIukEOx8WJlv0kQPM+d4w8Gp6u0uSdKND04IlTXBv0rwnQ==
4128+
dependencies:
4129+
expect "^29.0.0"
4130+
pretty-format "^29.0.0"
4131+
41244132
"@types/js-yaml@^4.0.0":
41254133
version "4.0.9"
41264134
resolved "https://registry.yarnpkg.com/@types/js-yaml/-/js-yaml-4.0.9.tgz#cd82382c4f902fed9691a2ed79ec68c5898af4c2"

0 commit comments

Comments
 (0)