-
Notifications
You must be signed in to change notification settings - Fork 5
/
int.dart
146 lines (117 loc) · 5.84 KB
/
int.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* Copyright (c) 2020, Birju Vachhani
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
// Author: Birju Vachhani
// Created Date: August 17, 2020
part of '../screwdriver.dart';
/// Provides extensions for [int].
extension IntScrewdriver<T> on int {
/// Returns true if [this] represents a leap year
bool get isLeapYear => checkLeapYear(this);
/// Returns [Duration] equal to [this] no. of weeks
Duration get weeks => Duration(days: this * 7);
/// Returns [DateTime] with date that is [this] weeks ago
DateTime get weeksAgo => DateTime.now() - Duration(days: this * 7);
/// Returns [DateTime] with date that is [this] weeks after
DateTime get weeksAfter => DateTime.now() + Duration(days: this * 7);
/// Returns [Duration] equal to [this] no. of days
Duration get days => Duration(days: this);
/// Returns [DateTime] with date that is [this] days ago
DateTime get daysAgo => DateTime.now() - Duration(days: this);
/// Returns [DateTime] with date that is [this] days after
DateTime get daysAfter => DateTime.now() + Duration(days: this);
/// Returns [Duration] equal to [this] no. of hours
Duration get hours => Duration(hours: this);
/// Returns [DateTime] with time that is [this] hours ago
DateTime get hoursAgo => DateTime.now() - Duration(hours: this);
/// Returns [DateTime] with time that is [this] hours after
DateTime get hoursAfter => DateTime.now() + Duration(hours: this);
/// Returns [Duration] equal to [this] no. of minutes
Duration get minutes => Duration(minutes: this);
/// Returns [DateTime] with time that is [this] minutes ago
DateTime get minutesAgo => DateTime.now() - Duration(minutes: this);
/// Returns [DateTime] with time that is [this] minutes after
DateTime get minutesAfter => DateTime.now() + Duration(minutes: this);
/// Returns [Duration] equal to [this] no. of seconds
Duration get seconds => Duration(seconds: this);
/// Returns [Duration] equal to [this] no. of milliseconds
Duration get milliseconds => Duration(milliseconds: this);
/// Returns [Duration] equal to [this] no. of microseconds
Duration get microseconds => Duration(microseconds: this);
/// Returns no. of digits
/// e.g. 3.length // returns 1
/// 21.length // returns 2
/// 541.length // returns 3
int get length => toString().length;
/// Returns list of digits of [this]
/// e.g 12345.digits // returns [1, 2, 3, 4, 5]
/// e.g 8564.digits // returns [8, 5, 6, 4]
List<int> get digits => toString().split('').map(int.parse).toList();
/// Returns true if [this] can be completely divisible by [divider]
bool isDivisibleBy(int divider) => this % divider == 0;
/// Returns true if [this] can be completely divisible
/// by all of the [dividers].
bool isDivisibleByAll(List<int> dividers) =>
dividers.every((divider) => this % divider == 0);
/// runs [func] for [this] number of times.
/// This is irrespective of the sign of [this]. the for loop will always
/// run from 1 to absolute value of [this].
///
/// Returns [List] of type [T] where T is the return type of [func]
List<T> repeat(T Function(int count) func) {
return [for (var i = 1; i <= abs(); i++) func(i)];
}
/// Returns true for non-zero values just like C language.
/// e.g
/// 1.asBool // returns true
/// 0.asBool // returns false
/// 452.asBool // returns true
bool get asBool => this != 0;
/// Returns [int] as string which has a zero appended as prefix if [this]
/// is a single digit value.
String twoDigits() => this < 10 ? '0$this' : toString();
/// Creates an [IntRange] starting from this to [end] inclusively with
/// default step size of 1.
/// Example:
/// 5.rangeTo(10); // creates range 5-6-7-8-9-10
IntRange rangeTo(int end) => IntRange(this, end);
/// Creates an [IntRange] starting from this to [end] inclusively with
/// default step size of 1.
/// Example:
/// 5.downTo(1); // creates range 5-4-3-2-1
IntRange downTo(int end) => IntRange(this, end, step: -1);
/// Returns a range from this value up to but excluding the
/// specified [end] value.
IntRange until(int end) => IntRange(this, end - 1);
}
/// Generates a non-negative random integer uniformly distributed in the range
/// rom 0, inclusive, to [max], exclusive.
/// default [max] is 1_000_000
int randomInt({int? max}) => Random().nextInt(max ?? 1000000);