33 *
44 * The lucky javascript library to identify and convert strings from any letter case to another
55 *
6- * @version 1.1.0
7- * @date 2020-12-13T12:50 :32.957Z
6+ * @version 1.1.1
7+ * @date 2021-01-10T19:15 :32.684Z
88 * @link https://github.com/magynhard/lucky-case
99 * @author Matthäus J. N. Beyrle
1010 * @copyright Matthäus J. N. Beyrle
@@ -93,12 +93,13 @@ class LuckyCase {
9393 * Convert a string into the given case type
9494 *
9595 * @param {string } string to convert
96- * @param {string } case_type
96+ * @param {string } case_type can be UPPER_CASE or lower_case, e.g. 'SNAKE_CASE' or 'snake_case'
9797 * @param {boolean } preserve_prefixed_underscores
9898 * @returns {string }
9999 */
100100 static convertCase ( string , case_type , preserve_prefixed_underscores = true ) {
101101 const self = LuckyCase ;
102+ case_type = self . toUpperCase ( case_type ) ;
102103 if ( Object . keys ( self . CASES ) . includes ( case_type ) ) {
103104 return self [ 'to' + self . toPascalCase ( case_type ) ] ( string , preserve_prefixed_underscores ) ;
104105 }
@@ -710,7 +711,7 @@ class LuckyCase {
710711 } else {
711712 s = string ;
712713 }
713- return self . _isCaseMatch ( s , self . CAPITAL ) ;
714+ return self . isUpperCase ( s [ 0 ] ) ;
714715 }
715716
716717 /**
@@ -722,7 +723,57 @@ class LuckyCase {
722723 */
723724 static isCapitalized ( string , skip_prefixed_underscores = false ) {
724725 const self = LuckyCase ;
725- return self . isCamelCase ( string , skip_prefixed_underscores ) ;
726+ return self . isCapital ( string , skip_prefixed_underscores ) ;
727+ }
728+
729+ /**
730+ * Convert the first character to lower case
731+ *
732+ * @param {string } string to convert
733+ * @param {boolean } skip_prefixed_underscores
734+ * @returns {string }
735+ */
736+ static decapitalize ( string , skip_prefixed_underscores = false ) {
737+ const self = LuckyCase ;
738+ if ( ! string || string === '' ) {
739+ return string ;
740+ }
741+ let s ;
742+ if ( skip_prefixed_underscores ) {
743+ s = self . cutUnderscoresAtStart ( string ) ;
744+ } else {
745+ s = string ;
746+ }
747+ s = self . toLowerCase ( s [ 0 ] ) + s . substr ( 1 ) ;
748+ if ( skip_prefixed_underscores ) {
749+ return self . getUnderscoresAtStart ( string ) + s ;
750+ } else {
751+ return s ;
752+ }
753+ }
754+
755+ /**
756+ * Check if the strings first character is a lower case letter
757+ *
758+ * @param {string } string
759+ * @param {boolean } skip_prefixed_underscores
760+ * @returns {boolean }
761+ */
762+ static isNotCapital ( string , skip_prefixed_underscores = false ) {
763+ const self = LuckyCase ;
764+ return ! self . isCapital ( string , skip_prefixed_underscores ) ;
765+ }
766+
767+ /**
768+ * Check if the strings first character is a lower case letter
769+ *
770+ * @param {string } string
771+ * @param {boolean } skip_prefixed_underscores
772+ * @returns {boolean }
773+ */
774+ static isDecapitalized ( string , skip_prefixed_underscores = false ) {
775+ const self = LuckyCase ;
776+ return self . isNotCapital ( string , skip_prefixed_underscores ) ;
726777 }
727778
728779 //----------------------------------------------------------------------------------------------------
@@ -947,9 +998,18 @@ class LuckyCase {
947998 */
948999 static _isCaseMatch ( string , case_type ) {
9491000 const self = LuckyCase ;
950- const regex = self . CASES [ case_type ] ;
951- regex . lastIndex = 0 ; // reset state
952- return self . CASES [ case_type ] . test ( string ) ;
1001+ if ( self . isValidCaseType ( case_type ) ) {
1002+ const regex = self . CASES [ case_type ] ;
1003+ regex . lastIndex = 0 ; // reset state
1004+ return self . CASES [ case_type ] . test ( string ) ;
1005+ } else if ( self . FORMATS [ case_type ] ) {
1006+ const regex = self . FORMATS [ case_type ] ;
1007+ regex . lastIndex = 0 ;
1008+ return self . FORMATS [ case_type ] . test ( string ) ;
1009+ } else {
1010+ const error_message = `Invalid case type '${ case_type } '. Valid types are: ${ Object . keys ( self . CASES ) . join ( ', ' ) } ` ;
1011+ throw new InvalidCaseError ( error_message ) ;
1012+ }
9531013 }
9541014
9551015 /**
0 commit comments