-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
202 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import 'dart:math'; | ||
|
||
import 'package:json_path/src/result.dart'; | ||
import 'package:json_path/src/selector/selector.dart'; | ||
|
||
class Slice extends Selector { | ||
Slice({int first, this.last, int step}) | ||
: first = first ?? 0, | ||
step = step ?? 1; | ||
|
||
final int first; | ||
|
||
final int last; | ||
|
||
final int step; | ||
|
||
@override | ||
Iterable<Result> call(Iterable<Result> results) => results.map((r) { | ||
if (step > 0 && r.value is List) { | ||
return _filterList(r.value, r.path); | ||
} | ||
return const <Result>[]; | ||
}).expand((_) => _); | ||
|
||
@override | ||
String get expression => | ||
'[${first == 0 ? '' : first}:${last ?? ''}${step != 1 ? ':$step' : ''}]'; | ||
|
||
Iterable<Result> _filterList(List list, String path) => | ||
_for(_actualFirst(list.length), _actualLast(list.length), step) | ||
.map((i) => Result(list[i], path + '[$i]')); | ||
|
||
int _actualFirst(int len) => max(0, first < 0 ? (len + first) : first); | ||
|
||
int _actualLast(int len) { | ||
if (last == null) return len; | ||
if (last < 0) return min(len, len + last); | ||
return min(len, last); | ||
} | ||
|
||
Iterable<int> _for(int from, int to, int step) sync* { | ||
for (var i = from; i < to; i += step) { | ||
yield i; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters