From 0a203b83ed70fa27c13641005232c45ccb5421e0 Mon Sep 17 00:00:00 2001 From: Y-Eker <73786701+Y-Eker@users.noreply.github.com> Date: Tue, 21 Sep 2021 20:40:56 +0300 Subject: [PATCH 1/2] Add files via upload --- snippets/toPascalCase.md | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 snippets/toPascalCase.md diff --git a/snippets/toPascalCase.md b/snippets/toPascalCase.md new file mode 100644 index 000000000..23b88aa96 --- /dev/null +++ b/snippets/toPascalCase.md @@ -0,0 +1,33 @@ +--- +title: toPascalCase +tags: string,regexp,intermediate +firstSeen: 2021-09-08T19:21:13+00:00 +--- + +Converts a string to pascalcase. + +- Use `String.prototype.match()` to break the string into words using an appropriate regexp. +- Use `Array.prototype.map()`, `Array.prototype.slice()`, `Array.prototype.join()`, `String.prototype.toLowerCase()` and `String.prototype.toUpperCase()` to combine them, capitalizing the first letter of each one. + +```js +const toPascalCase = str => { + const s = + str && + str + .match( + /[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g + ) + .map(x => x.slice(0, 1).toUpperCase() + x.slice(1).toLowerCase()) + .join(''); + return s; +}; +``` + +```js +toPascalCase('some_database_field_name'); // 'SomeDatabaseFieldName' +toPascalCase('Some label that needs to be pascalized'); +// 'SomeLabelThatNeedsToBePascalized' +toPascalCase('some-javascript-property'); // 'SomeJavascriptProperty' +toPascalCase('some-mixed_string with spaces_underscores-and-hyphens'); +// 'SomeMixedStringWithSpacesUnderscoresAndHyphens' +``` \ No newline at end of file From 480a0e5824716dbc89beb19560b2c19a2c3b371e Mon Sep 17 00:00:00 2001 From: Isabelle Viktoria Maciohsek Date: Thu, 23 Sep 2021 18:21:02 +0300 Subject: [PATCH 2/2] Update toPascalCase.md --- snippets/toPascalCase.md | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/snippets/toPascalCase.md b/snippets/toPascalCase.md index 23b88aa96..d40e8d2f7 100644 --- a/snippets/toPascalCase.md +++ b/snippets/toPascalCase.md @@ -4,23 +4,17 @@ tags: string,regexp,intermediate firstSeen: 2021-09-08T19:21:13+00:00 --- -Converts a string to pascalcase. +Converts a string to pascal case. - Use `String.prototype.match()` to break the string into words using an appropriate regexp. -- Use `Array.prototype.map()`, `Array.prototype.slice()`, `Array.prototype.join()`, `String.prototype.toLowerCase()` and `String.prototype.toUpperCase()` to combine them, capitalizing the first letter of each one. +- Use `Array.prototype.map()`, `Array.prototype.slice()`, `Array.prototype.join()`, `String.prototype.toUpperCase()` and `String.prototype.toLowerCase()` to combine them, capitalizing the first letter of each word and lowercasing the rest. ```js -const toPascalCase = str => { - const s = - str && - str - .match( - /[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g - ) - .map(x => x.slice(0, 1).toUpperCase() + x.slice(1).toLowerCase()) - .join(''); - return s; -}; +const toPascalCase = str => + str + .match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g) + .map(x => x.charAt(0).toUpperCase() + x.slice(1).toLowerCase()) + .join(''); ``` ```js @@ -30,4 +24,4 @@ toPascalCase('Some label that needs to be pascalized'); toPascalCase('some-javascript-property'); // 'SomeJavascriptProperty' toPascalCase('some-mixed_string with spaces_underscores-and-hyphens'); // 'SomeMixedStringWithSpacesUnderscoresAndHyphens' -``` \ No newline at end of file +```