I was trying to avoid chaining together multiple replace statements. Something where I'd have $1, $2, $3 in my function. But that type of grouping is hard to understand, and your mention about cross browser problems is something I never thought about as well.
I was trying to find a JavaScript function to camelCase a string, and wanted to make sure special characters would be removed (and I had trouble understanding what some of the answers above were doing). This is based on c c young's answer, with added comments and the removal of $peci&l characters.
To effectively create a function that converts the casing of a string to camel-case, the function will also need to convert each string to lower-case first, before transforming the casing of the first character of non-first strings to an uppercase letter.
It splits the lower-cased string based on the list of characters provided in the RegExp [.\-_\s] (add more inside the []!) and returns a word array . Then, it reduces the array of strings to one concatenated string of words with uppercased first letters. Because the reduce has no initial value, it will start uppercasing first letters starting with the second word.
I tried several of the previous solutions, and all of them had one flaw or another. Some didn't remove punctuation; some didn't handle cases with numbers; some didn't handle multiple punctuations in a row.
I doubt this is the most performant answer (three regex passes through the string, rather than one or two), but it passes all the tests I can think of. To be honest, though, I really can't imagine a case where you're doing so many camel-case conversions that performance would matter.
String.prototype.toCamelCase = function() return this.replace(/[^a-z ]/ig, '') // Replace everything but letters and spaces. .replace(/(?:^\wString.toCamelCase = function(str) return str.toCamelCase();var testCases = [ "equipment class name", "equipment class Name", "equipment Class name", "equipment Class Name", "Equipment class name", "Equipment class Name", "Equipment Class name", "Equipment Class Name", "equipment className", "equipment ClassName", "Equipment ClassName", "equipmentClass name", "equipmentClass Name", "EquipmentClass Name"];for (var i = 0; i < testCases.length; i++) console.log(testCases[i].toCamelCase());;
In fact it seems that the context of the usage of my function is important. This function will be used to convert C structure, union, enum, GObject object or interface names to their corresponding OCaml module name in Capitalized_snake_case name. In my case, I think that I should not separate each capitalized char with an undescore. For example :
I wrote a blog post Recipes for OCamlLex. Edit: and here is a sketch. The idea is to use a scanner to split a string into a list of words. It would now be easy to add special cases, for example for common abbreviations. The file below is camel.mll for OCamlLex.
Thanks for taking the time to write this little example. Could you add information about how to use the lexer in a lib. For example, it was not obvious for me that I needed to just add this part in a lib/lexer.ml file :
Thanks for the precision. When you say these exceptions denote a programming error do you mean that a program should never throw such exception and that it is the duty of the caller to check that the callee will never throw an Invalid_argument exception ? Is this denotation a tacit convention between OCaml programmer ?
When you say these exceptions denote a programming error do you mean that a program should never throw such exception and that it is the duty of the caller to check that the callee will never throw an Invalid_argument exception ?
I started the morning working on an algorithm and realized it wasn't going to be a quick practice for me. I wanted to go over my thinking process, and the top solution after I submitted as well. We can always learn to be better, so why not try to think through someone's solution that seems cleaner?
Complete the method/function so that it converts dash/underscore delimited words into camel casing. The first word within the output should be capitalized only if the original word was capitalized (known as Upper Camel Case, also often referred to as Pascal case).
I replaced any odd characters, so that I could know for certain that all my words are separated by the same character. I know that all my words are separated by the underscore _, so now it makes it easier for my to use the split() method to separate each word by commas, and place them in an array, [ 'the', 'stealth', 'warrior' ].
3) My next big hurdle will be capitalizing every first letter of every word...except the first word. To deal with this I used the splice() method to remove the first word from the original array, then push it into my containmentArea array. I created an array to temp hold my strings, as I plan to later use the join() method to smush them back into strings right before returning them. But there's still more work to be done.
4) On to the for loop which I wrote with the ES6 syntax. Remember that splitString is an array with my string split into comma separated elements. Let's start iterating through every element, using the variable word. I will use the split method on every word, splitString[word], to further break down my string into something that would look like
[ 's', 't', 'e', 'a', 'l', 't', 'h' ], for example & I'll store them in an array called splitWords.
5) I can than grab the first element in my splitWords array with splitWords[0], and transform it with the the toUppercase() method. This is how I capitalize every first letter of every word, except the first word that we splice() -d off at the start of this algorithm.
6) Since our process of transforming the first character in the string hasn't modified the original array, we'll have to do that with another splice() method, only this time we'll have to supply our method with a third value, which will be what we want to replace our un-capitalized letter with. In this case, that's represented by the capitalLetter variable in splitWords.splice(0,1, capitalLetter). And we then use our join() method to squish our letters back together into one word, ['Stealth'].
7) We still need to glue our camlCased sentence back together though. I write containmentArea.push(joinedWord) to push our words with the first capitalized into our containment array that still holds the first word we spliced off. And then use another joind, let newSentence = containmentArea.join('') to create my camelCased sentence.
8) We should be done at this point, but can't simply return the containmanetArea variable that's holding our string. This is because the initial split() we ran resulted in an array of strings, that was pushed into another array. So right before returning we use another join() to condense our two arrays into one.
1) The variable regExp is set to equal a regular expression to find all word characters (Alphanumeric or underscore), which is the \w portion of the regex. But with just that, you can't also highlight dashes. Which is why that symbol is proceeded by [-_], making an explicit statement that we want to match dashes and underscores. And as always, i is to ignore case, and g is for a global search. It's finding matches that when console logged to the screen would look like -S match and _s match; whis a (dash underscore) + a (word character or number).
The replace() method returns a new string with some or all matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp, and the replacement can be a string or a function to be called for each match. If pattern is a string, only the first occurrence will be replaced.
In just 3 steps someone was able to create a solution that was far easier to implement. Though this proves I need to brush up on my regular expressions, I'm glad I could break it down and understand every line.
Just to get you started, using clipboard actions
Copy the string to a named clipboard
Filter Named clipboard action with whatever you want to do to Named Clipboard2
Paste from named clipboard2
I'm evaluating at the moment and was thinking of buying. This would be a killer feature for me well worth the ticket price, but the investment in learning how to DYI it is more than I can invest right now.
OK give a couple of test cases of the input and your required output, and by tomorrow someone on the forum will probably give you an answer.
Also please read the first few chapters of the pdf I have sent you as that will give you a framework to proceed further.
I'll make you the full macro shortly.
First, a non-forum message that you have to promise us... Never... Ever give up on Keyboard Maestro. Even if it couldn't do one feature, there are tons upon tons of other features that it solves. It's almost like turning down a $9,999 loan, from a bank, because you asked for $10,000. I'm messing around but I'm 99% not.
I am blown away by the support here! OK, I'm buying KM. Yes that sounds great, or a separate shortcut for each case type. Ideally it would convert between all case types (Camel Case => Kebab Case => Spaced String, etc.)
This is actually a very easy use case for KM, IF you know a little about RegEx.
KM makes Regular Expressions very easy to use. The challenge, of course, is coming up with the RegEx. But if you can't figure it out, you can always google it. Your use case is very common, and very old. I'm sure many have already developed the RegEx for it.
camelCase - Converts the input string to camel case with the first word in lowercase followed by the next word capitalized with no spaces in between. For example, caseConverter. Commonly used for naming variables and functions.
7fc3f7cf58