v-transform 3.0.0 → 3.1.0

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": "3.0.0",
7
+ "version": "3.1.0",
8
8
  "main": "index.js",
9
9
  "scripts": {
10
10
  "clean": "npx rimraf test",
package/src/dump.js CHANGED
@@ -15,15 +15,14 @@ function jp(data, space) {
15
15
  return `JSON.parse(\n\t'${json}'\n)`
16
16
  }
17
17
 
18
- function map(raw, pk, space) {
19
- const isArray = !pk || Array.isArray(raw)
20
- if (isArray || typeof raw !== 'object') {
21
- const data = jp(j(raw, space))
22
- return { data: jp(j(raw, space)), type: isArray ? 'array' : 'object' }
18
+ function map(raw, pk, space, enable) {
19
+ if (Array.isArray(raw)) {
20
+ return { data: jp(raw, space), type: 'array' }
23
21
  }
22
+ if (!pk || !enable) return { data: jp(raw, space), type: 'object' }
24
23
  const data = Object.values(raw).map(v => [v[pk], v])
25
24
  console.info(`🔁 [Dump] Object -> Map [${pk}]`)
26
- return { data: `new Map(${jp(j(data, space))})`, type: 'map' }
25
+ return { data: `new Map(${jp(data, space)})`, type: 'map' }
27
26
  }
28
27
 
29
28
  function withType(main, sub, pk) {
@@ -34,28 +33,28 @@ function withType(main, sub, pk) {
34
33
  if (sub === 'any') return `Map<${sub}, ${sub}>`
35
34
  return `Map<${sub}['${pk}'], ${sub}>`
36
35
  case 'object':
37
- return `{ [key: string]: ${sub} }`
36
+ return `Record<${sub}['${pk}'], ${sub}>`
38
37
  default:
39
38
  return sub
40
39
  }
41
40
  }
42
41
 
43
- function jsonify(name, data, space) {
42
+ function jsonify({ name, data, space }) {
44
43
  const { raw, addition, dts } = data
45
44
  const json = j(addition ? { [name]: raw, ...addition } : raw, space)
46
45
  return [json, dts]
47
46
  }
48
47
 
49
- function yamlify(name, data, indent) {
48
+ function yamlify({ name, data, space }) {
50
49
  const { raw, addition, dts } = data
51
- const opt = { indent: indent || undefined }
50
+ const opt = { indent: space || undefined }
52
51
  const yaml = dumpYAML(addition ? { [name]: raw, ...addition } : raw, opt)
53
52
  return [yaml, dts]
54
53
  }
55
54
 
56
- function cjsify(name, data, space, types) {
55
+ function cjsify({ name, data, space, types, toMap }) {
57
56
  const { type, pk, dts, addition } = data
58
- const converted = map(data.raw, pk, space)
57
+ const converted = map(data.raw, pk, space, toMap)
59
58
  const r = []
60
59
  if (types && dts) {
61
60
  r.push(`/** @typedef {import('./${name}').${type}} ${type} */`)
@@ -73,9 +72,9 @@ function cjsify(name, data, space, types) {
73
72
  return [r.join('\n'), dts]
74
73
  }
75
74
 
76
- function esmify(name, data, space, types) {
75
+ function esmify({ name, data, space, types, toMap }) {
77
76
  const { type, pk, dts, addition } = data
78
- const converted = map(data.raw, pk, space)
77
+ const converted = map(data.raw, pk, space, toMap)
79
78
  const rows = []
80
79
  if (types && dts) {
81
80
  rows.push(`/** @typedef {import('./${name}').${type}} ${type} */`)
@@ -91,9 +90,9 @@ function esmify(name, data, space, types) {
91
90
  return [rows.join('\n'), dts]
92
91
  }
93
92
 
94
- function tsify(name, data, space) {
93
+ function tsify({ name, data, space, toMap }) {
95
94
  const { pk, addition } = data
96
- const converted = map(data.raw, pk, space)
95
+ const converted = map(data.raw, pk, space, toMap)
97
96
  const type = withType(converted.type, data.type, pk)
98
97
  const rows = [data.dts]
99
98
  rows.push(`export const ${name} = ${converted.data} as unknown as ${type};`)
@@ -123,7 +122,7 @@ async function write(sheet, data) {
123
122
  await writeFile(sheet, data)
124
123
  }
125
124
 
126
- export async function dump({ sheet, data, type, space, name, ext, types }) {
125
+ export async function dump({ sheet, type, ext, map, types, ...args }) {
127
126
  let e, ify
128
127
  switch (type) {
129
128
  case 'ts':
@@ -152,29 +151,8 @@ export async function dump({ sheet, data, type, space, name, ext, types }) {
152
151
  break
153
152
  }
154
153
 
155
- const [main, dts] = ify(name, data, space, types)
154
+ const [main, dts] = ify({ ...args, types, toMap: map })
156
155
  const result = await write(`${sheet}${ext || e}`, main)
157
156
  if (types && dts) await write(`${sheet}.d.ts`, dts)
158
-
159
- // if (type !== 'ts' && data?.extra?.types) {
160
- // const { types, typeSign } = data.extra
161
- // const cleanTypes = String(types).trim() + '\n'
162
-
163
- // const dtsLines = [
164
- // cleanTypes,
165
- // `export declare const ${name}: ${typeSign};`,
166
- // `export default ${name};`,
167
- // ]
168
-
169
- // if (data.addition) {
170
- // Object.entries(data.addition).forEach(([key, value]) => {
171
- // dtsLines.push(`export declare const ${key}: ${typeof value};`)
172
- // })
173
- // }
174
-
175
- // const dtsContent = dtsLines.join('\n') + '\n'
176
- // await write(`${sheet}.d.ts`, dtsContent)
177
- // }
178
-
179
157
  return result
180
158
  }
package/src/index.js CHANGED
@@ -22,6 +22,7 @@ program
22
22
  .option('-e, --ext <ext>', 'file ext', null)
23
23
  .option('-a, --addition', 'addition version', false)
24
24
  .option('-x, --notypes', 'disable types generation', false)
25
+ .option('-m, --map', 'enable map generation', false)
25
26
  .action((list, options) => {
26
27
  if (!options.dest) options.dest = options.output
27
28
  options.list = list
package/src/loader.js CHANGED
@@ -4,14 +4,15 @@ import path from 'node:path'
4
4
  import { readFile } from 'node:fs/promises'
5
5
 
6
6
  export async function load(args) {
7
- const { config, list, addition, ext } = args
7
+ const { config, list, ...a } = args
8
8
  const version = new Date().toISOString()
9
9
  const type = args.type || 'json'
10
10
  const cwd = args.cwd || process.cwd()
11
11
  const space = Number(args.space) || 0
12
12
  const dest = args.dest || cwd
13
13
  const types = !args.notypes
14
- const def = { cwd, type, space, dest, addition, ext, types }
14
+ const map = args.map || false
15
+ const def = { ...a, cwd, type, space, dest, types, map }
15
16
  const m = (...l) => globit(Object.assign({}, ...l))
16
17
  const cfgs = []
17
18
 
package/src/transform.js CHANGED
@@ -13,8 +13,8 @@ export async function transform(options) {
13
13
  }
14
14
 
15
15
  async function task(options) {
16
- const { files, dest, cwd, type, space, addition, ext, types } = options
17
16
  console.info('Transform task config:', options)
17
+ const { files, dest, cwd, addition, ...opts } = options
18
18
  const m = new Map()
19
19
  for (const file of files) {
20
20
  const dir = path.resolve(dest, path.dirname(file))
@@ -22,8 +22,7 @@ async function task(options) {
22
22
  }
23
23
  for (const [sheet, job] of m) {
24
24
  const data = await job.result(addition)
25
- const d = { sheet, data, type, space, name: job.name, ext, types }
26
- await dump(d)
25
+ await dump({ ...opts, sheet, data, name: job.name })
27
26
  }
28
27
  }
29
28