File tree Expand file tree Collapse file tree 4 files changed +28
-2
lines changed
03-objects-arrays-intro-to-testing Expand file tree Collapse file tree 4 files changed +28
-2
lines changed Original file line number Diff line number Diff line change 4
4
* @returns {function } - function-getter which allow get value from object by set path
5
5
*/
6
6
export function createGetter ( path ) {
7
-
7
+ const arr = path . split ( '.' ) ;
8
+
9
+ return function ( obj ) {
10
+ if ( ! obj . hasOwnProperty ( arr [ 0 ] ) ) return ;
11
+ return arr . reduce ( ( accum , item ) =>
12
+ accum . hasOwnProperty ( item ) ? accum [ item ] : undefined , obj )
13
+
14
+ }
8
15
}
Original file line number Diff line number Diff line change 4
4
* @returns {object | undefined } - returns new object or undefined if nothing did't pass
5
5
*/
6
6
export function invertObj ( obj ) {
7
+ if ( obj === undefined )
8
+ return ;
7
9
10
+ const arr = Object . entries ( obj ) ;
11
+ return Object . fromEntries ( arr . map ( ( [ keys , values ] ) => [ values , keys ] ) ) ;
8
12
}
Original file line number Diff line number Diff line change 5
5
* @returns {string } - the new string without extra symbols according passed size
6
6
*/
7
7
export function trimSymbols ( string , size ) {
8
+ if ( string === '' || size === 0 ) return '' ;
9
+ if ( size === undefined ) return string ;
8
10
11
+ let newStr = '' ;
12
+ let n = 1 ;
13
+ for ( let i = 0 ; i < string . length ; i ++ ) {
14
+ if ( string [ i ] === string [ i - 1 ] ) {
15
+ n ++ ;
16
+ }
17
+ else n = 1 ;
18
+
19
+ if ( n <= size ) {
20
+ newStr = newStr + string [ i ] ;
21
+ }
22
+ }
23
+ return newStr ;
9
24
}
Original file line number Diff line number Diff line change 4
4
* @returns {*[] } - the new array with uniq values
5
5
*/
6
6
export function uniq ( arr ) {
7
-
7
+ return Array . from ( new Set ( arr ) ) ;
8
8
}
You can’t perform that action at this time.
0 commit comments