v-transform 2.0.2 → 2.0.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 (2) hide show
  1. package/package.json +5 -2
  2. package/src/transform.js +41 -29
package/package.json CHANGED
@@ -4,14 +4,17 @@
4
4
  "vt": "src/index.js"
5
5
  },
6
6
  "type": "module",
7
- "version": "2.0.2",
7
+ "version": "2.0.3",
8
8
  "main": "index.js",
9
9
  "scripts": {
10
10
  "clean": "npx rimraf test",
11
11
  "vt": "node src/index.js",
12
12
  "transform": "node src/index.js transform"
13
13
  },
14
- "repository": "https://github.com/VickScarlet/vTransform.git",
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/VickScarlet/vTransform.git"
17
+ },
15
18
  "author": "Vick Scarlet <scarlet_vick@outlook.com>",
16
19
  "license": "MIT",
17
20
  "dependencies": {
package/src/transform.js CHANGED
@@ -7,37 +7,37 @@ import { dump } from './dump.js';
7
7
  export async function transform(options) {
8
8
  const now = Date.now();
9
9
  const configurations = await load(options);
10
- for(const config of configurations)
10
+ for (const config of configurations)
11
11
  await task(config);
12
12
  console.info(`Transformed in ${Date.now() - now}ms`);
13
13
  }
14
14
 
15
- async function task({files, dest, cwd, type, space}) {
16
- console.info('Transform task config:', {files, dest, cwd, type, space});
15
+ async function task({ files, dest, cwd, type, space }) {
16
+ console.info('Transform task config:', { files, dest, cwd, type, space });
17
17
  const m = new Map();
18
- for(const file of files) {
18
+ for (const file of files) {
19
19
  const dir = path.resolve(dest, path.dirname(file));
20
- for(const {name, data} of (await prepare(path.resolve(cwd, file)))) {
21
- if(name[0] === "#") continue;
20
+ for (const { name, data } of (await prepare(path.resolve(cwd, file)))) {
21
+ if (name[0] === "#") continue;
22
22
  let sheet = name.split('#')[0];
23
23
  sheet = sheet.replace('<arr>', '');
24
- if(sheet[0] === '>') sheet = sheet.substring(1);
24
+ if (sheet[0] === '>') sheet = sheet.substring(1);
25
25
  const keys = sheet.split('.');
26
26
  sheet = path.resolve(dir, keys.shift());
27
- if(!m.has(sheet))
27
+ if (!m.has(sheet))
28
28
  m.set(sheet, new JobData());
29
29
  m.get(sheet).append({
30
30
  keys, data: parser(data)
31
31
  });
32
32
  }
33
33
  }
34
- for(const [sheet, data] of m)
34
+ for (const [sheet, data] of m)
35
35
  await dump(sheet, data.result(), type, space);
36
36
  }
37
37
 
38
38
  class JobData {
39
39
  constructor(data) {
40
- if(data) this.append(data);
40
+ if (data) this.append(data);
41
41
  }
42
42
 
43
43
  #data = [];
@@ -47,28 +47,40 @@ class JobData {
47
47
  }
48
48
 
49
49
  result() {
50
- if(!this.#data.length) return {};
51
- const d = this.#data;
50
+ if (!this.#data.length) return {};
52
51
  let result;
53
- if(Array.isArray(d[0].data)) {
54
- result = [];
55
- for(const {data} of d)
56
- result.push(...Object.values(data));
57
- } else {
58
- result = {};
59
- for(const {keys, data} of d) {
60
- if(!keys.length) {
61
- Object.assign(result, data);
62
- continue;
63
- }
64
- let r = result;
65
- for(const key of keys) {
66
- if(!r[key]) r[key] = {};
67
- r = r[key];
68
- }
69
- Object.assign(r, data);
52
+ for (const { keys, data } of this.#data) {
53
+ if (!keys.length) {
54
+ result = this.#combine(result, data);
55
+ continue;
70
56
  }
57
+ if (!result) result = {};
58
+ let r = result;
59
+ let last = keys.pop();
60
+ for (const key of keys) {
61
+ if (!r[key]) r[key] = {};
62
+ r = r[key];
63
+ }
64
+ r[last] = this.#combine(r[last], data);
65
+ }
66
+ return result;
67
+ }
68
+
69
+ #combine(a, b) {
70
+ if (a == null) return b;
71
+ if (b == null) return a;
72
+ if (Array.isArray(a) && Array.isArray(b)) {
73
+ if (Array.isArray(b)) return a.concat(b);
74
+ a.push(b);
75
+ return a;
71
76
  }
77
+ if (Array.isArray(a))
78
+ return this.#combine(a, Object.values(b));
79
+ if (Array.isArray(b))
80
+ return this.#combine(Object.values(a), b);
81
+ const result = {};
82
+ for (const key in a) result[key] = this.#combine(a[key], b[key]);
83
+ for (const key in b) if (!result[key]) result[key] = b[key];
72
84
  return result;
73
85
  }
74
86
  }