-
Notifications
You must be signed in to change notification settings - Fork 11
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
16 changed files
with
357 additions
and
343 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,197 +1,5 @@ | ||
final class LoopContext extends Iterable<Object?> { | ||
LoopContext(this.values, this.depth0, this.recurse) | ||
: length = values.length, | ||
index0 = -1; | ||
@Deprecated('Use `package:jinja/src/runtime.dart` instead') | ||
library; | ||
|
||
final List<Object?> values; | ||
|
||
@override | ||
final int length; | ||
|
||
final int depth0; | ||
|
||
final String Function(Object? data, [int depth]) recurse; | ||
|
||
int index0; | ||
|
||
@override | ||
LoopIterator get iterator { | ||
return LoopIterator(this); | ||
} | ||
|
||
int get index { | ||
return index0 + 1; | ||
} | ||
|
||
int get depth { | ||
return depth0 + 1; | ||
} | ||
|
||
int get revindex0 { | ||
return length - index; | ||
} | ||
|
||
int get revindex { | ||
return length - index0; | ||
} | ||
|
||
@override | ||
bool get first { | ||
return index0 == 0; | ||
} | ||
|
||
@override | ||
bool get last { | ||
return index == length; | ||
} | ||
|
||
Object? get next { | ||
if (last) { | ||
return null; | ||
} | ||
|
||
return values[index0 + 1]; | ||
} | ||
|
||
Object? get prev { | ||
if (first) { | ||
return null; | ||
} | ||
|
||
return values[index0 - 1]; | ||
} | ||
|
||
Object? operator [](String key) { | ||
switch (key) { | ||
case 'length': | ||
return length; | ||
case 'index0': | ||
return index0; | ||
case 'depth0': | ||
return depth0; | ||
case 'index': | ||
return index; | ||
case 'depth': | ||
return depth; | ||
case 'revindex0': | ||
return revindex0; | ||
case 'revindex': | ||
return revindex; | ||
case 'first': | ||
return first; | ||
case 'last': | ||
return last; | ||
case 'prev': | ||
case 'previtem': | ||
return prev; | ||
case 'next': | ||
case 'nextitem': | ||
return next; | ||
case 'call': | ||
return call; | ||
case 'cycle': | ||
return cycle; | ||
case 'changed': | ||
return changed; | ||
default: | ||
var invocation = Invocation.getter(Symbol(key)); | ||
throw NoSuchMethodError.withInvocation(this, invocation); | ||
} | ||
} | ||
|
||
String call(Object? data) { | ||
return recurse(data, depth); | ||
} | ||
|
||
Object? cycle(Iterable<Object?> values) { | ||
var list = values.toList(); | ||
|
||
if (list.isEmpty) { | ||
// TODO(loop): update error | ||
throw TypeError(); | ||
} | ||
|
||
return list[index0 % list.length]; | ||
} | ||
|
||
bool changed(Object? item) { | ||
if (index0 == 0) { | ||
return true; | ||
} | ||
|
||
if (item == prev) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} | ||
|
||
final class LoopIterator implements Iterator<Object?> { | ||
LoopIterator(this.context); | ||
|
||
final LoopContext context; | ||
|
||
@override | ||
Object? get current { | ||
return context.values[context.index0]; | ||
} | ||
|
||
@override | ||
bool moveNext() { | ||
if (context.index < context.length) { | ||
context.index0 += 1; | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
|
||
final class Cycler extends Iterable<Object?> { | ||
Cycler(Iterable<Object?> values) | ||
: values = List<Object?>.of(values), | ||
length = values.length, | ||
index = 0; | ||
|
||
final List<Object?> values; | ||
|
||
@override | ||
final int length; | ||
|
||
int index; | ||
|
||
Object? get current { | ||
return values[index]; | ||
} | ||
|
||
@override | ||
Iterator<Object?> get iterator { | ||
return CyclerIterator(this); | ||
} | ||
|
||
Object? next() { | ||
var result = current; | ||
index = (index + 1) % length; | ||
return result; | ||
} | ||
|
||
void reset() { | ||
index = 0; | ||
} | ||
} | ||
|
||
final class CyclerIterator implements Iterator<Object?> { | ||
CyclerIterator(this.cycler); | ||
|
||
final Cycler cycler; | ||
|
||
@override | ||
Object? current; | ||
|
||
@override | ||
bool moveNext() { | ||
current = cycler.next(); | ||
return true; | ||
} | ||
} | ||
export 'package:jinja/src/runtime.dart' | ||
show LoopContext, LoopIterator, Cycler, CyclerIterator; |
Oops, something went wrong.