diff --git a/CHANGELOG.md b/CHANGELOG.md index 2f439d2..88b0ff8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## 3.1.1 + +- `parseBool`: + - Improve possible valid values. + ## 3.1.0 - Migrate to `lints`: diff --git a/lib/src/math.dart b/lib/src/math.dart index d9abda2..d691018 100644 --- a/lib/src/math.dart +++ b/lib/src/math.dart @@ -388,7 +388,7 @@ num? parseNum(Object? v, [num? def]) { /// /// if [v] is [num]: true when [v > 0] /// -/// if [v] is [String]: true when [v == "true"|"yes"|"ok"|"1"|"y"|"s"|"t"|"+" +/// if [v] is [String]: true when [v == "true"|"yes"|"ok"|"on"|"enabled"|"1"|"y"|"s"|"t"|"+" /// /// [def] The default value if [v] is invalid. bool? parseBool(Object? v, [bool? def]) { @@ -412,6 +412,11 @@ bool? parseBool(Object? v, [bool? def]) { return s == 'true' || s == 'yes' || s == 'ok' || + s == 'on' || + s == 'enabled' || + s == 'active' || + s == 'activated' || + s == 'selected' || s == '1' || s == 'y' || s == 's' || diff --git a/pubspec.yaml b/pubspec.yaml index 48b7b98..f09f538 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: swiss_knife description: Dart Useful Tools - collections, math, date, uri, json, events, resources, regexp, etc... -version: 3.1.0 +version: 3.1.1 homepage: https://github.com/gmpassos/swiss_knife environment: diff --git a/test/swiss_knife_test.dart b/test/swiss_knife_test.dart index 34ac520..6eb4d5a 100644 --- a/test/swiss_knife_test.dart +++ b/test/swiss_knife_test.dart @@ -347,6 +347,41 @@ void main() { expect(Math.mean([2, 4]), equals(3)); }); + test('parseBool', () { + expect(parseBool(true), isTrue); + expect(parseBool(false), isFalse); + + expect(parseBool('true'), isTrue); + expect(parseBool('false'), isFalse); + + expect(parseBool(1), isTrue); + expect(parseBool(0), isFalse); + + expect(parseBool('1'), isTrue); + expect(parseBool('0'), isFalse); + + expect(parseBool('yes'), isTrue); + expect(parseBool('no'), isFalse); + + expect(parseBool('ok'), isTrue); + expect(parseBool('no'), isFalse); + + expect(parseBool('on'), isTrue); + expect(parseBool('off'), isFalse); + + expect(parseBool('On'), isTrue); + expect(parseBool('Off'), isFalse); + + expect(parseBool('enabled'), isTrue); + expect(parseBool('disabled'), isFalse); + + expect(parseBool('active'), isTrue); + expect(parseBool('disabled'), isFalse); + + expect(parseBool('activated'), isTrue); + expect(parseBool('deactivated'), isFalse); + }); + test('parseNum', () { expect(parseNum(0), equals(0)); expect(parseNum(1), equals(1));