Skip to content

Commit

Permalink
feat(x): 新增 parseXml、createXml
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Dec 21, 2020
1 parent c5dd7b1 commit 1ee40c7
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 30 deletions.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
"./validator": {
"require": "./_cjs/validator/index.js",
"import": "./validator/index.js"
},
"./x": {
"require": "./_cjs/x/index.js",
"import": "./x/index.js"
}
},
"main": "_cjs/utils/index.js",
Expand Down Expand Up @@ -92,6 +96,7 @@
"dependencies": {
"@babel/runtime": "^7.12.5",
"date-fns": "^2.16.1",
"fast-xml-parser": "^3.17.5",
"lodash-uni": "^1.1.0",
"miniprogram-api-typings": "^3.2.0",
"react-use": "^15.3.4",
Expand Down
72 changes: 42 additions & 30 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions src/x/__snapshots__/createXml.test.ts.snap
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>
`;
13 changes: 13 additions & 0 deletions src/x/__snapshots__/parseXml.test.ts.snap
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",
},
}
`;
20 changes: 20 additions & 0 deletions src/x/createXml.test.ts
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()
})
})
32 changes: 32 additions & 0 deletions src/x/createXml.ts
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 }
}
12 changes: 12 additions & 0 deletions src/x/index.ts
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
18 changes: 18 additions & 0 deletions src/x/parseXml.test.ts
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()
})
})
11 changes: 11 additions & 0 deletions src/x/parseXml.ts
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)
}

0 comments on commit 1ee40c7

Please sign in to comment.