Skip to content

Instantly share code, notes, and snippets.

@pacifio
Created December 17, 2019 09:19
Show Gist options
  • Save pacifio/012d19bfc150310be507c450fa93d781 to your computer and use it in GitHub Desktop.
Save pacifio/012d19bfc150310be507c450fa93d781 to your computer and use it in GitHub Desktop.
Solved mafinar sir's quiz
/*
* String formatter lib by Ahsan Islam
* https://www.facebook.com/ahsan.islam.505523
*/
class IntegerToDate {
int number;
String period;
IntegerToDate(int number, String period) {
this.number = number;
this.period = period;
}
DateTime later() {
var now = DateTime.now();
return now.add(getDuration(number));
}
DateTime ago() {
var now = DateTime.now();
return now.add(getDuration(-number));
}
getDuration(int value) {
if (period == 'days') {
return new Duration(days: value);
}
if (period == 'weeks') {
value *= 7;
return new Duration(days: value);
}
if (period == 'months') {
value *= 30;
return new Duration(days: value);
}
if (period == 'years') {
value *= 365;
return new Duration(days: value);
}
}
}
extension NumberToDate on int {
IntegerToDate days() {
return new IntegerToDate(this, 'days');
}
IntegerToDate weeks() {
return new IntegerToDate(this, 'weeks');
}
IntegerToDate months() {
return new IntegerToDate(this, 'months');
}
IntegerToDate years() {
return new IntegerToDate(this, 'years');
}
}
/*
* Array parser lib by Adib Mohsin
* https://www.facebook.com/adib.mohsin.39
*/
class MainParser {
List<int> numbers;
MainParser(List<int> total) : numbers = total;
List<int> by(int differ) {
int curr;
List<int> total = [];
for (var i = 0; i < numbers.length; i += differ) {
curr = numbers[i];
total.add(curr);
}
return total;
}
}
extension MafinarParser on int {
MainParser to([int number]) {
if (number < this) {
throw new Exception("Number must be greater than this");
} else {
List<int> total = [];
int curr = this;
int diff = number - this;
for (var i = -1; i < diff; i++) {
total.add(curr++);
}
return MainParser(total);
}
}
}
main() {
print(10.years().ago());
print(7.to(11).by(2));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment