diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 40e6f2a17..37b9cc767 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -29,6 +29,7 @@ Here's what you can do to help: - Try to keep your snippets' code short and to the point. Use modern techniques and features. Make sure to test your code before submitting. - All snippets must be followed by one (more if necessary) test case after the code, on a new line, in the form of a comment, along with the expected output. The syntax for this is `myFunction('testInput') -> 'testOutput'`. Use multiline comments only if necessary. - Try to make your function name unique, so that it does not conflict with existing snippets. + - Snippet functions do not have to handle errors in input, unless it's necessary (e.g. a mathematical function that cannot be extended to negative numbers should handle negative input appropriately). - Snippets should be short (usually below 10 lines). If your snippet is longer than that, you can still submit it, and we can help you shorten it or figure out ways to improve it. - Snippets *should* solve real-world problems, no matter how simple. - Snippets *should* be abstract enough to be applied to different scenarios. diff --git a/README.md b/README.md index babf49a07..357c4b642 100644 --- a/README.md +++ b/README.md @@ -101,6 +101,8 @@ * [Capitalize first letter of every word](#capitalize-first-letter-of-every-word) * [Capitalize first letter](#capitalize-first-letter) * [Check for palindrome](#check-for-palindrome) +* [Convert string from camelcase](#convert-string-from-camelcase) +* [Convert string to camelcase](#convert-string-to-camelcase) * [Reverse a string](#reverse-a-string) * [Sort characters in string (alphabetical)](#sort-characters-in-string-alphabetical) * [Truncate a string](#truncate-a-string) @@ -175,7 +177,7 @@ const remove = (arr, func) => arr.splice(arr.indexOf(val), 1); return acc.concat(val); }, []) : []; -//remove([1, 2, 3, 4], n => n % 2 == 0) -> [2, 4] +// remove([1, 2, 3, 4], n => n % 2 == 0) -> [2, 4] ``` [⬆ back to top](#table-of-contents) @@ -1152,6 +1154,37 @@ const palindrome = str => { [⬆ back to top](#table-of-contents) +### Convert string from camelcase + +Use `replace()` to remove underscores, hyphens and spaces and convert words to camelcase. +Omit the scond argument to use a default separator of '_'. + +```js +const fromCamelCase = (str, separator = '_') => + str.replace(/([a-z\d])([A-Z])/g, '$1' + separator + '$2') + .replace(/([A-Z]+)([A-Z][a-z\d]+)/g, '$1' + separator + '$2').toLowerCase(); +// decamelize('someDatabaseFieldName', ' ') -> 'some database field name' +// decamelize('someLabelThatNeedsToBeCamelized', '-') -> 'some-label-that-needs-to-be-camelized' +// decamelize('someJavascriptProperty', '_') -> 'some_javascript_property' +``` + +[⬆ back to top](#table-of-contents) + +### Convert string to camelcase + +Use `replace()` to remove underscores, hyphens and spaces and convert words to camelcase. + +```js +const toCamelCase = str => + str.replace(/^([A-Z])|[\s-_]+(\w)/g, (match, p1, p2, offset) => p2 ? p2.toUpperCase() : p1.toLowerCase()); +// camelize("some_database_field_name") -> 'someDatabaseFieldName' +// camelize("Some label that needs to be camelized") -> 'someLabelThatNeedsToBeCamelized' +// camelize("some-javascript-property") -> 'someJavascriptProperty' +// camelize("some-mixed_string with spaces_underscores-and-hyphens") -> 'someMixedStringWithSpacesUnderscoresAndHyphens' +``` + +[⬆ back to top](#table-of-contents) + ### Reverse a string Use array destructuring and `Array.reverse()` to reverse the order of the characters in the string. diff --git a/scripts/lint-script.js b/scripts/lint-script.js index 5d6d8721e..4aaa7c240 100644 --- a/scripts/lint-script.js +++ b/scripts/lint-script.js @@ -1,6 +1,7 @@ /* This is the linter script that lints snippets. Run using `npm run linter`. + You might have to run `npm i -g semistandard` for this script to run properly. */ // Load modules const fs = require('fs-extra'), cp = require('child_process'), path = require('path'), chalk = require('chalk'); diff --git a/snippets/array-remove.md b/snippets/array-remove.md index e08a4a02f..df2a83867 100644 --- a/snippets/array-remove.md +++ b/snippets/array-remove.md @@ -9,5 +9,5 @@ const remove = (arr, func) => arr.splice(arr.indexOf(val), 1); return acc.concat(val); }, []) : []; -//remove([1, 2, 3, 4], n => n % 2 == 0) -> [2, 4] +// remove([1, 2, 3, 4], n => n % 2 == 0) -> [2, 4] ``` diff --git a/tag_database b/tag_database index 927d17274..8181032df 100644 --- a/tag_database +++ b/tag_database @@ -10,7 +10,6 @@ array-without:array array-zip:array average-of-array-of-numbers:array bottom-visible:browser -convert-string-to-camelcase:string capitalize-first-letter-of-every-word:string capitalize-first-letter:string chain-asynchronous-functions:function @@ -20,10 +19,11 @@ clean-JSON-object:object collatz-algorithm:math compact:array compose-functions:function +convert-string-from-camelcase:string +convert-string-to-camelcase:string count-occurrences-of-a-value-in-array:array current-URL:browser curry:function -convert-string-from-camelcase:string deep-flatten-array:array distance-between-two-points:math divisible-by-number:math