Skip to content

Commit b1de754

Browse files
committed
added toCamelCase
1 parent 6b97628 commit b1de754

File tree

1 file changed

+41
-4
lines changed

1 file changed

+41
-4
lines changed

lib/super_string.dart

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ extension SuperString on String {
179179

180180
/// Return a `String` of specified length width which is align in center, using the specified
181181
/// character.
182-
///
182+
///
183183
/// The default value of character is space ' '.
184184
///
185185
/// Example :
@@ -192,8 +192,7 @@ extension SuperString on String {
192192
/// Throws an [AssertionError] if character's length is greater than 1.
193193
///
194194
String center(int length, [String character = ' ']) {
195-
assert(character.length <= 1,
196-
"character's length should be equal to 1");
195+
assert(character.length <= 1, "character's length should be equal to 1");
197196

198197
StringBuffer str = StringBuffer();
199198
String char = character;
@@ -222,7 +221,7 @@ extension SuperString on String {
222221
/// ```
223222
///
224223
int count(String value, [int start = 0, int? end]) =>
225-
value.allMatches(this.substring(start , end)).length;
224+
value.allMatches(this.substring(start, end)).length;
226225

227226
/// expandTabs method sets the tab size to the specified number of whitespaces.
228227
///
@@ -236,4 +235,42 @@ extension SuperString on String {
236235
///
237236
String expandTabs([int? size]) =>
238237
size == null ? this : this.replaceAll('\t', ' ' * (size - 1));
238+
239+
/// Convert the given string to camelCase.
240+
///
241+
/// By default `isLowerCamelCase` is set as `false` and the given string is converted into UpperCamelCase.
242+
/// That means the first letter of String is converted into upperCase.
243+
///
244+
/// If the `isLowerCamelCase` is set to `true` then camelCase produces lowerCamelCase.
245+
/// That means the first letter of String is converted into lowerCase.
246+
///
247+
/// If the String is empty, this method returns `this`.
248+
///
249+
/// Example :
250+
///
251+
/// ```
252+
/// print('hello World'.toCamelCase()); // HelloWorld
253+
/// print('hello_World'.toCamelCase()); // HelloWorld
254+
/// print('hello World'.toCamelCase(isLowerCamelCase: true)); // helloWorld
255+
/// ```
256+
///
257+
String toCamelCase({bool isLowerCamelCase = false}) {
258+
if (this.isEmpty) return this;
259+
260+
StringBuffer str = StringBuffer();
261+
262+
isLowerCamelCase
263+
? str.write(this[0].toLowerCase())
264+
: str.write(this[0].toUpperCase());
265+
266+
for (int i = 1; i < this.length; i++) {
267+
if (this[i] == ' ' || this[i] == '_') {
268+
str.write(this[i + 1].toUpperCase());
269+
} else {
270+
if (this[i - 1] != ' ' && this[i - 1] != '_')
271+
str.write(this[i].toLowerCase());
272+
}
273+
}
274+
return str.toString();
275+
}
239276
}

0 commit comments

Comments
 (0)