File tree 2 files changed +21
-16
lines changed
2 files changed +21
-16
lines changed Original file line number Diff line number Diff line change 1
1
/**
2
2
* @function lower
3
3
* @description Will convert the entire string to lowercase letters.
4
- * @param {String } url - The input URL string
5
- * @return {String } Lowercase string
4
+ * @param {String } str - The input string
5
+ * @returns {String } Lowercase string
6
6
* @example lower("HELLO") => hello
7
7
* @example lower("He_llo") => he_llo
8
8
*/
@@ -12,17 +12,12 @@ const lower = (str) => {
12
12
throw new TypeError ( 'Invalid Input Type' )
13
13
}
14
14
15
- let lowerString = ''
15
+ return str
16
+ . replace ( / [ A - Z ] / g, ( _ , indexOfUpperChar ) => {
17
+ const asciiCode = str . charCodeAt ( indexOfUpperChar )
16
18
17
- for ( const char of str ) {
18
- let asciiCode = char . charCodeAt ( 0 )
19
- if ( asciiCode >= 65 && asciiCode <= 90 ) {
20
- asciiCode += 32
21
- }
22
- lowerString += String . fromCharCode ( asciiCode )
23
- }
24
-
25
- return lowerString
19
+ return String . fromCharCode ( asciiCode + 32 )
20
+ } )
26
21
}
27
22
28
23
export { lower }
Original file line number Diff line number Diff line change 1
1
import { lower } from '../Lower'
2
2
3
- describe ( 'Lower' , ( ) => {
4
- it ( 'return uppercase strings' , ( ) => {
5
- expect ( lower ( 'hello' ) ) . toBe ( 'hello' )
3
+ describe ( 'Testing the Lower function' , ( ) => {
4
+ it ( 'Test 1: Check by invalid type' , ( ) => {
5
+ expect ( ( ) => lower ( 345 ) ) . toThrowError ( )
6
+ expect ( ( ) => lower ( true ) ) . toThrowError ( )
7
+ expect ( ( ) => lower ( null ) ) . toThrowError ( )
8
+ } )
9
+
10
+ it ( 'Test 2: Check by uppercase string' , ( ) => {
6
11
expect ( lower ( 'WORLD' ) ) . toBe ( 'world' )
7
- expect ( lower ( 'hello_WORLD' ) ) . toBe ( 'hello_world' )
12
+ expect ( lower ( 'Hello_WORLD' ) ) . toBe ( 'hello_world' )
13
+ } )
14
+
15
+ it ( 'Test 3: Check by lowercase string' , ( ) => {
16
+ expect ( lower ( 'hello' ) ) . toBe ( 'hello' )
17
+ expect ( lower ( 'hello_world' ) ) . toBe ( 'hello_world' )
8
18
} )
9
19
} )
You can’t perform that action at this time.
0 commit comments