Skip to content

Commit

Permalink
fix empty start/end
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagocpeixoto committed Jan 6, 2021
1 parent f0ecd24 commit 3d97fe5
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 9 deletions.
5 changes: 3 additions & 2 deletions lib/mudder_dart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,9 @@ class SymbolTable {
throw Exception('end param must be of type String or List<String>');
}

start ??= num2sym[0];
end ??= JSShim.repeat(num2sym[num2sym.length - 1], start.length + 6);
start = notEmptyString(start) ?? num2sym[0];
end = notEmptyString(end) ??
JSShim.repeat(num2sym[num2sym.length - 1], start.length + 6);
numStrings ??= 1;
base ??= maxBase;
numDivisions ??= numStrings + 1;
Expand Down
16 changes: 16 additions & 0 deletions lib/src/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,22 @@ bool _isPrefixCodeLogLinear(List<String> _strings) {

bool Function(List<String>) isPrefixCode = _isPrefixCodeLogLinear;

bool isValidString(dynamic string) {
return string is List<String>
? string.isNotEmpty
: string is String
? string.trim().isNotEmpty
: false;
}

dynamic notEmptyString(dynamic string) {
return isValidString(string)
? string is String
? string.trim()
: string
: null;
}

List<String> iter(String char, int len) => List.generate(
len,
(i) => String.fromCharCode(char.codeUnitAt(0) + i),
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: mudder_dart

version: 0.1.0
version: 0.1.1

description: >-
Dart version of mudderjs (https://github.com/fasiha/mudderjs).
Expand Down
20 changes: 20 additions & 0 deletions test/helpers_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,26 @@ void main() {
group('helpers tests', () {
final digits = <int>[1, 2, 3];

test('test isValidString', () {
expect(isValidString(null), false);
expect(isValidString(""), false);
expect(isValidString(" "), false);
expect(isValidString([]), false);
expect(isValidString("a"), true);
expect(isValidString(" a "), true);
expect(isValidString(["a"]), true);
});

test('test notEmptyString', () {
expect(notEmptyString(null), null);
expect(notEmptyString(""), null);
expect(notEmptyString(" "), null);
expect(notEmptyString("a"), "a");
expect(notEmptyString(" a "), "a");
expect(notEmptyString([]), null);
expect(notEmptyString(["a"]), ["a"]);
});

test('test iter', () {
final result = iter('0', 3);
expect(result, ['0', '1', '2']);
Expand Down
28 changes: 22 additions & 6 deletions test/mudder_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,9 @@ void main() {

group('mudder tests', () {
group('test mudder', () {
test('test invalid prev and next', () {
expect(() => base62.mudder(start: 1), throwsException);
test('test invalid start and end', () {
expect(() => base62.mudder(start: 1), throwsException);
expect(() => base62.mudder(end: 1), throwsException);
});

test('test alphabet mudder simple ', () {
Expand All @@ -177,19 +177,35 @@ void main() {
expect(result[0], "U");
});

test('test base62 mudder prev 1', () {
test('test base62 mudder start v and empty end string', () {
expect(base62.mudder(start: "v", end: ""), ["x"]);
});

test('test base62 mudder start v only', () {
expect(base62.mudder(start: "v"), ["x"]);
});

test('test base62 mudder empty start and end v', () {
expect(base62.mudder(start: "", end: "v"), ["S"]);
});

test('test base62 mudder end v only', () {
expect(base62.mudder(end: "v"), ["S"]);
});

test('test base62 mudder start 1', () {
expect(base62.mudder(start: "1"), ["V"]);
});

test('test base62 mudder prev [1]', () {
test('test base62 mudder start [1]', () {
expect(base62.mudder(start: ["1"]), ["V"]);
});

test('test base62 mudder next 1', () {
test('test base62 mudder end 1', () {
expect(base62.mudder(end: "1"), ["0V"]);
});

test('test base62 mudder next [1]', () {
test('test base62 mudder end [1]', () {
expect(base62.mudder(end: ["1"]), ["0V"]);
});

Expand Down

0 comments on commit 3d97fe5

Please sign in to comment.