Skip to content

Commit

Permalink
fix(composer): fix issue displaying 0 in overlay
Browse files Browse the repository at this point in the history
  • Loading branch information
sheilaXu committed Aug 7, 2023
1 parent bb96f3b commit b37b926
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { replaceBindingVariables } from './dataBindingVariableUtils';

describe('replaceBindingVariables', () => {
const bindingValuesMap: Record<string, string> = {
const bindingValuesMap: Record<string, unknown> = {
zero: 0,
false: false,
'binding a': '11',
'binding~!@#$%^&*()': '22',
'<随机>': '33',
Expand All @@ -15,6 +17,22 @@ describe('replaceBindingVariables', () => {
expect(result).toEqual(expected);
});

it('should replace variable with value 0 correctly', () => {
const original = 'abc ${zero} def,.';
const expected = 'abc 0 def,.';

const result = replaceBindingVariables(original, bindingValuesMap);
expect(result).toEqual(expected);
});

it('should replace variable with value false correctly', () => {
const original = 'abc ${false} def,.';
const expected = 'abc false def,.';

const result = replaceBindingVariables(original, bindingValuesMap);
expect(result).toEqual(expected);
});

it('should replace variable containing special character with value correctly', () => {
const original = 'abc ${binding~!@#$%^&*()} def,.';
const expected = 'abc 22 def,.';
Expand Down
6 changes: 3 additions & 3 deletions packages/scene-composer/src/utils/dataBindingVariableUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
const bindingVariableRegex = /\$\{.+?\}/gi;
const UNAVAILABLE_DATA = '-';

export function replaceBindingVariables(content: string, bindingValuesMap: Record<string, string>): string {
export function replaceBindingVariables(content: string, bindingValuesMap: Record<string, unknown>): string {
let result = content;

const variableMatches = content.match(bindingVariableRegex) || [];
const variables = variableMatches.map((v: string) => v.substring(2, v.length - 1));
// Only support replace variable with their latest property value for now.
variables.forEach((variable, index) => {
// Display "-" when value not available
const value = bindingValuesMap[variable] || UNAVAILABLE_DATA;
result = result.replaceAll(variableMatches[index], value);
const value = bindingValuesMap[variable] ?? UNAVAILABLE_DATA;
result = result.replaceAll(variableMatches[index], String(value));
});

return result;
Expand Down
2 changes: 1 addition & 1 deletion release-please-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
},
{
"type": "linked-versions",
"group-name": "iot-app-kit",
"groupName": "iot-app-kit",
"components": [
"root",
"components",
Expand Down

0 comments on commit b37b926

Please sign in to comment.