Skip to content

Commit fc4c0ba

Browse files
authored
Minor cleanup (#73)
1 parent ba70f05 commit fc4c0ba

14 files changed

+46
-59
lines changed

example/custom_functions.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ void main() {
1111
jsonPath.readValues(json).forEach(print);
1212
}
1313

14-
/// An implementation of the classic FizzBuzz function.
14+
/// An implementation of a palindrome test.
1515
class IsPalindrome implements Fun1<bool, Maybe> {
1616
@override
1717
final name = 'is_palindrome';

lib/src/fun/extra/reverse.dart

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'package:json_path/fun_sdk.dart';
33
/// Reverses the string.
44
class Reverse implements Fun1<Maybe, Maybe> {
55
const Reverse();
6+
67
@override
78
final name = 'reverse';
89

lib/src/fun/extra/siblings.dart

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'package:json_path/fun_sdk.dart';
33
/// Returns all siblings of the given nodes.
44
class Siblings implements Fun1<Nodes, Nodes> {
55
const Siblings();
6+
67
@override
78
final name = 'siblings';
89

lib/src/fun/fun_factory.dart

+4-12
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,8 @@ class FunFactory {
1212
throw ArgumentError('Function validation errors: ${errors.join(', ')}');
1313
}
1414
for (final f in functions) {
15-
if (f is Fun1) {
16-
_fun1[f.name] = f;
17-
}
18-
if (f is Fun2) {
19-
_fun2[f.name] = f;
20-
}
15+
if (f is Fun1) _fun1[f.name] = f;
16+
if (f is Fun2) _fun2[f.name] = f;
2117
}
2218
}
2319

@@ -37,12 +33,8 @@ class FunFactory {
3733
final name = call.name;
3834
final args = call.args;
3935
try {
40-
if (args.length == 1) {
41-
return any1<T>(name, args[0]);
42-
}
43-
if (args.length == 2) {
44-
return any2<T>(name, args[0], args[1]);
45-
}
36+
if (args.length == 1) return any1<T>(name, args[0]);
37+
if (args.length == 2) return any2<T>(name, args[0], args[1]);
4638
} on TypeError catch (e) {
4739
/// Thrown by
4840
throw FormatException('Invalid argument: $e');

lib/src/grammar/compare.dart

+5-5
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ bool _le(Maybe a, Maybe b) => _lt(a, b) || _eq(a, b);
2929
bool _lt(Maybe a, Maybe b) => a
3030
.merge(
3131
b,
32-
(a, b) =>
33-
(a is num && b is num && a < b) ||
34-
(a is String && b is String && a.compareTo(b) < 0))
32+
(x, y) =>
33+
(x is num && y is num && x < y) ||
34+
(x is String && y is String && x.compareTo(y) < 0))
3535
.or(false);
3636

3737
/// True if [a] is not equal to [b].
@@ -46,5 +46,5 @@ const _operations = <String, bool Function(Maybe, Maybe)>{
4646
'>': _gt,
4747
};
4848

49-
bool compare(String operation, Maybe a, Maybe b) => (_operations[operation] ??
50-
(throw StateError('Invalid operation "$operation"')))(a, b);
49+
bool compare(String op, Maybe a, Maybe b) =>
50+
(_operations[op] ?? (throw StateError('Invalid operation "$op"')))(a, b);

lib/src/grammar/comparison_expression.dart

+5-4
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import 'package:petitparser/parser.dart';
66

77
Parser<Expression<bool>> comparisonExpression(Parser<Expression<Maybe>> val) =>
88
(val & cmpOperator & val).map((v) {
9-
final Expression<Maybe> left = v[0];
10-
final String op = v[1];
11-
final Expression<Maybe> right = v[2];
12-
9+
final [
10+
Expression<Maybe> left,
11+
String op,
12+
Expression<Maybe> right,
13+
] = v;
1314
return left.merge(right, (l, r) => compare(op, l, r));
1415
});

lib/src/grammar/fun_name.dart

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ final _funNameFirst = lowercase();
44
final _funNameChar = [char('_'), digit(), lowercase()].toChoiceParser();
55

66
/// Function name.
7-
final funName = (_funNameFirst & _funNameChar.star()).flatten();
7+
final funName =
8+
(_funNameFirst & _funNameChar.star()).flatten('a function name expected');

lib/src/grammar/json_path.dart

+3-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ class JsonPathGrammarDefinition extends GrammarDefinition<Expression<Nodes>> {
102102
ref0(_parenExpr),
103103
comparisonExpression(_comparable()),
104104
_testExpr(),
105-
].toChoiceParser();
105+
].toChoiceParser(
106+
failureJoiner: (a, b) =>
107+
Failure(a.buffer, a.position, 'Expression expected'));
106108

107109
Parser<Expression<Nodes>> _filterPath() => [
108110
ref0(_relPath),

lib/src/grammar/number.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import 'package:petitparser/petitparser.dart';
33
final _digit1 = range('1', '9');
44

55
final integer = (char('0') | (char('-').optional() & _digit1 & digit().star()))
6-
.flatten()
6+
.flatten('an integer number expected')
77
.map(int.parse);
88

99
final float =
1010
(char('-').optional() & digit().star() & char('.') & digit().plus())
11-
.flatten()
11+
.flatten('a floating point number expected')
1212
.map(double.parse);
1313

1414
final Parser<num> number = [float, integer].toChoiceParser();

lib/src/grammar/slice_indices.dart

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
import 'dart:math';
22

33
Iterable<int> sliceIndices(int length, int? start, int? stop, int step) sync* {
4-
if (step > 0) {
5-
yield* _forward(length, start ?? 0, stop ?? length, step);
6-
}
7-
if (step < 0) {
8-
yield* _backward(length, start, stop, step);
9-
}
4+
if (step > 0) yield* _forward(length, start ?? 0, stop ?? length, step);
5+
if (step < 0) yield* _backward(length, start, stop, step);
106
}
117

128
Iterable<int> _forward(int length, int start, int stop, int step) sync* {

lib/src/grammar/strings.dart

+4-3
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,12 @@ final _singleQuotedString =
7777
final _nameFirst =
7878
(char('_') | letter() | range(String.fromCharCode(0x80), _unicodeBoundary))
7979
.plus()
80-
.flatten();
80+
.flatten('a correct member name expected');
8181

8282
final _nameChar = digit() | _nameFirst;
8383

8484
final quotedString = (_singleQuotedString | _doubleQuotedString).cast<String>();
8585

86-
final memberNameShorthand =
87-
(_nameFirst & _nameChar.star()).flatten().map(childSelector);
86+
final memberNameShorthand = (_nameFirst & _nameChar.star())
87+
.flatten('a member name shorthand expected')
88+
.map(childSelector);

lib/src/node.dart

+4-12
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,8 @@ class Node<T extends Object?> {
4242
/// All direct children of the node.
4343
Iterable<Node> get children sync* {
4444
final v = value;
45-
if (v is Map) {
46-
yield* v.keys.map((key) => _child(v, key));
47-
}
48-
if (v is List) {
49-
yield* v.asMap().keys.map((index) => _element(v, index));
50-
}
45+
if (v is Map) yield* v.keys.map((key) => _child(v, key));
46+
if (v is List) yield* v.asMap().keys.map((index) => _element(v, index));
5147
}
5248

5349
/// Returns the JSON array element at the [offset] if it exists,
@@ -56,9 +52,7 @@ class Node<T extends Object?> {
5652
final v = value;
5753
if (v is List) {
5854
final index = offset < 0 ? v.length + offset : offset;
59-
if (index >= 0 && index < v.length) {
60-
return _element(v, index);
61-
}
55+
if (index >= 0 && index < v.length) return _element(v, index);
6256
}
6357
return null;
6458
}
@@ -67,9 +61,7 @@ class Node<T extends Object?> {
6761
/// otherwise returns null.
6862
Node? child(String key) {
6963
final v = value;
70-
if (v is Map && v.containsKey(key)) {
71-
return _child(v, key);
72-
}
64+
if (v is Map && v.containsKey(key)) return _child(v, key);
7365
return null;
7466
}
7567

lib/src/node_match.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,5 @@ extension _NodeExt<T> on Node<T> {
3737
String path() => r'$' + trace().map(_segment).join();
3838
}
3939

40-
Object _segment(Object? e) =>
41-
e is int ? IndexSelector(e) : NameSelector(e.toString());
40+
Object _segment(Object? v) =>
41+
v is int ? IndexSelector(v) : NameSelector(v.toString());

test/cases/standard/expressions_equality.json

+10-10
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@
145145
"invalid_selector": true
146146
}, {
147147
"name": "relative non-singular query, index, less-or-equal",
148-
"selector": "$[?(@[0, 0]==42)]",
148+
"selector": "$[?(@[0, 0]<=42)]",
149149
"invalid_selector": true
150150
}, {
151151
"name": "relative non-singular query, name, equal",
@@ -157,7 +157,7 @@
157157
"invalid_selector": true
158158
}, {
159159
"name": "relative non-singular query, name, less-or-equal",
160-
"selector": "$[?(@['a', 'a']==42)]",
160+
"selector": "$[?(@['a', 'a']<=42)]",
161161
"invalid_selector": true
162162
}, {
163163
"name": "relative non-singular query, combined, equal",
@@ -169,7 +169,7 @@
169169
"invalid_selector": true
170170
}, {
171171
"name": "relative non-singular query, combined, less-or-equal",
172-
"selector": "$[?(@[0, '0']==42)]",
172+
"selector": "$[?(@[0, '0']<=42)]",
173173
"invalid_selector": true
174174
}, {
175175
"name": "relative non-singular query, wildcard, equal",
@@ -181,7 +181,7 @@
181181
"invalid_selector": true
182182
}, {
183183
"name": "relative non-singular query, wildcard, less-or-equal",
184-
"selector": "$[?(@.*==42)]",
184+
"selector": "$[?(@.*<=42)]",
185185
"invalid_selector": true
186186
}, {
187187
"name": "relative non-singular query, slice, equal",
@@ -193,7 +193,7 @@
193193
"invalid_selector": true
194194
}, {
195195
"name": "relative non-singular query, slice, less-or-equal",
196-
"selector": "$[?(@[0:0]==42)]",
196+
"selector": "$[?(@[0:0]<=42)]",
197197
"invalid_selector": true
198198
}, {
199199
"name": "absolute non-singular query, index, equal",
@@ -205,7 +205,7 @@
205205
"invalid_selector": true
206206
}, {
207207
"name": "absolute non-singular query, index, less-or-equal",
208-
"selector": "$[?($[0, 0]==42)]",
208+
"selector": "$[?($[0, 0]<=42)]",
209209
"invalid_selector": true
210210
}, {
211211
"name": "absolute non-singular query, name, equal",
@@ -217,7 +217,7 @@
217217
"invalid_selector": true
218218
}, {
219219
"name": "absolute non-singular query, name, less-or-equal",
220-
"selector": "$[?($['a', 'a']==42)]",
220+
"selector": "$[?($['a', 'a']<=42)]",
221221
"invalid_selector": true
222222
}, {
223223
"name": "absolute non-singular query, combined, equal",
@@ -229,7 +229,7 @@
229229
"invalid_selector": true
230230
}, {
231231
"name": "absolute non-singular query, combined, less-or-equal",
232-
"selector": "$[?($[0, '0']==42)]",
232+
"selector": "$[?($[0, '0']<=42)]",
233233
"invalid_selector": true
234234
}, {
235235
"name": "absolute non-singular query, wildcard, equal",
@@ -241,7 +241,7 @@
241241
"invalid_selector": true
242242
}, {
243243
"name": "absolute non-singular query, wildcard, less-or-equal",
244-
"selector": "$[?($.*==42)]",
244+
"selector": "$[?($.*<=42)]",
245245
"invalid_selector": true
246246
}, {
247247
"name": "absolute non-singular query, slice, equal",
@@ -253,7 +253,7 @@
253253
"invalid_selector": true
254254
}, {
255255
"name": "absolute non-singular query, slice, less-or-equal",
256-
"selector": "$[?($[0:0]==42)]",
256+
"selector": "$[?($[0:0]<=42)]",
257257
"invalid_selector": true
258258
}
259259
]

0 commit comments

Comments
 (0)