-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from warrenrhodes/add_custum_function
add custom function
- Loading branch information
Showing
9 changed files
with
265 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/// A formatted print function. | ||
/// | ||
/// The function takes a list of objects to print, a separator to use between | ||
/// the objects, an end character to append at the end of the output, and an | ||
/// optional file to write the output to. | ||
/// | ||
/// Args: | ||
/// args (Object?): The list of objects to print. | ||
/// sep (String?): The separator to use between objects. Defaults to a space. | ||
/// end (String): The end character to append at the end. Defaults to a newline. | ||
/// unpack (bool): Whether to unpack the map or iterable objects. Defaults to false. | ||
/// | ||
/// Example: | ||
/// fprint(['Hello', 'world', 123], sep: ', ', end: '!\n'); | ||
/// fprint(['Dart', 'is', 'fun']); | ||
void fprint(Object? args, | ||
{String sep = ' ', String end = '\n', bool unpack = false}) { | ||
String output; | ||
if (args is Map) { | ||
if (unpack) { | ||
output = args.keys.join(sep); | ||
} else { | ||
output = args.toString(); | ||
} | ||
} else if (args is Iterable) { | ||
output = unpack ? args.join(sep) : args.toString(); | ||
} else { | ||
output = args.toString(); | ||
} | ||
|
||
output += end; | ||
|
||
print(output); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import 'package:dart_advanced_utils/src/custom_func.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
group('fprint', () { | ||
test('should print a map with unpacked keys', () { | ||
final map = {'key1': 'value1', 'key2': 'value2'}; | ||
final expectedOutput = 'key1 key2'; | ||
|
||
expect(() => fprint(map, unpack: true), prints('$expectedOutput\n\n')); | ||
}); | ||
|
||
test('should print a map with string representation', () { | ||
final map = {'key1': 'value1', 'key2': 'value2'}; | ||
final expectedOutput = '{key1: value1, key2: value2}'; | ||
|
||
expect(() => fprint(map), prints('$expectedOutput\n\n')); | ||
}); | ||
|
||
test('should print an iterable with unpacked elements', () { | ||
final iterable = ['value1', 'value2']; | ||
final expectedOutput = 'value1 value2'; | ||
|
||
output() => fprint(iterable, unpack: true); | ||
|
||
expect(output, prints('$expectedOutput\n\n')); | ||
}); | ||
|
||
test( | ||
'should print an iterable with unpacked elements and string representation', | ||
() { | ||
final iterable = ['value1', 'value2']; | ||
final expectedOutput = 'value1-value2'; | ||
|
||
output() => fprint(iterable, unpack: true, sep: '-'); | ||
|
||
expect(output, prints('$expectedOutput\n\n')); | ||
}); | ||
|
||
test('should print an iterable with string representation', () { | ||
final iterable = ['value1', 'value2']; | ||
final expectedOutput = '[value1, value2]'; | ||
|
||
output() => fprint(iterable); | ||
|
||
expect(output, prints('$expectedOutput\n\n')); | ||
}); | ||
|
||
test('should print a single value', () { | ||
final value = 'value'; | ||
final expectedOutput = 'value'; | ||
|
||
output() => fprint(value); | ||
|
||
expect(output, prints('$expectedOutput\n\n')); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters