未验证 提交 17e343fa 编写于 作者: O openharmony_ci 提交者: Gitee

!15143 修改promisify示例代码,uuid函数描述,增加convertToJSObject接口的错误码捕获

Merge pull request !15143 from jwx1068251/master
...@@ -47,21 +47,27 @@ convertToJSObject(xml: string, options?: ConvertOptions) : Object ...@@ -47,21 +47,27 @@ convertToJSObject(xml: string, options?: ConvertOptions) : Object
**示例:** **示例:**
```js ```js
let xml = try {
'<?xml version="1.0" encoding="utf-8"?>' + let xml =
'<note importance="high" logged="true">' + '<?xml version="1.0" encoding="utf-8"?>' +
' <title>Happy</title>' + '<note importance="high" logged="true">' +
' <todo>Work</todo>' + ' <title>Happy</title>' +
' <todo>Play</todo>' + ' <todo>Work</todo>' +
'</note>'; ' <todo>Play</todo>' +
let conv = new convertxml.ConvertXML() '</note>';
let options = {trim : false, declarationKey:"_declaration", let conv = new convertxml.ConvertXML()
instructionKey : "_instruction", attributesKey : "_attributes", let options = {
textKey : "_text", cdataKey:"_cdata", doctypeKey : "_doctype", trim: false, declarationKey: "_declaration",
commentKey : "_comment", parentKey : "_parent", typeKey : "_type", instructionKey: "_instruction", attributesKey: "_attributes",
nameKey : "_name", elementsKey : "_elements"} textKey: "_text", cdataKey: "_cdata", doctypeKey: "_doctype",
let result = JSON.stringify(conv.convertToJSObject(xml, options)); commentKey: "_comment", parentKey: "_parent", typeKey: "_type",
console.log(result); nameKey: "_name", elementsKey: "_elements"
}
let result = JSON.stringify(conv.convertToJSObject(xml, options));
console.log(result);
} catch (e) {
console.log(e.toString());
}
// 输出(宽泛型) // 输出(宽泛型)
// {"_declaration":{"_attributes":{"version":"1.0","encoding":"utf-8"}},"_elements":[{"_type":"element","_name":"note","_attributes":{"importance":"high","logged":"true"},"_elements":[{"_type":"element","_name":"title","_elements":[{"_type":"text","_text":"Happy"}]},{"_type":"element","_name":"todo","_elements":[{"_type":"text","_text":"Work"}]},{"_type":"element","_name":"todo","_elements":[{"_type":"text","_text":"Play"}]}]}]} // {"_declaration":{"_attributes":{"version":"1.0","encoding":"utf-8"}},"_elements":[{"_type":"element","_name":"note","_attributes":{"importance":"high","logged":"true"},"_elements":[{"_type":"element","_name":"title","_elements":[{"_type":"text","_text":"Happy"}]},{"_type":"element","_name":"todo","_elements":[{"_type":"text","_text":"Work"}]},{"_type":"element","_name":"todo","_elements":[{"_type":"text","_text":"Play"}]}]}]}
``` ```
......
...@@ -26,7 +26,7 @@ format(format: string, ...args: Object[]): string ...@@ -26,7 +26,7 @@ format(format: string, ...args: Object[]): string
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| ------- | -------- | ---- | -------------- | | ------- | -------- | ---- | -------------- |
| format | string | 是 | 式样化字符串。 | | format | string | 是 | 式样化字符串。 |
| ...args | Object[] | 否 | 替换式样化字符串通配符的数据 | | ...args | Object[] | 否 | 替换式样化字符串通配符的数据,此参数缺失时,默认返回第一个参数。 |
**返回值:** **返回值:**
...@@ -69,6 +69,20 @@ let result = util.errnoToString(errnum); ...@@ -69,6 +69,20 @@ let result = util.errnoToString(errnum);
console.log("result = " + result); console.log("result = " + result);
``` ```
**部分错误码及信息示例:**
| 错误码 | 信息 |
| ------ | -------------------------------- |
| -1 | operation not permitted |
| -2 | no such file or directory |
| -3 | no such process |
| -4 | interrupted system call |
| -5 | i/o error |
| -11 | resource temporarily unavailable |
| -12 | not enough memory |
| -13 | permission denied |
| -100 | network is down |
## util.callbackWrapper ## util.callbackWrapper
callbackWrapper(original: Function): (err: Object, value: Object )=&gt;void callbackWrapper(original: Function): (err: Object, value: Object )=&gt;void
...@@ -92,15 +106,14 @@ callbackWrapper(original: Function): (err: Object, value: Object )=&gt;void ...@@ -92,15 +106,14 @@ callbackWrapper(original: Function): (err: Object, value: Object )=&gt;void
**示例:** **示例:**
```js ```js
async function promiseFn() { async function fn() {
return Promise.reject('value'); return 'hello world';
} }
let err = "type err"; let cb = util.callbackWrapper(fn);
let cb = util.callbackWrapper(promiseFn); cb((err, ret) => {
cb((err, ret) => { if (err) throw err;
console.log(err); console.log(ret);
console.log(ret); });
}, err)
``` ```
## util.promisify<sup>9+</sup> ## util.promisify<sup>9+</sup>
...@@ -126,24 +139,30 @@ promisify(original: (err: Object, value: Object) =&gt; void): Function ...@@ -126,24 +139,30 @@ promisify(original: (err: Object, value: Object) =&gt; void): Function
**示例:** **示例:**
```js ```js
function aysnFun(str1, str2) { function fun(num, callback) {
if (typeof str1 === 'object' && typeof str2 === 'object') { if (typeof num === 'number') {
return str2 callback(null, num + 3);
} else { } else {
return str1 callback("type err");
} }
} }
let newPromiseObj = util.promisify(aysnFun);
newPromiseObj({ err: "type error" }, {value:'HelloWorld'}).then(res => { const addCall = util.promisify(fun);
console.log(res); (async () => {
}) try {
let res = await addCall(2);
console.log(res);
} catch (err) {
console.log(err);
}
})();
``` ```
## util.randomUUID<sup>9+</sup> ## util.generateRandomUUID<sup>9+</sup>
randomUUID(entropyCache?: boolean): string generateRandomUUID(entropyCache?: boolean): string
使用加密安全随机数生成器生成随机的RFC 4122版本4的 UUID。 使用加密安全随机数生成器生成随机的RFC 4122版本4的string类型UUID。
**系统能力:** SystemCapability.Utils.Lang **系统能力:** SystemCapability.Utils.Lang
...@@ -162,17 +181,17 @@ randomUUID(entropyCache?: boolean): string ...@@ -162,17 +181,17 @@ randomUUID(entropyCache?: boolean): string
**示例:** **示例:**
```js ```js
let uuid = util.randomUUID(true); let uuid = util.generateRandomUUID(true);
console.log("RFC 4122 Version 4 UUID:" + uuid); console.log("RFC 4122 Version 4 UUID:" + uuid);
// 输出: // 输出:
// RFC 4122 Version 4 UUID:88368f2a-d5db-47d8-a05f-534fab0a0045 // RFC 4122 Version 4 UUID:88368f2a-d5db-47d8-a05f-534fab0a0045
``` ```
## util.randomBinaryUUID<sup>9+</sup> ## util.generateRandomBinaryUUID<sup>9+</sup>
randomBinaryUUID(entropyCache?: boolean): Uint8Array generateRandomBinaryUUID(entropyCache?: boolean): Uint8Array
使用加密安全随机数生成器生成随机的RFC 4122版本4的 UUID。 使用加密安全随机数生成器生成随机的RFC 4122版本4的Uint8Array类型UUID。
**系统能力:** SystemCapability.Utils.Lang **系统能力:** SystemCapability.Utils.Lang
...@@ -191,7 +210,7 @@ randomBinaryUUID(entropyCache?: boolean): Uint8Array ...@@ -191,7 +210,7 @@ randomBinaryUUID(entropyCache?: boolean): Uint8Array
**示例:** **示例:**
```js ```js
let uuid = util.randomBinaryUUID(true); let uuid = util.generateRandomBinaryUUID(true);
console.log(JSON.stringify(uuid)); console.log(JSON.stringify(uuid));
// 输出: // 输出:
// 138,188,43,243,62,254,70,119,130,20,235,222,199,164,140,150 // 138,188,43,243,62,254,70,119,130,20,235,222,199,164,140,150
...@@ -201,7 +220,7 @@ randomBinaryUUID(entropyCache?: boolean): Uint8Array ...@@ -201,7 +220,7 @@ randomBinaryUUID(entropyCache?: boolean): Uint8Array
parseUUID(uuid: string): Uint8Array parseUUID(uuid: string): Uint8Array
从字符串中解析UUID,如RFC 4122版本4中所述。 将generateRandomUUID生成的string类型UUID转换为generateRandomBinaryUUID生成的Uint8Array类型UUID,如RFC 4122版本4中所述。
**系统能力:** SystemCapability.Utils.Lang **系统能力:** SystemCapability.Utils.Lang
...@@ -243,7 +262,7 @@ printf(format: string, ...args: Object[]): string ...@@ -243,7 +262,7 @@ printf(format: string, ...args: Object[]): string
| 参数名 | 类型 | 必填 | 说明 | | 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- | | -------- | -------- | -------- | -------- |
| format | string | 是 | 式样化字符串。 | | format | string | 是 | 式样化字符串。 |
| ...args | Object[] | 否 | 替换式样化字符串通配符的数据。 | | ...args | Object[] | 否 | 替换式样化字符串通配符的数据,此参数缺失时,默认返回第一个参数。 |
**返回值:** **返回值:**
...@@ -361,8 +380,8 @@ create(encoding?: string,options?: { fatal?: boolean; ignoreBOM?: boolean }): Te ...@@ -361,8 +380,8 @@ create(encoding?: string,options?: { fatal?: boolean; ignoreBOM?: boolean }): Te
**示例:** **示例:**
```js ```js
let textDecoder = new util.TextDecoder() let result = util.TextDecoder.create('utf-8', { ignoreBOM : true })
textDecoder.create('utf-8', { ignoreBOM : true }); let retStr = result.encoding
``` ```
### decodeWithStream<sup>9+</sup> ### decodeWithStream<sup>9+</sup>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册