Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
xxadev
vscode
提交
c08fb277
V
vscode
项目概览
xxadev
/
vscode
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
V
vscode
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
c08fb277
编写于
5月 19, 2016
作者:
M
Martin Aeschlimann
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
json: Add findNodeAtLocation
上级
73b78b70
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
64 addition
and
1 deletion
+64
-1
src/vs/base/common/json.ts
src/vs/base/common/json.ts
+43
-0
src/vs/base/test/common/json.test.ts
src/vs/base/test/common/json.test.ts
+21
-1
未找到文件。
src/vs/base/common/json.ts
浏览文件 @
c08fb277
...
...
@@ -931,6 +931,49 @@ export function parseTree(text:string, errors: ParseError[] = [], options?: Pars
return
result
;
}
export
function
findNodeAtLocation
(
root
:
Node
,
segments
:
Segment
[])
:
Node
{
let
node
=
root
;
for
(
let
segment
of
segments
)
{
if
(
typeof
segment
===
'
string
'
)
{
if
(
node
.
type
!==
'
object
'
)
{
return
void
0
;
}
let
found
=
false
;
for
(
let
propertyNode
of
node
.
children
)
{
if
(
propertyNode
.
children
[
0
].
value
===
segment
)
{
node
=
propertyNode
.
children
[
1
];
found
=
true
;
break
;
}
}
if
(
!
found
)
{
return
void
0
;
}
}
else
{
let
index
=
<
number
>
segment
;
if
(
node
.
type
!==
'
array
'
||
index
<
0
||
index
>=
node
.
children
.
length
)
{
return
void
0
;
}
node
=
node
.
children
[
index
];
}
}
return
node
;
}
export
function
getNodeValue
(
node
:
Node
)
:
any
{
if
(
node
.
type
===
'
array
'
)
{
return
node
.
children
.
map
(
getNodeValue
);
}
else
if
(
node
.
type
===
'
object
'
)
{
let
obj
=
{};
for
(
let
prop
of
node
.
children
)
{
obj
[
prop
.
children
[
0
].
value
]
=
getNodeValue
(
prop
.
children
[
1
]);
}
return
obj
;
}
return
node
.
value
;
}
/**
* Parses the given text and invokes the visitor functions for each object, array and literal reached.
*/
...
...
src/vs/base/test/common/json.test.ts
浏览文件 @
c08fb277
...
...
@@ -5,7 +5,8 @@
'
use strict
'
;
import
*
as
assert
from
'
assert
'
;
import
{
SyntaxKind
,
createScanner
,
parse
,
getLocation
,
Node
,
ParseError
,
parseTree
,
ParseErrorCode
,
getParseErrorMessage
,
ParseOptions
,
Segment
}
from
'
vs/base/common/json
'
;
import
{
SyntaxKind
,
createScanner
,
parse
,
getLocation
,
Node
,
ParseError
,
parseTree
,
ParseErrorCode
,
getParseErrorMessage
,
ParseOptions
,
Segment
,
findNodeAtLocation
,
getNodeValue
}
from
'
vs/base/common/json
'
;
function
assertKinds
(
text
:
string
,
...
kinds
:
SyntaxKind
[]):
void
{
var
_json
=
createScanner
(
text
);
...
...
@@ -54,6 +55,11 @@ function assertTree(input:string, expected:any) : void {
assert
.
deepEqual
(
actual
,
expected
);
}
function
assertNodeAtLocation
(
input
:
Node
,
segments
:
Segment
[],
expected
:
any
)
{
let
actual
=
findNodeAtLocation
(
input
,
segments
);
assert
.
deepEqual
(
actual
?
getNodeValue
(
actual
)
:
void
0
,
expected
);
}
function
assertLocation
(
input
:
string
,
expectedSegments
:
Segment
[],
expectedNodeType
:
string
,
expectedCompleteProperty
:
boolean
)
:
void
{
var
errors
:
{
error
:
ParseErrorCode
}[]
=
[];
...
...
@@ -304,4 +310,18 @@ suite('JSON', () => {
]}
);
});
test
(
'
tree: find location
'
,
()
=>
{
let
root
=
parseTree
(
'
{ "key1": { "key11": [ "val111", "val112" ] }, "key2": [ { "key21": false, "key22": 221 }, null, [{}] ] }
'
);
assertNodeAtLocation
(
root
,
[
"
key1
"
],
{
key11
:
[
'
val111
'
,
'
val112
'
]});
assertNodeAtLocation
(
root
,
[
"
key1
"
,
"
key11
"
],
[
'
val111
'
,
'
val112
'
]);
assertNodeAtLocation
(
root
,
[
"
key1
"
,
"
key11
"
,
0
],
'
val111
'
);
assertNodeAtLocation
(
root
,
[
"
key1
"
,
"
key11
"
,
1
],
'
val112
'
);
assertNodeAtLocation
(
root
,
[
"
key1
"
,
"
key11
"
,
2
],
void
0
);
assertNodeAtLocation
(
root
,
[
"
key2
"
,
0
,
"
key21
"
],
false
);
assertNodeAtLocation
(
root
,
[
"
key2
"
,
0
,
"
key22
"
],
221
);
assertNodeAtLocation
(
root
,
[
"
key2
"
,
1
],
null
);
assertNodeAtLocation
(
root
,
[
"
key2
"
,
2
],
[{}]);
assertNodeAtLocation
(
root
,
[
"
key2
"
,
2
,
0
],
{});
});
});
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录