v-transform 1.0.1 → 1.0.4

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,7 +4,7 @@
4
4
  "vt": "src/index.js"
5
5
  },
6
6
  "type": "module",
7
- "version": "1.0.1",
7
+ "version": "1.0.4",
8
8
  "main": "index.js",
9
9
  "repository": "https://github.com/VickScarlet/vTransform.git",
10
10
  "author": "Vick Scarlet <scarlet_vick@outlook.com>",
package/src/index.js CHANGED
File without changes
package/src/io.js CHANGED
@@ -53,7 +53,7 @@ async function req(config) {
53
53
  job.source = [];
54
54
  for(const p of source)
55
55
  job.source.push(await walk(resolve(dir, p)))
56
- job.source.flat();
56
+ job.source = job.source.flat();
57
57
  } else {
58
58
  job.source = await walk(resolve(dir, source));
59
59
  }
@@ -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,17 @@
1
1
  function parseStruct(head) {
2
2
  const layer = [];
3
3
  const subs = {};
4
+ const json = [];
4
5
 
5
6
  for(const col in head) {
6
- const key = head[col];
7
+ let key = head[col].trim();
7
8
  if(key[0] == '#') continue;
8
- const [first, ...last] = key.split(/[\.:]/);
9
+ if(key[0] == '@') {
10
+ json.push(col);
11
+ key = key.substr(1);
12
+ }
13
+ let [first, ...last] = key.split(/[\.:]/);
14
+ first = first.trim();
9
15
  if(!subs[first]) {
10
16
  layer.push(first);
11
17
  }
@@ -26,6 +32,7 @@ function parseStruct(head) {
26
32
  const struct = {
27
33
  type: 'object',
28
34
  subs: [],
35
+ json
29
36
  };
30
37
 
31
38
  for(const key of layer) {
@@ -74,20 +81,29 @@ function parseStruct(head) {
74
81
  return struct;
75
82
  }
76
83
 
77
- function formatRow({type, key, source, subs}, row, original) {
84
+ function formatRow({type, key, source, subs}, row, original, json) {
78
85
  if(row[0] == '#' || (''+row[0])[0] == '#') return null;
79
86
  if(key && key[0] == '#') key = row[key.substr(1)];
80
87
  switch(type) {
81
88
  case 'value':
82
89
  const value = row[source];
83
90
  if(value != void 0) {
84
- return { key, value };
91
+ try {
92
+ return {
93
+ key,
94
+ value: json.includes(source)
95
+ ? JSON.parse(value)
96
+ : value
97
+ };
98
+ } catch(e) {
99
+ return null;
100
+ }
85
101
  }
86
102
  return null;
87
103
  case 'array':
88
104
  const arr = original?.[key] || [];
89
105
  for(const sub of subs) {
90
- const data = formatRow(sub, row, arr);
106
+ const data = formatRow(sub, row, arr, json);
91
107
  if(data) {
92
108
  arr.push(data.value);
93
109
  }
@@ -100,7 +116,7 @@ function formatRow({type, key, source, subs}, row, original) {
100
116
  const obj = original?.[key] || {};
101
117
  let r = false;
102
118
  for(const sub of subs) {
103
- const data = formatRow(sub, row, obj);
119
+ const data = formatRow(sub, row, obj, json);
104
120
  if(data) {
105
121
  r = true;
106
122
  obj[data.key] = data.value;
@@ -113,19 +129,18 @@ function formatRow({type, key, source, subs}, row, original) {
113
129
  }
114
130
  }
115
131
 
116
- function formatSheet(struct, rawSheet) {
132
+ function formatSheet(struct, rawSheet, json) {
117
133
  let data;
118
134
  if(struct.key == void 0) {
119
- const subs = struct.subs;
120
135
  data = [];
121
136
  for(const row of rawSheet) {
122
- const temp = formatRow(struct, row);
137
+ const temp = formatRow(struct, row, null, json);
123
138
  if(temp) data.push(temp.value);
124
139
  }
125
140
  } else {
126
141
  data = {};
127
142
  for(const row of rawSheet) {
128
- const temp = formatRow(struct, row, data);
143
+ const temp = formatRow(struct, row, data, json);
129
144
  if(temp) data[temp.key] = temp.value;
130
145
  }
131
146
  }
@@ -136,7 +151,7 @@ function formatSheet(struct, rawSheet) {
136
151
  function parser(rawSheet) {
137
152
  const struct = parseStruct(rawSheet.shift());
138
153
  rawSheet.shift();
139
- return formatSheet(struct, rawSheet);
154
+ return formatSheet(struct, rawSheet, struct.json);
140
155
  }
141
156
 
142
157
  export { parser };