-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
172 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`createXml 表现正常 1`] = ` | ||
<id> | ||
1 | ||
</id> | ||
<name> | ||
jay | ||
</name> | ||
<close> | ||
false | ||
</close> | ||
<content> | ||
<!--[CDATA[大家好 > kkkdkkdkd ?]]--> | ||
</content> | ||
<desc time="2020"> | ||
我是xxxx | ||
</desc> | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`parseXml 表现正常 1`] = ` | ||
Object { | ||
"xml": Object { | ||
"Content": "你好", | ||
"CreateTime": 12345678, | ||
"FromUserName": "fromUser", | ||
"MsgType": "text", | ||
"ToUserName": "toUser", | ||
}, | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { createXml } from './createXml' | ||
|
||
describe('createXml', () => { | ||
test('表现正常', () => { | ||
expect( | ||
createXml({ | ||
id: 1, | ||
name: 'jay', | ||
close: false, | ||
content: createXml.cdata('大家好 > kkkdkkdkd ?'), | ||
desc: { | ||
...createXml.attr({ | ||
time: '2020', | ||
}), | ||
...createXml.text('我是xxxx'), | ||
}, | ||
}), | ||
).toMatchSnapshot() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { j2xParser as J2XParser, J2xOptionsOptional } from 'fast-xml-parser' | ||
|
||
const attrTagName = '@__attr__@' as const | ||
const textTagName = '@__text__@' as const | ||
const cdataTagName = '@__cdata__@' as const | ||
|
||
/** | ||
* 创建 XML 文本。 | ||
* | ||
* @param data 数据 | ||
* @param options 选项 | ||
*/ | ||
export function createXml(data: any, options?: J2xOptionsOptional): string { | ||
return new J2XParser({ | ||
...options, | ||
attrNodeName: attrTagName, | ||
textNodeName: textTagName, | ||
cdataTagName: cdataTagName, | ||
}).parse(data) | ||
} | ||
|
||
createXml.attr = function <T = any>(value: T): any { | ||
return { [attrTagName]: value } | ||
} | ||
|
||
createXml.text = function <T = any>(value: T): any { | ||
return { [textTagName]: value } | ||
} | ||
|
||
createXml.cdata = function <T = any>(value: T): any { | ||
return { [cdataTagName]: value } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/** | ||
* 第三方工具封装库。 | ||
* | ||
* @packageDocumentation | ||
*/ | ||
|
||
/* istanbul ignore file */ | ||
|
||
// @index(['./**/*.ts', '!./**/*.test.*', '!**/__*'], f => `export * from '${f.path}'`) | ||
export * from './createXml' | ||
export * from './parseXml' | ||
// @endindex |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { dedent } from '../utils' | ||
import { parseXml } from './parseXml' | ||
|
||
describe('parseXml', () => { | ||
test('表现正常', () => { | ||
expect( | ||
parseXml(dedent` | ||
<xml> | ||
<ToUserName><![CDATA[toUser]]></ToUserName> | ||
<FromUserName><![CDATA[fromUser]]></FromUserName> | ||
<CreateTime>12345678</CreateTime> | ||
<MsgType><![CDATA[text]]></MsgType> | ||
<Content><![CDATA[你好]]></Content> | ||
</xml> | ||
`), | ||
).toMatchSnapshot() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { parse, X2jOptionsOptional } from 'fast-xml-parser' | ||
|
||
/** | ||
* 解析 XML 文本。 | ||
* | ||
* @param text XML 文本 | ||
* @param options 选项 | ||
*/ | ||
export function parseXml<T>(text: string, options?: X2jOptionsOptional): T { | ||
return parse(text, options) | ||
} |