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
0
Merge Requests
0
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
Léo-Paul Géneau
gitlab-ce
Commits
39763688
Commit
39763688
authored
Feb 07, 2018
by
Kushal Pandya
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add `convertObjectPropsToCamelCase` helper method
parent
99a5c4b9
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
50 additions
and
0 deletions
+50
-0
app/assets/javascripts/lib/utils/common_utils.js
app/assets/javascripts/lib/utils/common_utils.js
+21
-0
spec/javascripts/lib/utils/common_utils_spec.js
spec/javascripts/lib/utils/common_utils_spec.js
+29
-0
No files found.
app/assets/javascripts/lib/utils/common_utils.js
View file @
39763688
import
axios
from
'
./axios_utils
'
;
import
axios
from
'
./axios_utils
'
;
import
{
getLocationHash
}
from
'
./url_utility
'
;
import
{
getLocationHash
}
from
'
./url_utility
'
;
import
{
convertToCamelCase
}
from
'
./text_utility
'
;
export
const
getPagePath
=
(
index
=
0
)
=>
$
(
'
body
'
).
attr
(
'
data-page
'
).
split
(
'
:
'
)[
index
];
export
const
getPagePath
=
(
index
=
0
)
=>
$
(
'
body
'
).
attr
(
'
data-page
'
).
split
(
'
:
'
)[
index
];
...
@@ -395,6 +396,26 @@ export const spriteIcon = (icon, className = '') => {
...
@@ -395,6 +396,26 @@ export const spriteIcon = (icon, className = '') => {
return
`<svg
${
classAttribute
}
><use xlink:href="
${
gon
.
sprite_icons
}
#
${
icon
}
" /></svg>`
;
return
`<svg
${
classAttribute
}
><use xlink:href="
${
gon
.
sprite_icons
}
#
${
icon
}
" /></svg>`
;
};
};
/**
* This method takes in object with snake_case property names
* and returns new object with camelCase property names
*
* Reasoning for this method is to ensure consistent property
* naming conventions across JS code.
*/
export
const
convertObjectPropsToCamelCase
=
(
obj
=
{})
=>
{
if
(
obj
===
null
)
{
return
{};
}
return
Object
.
keys
(
obj
).
reduce
((
acc
,
prop
)
=>
{
const
result
=
acc
;
result
[
convertToCamelCase
(
prop
)]
=
obj
[
prop
];
return
acc
;
},
{});
};
export
const
imagePath
=
imgUrl
=>
`
${
gon
.
asset_host
||
''
}${
gon
.
relative_url_root
||
''
}
/assets/
${
imgUrl
}
`
;
export
const
imagePath
=
imgUrl
=>
`
${
gon
.
asset_host
||
''
}${
gon
.
relative_url_root
||
''
}
/assets/
${
imgUrl
}
`
;
window
.
gl
=
window
.
gl
||
{};
window
.
gl
=
window
.
gl
||
{};
...
...
spec/javascripts/lib/utils/common_utils_spec.js
View file @
39763688
...
@@ -480,4 +480,33 @@ describe('common_utils', () => {
...
@@ -480,4 +480,33 @@ describe('common_utils', () => {
expect
(
commonUtils
.
spriteIcon
(
'
test
'
,
'
fa fa-test
'
)).
toEqual
(
'
<svg class="fa fa-test"><use xlink:href="icons.svg#test" /></svg>
'
);
expect
(
commonUtils
.
spriteIcon
(
'
test
'
,
'
fa fa-test
'
)).
toEqual
(
'
<svg class="fa fa-test"><use xlink:href="icons.svg#test" /></svg>
'
);
});
});
});
});
describe
(
'
convertObjectPropsToCamelCase
'
,
()
=>
{
it
(
'
returns new object with camelCase property names by converting object with snake_case names
'
,
()
=>
{
const
snakeRegEx
=
/
(
_
\w)
/g
;
const
mockObj
=
{
id
:
1
,
group_name
:
'
GitLab.org
'
,
absolute_web_url
:
'
https://gitlab.com/gitlab-org/
'
,
};
const
mappings
=
{
id
:
'
id
'
,
groupName
:
'
group_name
'
,
absoluteWebUrl
:
'
absolute_web_url
'
,
};
const
convertedObj
=
commonUtils
.
convertObjectPropsToCamelCase
(
mockObj
);
Object
.
keys
(
convertedObj
).
forEach
((
prop
)
=>
{
expect
(
snakeRegEx
.
test
(
prop
)).
toBeFalsy
();
expect
(
convertedObj
[
prop
]).
toBe
(
mockObj
[
mappings
[
prop
]]);
});
});
it
(
'
return empty object if method is called with null or undefined
'
,
()
=>
{
expect
(
Object
.
keys
(
commonUtils
.
convertObjectPropsToCamelCase
(
null
)).
length
).
toBe
(
0
);
expect
(
Object
.
keys
(
commonUtils
.
convertObjectPropsToCamelCase
()).
length
).
toBe
(
0
);
expect
(
Object
.
keys
(
commonUtils
.
convertObjectPropsToCamelCase
({})).
length
).
toBe
(
0
);
});
});
});
});
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