Skip to content

Commit 3efbb3a

Browse files
committed
feat: notNull function
1 parent 212cbfb commit 3efbb3a

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

packages/jmespath/src/functions/Functions.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,11 @@ class Functions {
6262
}
6363

6464
/**
65-
* Determine if the provided value is contained in the provided item.
66-
* TODO: write docs for funcContains()
65+
* Determine if the given value is contained in the provided array or string.
66+
*
67+
* @param haystack The array or string to check
68+
* @param needle The value to check for
69+
* @returns True if the value is in the array or string, false otherwise
6770
*/
6871
@Functions.signature({
6972
argumentsSpecs: [['array', 'string'], ['any']],
@@ -128,6 +131,13 @@ class Functions {
128131
}
129132
}
130133

134+
/**
135+
* Map the provided function over the provided array.
136+
*
137+
* @param expression The expression to map over the array
138+
* @param args The array to map the expression over
139+
* @returns The result of mapping the expression over the array
140+
*/
131141
@Functions.signature({
132142
argumentsSpecs: [['any'], ['array']],
133143
})
@@ -203,7 +213,7 @@ class Functions {
203213
argumentsSpecs: [[]],
204214
variadic: true,
205215
})
206-
public funcNotNull(args: Array<JSONValue>): JSONValue | null {
216+
public funcNotNull(...args: Array<JSONValue>): JSONValue | null {
207217
return args.find((arg) => !Object.is(arg, null)) || null;
208218
}
209219

packages/jmespath/src/functions/typeChecking.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,21 @@ const typeCheck = (
4444
};
4545

4646
/**
47-
* TODO: write docs for Functions.#typeCheckArgument()
47+
* Type checks an argument against a list of types.
4848
*
4949
* Type checking at runtime involves checking the top level type,
5050
* and in the case of arrays, potentially checking the types of
5151
* the elements in the array.
5252
*
53+
* If the list of types includes 'any', then the type check is a
54+
* no-op.
55+
*
56+
* If the list of types includes more than one type, then the
57+
* argument is checked against each type in the list. If the
58+
* argument matches any of the types, then the type check
59+
* passes. If the argument does not match any of the types, then
60+
* a JMESPathTypeError is thrown.
61+
*
5362
* @param arg
5463
* @param argumentSpec
5564
*/

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,7 +1479,7 @@ describe('Functions tests', () => {
14791479
expect(() => search(expression, data)).toThrow(error);
14801480
});
14811481

1482-
/* it.each([
1482+
it.each([
14831483
{
14841484
expression: 'not_null(unknown_key, str)',
14851485
expected: 'Str',
@@ -1521,25 +1521,25 @@ describe('Functions tests', () => {
15211521

15221522
// Assess
15231523
expect(result).toStrictEqual(expected);
1524-
}); */
1525-
/* it.each([
1524+
});
1525+
1526+
it.each([
15261527
{
15271528
expression: 'not_null()',
15281529
error:
1529-
'ArgumentError: not_null() takes at least 1 argument but received 0',
1530+
'Expected 1 argument for function not_null(), received 0 in expression: not_null()',
15301531
},
15311532
])('not_null() function errors', ({ expression, error }) => {
1532-
// TODO: see if we can assert the error type as well in not_null() errors tests
15331533
// Prepare
15341534
const data = {
15351535
type: 'object',
15361536
};
15371537

15381538
// Act & Assess
15391539
expect(() => search(expression, data)).toThrow(error);
1540-
}); */
1540+
});
15411541

1542-
/* it.each([
1542+
it.each([
15431543
{
15441544
description: 'function projection on variadic function',
15451545
expression: 'foo[].not_null(f, e, d, c, b, a)',
@@ -1580,7 +1580,7 @@ describe('Functions tests', () => {
15801580
// Assess
15811581
expect(result).toStrictEqual(expected);
15821582
}
1583-
); */
1583+
);
15841584

15851585
/* it.each([
15861586
{

0 commit comments

Comments
 (0)