Skip to content

Commit 2f2c52a

Browse files
committed
feat: join function
1 parent 354c156 commit 2f2c52a

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

packages/jmespath/src/functions/Functions.ts

+14
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,20 @@ class Functions {
9999
return Math.floor(args);
100100
}
101101

102+
/**
103+
* Join the provided array into a single string.
104+
*
105+
* @param separator The separator to use
106+
* @param items The array of itmes to join
107+
* @returns The joined array
108+
*/
109+
@Functions.signature({
110+
argumentsSpecs: [['string'], ['array-string']],
111+
})
112+
public funcJoin(separator: string, items: Array<string>): string {
113+
return items.join(separator);
114+
}
115+
102116
/**
103117
* Get the keys of the provided object.
104118
*

packages/jmespath/tests/unit/functions.test.ts

+23-11
Original file line numberDiff line numberDiff line change
@@ -1057,7 +1057,7 @@ describe('Functions tests', () => {
10571057
}
10581058
);
10591059

1060-
/* it.each([
1060+
it.each([
10611061
{
10621062
expression: `join(', ', strings)`,
10631063
expected: 'a, b, c',
@@ -1107,39 +1107,51 @@ describe('Functions tests', () => {
11071107

11081108
// Assess
11091109
expect(result).toStrictEqual(expected);
1110-
}); */
1110+
});
11111111

1112-
/* it.each([
1112+
it.each([
11131113
{
11141114
expression: 'join(\',\', `["a", 0]`)',
11151115
error:
1116-
'TypeError: join() expected argument 2 to be type (Array<string>) but received type array instead.',
1116+
'Invalid argument type for function join(), expected "string" but found "number" in expression: join(\',\', `["a", 0]`)',
11171117
},
11181118
{
11191119
expression: `join(', ', str)`,
1120-
error:
1121-
'TypeError: join() expected argument 2 to be type (Array<string>) but received type string instead.',
1120+
error: `Invalid argument type for function join(), expected "array-string" but found "string" in expression: join(', ', str)`,
11221121
},
11231122
{
11241123
expression: 'join(`2`, strings)',
11251124
error:
1126-
'TypeError: join() expected argument 1 to be type (string) but received type number instead.',
1125+
'Invalid argument type for function join(), expected "string" but found "number" in expression: join(`2`, strings)',
11271126
},
11281127
{
11291128
expression: `join('|', decimals)`,
11301129
error:
1131-
'TypeError: join() expected argument 2 to be type (Array<string>) but received type array instead.',
1130+
'Invalid argument type for function join(), expected "string" but found "number" in expression: join(\'|\', decimals)',
11321131
},
11331132
])('join() function errors', ({ expression, error }) => {
1134-
// TODO: see if we can assert the error type as well in join() errors tests
11351133
// Prepare
11361134
const data = {
1137-
type: 'object',
1135+
foo: -1,
1136+
zero: 0,
1137+
numbers: [-1, 3, 4, 5],
1138+
array: [-1, 3, 4, 5, 'a', '100'],
1139+
strings: ['a', 'b', 'c'],
1140+
decimals: [1.01, 1.2, -1.5],
1141+
str: 'Str',
1142+
false: false,
1143+
empty_list: [],
1144+
empty_hash: {},
1145+
objects: {
1146+
foo: 'bar',
1147+
bar: 'baz',
1148+
},
1149+
null_key: null,
11381150
};
11391151

11401152
// Act & Assess
11411153
expect(() => search(expression, data)).toThrow(error);
1142-
}); */
1154+
});
11431155

11441156
/* it.each([
11451157
{

0 commit comments

Comments
 (0)