From ec407bae9147ec2bb33353dd3d0b9cdec8c49cdb Mon Sep 17 00:00:00 2001 From: gnipbao Date: Sat, 16 Dec 2017 21:24:29 +0800 Subject: [PATCH 1/4] add camelize and decamelize. --- snippets/camelize.md | 17 +++++++++++++++++ snippets/decamelize.md | 16 ++++++++++++++++ tag_database | 2 ++ 3 files changed, 35 insertions(+) create mode 100644 snippets/camelize.md create mode 100644 snippets/decamelize.md diff --git a/snippets/camelize.md b/snippets/camelize.md new file mode 100644 index 000000000..937a974e8 --- /dev/null +++ b/snippets/camelize.md @@ -0,0 +1,17 @@ +### Camelize + +Camelize a string, cutting the string by multiple separators like + hyphens, underscores and spaces. + +```js +/** + * @param {str} string str to camelize + * @return string Camelized text + */ +const camelize = 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' +``` diff --git a/snippets/decamelize.md b/snippets/decamelize.md new file mode 100644 index 000000000..ab5ac6fa7 --- /dev/null +++ b/snippets/decamelize.md @@ -0,0 +1,16 @@ +### decamelize + +Decamelizes a string with/without a custom separator (underscore by default). + +```js +const decamelize = (str, separator) => { + separator = typeof separator === 'undefined' ? '_' : separator; + return 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' +``` diff --git a/tag_database b/tag_database index b2535a484..aed0ec9d8 100644 --- a/tag_database +++ b/tag_database @@ -5,6 +5,7 @@ array-intersection:array array-union:array average-of-array-of-numbers:array bottom-visible:browser +camelize:string capitalize-first-letter-of-every-word:string capitalize-first-letter:string chain-asynchronous-functions:function @@ -15,6 +16,7 @@ compact:array count-occurrences-of-a-value-in-array:array current-URL:browser curry:function +decamelize:string deep-flatten-array:array distance-between-two-points:math divisible-by-number:math From 3e770710c720f1acfcd2e0dbd1b0fbb84257b741 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sat, 16 Dec 2017 17:11:49 +0200 Subject: [PATCH 2/4] Update and rename camelize.md to convert-string-to-camelcase.md --- .../{camelize.md => convert-string-to-camelcase.md} | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) rename snippets/{camelize.md => convert-string-to-camelcase.md} (52%) diff --git a/snippets/camelize.md b/snippets/convert-string-to-camelcase.md similarity index 52% rename from snippets/camelize.md rename to snippets/convert-string-to-camelcase.md index 937a974e8..ba34a6835 100644 --- a/snippets/camelize.md +++ b/snippets/convert-string-to-camelcase.md @@ -1,15 +1,10 @@ -### Camelize +### Convert string to camelcase -Camelize a string, cutting the string by multiple separators like - hyphens, underscores and spaces. +Use `replace()` to remove underscores, hyphens and spaces and convert words to camelcase. ```js -/** - * @param {str} string str to camelize - * @return string Camelized text - */ -const camelize = str => str.replace(/^([A-Z])|[\s-_]+(\w)/g, - (match, p1, p2, offset) => p2 ? p2.toUpperCase() : p1.toLowerCase()); +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' From 8c2423138450e6c5bd8ab1345ae6d70d337d3153 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sat, 16 Dec 2017 17:14:16 +0200 Subject: [PATCH 3/4] Update and rename decamelize.md to convert-string-from-camelcase.md --- snippets/convert-string-from-camelcase.md | 13 +++++++++++++ snippets/decamelize.md | 16 ---------------- 2 files changed, 13 insertions(+), 16 deletions(-) create mode 100644 snippets/convert-string-from-camelcase.md delete mode 100644 snippets/decamelize.md diff --git a/snippets/convert-string-from-camelcase.md b/snippets/convert-string-from-camelcase.md new file mode 100644 index 000000000..969be904d --- /dev/null +++ b/snippets/convert-string-from-camelcase.md @@ -0,0 +1,13 @@ +### 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' +``` diff --git a/snippets/decamelize.md b/snippets/decamelize.md deleted file mode 100644 index ab5ac6fa7..000000000 --- a/snippets/decamelize.md +++ /dev/null @@ -1,16 +0,0 @@ -### decamelize - -Decamelizes a string with/without a custom separator (underscore by default). - -```js -const decamelize = (str, separator) => { - separator = typeof separator === 'undefined' ? '_' : separator; - return 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' -``` From b236f39dfa5399e90292e5c308204116704b8322 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sat, 16 Dec 2017 17:14:45 +0200 Subject: [PATCH 4/4] Update tag_database --- tag_database | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tag_database b/tag_database index aed0ec9d8..458604576 100644 --- a/tag_database +++ b/tag_database @@ -5,7 +5,7 @@ array-intersection:array array-union:array average-of-array-of-numbers:array bottom-visible:browser -camelize:string +convert-string-to-camelcase:string capitalize-first-letter-of-every-word:string capitalize-first-letter:string chain-asynchronous-functions:function @@ -16,7 +16,7 @@ compact:array count-occurrences-of-a-value-in-array:array current-URL:browser curry:function -decamelize:string +convert-string-from-camelcase:string deep-flatten-array:array distance-between-two-points:math divisible-by-number:math