chore(medium): add new package
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
// Copyright (C) 2022 WYATT GROUP
|
||||
// Please see the AUTHORS file for details.
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
extension ReadingTimeX on String {
|
||||
Duration readingTime({
|
||||
int wpm = 200,
|
||||
String suffix = 'min read',
|
||||
String lessMsg = 'less than a minute',
|
||||
}) {
|
||||
bool _ansiWordBound(String c) {
|
||||
return (' ' == c) || ('\n' == c) || ('\r' == c) || ('\t' == c);
|
||||
}
|
||||
|
||||
int words = 0, start = 0, end = length - 1;
|
||||
|
||||
while (_ansiWordBound(this[start])) {
|
||||
start++;
|
||||
}
|
||||
while (_ansiWordBound(this[end])) {
|
||||
end--;
|
||||
}
|
||||
|
||||
// Count real words
|
||||
for (int i = start; i <= end;) {
|
||||
for (; i <= end && !_ansiWordBound(this[i]); i++) {}
|
||||
words++;
|
||||
for (; i <= end && _ansiWordBound(this[i]); i++) {}
|
||||
}
|
||||
|
||||
final minutes = words / wpm;
|
||||
final seconds = (minutes - minutes.floor()) * 60;
|
||||
final Duration duration = Duration(minutes: minutes.floor(), seconds: seconds.floor());
|
||||
return duration;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user