Skip to content

Commit

Permalink
Align return values for data and dataRaw helpers
Browse files Browse the repository at this point in the history
Closes #918
  • Loading branch information
Guillaume committed Jan 12, 2023
1 parent ed5f62d commit 9fe1608
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const DataHelpers = function (

// ensure a value was found at path
const foundValue = objectGet(value, path);
value = foundValue !== undefined ? foundValue : value;
value = foundValue !== undefined ? foundValue : '';
}

if (Array.isArray(value) || typeof value === 'object') {
Expand All @@ -49,7 +49,6 @@ export const DataHelpers = function (
return new SafeString(value);
}
},

dataRaw: function (...args: any[]) {
const parameters = args.slice(0, -1);

Expand Down Expand Up @@ -83,7 +82,7 @@ export const DataHelpers = function (

// ensure a value was found at path
const foundValue = objectGet(value, path);
value = foundValue !== undefined ? foundValue : value;
value = foundValue !== undefined ? foundValue : '';

return value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,28 @@ describe('Data helpers', () => {
expect(parseResult).to.be.equal('value1');
});

it('should return empty string if property at a path does not exists', () => {
const parseResult = TemplateParser(
false,
"{{data 'pathDatabucket' 'object1.prop3'}}",
{} as any,
[
{
name: 'pathDatabucket',
id: 'w63q',
value: {
object1: { prop1: 'value1', prop2: 'value2' },
prop: 'value',
object2: []
},
parsed: true
}
],
{} as any
);
expect(parseResult).to.be.equal('');
});

it('should return falsy property at a path', () => {
const parseResult = TemplateParser(
false,
Expand Down Expand Up @@ -401,6 +423,28 @@ describe('Data helpers', () => {
expect(parseResult).to.be.equal('value1');
});

it('should return empty string if property does not exist', () => {
const parseResult = TemplateParser(
false,
"{{dataRaw 'pathDatabucket' 'object1.prop3'}}",
{} as any,
[
{
name: 'pathDatabucket',
id: 'w63q',
value: {
object1: { prop1: 'value1', prop2: 'value2' },
prop: 'value',
object2: []
},
parsed: true
}
],
{} as any
);
expect(parseResult).to.be.equal('');
});

it('should return falsy property at a path', () => {
const parseResult = TemplateParser(
false,
Expand Down

0 comments on commit 9fe1608

Please sign in to comment.