Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
322c6da5
Commit
322c6da5
authored
Jan 31, 2019
by
GitLab Bot
Browse files
Options
Browse Files
Download
Plain Diff
Automatic merge of gitlab-org/gitlab-ce master
parents
10a1d813
8a948a20
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
140 additions
and
38 deletions
+140
-38
app/assets/javascripts/lib/utils/common_utils.js
app/assets/javascripts/lib/utils/common_utils.js
+24
-2
spec/javascripts/lib/utils/common_utils_spec.js
spec/javascripts/lib/utils/common_utils_spec.js
+116
-36
No files found.
app/assets/javascripts/lib/utils/common_utils.js
View file @
322c6da5
...
...
@@ -614,10 +614,18 @@ export const spriteIcon = (icon, className = '') => {
/**
* This method takes in object with snake_case property names
* and returns new object with camelCase property names
* and returns
a
new object with camelCase property names
*
* Reasoning for this method is to ensure consistent property
* naming conventions across JS code.
*
* This method also supports additional params in `options` object
*
* @param {Object} obj - Object to be converted.
* @param {Object} options - Object containing additional options.
* @param {boolean} options.deep - FLag to allow deep object converting
* @param {Array[]} dropKeys - List of properties to discard while building new object
* @param {Array[]} ignoreKeyNames - List of properties to leave intact (as snake_case) while building new object
*/
export
const
convertObjectPropsToCamelCase
=
(
obj
=
{},
options
=
{})
=>
{
if
(
obj
===
null
)
{
...
...
@@ -625,12 +633,26 @@ export const convertObjectPropsToCamelCase = (obj = {}, options = {}) => {
}
const
initial
=
Array
.
isArray
(
obj
)
?
[]
:
{};
const
{
deep
=
false
,
dropKeys
=
[],
ignoreKeyNames
=
[]
}
=
options
;
return
Object
.
keys
(
obj
).
reduce
((
acc
,
prop
)
=>
{
const
result
=
acc
;
const
val
=
obj
[
prop
];
if
(
options
.
deep
&&
(
isObject
(
val
)
||
Array
.
isArray
(
val
)))
{
// Drop properties from new object if
// there are any mentioned in options
if
(
dropKeys
.
indexOf
(
prop
)
>
-
1
)
{
return
acc
;
}
// Skip converting properties in new object
// if there are any mentioned in options
if
(
ignoreKeyNames
.
indexOf
(
prop
)
>
-
1
)
{
result
[
prop
]
=
obj
[
prop
];
return
acc
;
}
if
(
deep
&&
(
isObject
(
val
)
||
Array
.
isArray
(
val
)))
{
result
[
convertToCamelCase
(
prop
)]
=
convertObjectPropsToCamelCase
(
val
,
options
);
}
else
{
result
[
convertToCamelCase
(
prop
)]
=
obj
[
prop
];
...
...
spec/javascripts/lib/utils/common_utils_spec.js
View file @
322c6da5
...
...
@@ -680,51 +680,131 @@ describe('common_utils', () => {
});
});
describe
(
'
deep: true
'
,
()
=>
{
it
(
'
converts object with child objects
'
,
()
=>
{
const
obj
=
{
snake_key
:
{
child_snake_key
:
'
value
'
,
},
};
expect
(
commonUtils
.
convertObjectPropsToCamelCase
(
obj
,
{
deep
:
true
})).
toEqual
({
snakeKey
:
{
childSnakeKey
:
'
value
'
,
},
});
});
describe
(
'
with options
'
,
()
=>
{
const
objWithoutChildren
=
{
project_name
:
'
GitLab CE
'
,
group_name
:
'
GitLab.org
'
,
license_type
:
'
MIT
'
,
};
it
(
'
converts array with child objects
'
,
()
=>
{
const
arr
=
[
{
child_snake_key
:
'
value
'
,
},
];
expect
(
commonUtils
.
convertObjectPropsToCamelCase
(
arr
,
{
deep
:
true
})).
toEqual
([
{
childSnakeKey
:
'
value
'
,
},
]);
});
const
objWithChildren
=
{
project_name
:
'
GitLab CE
'
,
group_name
:
'
GitLab.org
'
,
license_type
:
'
MIT
'
,
tech_stack
:
{
backend
:
'
Ruby
'
,
frontend_framework
:
'
Vue
'
,
database
:
'
PostgreSQL
'
,
},
};
describe
(
'
when options.deep is true
'
,
()
=>
{
it
(
'
converts object with child objects
'
,
()
=>
{
const
obj
=
{
snake_key
:
{
child_snake_key
:
'
value
'
,
},
};
expect
(
commonUtils
.
convertObjectPropsToCamelCase
(
obj
,
{
deep
:
true
})).
toEqual
({
snakeKey
:
{
childSnakeKey
:
'
value
'
,
},
});
});
it
(
'
converts array with child arrays
'
,
()
=>
{
const
arr
=
[
[
it
(
'
converts array with child objects
'
,
()
=>
{
const
arr
=
[
{
child_snake_key
:
'
value
'
,
},
],
];
];
expect
(
commonUtils
.
convertObjectPropsToCamelCase
(
arr
,
{
deep
:
true
})).
toEqual
([
[
expect
(
commonUtils
.
convertObjectPropsToCamelCase
(
arr
,
{
deep
:
true
})).
toEqual
([
{
childSnakeKey
:
'
value
'
,
},
],
]);
]);
});
it
(
'
converts array with child arrays
'
,
()
=>
{
const
arr
=
[
[
{
child_snake_key
:
'
value
'
,
},
],
];
expect
(
commonUtils
.
convertObjectPropsToCamelCase
(
arr
,
{
deep
:
true
})).
toEqual
([
[
{
childSnakeKey
:
'
value
'
,
},
],
]);
});
});
describe
(
'
when options.dropKeys is provided
'
,
()
=>
{
it
(
'
discards properties mentioned in `dropKeys` array
'
,
()
=>
{
expect
(
commonUtils
.
convertObjectPropsToCamelCase
(
objWithoutChildren
,
{
dropKeys
:
[
'
group_name
'
],
}),
).
toEqual
({
projectName
:
'
GitLab CE
'
,
licenseType
:
'
MIT
'
,
});
});
it
(
'
discards properties mentioned in `dropKeys` array when `deep` is true
'
,
()
=>
{
expect
(
commonUtils
.
convertObjectPropsToCamelCase
(
objWithChildren
,
{
deep
:
true
,
dropKeys
:
[
'
group_name
'
,
'
database
'
],
}),
).
toEqual
({
projectName
:
'
GitLab CE
'
,
licenseType
:
'
MIT
'
,
techStack
:
{
backend
:
'
Ruby
'
,
frontendFramework
:
'
Vue
'
,
},
});
});
});
describe
(
'
when options.ignoreKeyNames is provided
'
,
()
=>
{
it
(
'
leaves properties mentioned in `ignoreKeyNames` array intact
'
,
()
=>
{
expect
(
commonUtils
.
convertObjectPropsToCamelCase
(
objWithoutChildren
,
{
ignoreKeyNames
:
[
'
group_name
'
],
}),
).
toEqual
({
projectName
:
'
GitLab CE
'
,
licenseType
:
'
MIT
'
,
group_name
:
'
GitLab.org
'
,
});
});
it
(
'
leaves properties mentioned in `ignoreKeyNames` array intact when `deep` is true
'
,
()
=>
{
expect
(
commonUtils
.
convertObjectPropsToCamelCase
(
objWithChildren
,
{
deep
:
true
,
ignoreKeyNames
:
[
'
group_name
'
,
'
frontend_framework
'
],
}),
).
toEqual
({
projectName
:
'
GitLab CE
'
,
group_name
:
'
GitLab.org
'
,
licenseType
:
'
MIT
'
,
techStack
:
{
backend
:
'
Ruby
'
,
frontend_framework
:
'
Vue
'
,
database
:
'
PostgreSQL
'
,
},
});
});
});
});
});
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment