Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
李少辉-开发者
gitlab-foss
提交
39763688
G
gitlab-foss
项目概览
李少辉-开发者
/
gitlab-foss
通知
15
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
gitlab-foss
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
39763688
编写于
2月 07, 2018
作者:
K
Kushal Pandya
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add `convertObjectPropsToCamelCase` helper method
上级
99a5c4b9
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
50 addition
and
0 deletion
+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
未找到文件。
app/assets/javascripts/lib/utils/common_utils.js
浏览文件 @
39763688
import
axios
from
'
./axios_utils
'
;
import
{
getLocationHash
}
from
'
./url_utility
'
;
import
{
convertToCamelCase
}
from
'
./text_utility
'
;
export
const
getPagePath
=
(
index
=
0
)
=>
$
(
'
body
'
).
attr
(
'
data-page
'
).
split
(
'
:
'
)[
index
];
...
...
@@ -395,6 +396,26 @@ export const spriteIcon = (icon, className = '') => {
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
}
`
;
window
.
gl
=
window
.
gl
||
{};
...
...
spec/javascripts/lib/utils/common_utils_spec.js
浏览文件 @
39763688
...
...
@@ -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>
'
);
});
});
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
);
});
});
});
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录