@0xtorch/csv 0.0.51 → 0.0.53

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 (48) hide show
  1. package/_cjs/parseRowsToActions/allValue.js +8 -0
  2. package/_cjs/parseRowsToActions/allValue.js.map +1 -1
  3. package/_cjs/parseRowsToActions/joinValue.js +8 -0
  4. package/_cjs/parseRowsToActions/joinValue.js.map +1 -1
  5. package/_cjs/parseRowsToActions/matchValue.js +11 -0
  6. package/_cjs/parseRowsToActions/matchValue.js.map +1 -0
  7. package/_cjs/parseRowsToActions/mathValue.js +8 -0
  8. package/_cjs/parseRowsToActions/mathValue.js.map +1 -1
  9. package/_cjs/parseRowsToActions/splitValue.js +15 -1
  10. package/_cjs/parseRowsToActions/splitValue.js.map +1 -1
  11. package/_cjs/schemas/parser.js +19 -2
  12. package/_cjs/schemas/parser.js.map +1 -1
  13. package/_cjs/streamCsvToActions.js +15 -0
  14. package/_cjs/streamCsvToActions.js.map +1 -1
  15. package/_esm/parseRowsToActions/allValue.js +8 -0
  16. package/_esm/parseRowsToActions/allValue.js.map +1 -1
  17. package/_esm/parseRowsToActions/joinValue.js +8 -0
  18. package/_esm/parseRowsToActions/joinValue.js.map +1 -1
  19. package/_esm/parseRowsToActions/matchValue.js +7 -0
  20. package/_esm/parseRowsToActions/matchValue.js.map +1 -0
  21. package/_esm/parseRowsToActions/mathValue.js +8 -0
  22. package/_esm/parseRowsToActions/mathValue.js.map +1 -1
  23. package/_esm/parseRowsToActions/splitValue.js +15 -1
  24. package/_esm/parseRowsToActions/splitValue.js.map +1 -1
  25. package/_esm/schemas/parser.js +21 -1
  26. package/_esm/schemas/parser.js.map +1 -1
  27. package/_esm/streamCsvToActions.js +15 -0
  28. package/_esm/streamCsvToActions.js.map +1 -1
  29. package/_types/parseRowsToActions/allValue.d.ts.map +1 -1
  30. package/_types/parseRowsToActions/joinValue.d.ts.map +1 -1
  31. package/_types/parseRowsToActions/matchValue.d.ts +9 -0
  32. package/_types/parseRowsToActions/matchValue.d.ts.map +1 -0
  33. package/_types/parseRowsToActions/mathValue.d.ts.map +1 -1
  34. package/_types/parseRowsToActions/splitValue.d.ts.map +1 -1
  35. package/_types/schemas/format.d.ts +25467 -1843
  36. package/_types/schemas/format.d.ts.map +1 -1
  37. package/_types/schemas/parser.d.ts +58243 -4338
  38. package/_types/schemas/parser.d.ts.map +1 -1
  39. package/_types/streamCsvToActions.d.ts +2 -0
  40. package/_types/streamCsvToActions.d.ts.map +1 -1
  41. package/package.json +1 -1
  42. package/parseRowsToActions/allValue.ts +8 -0
  43. package/parseRowsToActions/joinValue.ts +8 -0
  44. package/parseRowsToActions/matchValue.ts +18 -0
  45. package/parseRowsToActions/mathValue.ts +8 -0
  46. package/parseRowsToActions/splitValue.ts +17 -1
  47. package/schemas/parser.ts +23 -1
  48. package/streamCsvToActions.ts +17 -0
@@ -0,0 +1,18 @@
1
+ import { stringify } from '@0xtorch/core'
2
+ import type { z } from 'zod'
3
+ import type { matchValueSchema } from '../schemas/parser'
4
+ import type { FormattedRow } from '../types'
5
+ import { getSimpleValue } from './simpleValue'
6
+
7
+ export const getMatchValue = ({
8
+ schema,
9
+ rows,
10
+ service,
11
+ }: {
12
+ schema: z.infer<typeof matchValueSchema>
13
+ rows: readonly FormattedRow[]
14
+ service: string
15
+ }) => {
16
+ const value = getSimpleValue({ schema: schema.value, rows, service })
17
+ return stringify(value).match(new RegExp(schema.pattern))?.[schema.index]
18
+ }
@@ -10,6 +10,7 @@ import type { z } from 'zod'
10
10
  import type { mathValueSchema } from '../schemas/parser'
11
11
  import type { FormattedRow } from '../types'
12
12
  import { getCellValue } from './cellValue'
13
+ import { getMatchValue } from './matchValue'
13
14
  import { getSliceValue } from './sliceValue'
14
15
  import { getSplitValue } from './splitValue'
15
16
 
@@ -105,6 +106,13 @@ const getValue = ({
105
106
  rows,
106
107
  })
107
108
  }
109
+ case 'match': {
110
+ return getMatchValue({
111
+ schema,
112
+ rows,
113
+ service,
114
+ })
115
+ }
108
116
  case 'service': {
109
117
  return service
110
118
  }
@@ -2,6 +2,7 @@ import { stringify } from '@0xtorch/core'
2
2
  import type { z } from 'zod'
3
3
  import type { splitValueSchema } from '../schemas/parser'
4
4
  import type { FormattedRow } from '../types'
5
+ import { getCellValue } from './cellValue'
5
6
  import { getSimpleValue } from './simpleValue'
6
7
 
7
8
  export const getSplitValue = ({
@@ -14,5 +15,20 @@ export const getSplitValue = ({
14
15
  service: string
15
16
  }) => {
16
17
  const value = getSimpleValue({ schema: schema.value, rows, service })
17
- return stringify(value).split(schema.splitter)[schema.index]
18
+ const stringifyValue = stringify(value)
19
+
20
+ if (typeof schema.splitter === 'string') {
21
+ return stringifyValue.split(schema.splitter)[schema.index]
22
+ }
23
+
24
+ switch (schema.splitter.type) {
25
+ case 'cell': {
26
+ const splitter = getCellValue({ schema: schema.splitter, rows })
27
+ return stringifyValue.split(stringify(splitter))[schema.index]
28
+ }
29
+ case 'reg-exp': {
30
+ const splitter = new RegExp(schema.splitter.pattern)
31
+ return stringifyValue.split(splitter)[schema.index]
32
+ }
33
+ }
18
34
  }
package/schemas/parser.ts CHANGED
@@ -22,10 +22,22 @@ export const simpleValueSchema = z.union([
22
22
  serviceValueSchema,
23
23
  ])
24
24
 
25
+ const splitterSchema = z.union([
26
+ // string
27
+ z.string(),
28
+ // reg-exp
29
+ z.object({
30
+ type: z.literal('reg-exp'),
31
+ pattern: z.string(),
32
+ }),
33
+ // cell
34
+ cellValueSchema,
35
+ ])
36
+
25
37
  export const splitValueSchema = z.object({
26
38
  type: z.literal('split'),
27
39
  value: simpleValueSchema,
28
- splitter: z.string(),
40
+ splitter: splitterSchema,
29
41
  index: indexSchema,
30
42
  })
31
43
 
@@ -36,6 +48,13 @@ export const sliceValueSchema = z.object({
36
48
  end: z.number().int().optional(),
37
49
  })
38
50
 
51
+ export const matchValueSchema = z.object({
52
+ type: z.literal('match'),
53
+ value: simpleValueSchema,
54
+ pattern: z.string(),
55
+ index: indexSchema,
56
+ })
57
+
39
58
  export const mathValueSchema = z.object({
40
59
  type: z.literal('math'),
41
60
  values: z
@@ -48,6 +67,7 @@ export const mathValueSchema = z.object({
48
67
  simpleValueSchema,
49
68
  splitValueSchema,
50
69
  sliceValueSchema,
70
+ matchValueSchema,
51
71
  ]),
52
72
  )
53
73
  .min(3),
@@ -61,6 +81,7 @@ export const joinValueSchema = z.object({
61
81
  simpleValueSchema,
62
82
  splitValueSchema,
63
83
  sliceValueSchema,
84
+ matchValueSchema,
64
85
  mathValueSchema,
65
86
  ]),
66
87
  )
@@ -71,6 +92,7 @@ export const allValueSchema = z.union([
71
92
  simpleValueSchema,
72
93
  splitValueSchema,
73
94
  sliceValueSchema,
95
+ matchValueSchema,
74
96
  mathValueSchema,
75
97
  joinValueSchema,
76
98
  ])
@@ -20,6 +20,8 @@ type StreamCsvToActionsReturnTypes = {
20
20
  error?: unknown
21
21
  rowCount: number
22
22
  ignoreRowCount: number
23
+ headerRow?: string[]
24
+ ignoreRows: string[][]
23
25
  }
24
26
 
25
27
  export const streamCsvToActions = async ({
@@ -36,8 +38,11 @@ export const streamCsvToActions = async ({
36
38
  let error: unknown
37
39
  let ignoreRowCount = 0
38
40
  let rowCount = 0
41
+ let headerRow: string[] | undefined
42
+ const ignoreRows: string[][] = []
39
43
  let currentKeyValue: string | undefined
40
44
  let currentRows: FormattedRow[] = []
45
+ let currentRawRows: string[][] = []
41
46
  let actions: Action[] = []
42
47
 
43
48
  let formatIndexRecord: Record<string, number> | undefined
@@ -67,6 +72,7 @@ export const streamCsvToActions = async ({
67
72
  formatIndexRecordTemp[key] = index
68
73
  }
69
74
  formatIndexRecord = formatIndexRecordTemp
75
+ headerRow = result.data
70
76
  return
71
77
  }
72
78
 
@@ -84,12 +90,14 @@ export const streamCsvToActions = async ({
84
90
  }
85
91
  const formattedRow = formatRow(row, format.formatter)
86
92
  if (formattedRow === undefined) {
93
+ ignoreRows.push(result.data)
87
94
  ignoreRowCount += 1
88
95
  return
89
96
  }
90
97
 
91
98
  // parse
92
99
  if (format.parser.key === undefined) {
100
+ currentRawRows = []
93
101
  currentRows = []
94
102
  const rowActions = parseTargetRowsToActions({
95
103
  rows: [formattedRow],
@@ -100,6 +108,7 @@ export const streamCsvToActions = async ({
100
108
  fiats,
101
109
  })
102
110
  if (rowActions.length === 0) {
111
+ ignoreRows.push(result.data)
103
112
  ignoreRowCount += 1
104
113
  }
105
114
  actions.push(...rowActions)
@@ -122,12 +131,15 @@ export const streamCsvToActions = async ({
122
131
  fiats,
123
132
  })
124
133
  if (keyActions.length === 0) {
134
+ ignoreRows.push(...currentRawRows)
125
135
  ignoreRowCount += currentRows.length
126
136
  }
127
137
  actions.push(...keyActions)
138
+ currentRawRows = []
128
139
  currentRows = []
129
140
  }
130
141
  currentKeyValue = keyValue
142
+ currentRawRows.push(result.data)
131
143
  currentRows.push(formattedRow)
132
144
  }
133
145
 
@@ -164,6 +176,8 @@ export const streamCsvToActions = async ({
164
176
  error,
165
177
  rowCount,
166
178
  ignoreRowCount,
179
+ headerRow,
180
+ ignoreRows,
167
181
  }
168
182
  }
169
183
 
@@ -178,6 +192,7 @@ export const streamCsvToActions = async ({
178
192
  fiats,
179
193
  })
180
194
  if (keyActions.length === 0) {
195
+ ignoreRows.push(...currentRawRows)
181
196
  ignoreRowCount += currentRows.length
182
197
  }
183
198
  actions.push(...keyActions)
@@ -198,6 +213,8 @@ export const streamCsvToActions = async ({
198
213
  error,
199
214
  rowCount,
200
215
  ignoreRowCount,
216
+ headerRow,
217
+ ignoreRows,
201
218
  }
202
219
  }
203
220