Skip to content

Commit

Permalink
Add find helper (mockoon#1650)
Browse files Browse the repository at this point in the history
  • Loading branch information
255kb authored Jan 17, 2025
1 parent 1c9dacd commit ff9dfca
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { fromSafeString } from '../../utils';

const find = function (...args) {
const parameters = args.slice(0, -1);

if (parameters.length === 0) {
return '';
}

const [arr, ...filters] = parameters;

if (!(arr instanceof Array)) {
return '';
}
if (!arr.length || !filters.length) {
return undefined;
}

const validate = (payload, condition) => {
if (
condition !== null &&
typeof condition === 'object' &&
!Array.isArray(condition)
) {
if (typeof payload === 'object') {
const keys = Object.keys(condition);

return keys.every((k) =>
validate(payload[k], fromSafeString(condition[k]))
);
}

return false;
}

return payload === condition;
};

const and = (item, conditions, { or }) =>
conditions.every((subConditions) => {
if (Array.isArray(subConditions)) {
return or(item, subConditions);
}

return validate(item, subConditions);
});
const or = (item, conditions) =>
conditions.some((subConditions) => {
if (Array.isArray(subConditions)) {
return and(item, subConditions, { or });
}

return validate(item, subConditions);
});

return arr.find((item) => or(item, filters));
};

export default find;
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import domain from './domain';
import email from './email';
import eq from './eq';
import filter from './filter';
import find from './find';
import firstName from './firstName';
import float from './float';
import floor from './floor';
Expand Down Expand Up @@ -117,6 +118,7 @@ export const Helpers = {
email,
eq,
filter,
find,
firstName,
float,
floor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3126,6 +3126,53 @@ describe('Template parser', () => {
});
});

describe('Helper: find', () => {
it('should return the first item matching the condition', () => {
const parseResult = TemplateParser({
shouldOmitDataHelper: false,
content:
'{{{ stringify (find (array (object b="b1" a="a1") (object b="b2" a="a2") 3) (object b="b1")) }}}',
environment: {} as any,
processedDatabuckets: [],
globalVariables: {},
request: {} as any,
envVarsPrefix: ''
});
strictEqual(parseResult, JSON.stringify({ a: 'a1', b: 'b1' }, null, 2));
});

it('should return the first item matching nested values', () => {
const parseResult = TemplateParser({
shouldOmitDataHelper: false,
content:
'{{{ stringify (find (array (object parent=(object child="child-val") b="b1") (object parent=(object child="child-val2") b="b2") 2 3) (object parent=(object child="child-val"))) }}}',
environment: {} as any,
processedDatabuckets: [],
globalVariables: {},
request: {} as any,
envVarsPrefix: ''
});
strictEqual(
parseResult,
JSON.stringify({ b: 'b1', parent: { child: 'child-val' } }, null, 2)
);
});

it('should return undefined when no item matches the condition', () => {
const parseResult = TemplateParser({
shouldOmitDataHelper: false,
content:
'{{{ stringify (find (array (object parent=(object child="child-val") b="b1") (object parent=(object child="child-val2") b="b2") 2 3) (object parent="parent-val")) }}}',
environment: {} as any,
processedDatabuckets: [],
globalVariables: {},
request: {} as any,
envVarsPrefix: ''
});
strictEqual(parseResult, '');
});
});

describe('Helper: sort', () => {
it('should return correctly sorted array of numbers in ascending order', () => {
const parseResult = TemplateParser({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export const helpersAutocompletions = [
{ caption: 'concat', value: '{{concat value value}}', meta: '' },
{ caption: 'jwtPayload', value: "{{jwtPayload token 'key'}}", meta: '' },
{ caption: 'jwtHeader', value: "{{jwtHeader token 'key'}}", meta: '' },
{ caption: 'filter', value: "{{filter array 'key'}}", meta: '' },
{
caption: 'indexOf',
value: '{{indexOf value search startIndex}}',
Expand Down Expand Up @@ -138,6 +139,7 @@ export const helpersAutocompletions = [
{ caption: 'color', value: '{{color}}', meta: '' },
{ caption: 'hexColor', value: '{{hexColor}}', meta: '' },
{ caption: 'guid', value: '{{guid}}', meta: '' },
{ caption: 'uuid', value: '{{uuid}}', meta: '' },
{ caption: 'ipv4', value: '{{ipv4}}', meta: '' },
{ caption: 'ipv6', value: '{{ipv6}}', meta: '' },
{ caption: 'lorem', value: '{{lorem length}}', meta: '' }
Expand Down

0 comments on commit ff9dfca

Please sign in to comment.