Skip to content

Commit

Permalink
🐛 Improve day parsing. Closes #10 & #8
Browse files Browse the repository at this point in the history
  • Loading branch information
iqfareez committed Aug 7, 2024
1 parent d755061 commit 28a3137
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 70 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
## 1.4.1

- :bug: Fix issue where some compounded days format (eg: `TWTH`) could not be parse. See [#8](https://github.com/iiumschedule/albiruni/issues/8), [#10](https://github.com/iiumschedule/albiruni/issues/10)
- :pencil2: Update some classes documentation

## 1.4.0

- Upgrade package [http](https://pub.dev/packages/http) to `1.1.0` (upgrade major version, might be breaking changes)
- :arrow_up: Upgrade package [http](https://pub.dev/packages/http) to `1.1.0` (upgrade major version, might be breaking changes)

## 1.3.0

Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,11 @@ I think that's it for the basic usage of this library, of course, you can always

This happens when the albiruni server has some [certificate issues](https://github.com/iqfareez/iium_schedule/issues/10#issuecomment-1086550494). Some clients might reject the requests. If you're in development, try [this answer from SO](https://stackoverflow.com/a/61312927/13617136).

## List of available kulliyyah (as of 31/7/2022)
## List of available kulliyyah (as of 7 August 2024)

| Code | Name |
| :-----: | ------------------------------------------------------- |
| `IRKHS` | AHAS KIRKHS |
| `KAHS` | ALLIED HEALTH SCIENCES |
| `AED` | ARCHITECTURE |
| `BRIDG` | BRIDGING PROGRAMME |
Expand All @@ -134,14 +135,14 @@ I think that's it for the basic usage of this library, of course, you can always
| `ECONS` | ENMS |
| `KICT` | ICT |
| `IHART` | INTERNATIONAL INSTITUTE FOR HALAL RESEARCH AND TRAINING |
| `IRKHS` | IRKHS |
| `IIBF` | ISLAMIC BANKING AND FINANCE |
| `ISTAC` | ISTAC |
| `KLM` | KLM |
| `KLM` | KSTCL KLM |
| `LAWS` | LAWS |
| `MEDIC` | MEDICINE |
| `NURS` | NURSING |
| `PHARM` | PHARMACY |
| `PLNET` | PLANETARY SURVIVAL FOR SUSTAINABLE WELL-BEING |
| `KOS` | SCIENCE |
| `SC4SH` | SEJAHTERA CENTRE FOR SUSTAINABILTY AND HUMANITY |

Expand Down
4 changes: 3 additions & 1 deletion lib/src/albiruni_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ class Albiruni {
final _baseUrl = 'albiruni.iium.edu.my/myapps/StudentOnline/schedule1.php';
final _proxyUrl = 'corsproxyuia.up.railway.app/';

/// Fetch a list of subjects based on your specification.
/// Fetch a list of subjects for the given [kulliyah].
///
/// See list of [kulliyah] here: https://iiumschedule.iqfareez.com/docs/devs/albiruni#list-of-available-kulliyyah (Look at column "keys")
///
/// [course] is optional. Example: "AAD 3190", "MCTE 3312". The 4 digit number can be omitted. Example: "EECE".
///
Expand Down
75 changes: 37 additions & 38 deletions lib/src/util/date_time_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import 'package:intl/intl.dart';

/// Utility classes to parse raw data to proper data
class DateTimeUtil {
/// Map day in String to integers
static int _dayMap(String day) {
/// Map day in String to DateTime day integer
/// Return null if cannot parse
static int? _dayMap(String day) {
switch (day) {
case 'MON':
case 'M':
Expand All @@ -26,54 +27,52 @@ class DateTimeUtil {
case 'SUN':
return DateTime.sunday;
default:
throw DayTimeParseException(message: "Unable to parse day: $day");
return null;
}
}

/// Parse the given raw day format and return a Day number
/// Parse the given raw day format and return a Day number. If failed to parse,
/// it will return empty list
///
/// Eg: Given 'MON', return [DateTime.monday]
static List<int> parseDays(String rawDay) {
rawDay = rawDay.trim(); // remove all whitespaces (just in case)

// Attempt to parse single day
final resultDay = _dayMap(rawDay);
if (resultDay != null) return [resultDay];

// Attempt to hyphen-seperated compound days
var days = rawDay.split('-'); // split 'M-W' to ['M', 'W']
final resultDays =
days.map((e) => _dayMap(e)).where((e) => e != null).toList();
if (resultDays.isNotEmpty) return resultDays.map((e) => e!).toList();

// Attempt to non-hyphen-seperated compound days

// check for special cases (https://github.com/iqfareez/albiruni/issues/1)
if (rawDay == 'MTW') {
return [
DateTime.monday,
DateTime.tuesday,
DateTime.wednesday,
];
}
if (rawDay == 'MTWTH') {
return [
DateTime.monday,
DateTime.tuesday,
DateTime.wednesday,
DateTime.thursday
];
}
if (rawDay == 'MTWTHF') {
return [
DateTime.monday,
DateTime.tuesday,
DateTime.wednesday,
DateTime.thursday,
DateTime.friday
];
}

if (rawDay == 'MTTHF') {
return [
DateTime.monday,
DateTime.tuesday,
DateTime.thursday,
DateTime.friday
];
List<int> complexCompoundResult = [];
int i = 0;

while (i < rawDay.length) {
// Check for two-character day codes first (like 'TH' or 'SU')
if (i < rawDay.length - 1 &&
_dayMap(rawDay.substring(i, i + 2)) != null) {
complexCompoundResult.add(_dayMap(rawDay.substring(i, i + 2))!);
i += 2;
} else {
// Otherwise, check for one-character day codes
if (_dayMap(rawDay[i]) != null) {
complexCompoundResult.add(_dayMap(rawDay[i])!);
}
i += 1;
}
}

// check for other days
var days = rawDay.split('-'); // split 'M-W' to ['M', 'W']
return days.map((e) => _dayMap(e)).toList();
if (complexCompoundResult.isNotEmpty) return complexCompoundResult;

return [];
}

/// Parse weirdly formatted time in String to [DayTime] object
Expand Down
7 changes: 4 additions & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: albiruni
description: A wrapper to easily access IIUM's Course Schedule website data.
version: 1.4.0
description: A wrapper to easily access IIUM's Course Schedule data.
version: 1.4.1
repository: https://github.com/iqfareez/albiruni
issue_tracker: https://github.com/iqfareez/albiruni/issues
topics:
Expand All @@ -14,8 +14,9 @@ environment:
sdk: ">=3.0.0 <4.0.0"

dev_dependencies:
lints: ^2.0.1
lints: ^4.0.0
test: ^1.16.0

dependencies:
html: ^0.15.0
http: ^1.1.0
Expand Down
46 changes: 22 additions & 24 deletions test/src/util/date_time_util_test.dart
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
import 'package:albiruni/src/util/date_time_util.dart';
import 'package:albiruni/src/util/exceptions.dart';
import 'package:test/test.dart';

void main() {
group('Day parsing', () {
test('Parse days from raw', () {
/**
* Single day
*/

group('Day Parsing', () {
test('Single Day Parse', () {
var rawDay = 'MON';
var resDay = DateTimeUtil.parseDays(rawDay);
expect(resDay, [DateTime.monday]);

rawDay = 'FRI';
resDay = DateTimeUtil.parseDays(rawDay);
expect(resDay, [DateTime.friday]);
});

/**
* Compound days
*/

rawDay = 'M-W'; // Monday & Wednesday
resDay = DateTimeUtil.parseDays(rawDay);
test('Compounded Days Parse (With hyphen)', () {
var rawDay = 'M-W'; // Monday & Wednesday
var resDay = DateTimeUtil.parseDays(rawDay);
expect(resDay, [DateTime.monday, DateTime.wednesday]);

rawDay = 'T-TH'; // Tuesday & Thursday
Expand All @@ -49,12 +42,11 @@ void main() {
DateTime.friday,
]),
);
});

/**
* Special cases
*/
rawDay = 'MTWTH';
resDay = DateTimeUtil.parseDays(rawDay);
test('Compounded Days Parse (Without hyphen)', () {
var rawDay = 'MTWTH';
var resDay = DateTimeUtil.parseDays(rawDay);
expect(resDay, [
DateTime.monday,
DateTime.tuesday,
Expand All @@ -80,12 +72,18 @@ void main() {
DateTime.wednesday,
]);

/**
* Invalid
*/
rawDay = 'MEOW';
expect(() => DateTimeUtil.parseDays(rawDay),
throwsA(isA<DayTimeParseException>()));
rawDay = 'TWTH';
resDay = DateTimeUtil.parseDays(rawDay);
expect(resDay, [
DateTime.tuesday,
DateTime.wednesday,
DateTime.thursday,
]);
});

test('Invalid day parse', () {
var rawDay = 'XYZ';
expect(DateTimeUtil.parseDays(rawDay), []);
});
});

Expand Down

0 comments on commit 28a3137

Please sign in to comment.