From f82097121dd0dee2fcc8bf4d6662dca9bff2cb49 Mon Sep 17 00:00:00 2001 From: terminal Date: Sun, 20 Jul 2014 17:04:02 -0700 Subject: [PATCH 1/4] String Additions String capitalization, as well as index-based insertion, replacement, and removal. --- README.md | 25 ++++++++++++++ lib/strings.dart | 74 ++++++++++++++++++++++++++++++++++++++++++ test/strings_test.dart | 45 +++++++++++++++++++++++++ 3 files changed, 144 insertions(+) diff --git a/README.md b/README.md index 528d8861..e8ce1bb4 100644 --- a/README.md +++ b/README.md @@ -188,6 +188,31 @@ supporting a number of use-cases, including: `loop('la ', 0, 8) => 'la la la' // no tailing space` * Tailing: `loop('/path/to/some/file.txt', -3) => 'txt'` * Reversing: `loop('top', 3, 0) => 'pot'` + +`capitalize` allows you to capitalize every first letter of every word in a given +string. For example" + +`capitalize('this was a triumph!') => 'This Was A Triumph!'` + +`insertAt` inserts a string into another string at the given index. Example: + +`insertAt(4, 'was ', 'this a triumph!') => 'this was a triumph!'` + +`replaceAt` allows you to replace a porton of a string starting from a specified index. +Optionally, you can specifiy whether or not to overwrite the original string from the specified +index with the input string. Otherwise, only the initial character of the string will +be replaced with the rest of the string being inserted. + +`replaceAt(5, 'making', "I'm doing a note here", overwrite: true)` => "I'm making a note here"` + +`replaceAt(8, 'c', 'Huge suckess!') => 'Huge success!'` + +`removeAt` allows you to remove text from a string at a specified index. +Optionally, you can specify a length of how many characters to remove. + +`removeAt(2, 'Whaat?') => 'What?'` + +`removeAt(2, 'Whaaaaaat?', length: 5) => 'What?'` [quiver.strings]: http://google.github.io/quiver-dart/#quiver/quiver-strings diff --git a/lib/strings.dart b/lib/strings.dart index 4dff7ff1..85a10246 100644 --- a/lib/strings.dart +++ b/lib/strings.dart @@ -241,3 +241,77 @@ bool equalsIgnoreCase(String a, String b) => */ int compareIgnoreCase(String a, String b) => a.toLowerCase().compareTo(b.toLowerCase()); + +/** + * Returns a copy of the [input] where the first (a-z) letter of each word is + * capitalized. + */ +String capitalize(String input) => + input.replaceAllMapped(new RegExp(r'(^| )(\w)'), (Match m) => m.group(0).toUpperCase()); + +/** + * Inserts the value of [input] at the given [index] character of + * the [original] string. + */ +String insertAt(int index, String input, String original) { + StringBuffer str = new StringBuffer(); + if (index > 0) { + str.write(original.substring(0, index)); + } + + str.write(input); + str.write(original.substring(index)); + return str.toString(); +} + +/** + * Returns a new string where [input] is inserted at the [index] of + * the [original] string. + * + * If [overwrite] is true, any text longer than a single character will + * overwrite the text that follows the initially replaced character. + * + * If the [input] is longer than [original], the returned string will + * be longer than [original]. + */ +String replaceAt(int index, String input, String original, {bool overwrite : false}) { + StringBuffer str = new StringBuffer(); + if (index > 0) { + str.write(original.substring(0, index)); + } + + str.write(input); + if (overwrite) { + if (index + input.length < original.length) { + str.write(original.substring((index + input.length))); + } + } else { + str.write(original.substring((index + 1))); + } + + return str.toString(); +} + +/** + * Removes a portion of the [original] string starting from the + * given [index]. + * + * Optionally, a [length] can be specified for how much text + * to remove. (default: 1 character); + */ +String removeAt(int index, String original, {int length: 1}) { + StringBuffer str = new StringBuffer(); + if (index > 0) { + str.write(original.substring(0, index)); + } + + if (length > 1) { + if (index + length < original.length) { + str.write(original.substring((index + length))); + } + } else { + str.write(original.substring(index + 1)); + } + + return str.toString(); +} \ No newline at end of file diff --git a/test/strings_test.dart b/test/strings_test.dart index ade57878..d8e7ab0d 100644 --- a/test/strings_test.dart +++ b/test/strings_test.dart @@ -311,4 +311,49 @@ main() { }); }); + + group('capitalize', () { + test('should return "This Was A Triumph!" after capitalizing non-capitalized the input string', () { + expect(capitalize('this was a triumph!'), 'This Was A Triumph!'); + }); + }); + + group('insertAt', () { + test('should return "abc" after inserting "b" into the string "ac" at index [1]', () { + expect(insertAt(1, 'b', 'ac'), 'abc'); + }); + }); + + group('replaceAt', () { + test('should return "abc" after replacing the index [1] character in the string "adc" with "b" ' + 'with overwrite disabled (default)', () { + expect(replaceAt(1, 'b', 'adc'), 'abc'); + }); + + test('should return "aabbcc" after replacing the index [1] character in the string "abc"' + 'with "abbc" with overwrite disabled (default)', () { + expect(replaceAt(1, 'abbc', 'abc'), 'aabbcc'); + }); + + test('should return "aabbccg" after replacing the index [1] character in the string "abcdefg" with ' + '"abbcc" and enabling overwrite', () { + expect(replaceAt(1, 'abbcc', 'abcdefg', overwrite: true), 'aabbccg'); + }); + + test('should return "aabbccdd" after replacing the index [1] character in the string "abcdefg" with ' + '"abbccdd" and enabling overwrite', () { + expect(replaceAt(1, 'abbccdd', 'abcdefg', overwrite: true), 'aabbccdd'); + }); + + }); + + group('removeAt', () { + test('should return "abc" after remvoing the character at index [1] from "abbc"', () { + expect(removeAt(1, 'abbc'), 'abc'); + }); + + test('should return "abc" after removing 5 characters starting at index [1] from "abbbbbbc"', () { + expect(removeAt(1, 'abbbbbbc', length: 5), 'abc'); + }); + }); } From df36c4cb8529027a25b3230c43d1fb728a65b2f3 Mon Sep 17 00:00:00 2001 From: terminal Date: Mon, 21 Jul 2014 01:01:45 -0700 Subject: [PATCH 2/4] Added C#-like string interpolation. Allows for terse, index-based string to act as mustache-esque templates for interpolation. sprintf() does not satisfy this requirement. --- README.md | 44 +++++++++++++++++++++++-- lib/strings.dart | 73 ++++++++++++++++++++++++++++++++++++++++++ pubspec.yaml | 6 ++-- test/strings_test.dart | 42 ++++++++++++++++++++---- 4 files changed, 152 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index e8ce1bb4..a47e6e40 100644 --- a/README.md +++ b/README.md @@ -190,7 +190,7 @@ supporting a number of use-cases, including: * Reversing: `loop('top', 3, 0) => 'pot'` `capitalize` allows you to capitalize every first letter of every word in a given -string. For example" +string. For example: `capitalize('this was a triumph!') => 'This Was A Triumph!'` @@ -201,19 +201,57 @@ string. For example" `replaceAt` allows you to replace a porton of a string starting from a specified index. Optionally, you can specifiy whether or not to overwrite the original string from the specified index with the input string. Otherwise, only the initial character of the string will -be replaced with the rest of the string being inserted. +be replaced with the rest of the string being inserted. Examples: `replaceAt(5, 'making', "I'm doing a note here", overwrite: true)` => "I'm making a note here"` `replaceAt(8, 'c', 'Huge suckess!') => 'Huge success!'` `removeAt` allows you to remove text from a string at a specified index. -Optionally, you can specify a length of how many characters to remove. +Optionally, you can specify a length of how many characters to remove. Examples: `removeAt(2, 'Whaat?') => 'What?'` `removeAt(2, 'Whaaaaaat?', length: 5) => 'What?'` +`formatString` allows you to provide a terse, index-based string template +to use for interpolating an arbitrary-length set of parameters into their +corresponding indexed placeholders. All parameters that follow the first +parameter can be of any type. The objects have their `toString()` method +called to extract the string to be interpolated into the template string. +The placeholders in the string follow the pattern: `{index}`. For example: + +`formatString('A{0}{0}L{1}', 'P', 'E') => 'APPLE'` + +`formatString('There are {0} {2}!', 4, 'lights') => 'There are 4 lights!'` + +This allows you to use a variable as the template string instead of a +literal string unlike standard interpolation in Dart. + +` +var template = 'There are {0} {2}!'; +formatString(template, 4, 'lights') => 'There are 4 lights!'` +` + +`formatStringList` allows you to provide a terse, index-based string template +to use for interpolating a List of elements into their corresponding +indexed placeholders. All elements that follow the first element can be +of any type. The objects have their `toString()` method called to +extract the string to be interpolated into the template string. The +placeholders in the string follow the pattern: `{index}`. For example: + +`formatStringList(['A{0}{0}L{1}', 'P', 'E']) => 'APPLE'` + +`formatString(['There are {0} {2}!', 4, 'lights']) => 'There are 4 lights!'` + +This allows you to use a variable as the template string instead of a +literal string unlike standard interpolation in Dart. + +` +var template = 'There are {0} {2}!'; +formatString([template, 4, 'lights']) => 'There are 4 lights!'` +` + [quiver.strings]: http://google.github.io/quiver-dart/#quiver/quiver-strings ## [quiver.time][] diff --git a/lib/strings.dart b/lib/strings.dart index 85a10246..702796ac 100644 --- a/lib/strings.dart +++ b/lib/strings.dart @@ -314,4 +314,77 @@ String removeAt(int index, String original, {int length: 1}) { } return str.toString(); +} + +/** + * Here be voodoo magic. (that I got from a blog post) + * [Yu Asakusa's Blog](http://yuasakusa.github.io/dart/2014/01/23/dart-variadic.html) + */ +typedef dynamic _ApplyType(List positionalArguments); + +/** + * Further voodoo magic. + */ +class _Variadic implements Function { + final _ApplyType _apply; + + _Variadic(this._apply) {} + + @override + dynamic noSuchMethod(Invocation invocation) { + if (invocation.memberName == #call) { + if (invocation.isMethod) + return _apply(invocation.positionalArguments); + if (invocation.isGetter) + return this; + } + return super.noSuchMethod(invocation); + } +} + +/** + * This allows you to call [formatStringList] by only needing + * to specify an arbitrary number of positional paramters. + * + * This allows one to specify an easy, terse, index-based template + * similar to that in C# with String.Format(String, arg1, arg2, ...) + * + * Example: + * var template = 'A{0}{0}L{1}S'; + * formatString(template, 'P', 'E'); + * // "APPLES" + */ +final Function formatString = new _Variadic(formatStringList); + +/** + * Returns a string with the indexed placeholder tokens + * replaced with their corresponding List index + * derrived from [args]. The 0th element of [args] contains + * the original string with the formatting of the placeholders. + * + * Any element after the 0th element is made into a new List + * and interpolated into the placeholders at the corresponding index + * starting from 0 and on. + * + * Elements used for interpolation have their toString() method + * called. Therefore any type can be placed in the paramters. + * + * Example: + * formatStringList(['A{0}{0}L{1}S', 'P', 'E']); + * // "APPLES" + */ +String formatStringList(List args) +{ + if (args == null || args.length < 2) { + throw new ArgumentError('expected at least two paramters.'); + } + if (args[0] is !String) { + throw new ArgumentError('expected a string for the first paramter.'); + } + var str = args[0]; + var inserts = args.sublist(1); + for (var i = 0; i < inserts.length; i++) { + str = str.replaceAll('{$i}', inserts[i].toString()); + } + return str; } \ No newline at end of file diff --git a/pubspec.yaml b/pubspec.yaml index 22ad90a9..00bf34ae 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: quiver -version: 0.19.0-dev.3 +version: 0.19.0-dev.4 authors: - Justin Fagnani - Yegor Jbanov @@ -9,11 +9,11 @@ authors: - John McDole description: A set of utility libraries for Dart homepage: https://github.com/google/quiver-dart -documentation: http://google.github.io/quiver-dart/ environment: sdk: '>=1.0.0' +documentation: http://google.github.io/quiver-dart/ dependencies: path: '>=1.0.0 <2.0.0' dev_dependencies: - unittest: '>=0.11.0' matcher: '>=0.10.0' + unittest: '>=0.11.0' diff --git a/test/strings_test.dart b/test/strings_test.dart index d8e7ab0d..21c4e908 100644 --- a/test/strings_test.dart +++ b/test/strings_test.dart @@ -348,12 +348,40 @@ main() { }); group('removeAt', () { - test('should return "abc" after remvoing the character at index [1] from "abbc"', () { - expect(removeAt(1, 'abbc'), 'abc'); - }); - - test('should return "abc" after removing 5 characters starting at index [1] from "abbbbbbc"', () { - expect(removeAt(1, 'abbbbbbc', length: 5), 'abc'); - }); + test('should return "abc" after remvoing the character at index [1] from "abbc"', () { + expect(removeAt(1, 'abbc'), 'abc'); }); + + test('should return "abc" after removing 5 characters starting at index [1] from "abbbbbbc"', () { + expect(removeAt(1, 'abbbbbbc', length: 5), 'abc'); + }); + }); + + group('formatString', () { + test('should return "abc123" after interpolating the arguments [\'b\', 2] into the template "a{0}c1{1}3"', () { + expect(formatString('a{0}c1{1}3', 'b', 2), 'abc123'); + }); + + test('should throw when not enough arguments (minimum 2)', () { + expect(() => formatString(null), throws); + }); + + test('should throw with a non-string value in the first argument', () { + expect(() => formatString(0, 0), throws); + }); + }); + + group('formatStringList', () { + test('should return "abc123" after interpolating the arguments [\'b\', 2] into the template "a{0}c1{1}3"', () { + expect(formatStringList(['a{0}c1{1}3', 'b', 2]), 'abc123'); + }); + + test('should throw when not enough arguments (minimum 2)', () { + expect(() => formatString(null), throws); + }); + + test('should throw with a non-string value in the first element in the argument list', () { + expect(() => formatStringList([0, 0]), throws); + }); + }); } From d5a6aaf5f179c456597e1ff6b0568684cb20d7ac Mon Sep 17 00:00:00 2001 From: Term1nal Date: Mon, 21 Jul 2014 01:10:03 -0700 Subject: [PATCH 3/4] Fix markdown --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index a47e6e40..a3024c14 100644 --- a/README.md +++ b/README.md @@ -228,10 +228,10 @@ The placeholders in the string follow the pattern: `{index}`. For example: This allows you to use a variable as the template string instead of a literal string unlike standard interpolation in Dart. -` +``` var template = 'There are {0} {2}!'; formatString(template, 4, 'lights') => 'There are 4 lights!'` -` +``` `formatStringList` allows you to provide a terse, index-based string template to use for interpolating a List of elements into their corresponding @@ -247,10 +247,10 @@ placeholders in the string follow the pattern: `{index}`. For example: This allows you to use a variable as the template string instead of a literal string unlike standard interpolation in Dart. -` +``` var template = 'There are {0} {2}!'; -formatString([template, 4, 'lights']) => 'There are 4 lights!'` -` +formatString([template, 4, 'lights']) => 'There are 4 lights!' +``` [quiver.strings]: http://google.github.io/quiver-dart/#quiver/quiver-strings From a44899b5062d346949c3b71ac79a1488f590b3ae Mon Sep 17 00:00:00 2001 From: terminal Date: Sun, 24 Aug 2014 22:44:02 -0700 Subject: [PATCH 4/4] Removed formatStringList, rewrote replace Removed formatStringList to add later after performance concerns are researched further. Rewrote replacement to have replace be the base function with remove and insert being syntactical sugar for calls to replace. --- README.md | 61 +++--------------- lib/strings.dart | 136 +++++++---------------------------------- test/strings_test.dart | 64 +++++-------------- 3 files changed, 47 insertions(+), 214 deletions(-) diff --git a/README.md b/README.md index a3024c14..18edd182 100644 --- a/README.md +++ b/README.md @@ -194,63 +194,22 @@ string. For example: `capitalize('this was a triumph!') => 'This Was A Triumph!'` -`insertAt` inserts a string into another string at the given index. Example: +`replace` allows you to replace a porton of a string starting from a specified index. +Optionally, you can specifiy the length to overwrite the original string from the specified +index with the input string. Examples: -`insertAt(4, 'was ', 'this a triumph!') => 'this was a triumph!'` +`replace("I'm doing a note here", 5, 'making', overwrite: 5)` => "I'm making a note here"` -`replaceAt` allows you to replace a porton of a string starting from a specified index. -Optionally, you can specifiy whether or not to overwrite the original string from the specified -index with the input string. Otherwise, only the initial character of the string will -be replaced with the rest of the string being inserted. Examples: +`replace('Huge suckess!', 8, 'c') => 'Huge success!'` -`replaceAt(5, 'making', "I'm doing a note here", overwrite: true)` => "I'm making a note here"` +`insert` inserts a string into another string at the given index. Example: -`replaceAt(8, 'c', 'Huge suckess!') => 'Huge success!'` +`insert('this a triumph!', 4, 'was ') => 'this was a triumph!'` -`removeAt` allows you to remove text from a string at a specified index. -Optionally, you can specify a length of how many characters to remove. Examples: +`remove` allows you to remove a specified length of text from a string at + a specified index. Example: -`removeAt(2, 'Whaat?') => 'What?'` - -`removeAt(2, 'Whaaaaaat?', length: 5) => 'What?'` - -`formatString` allows you to provide a terse, index-based string template -to use for interpolating an arbitrary-length set of parameters into their -corresponding indexed placeholders. All parameters that follow the first -parameter can be of any type. The objects have their `toString()` method -called to extract the string to be interpolated into the template string. -The placeholders in the string follow the pattern: `{index}`. For example: - -`formatString('A{0}{0}L{1}', 'P', 'E') => 'APPLE'` - -`formatString('There are {0} {2}!', 4, 'lights') => 'There are 4 lights!'` - -This allows you to use a variable as the template string instead of a -literal string unlike standard interpolation in Dart. - -``` -var template = 'There are {0} {2}!'; -formatString(template, 4, 'lights') => 'There are 4 lights!'` -``` - -`formatStringList` allows you to provide a terse, index-based string template -to use for interpolating a List of elements into their corresponding -indexed placeholders. All elements that follow the first element can be -of any type. The objects have their `toString()` method called to -extract the string to be interpolated into the template string. The -placeholders in the string follow the pattern: `{index}`. For example: - -`formatStringList(['A{0}{0}L{1}', 'P', 'E']) => 'APPLE'` - -`formatString(['There are {0} {2}!', 4, 'lights']) => 'There are 4 lights!'` - -This allows you to use a variable as the template string instead of a -literal string unlike standard interpolation in Dart. - -``` -var template = 'There are {0} {2}!'; -formatString([template, 4, 'lights']) => 'There are 4 lights!' -``` +`remove('Whaaaaaat?', 2, 5) => 'What?'` [quiver.strings]: http://google.github.io/quiver-dart/#quiver/quiver-strings diff --git a/lib/strings.dart b/lib/strings.dart index 702796ac..3c4a249c 100644 --- a/lib/strings.dart +++ b/lib/strings.dart @@ -250,141 +250,49 @@ String capitalize(String input) => input.replaceAllMapped(new RegExp(r'(^| )(\w)'), (Match m) => m.group(0).toUpperCase()); /** - * Inserts the value of [input] at the given [index] character of - * the [original] string. - */ -String insertAt(int index, String input, String original) { - StringBuffer str = new StringBuffer(); - if (index > 0) { - str.write(original.substring(0, index)); - } - - str.write(input); - str.write(original.substring(index)); - return str.toString(); -} - -/** - * Returns a new string where [input] is inserted at the [index] of + * Returns a new string where [replacement] is inserted at the [index] of * the [original] string. * - * If [overwrite] is true, any text longer than a single character will - * overwrite the text that follows the initially replaced character. + * If [overwrite] is given, only the specified number of characters will be + * overwritten. (Default: length of [replacement]) * - * If the [input] is longer than [original], the returned string will + * If the [replacement] is longer than [original], the returned string will * be longer than [original]. */ -String replaceAt(int index, String input, String original, {bool overwrite : false}) { +String replace(String original, int index, String replacement, {int overwrite}) { StringBuffer str = new StringBuffer(); if (index > 0) { str.write(original.substring(0, index)); } - str.write(input); - if (overwrite) { - if (index + input.length < original.length) { - str.write(original.substring((index + input.length))); - } - } else { - str.write(original.substring((index + 1))); + if (overwrite == null) { + overwrite = replacement.length; } - - return str.toString(); -} -/** - * Removes a portion of the [original] string starting from the - * given [index]. - * - * Optionally, a [length] can be specified for how much text - * to remove. (default: 1 character); - */ -String removeAt(int index, String original, {int length: 1}) { - StringBuffer str = new StringBuffer(); - if (index > 0) { - str.write(original.substring(0, index)); - } - - if (length > 1) { - if (index + length < original.length) { - str.write(original.substring((index + length))); + str.write(replacement); + if (overwrite > 0) { + if (overwrite < original.length) { + str.write(original.substring(index + overwrite)); } - } else { - str.write(original.substring(index + 1)); + } else { + str.write(original.substring(index)); } - return str.toString(); } /** - * Here be voodoo magic. (that I got from a blog post) - * [Yu Asakusa's Blog](http://yuasakusa.github.io/dart/2014/01/23/dart-variadic.html) - */ -typedef dynamic _ApplyType(List positionalArguments); - -/** - * Further voodoo magic. + * Removes a specified [length] of the [original] string starting from the + * given [index]. */ -class _Variadic implements Function { - final _ApplyType _apply; - - _Variadic(this._apply) {} - - @override - dynamic noSuchMethod(Invocation invocation) { - if (invocation.memberName == #call) { - if (invocation.isMethod) - return _apply(invocation.positionalArguments); - if (invocation.isGetter) - return this; - } - return super.noSuchMethod(invocation); - } +String remove(String original, int index, int length) { + return replace(original, index, '', overwrite: length); } /** - * This allows you to call [formatStringList] by only needing - * to specify an arbitrary number of positional paramters. - * - * This allows one to specify an easy, terse, index-based template - * similar to that in C# with String.Format(String, arg1, arg2, ...) - * - * Example: - * var template = 'A{0}{0}L{1}S'; - * formatString(template, 'P', 'E'); - * // "APPLES" + * Inserts the value of [insertion] at the given [index] character of + * the [original] string. */ -final Function formatString = new _Variadic(formatStringList); +String insert(String original, int index, String insertion) { + return replace(original, index, insertion, overwrite: 0); +} -/** - * Returns a string with the indexed placeholder tokens - * replaced with their corresponding List index - * derrived from [args]. The 0th element of [args] contains - * the original string with the formatting of the placeholders. - * - * Any element after the 0th element is made into a new List - * and interpolated into the placeholders at the corresponding index - * starting from 0 and on. - * - * Elements used for interpolation have their toString() method - * called. Therefore any type can be placed in the paramters. - * - * Example: - * formatStringList(['A{0}{0}L{1}S', 'P', 'E']); - * // "APPLES" - */ -String formatStringList(List args) -{ - if (args == null || args.length < 2) { - throw new ArgumentError('expected at least two paramters.'); - } - if (args[0] is !String) { - throw new ArgumentError('expected a string for the first paramter.'); - } - var str = args[0]; - var inserts = args.sublist(1); - for (var i = 0; i < inserts.length; i++) { - str = str.replaceAll('{$i}', inserts[i].toString()); - } - return str; -} \ No newline at end of file diff --git a/test/strings_test.dart b/test/strings_test.dart index 21c4e908..460d3ce7 100644 --- a/test/strings_test.dart +++ b/test/strings_test.dart @@ -318,70 +318,36 @@ main() { }); }); - group('insertAt', () { + group('insert', () { test('should return "abc" after inserting "b" into the string "ac" at index [1]', () { - expect(insertAt(1, 'b', 'ac'), 'abc'); + expect(insert('ac', 1, 'b'), 'abc'); }); }); - group('replaceAt', () { + group('replace', () { test('should return "abc" after replacing the index [1] character in the string "adc" with "b" ' - 'with overwrite disabled (default)', () { - expect(replaceAt(1, 'b', 'adc'), 'abc'); + 'with length defaulted to the length of the replacement', () { + expect(replace('adc', 1 , 'b'), 'abc'); }); - - test('should return "aabbcc" after replacing the index [1] character in the string "abc"' - 'with "abbc" with overwrite disabled (default)', () { - expect(replaceAt(1, 'abbc', 'abc'), 'aabbcc'); - }); - - test('should return "aabbccg" after replacing the index [1] character in the string "abcdefg" with ' - '"abbcc" and enabling overwrite', () { - expect(replaceAt(1, 'abbcc', 'abcdefg', overwrite: true), 'aabbccg'); + test('should return "abcde" after replacing the index [1] character in the string "adc" with "bcde" ' + 'with overwrite defaulted to the length of the replacement', () { + expect(replace('adc', 1 , 'bcde'), 'abcde'); }); - - test('should return "aabbccdd" after replacing the index [1] character in the string "abcdefg" with ' - '"abbccdd" and enabling overwrite', () { - expect(replaceAt(1, 'abbccdd', 'abcdefg', overwrite: true), 'aabbccdd'); + test('should return "aabbcc" after replacing the index [1] character in the string "abc"' + 'with "abbc" with overwrite = 1 (1 character overwritten)', () { + expect(replace('abc', 1, 'abbc', overwrite: 1), 'aabbcc'); }); - }); - group('removeAt', () { + group('remove', () { test('should return "abc" after remvoing the character at index [1] from "abbc"', () { - expect(removeAt(1, 'abbc'), 'abc'); + expect(remove('abbc', 1, 1), 'abc'); }); test('should return "abc" after removing 5 characters starting at index [1] from "abbbbbbc"', () { - expect(removeAt(1, 'abbbbbbc', length: 5), 'abc'); + expect(remove('abbbbbbc', 1, 5), 'abc'); }); }); - group('formatString', () { - test('should return "abc123" after interpolating the arguments [\'b\', 2] into the template "a{0}c1{1}3"', () { - expect(formatString('a{0}c1{1}3', 'b', 2), 'abc123'); - }); - - test('should throw when not enough arguments (minimum 2)', () { - expect(() => formatString(null), throws); - }); - - test('should throw with a non-string value in the first argument', () { - expect(() => formatString(0, 0), throws); - }); - }); - - group('formatStringList', () { - test('should return "abc123" after interpolating the arguments [\'b\', 2] into the template "a{0}c1{1}3"', () { - expect(formatStringList(['a{0}c1{1}3', 'b', 2]), 'abc123'); - }); - - test('should throw when not enough arguments (minimum 2)', () { - expect(() => formatString(null), throws); - }); - - test('should throw with a non-string value in the first element in the argument list', () { - expect(() => formatStringList([0, 0]), throws); - }); - }); + }