1 /**
2 	*  cameCase transform
3 	*
4 	* Copyright: © 2016 David Monagle
5 	* License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file.
6 	* Authors: David Monagle
7 */
8 module transforms.camel;
9 
10 import std.uni;
11 import std.algorithm;
12 
13 
14 /// Returns the camelcased version of the input string. 
15 /// The `upper` parameter specifies whether to uppercase the first character
16 string camelCase(const string input, bool upper = false, dchar[] separaters = ['_']) {
17 	string output;
18 	bool upcaseNext = upper;
19 	foreach(c; input) {
20 		if (!separaters.canFind(c)) {
21 			if (upcaseNext) {
22 				output ~= c.toUpper;
23 				upcaseNext = false;
24 			}
25 			else
26 				output ~= c.toLower;
27 		}
28 		else {
29 			upcaseNext = true;
30 		}
31 	}
32 	
33 	return output;
34 }
35 
36 string camelCaseUpper(const string input) {
37 	return camelCase(input, true);
38 }
39 
40 string camelCaseLower(const string input) {
41 	return camelCase(input, false);
42 }
43 
44 unittest {
45 	assert("c".camelCase == "c");
46 	assert("c".camelCase(true) == "C");
47 	assert("c_a".camelCase == "cA");
48 	assert("ca".camelCase(true) == "Ca");
49 	assert("camel".camelCase(true) == "Camel");
50 	assert("Camel".camelCase(false) == "camel");
51 	assert("camel_case".camelCase(true) == "CamelCase");
52 	assert("camel_camel_case".camelCase(true) == "CamelCamelCase");
53 	assert("caMel_caMel_caSe".camelCase(true) == "CamelCamelCase");
54 	assert("camel2_camel2_case".camelCase(true) == "Camel2Camel2Case");
55 	assert("get_http_response_code".camelCase == "getHttpResponseCode");
56 	assert("get2_http_response_code".camelCase == "get2HttpResponseCode");
57 	assert("http_response_code".camelCase(true) == "HttpResponseCode");
58 	assert("http_response_code_xyz".camelCase(true) == "HttpResponseCodeXyz");
59 }