v-transform 1.0.2 → 1.0.5

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.
package/package.json CHANGED
@@ -4,13 +4,14 @@
4
4
  "vt": "src/index.js"
5
5
  },
6
6
  "type": "module",
7
- "version": "1.0.2",
7
+ "version": "1.0.5",
8
8
  "main": "index.js",
9
9
  "repository": "https://github.com/VickScarlet/vTransform.git",
10
10
  "author": "Vick Scarlet <scarlet_vick@outlook.com>",
11
11
  "license": "MIT",
12
12
  "dependencies": {
13
13
  "commander": "^8.2.0",
14
+ "js-yaml": "^4.1.0",
14
15
  "xlsx": "^0.17.1"
15
16
  }
16
17
  }
@@ -21,6 +21,16 @@ async function job(source, target, r, w) {
21
21
 
22
22
  if(mergeData) {
23
23
  for(const sheetName in mergeData) {
24
+ const [a, b] = sheetName.split('.');
25
+ if(b) {
26
+ const data = {[b]: mergeData[sheetName]}
27
+ if(!merges[a]) {
28
+ merges[a] = [ data ];
29
+ } else {
30
+ merges[a].push(data);
31
+ }
32
+ continue;
33
+ }
24
34
  if(!merges[sheetName]) {
25
35
  merges[sheetName] = [ mergeData[sheetName] ];
26
36
  } else {
@@ -1,11 +1,19 @@
1
+ import yaml from 'js-yaml';
2
+
1
3
  function parseStruct(head) {
2
4
  const layer = [];
3
5
  const subs = {};
6
+ const json = [];
4
7
 
5
8
  for(const col in head) {
6
- const key = head[col];
9
+ let key = head[col].trim();
7
10
  if(key[0] == '#') continue;
8
- const [first, ...last] = key.split(/[\.:]/);
11
+ if(key[0] == '@') {
12
+ json.push(col);
13
+ key = key.substr(1);
14
+ }
15
+ let [first, ...last] = key.split(/[\.:]/);
16
+ first = first.trim();
9
17
  if(!subs[first]) {
10
18
  layer.push(first);
11
19
  }
@@ -26,6 +34,7 @@ function parseStruct(head) {
26
34
  const struct = {
27
35
  type: 'object',
28
36
  subs: [],
37
+ json
29
38
  };
30
39
 
31
40
  for(const key of layer) {
@@ -74,20 +83,29 @@ function parseStruct(head) {
74
83
  return struct;
75
84
  }
76
85
 
77
- function formatRow({type, key, source, subs}, row, original) {
86
+ function formatRow({type, key, source, subs}, row, original, json) {
78
87
  if(row[0] == '#' || (''+row[0])[0] == '#') return null;
79
88
  if(key && key[0] == '#') key = row[key.substr(1)];
80
89
  switch(type) {
81
90
  case 'value':
82
91
  const value = row[source];
83
92
  if(value != void 0) {
84
- return { key, value };
93
+ try {
94
+ return {
95
+ key,
96
+ value: json.includes(source)
97
+ ? yaml.load(value)
98
+ : value
99
+ };
100
+ } catch(e) {
101
+ return null;
102
+ }
85
103
  }
86
104
  return null;
87
105
  case 'array':
88
106
  const arr = original?.[key] || [];
89
107
  for(const sub of subs) {
90
- const data = formatRow(sub, row, arr);
108
+ const data = formatRow(sub, row, arr, json);
91
109
  if(data) {
92
110
  arr.push(data.value);
93
111
  }
@@ -100,7 +118,7 @@ function formatRow({type, key, source, subs}, row, original) {
100
118
  const obj = original?.[key] || {};
101
119
  let r = false;
102
120
  for(const sub of subs) {
103
- const data = formatRow(sub, row, obj);
121
+ const data = formatRow(sub, row, obj, json);
104
122
  if(data) {
105
123
  r = true;
106
124
  obj[data.key] = data.value;
@@ -113,19 +131,18 @@ function formatRow({type, key, source, subs}, row, original) {
113
131
  }
114
132
  }
115
133
 
116
- function formatSheet(struct, rawSheet) {
134
+ function formatSheet(struct, rawSheet, json) {
117
135
  let data;
118
136
  if(struct.key == void 0) {
119
- const subs = struct.subs;
120
137
  data = [];
121
138
  for(const row of rawSheet) {
122
- const temp = formatRow(struct, row);
139
+ const temp = formatRow(struct, row, null, json);
123
140
  if(temp) data.push(temp.value);
124
141
  }
125
142
  } else {
126
143
  data = {};
127
144
  for(const row of rawSheet) {
128
- const temp = formatRow(struct, row, data);
145
+ const temp = formatRow(struct, row, data, json);
129
146
  if(temp) data[temp.key] = temp.value;
130
147
  }
131
148
  }
@@ -136,7 +153,7 @@ function formatSheet(struct, rawSheet) {
136
153
  function parser(rawSheet) {
137
154
  const struct = parseStruct(rawSheet.shift());
138
155
  rawSheet.shift();
139
- return formatSheet(struct, rawSheet);
156
+ return formatSheet(struct, rawSheet, struct.json);
140
157
  }
141
158
 
142
159
  export { parser };