mm_expand 1.8.2 → 1.8.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.js +58 -13
  2. package/package.json +37 -37
  3. package/test.js +48 -16
package/index.js CHANGED
@@ -10,9 +10,10 @@ const {
10
10
  rimrafSync
11
11
  } = require('rimraf');
12
12
  const {
13
- j2xParser,
14
- parse
15
- } = require('fast-xml-parser');
13
+ XMLParser,
14
+ XMLBuilder,
15
+ XMLValidator
16
+ } = require("fast-xml-parser");
16
17
  const {
17
18
  writeFileSync,
18
19
  existsSync,
@@ -372,26 +373,67 @@ function toJson(obj, format) {
372
373
  }
373
374
  }
374
375
 
376
+
377
+
378
+ /**
379
+ * 递归将对象的所有字符串值转换为 CDATA 格式
380
+ * @param {Object} data 原始数据对象
381
+ * @returns 转换后的 CDATA 格式对象
382
+ */
383
+ function convertToCDATA(data) {
384
+ // 处理基本类型
385
+ if (typeof data === "string") {
386
+ return {
387
+ "_cdata": data
388
+ };
389
+ }
390
+
391
+ // 处理数组
392
+ if (Array.isArray(data)) {
393
+ return data.map((item) => convertToCDATA(item));
394
+ }
395
+
396
+ // 处理对象
397
+ if (typeof data === "object" && data !== null) {
398
+ return Object.entries(data).reduce((acc, [key, value]) => {
399
+ acc[key] = convertToCDATA(value);
400
+ return acc;
401
+ }, {});
402
+ }
403
+
404
+ // 其他类型直接返回
405
+ return data;
406
+ }
407
+
375
408
  /**
376
409
  * @description 转为xml字符串
377
410
  * @param {Object} obj 被转换的对象
378
411
  * @param {Boolean} format 是否格式化
379
412
  * @param {Boolean} mode 表示方式, true为属性格式
413
+ * @param {Boolean} cdata 是否将字符串转为cdata形式
380
414
  * @return {String} xml格式字符串
381
415
  */
382
- function toXml(obj, format, mode) {
383
- var param = {
416
+ function toXml(obj, format, mode, cdata = false) {
417
+ var options = {
384
418
  format
385
419
  };
386
420
  if (mode) {
387
- param = {
421
+ options = {
388
422
  format,
389
- attrNodeName: "_attrs",
390
- textNodeName: "#text",
391
- cdataTagName: "_cdata"
423
+ ignoreAttributes: false,
424
+ suppressBooleanAttributes: false,
425
+ suppressUnpairedNode: false,
426
+ processEntities: false // 禁用实体转换
392
427
  }
393
428
  };
394
- return xml = new j2xParser(param).parse(obj);
429
+
430
+ if (cdata) {
431
+ options.cdataPropName = cdata ? "_cdata" : undefined;
432
+ obj = convertToCDATA(Object.assign({}, obj));
433
+ }
434
+
435
+ const builder = new XMLBuilder(options);
436
+ return builder.build(obj);
395
437
  };
396
438
 
397
439
  /**
@@ -442,11 +484,13 @@ function saveJson(obj, file, format) {
442
484
  * @param {String} file 保存文件名
443
485
  * @param {Boolean} format 是否格式化
444
486
  * @param {Boolean} mode 表示方式, true为属性格式
487
+ * @param {Boolean} cdata 是否将字符串转为cdata形式
445
488
  * @return {Boolean} 保存结果
446
489
  */
447
- function saveXml(obj, file, format, mode) {
448
- return file.saveText(toXml(obj, format, mode));
490
+ function saveXml(obj, file, format = true, mode = true, cdata = false) {
491
+ return file.saveText(toXml(obj, format, mode, cdata));
449
492
  };
493
+
450
494
  /**
451
495
  * @description 拷贝对象
452
496
  * @param {Object} obj 被拷贝的对象
@@ -893,7 +937,8 @@ if (typeof($) === "undefined") {
893
937
  */
894
938
  String.prototype.toXml = function() {
895
939
  try {
896
- return parse(this);
940
+ const parser = new XMLParser();
941
+ return parser.parse(this);
897
942
  } catch (e) {
898
943
  // var text = this;
899
944
  // if(text.length > 1000) {
package/package.json CHANGED
@@ -1,38 +1,38 @@
1
1
  {
2
- "name": "mm_expand",
3
- "version": "1.8.2",
4
- "description": "这是超级美眉原型函数拓展模块,更有利于对string、array、object的操作,避免出错,简化业务逻辑。",
5
- "main": "index.js",
6
- "scripts": {
7
- "dev": "nodemon test.js",
8
- "test": "node test.js"
9
- },
10
- "repository": {
11
- "type": "git",
12
- "url": "git+https://github.com/qiuwenwu/mm_expand.git"
13
- },
14
- "keywords": [
15
- "string",
16
- "array",
17
- "object",
18
- "date",
19
- "get",
20
- "set",
21
- "prototype",
22
- "expand",
23
- "$"
24
- ],
25
- "author": "邱文武",
26
- "license": "ISC",
27
- "bugs": {
28
- "url": "https://github.com/qiuwenwu/mm_expand/issues"
29
- },
30
- "homepage": "https://github.com/qiuwenwu/mm_expand#readme",
31
- "dependencies": {
32
- "fast-xml-parser": "^4.5.0",
33
- "json5": "^2.2.3",
34
- "ncp": "^2.0.0",
35
- "pinyinlite": "^1.2.1",
36
- "rimraf": "^6.0.1"
37
- }
38
- }
2
+ "name": "mm_expand",
3
+ "version": "1.8.3",
4
+ "description": "这是超级美眉原型函数拓展模块,更有利于对string、array、object的操作,避免出错,简化业务逻辑。",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "dev": "nodemon test.js",
8
+ "test": "node test.js"
9
+ },
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://gitee.com/qiuwenwu91/mm_expand.git"
13
+ },
14
+ "keywords": [
15
+ "string",
16
+ "array",
17
+ "object",
18
+ "date",
19
+ "get",
20
+ "set",
21
+ "prototype",
22
+ "expand",
23
+ "$"
24
+ ],
25
+ "author": "邱文武",
26
+ "license": "ISC",
27
+ "bugs": {
28
+ "url": "https://gitee.com/qiuwenwu91/mm_expand/issues"
29
+ },
30
+ "homepage": "https://gitee.com/qiuwenwu91/mm_expand#readme",
31
+ "dependencies": {
32
+ "fast-xml-parser": "^5.2.0",
33
+ "json5": "^2.2.3",
34
+ "ncp": "^2.0.0",
35
+ "pinyinlite": "^1.2.1",
36
+ "rimraf": "^6.0.1"
37
+ }
38
+ }
package/test.js CHANGED
@@ -1,24 +1,56 @@
1
1
  require('./index.js');
2
2
 
3
3
  async function test() {
4
- console.log("./test.json".fullname());
5
-
6
- var bl = "./test.json".isFile();
7
- console.log("是文件否?", bl);
8
-
9
- var bl = "./test.json".fullname().isFile();
10
- console.log("是文件否?", bl);
11
-
12
- var bl = "./demo/".isDir();
13
- console.log("是目录否?", bl);
14
-
15
- var bl = "./demo/".fullname().isDir();
16
- console.log("是目录否?", bl);
17
-
18
- // var json = "./test.json".loadJson(__dirname);
19
- // console.log("读取json5", json);
4
+ var user = {
5
+ "name": "张三",
6
+ "age": 24,
7
+ "sex": true
8
+ }
9
+ var xml = $.toXml(user);
10
+ console.log("转为xml\n", xml);
11
+ xml = $.toXml(user, true);
12
+ console.log("转为xml并格式化\n", xml);
13
+ xml = $.toXml(user, true, true, true);
14
+ console.log("字符串转CDATA\n", xml);
15
+ var obj = xml.toXml();
16
+ console.log(obj);
17
+
18
+ var xml = `<name><![CDATA[张三]]></name><age>24</age><sex>true</sex>`
19
+ console.log(xml.toXml());
20
+
21
+ xml = `
22
+ <xml>
23
+ <ToUserName><![CDATA[toUser]]></ToUserName>
24
+ <FromUserName><![CDATA[fromUser]]></FromUserName>
25
+ <CreateTime>12345678</CreateTime>
26
+ <MsgType><![CDATA[text]]></MsgType>
27
+ <Content><![CDATA[Welcome to wechat world!]]></Content>
28
+ </xml>
29
+ `
30
+ var obj = xml.toXml()
31
+ console.log(obj);
32
+ console.log($.toXml(obj, true, true, true));
20
33
  }
21
34
 
35
+ // async function test() {
36
+ // console.log("./test.json".fullname());
37
+
38
+ // var bl = "./test.json".isFile();
39
+ // console.log("是文件否?", bl);
40
+
41
+ // var bl = "./test.json".fullname().isFile();
42
+ // console.log("是文件否?", bl);
43
+
44
+ // var bl = "./demo/".isDir();
45
+ // console.log("是目录否?", bl);
46
+
47
+ // var bl = "./demo/".fullname().isDir();
48
+ // console.log("是目录否?", bl);
49
+
50
+ // var json = "./test.json".loadJson(__dirname);
51
+ // console.log("读取json5", json);
52
+ // }
53
+
22
54
  // async function test() {
23
55
 
24
56
  // // 创建事件监听