@jackens/nnn 2024.4.5 → 2024.4.7
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/nnn.d.ts +12 -0
- package/nnn.js +16 -1
- package/package.json +3 -5
- package/readme.md +45 -1
package/nnn.d.ts
CHANGED
|
@@ -1,3 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A tiny helper for CSV parsing.
|
|
3
|
+
*
|
|
4
|
+
* Options:
|
|
5
|
+
* - `header`: flag indicating that the parsed CSV has a header row (default: `true`)
|
|
6
|
+
* - `separator`: field separator (default: `','`)
|
|
7
|
+
*/
|
|
8
|
+
export declare const csvParse: (text: string, { header, separator }?: {
|
|
9
|
+
header?: boolean | undefined;
|
|
10
|
+
separator?: string | undefined;
|
|
11
|
+
}) => Partial<Array<Partial<Record<PropertyKey, string>>>> | Partial<Array<Partial<Array<string>>>>;
|
|
12
|
+
|
|
1
13
|
/**
|
|
2
14
|
* The type of arguments of the `escapeValues` and `escape` helpers.
|
|
3
15
|
*/
|
package/nnn.js
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
// src/nnn/csvParse.ts
|
|
2
|
+
var csvParse = (text, { header = true, separator = "," } = {}) => {
|
|
3
|
+
const regExp = new RegExp(`${separator}|(?<!")\\s*"((?:[^"]|"")*)"\\s*(?!")`, "g");
|
|
4
|
+
const rows = text.replace(/\r/g, "").replace(/\n+$/, "").replace(/\n|(?<!")("(?:[^"]|"")*")(?!")/g, (_, chunk) => chunk ?? "\r").split("\r").map((line) => line.replace(regExp, (_, chunk) => chunk == null ? "\r" : chunk.replace(/""/g, '"')).split("\r"));
|
|
5
|
+
if (header) {
|
|
6
|
+
const keys = rows.shift();
|
|
7
|
+
return rows.map((row) => keys.reduce((record, key, index) => {
|
|
8
|
+
record[key] = row[index];
|
|
9
|
+
return record;
|
|
10
|
+
}, {}));
|
|
11
|
+
}
|
|
12
|
+
return rows;
|
|
13
|
+
};
|
|
14
|
+
|
|
1
15
|
// src/nnn/escape.ts
|
|
2
16
|
var escapeValues = (escapeMap, values) => values.map((value) => (escapeMap.get(value?.constructor) ?? escapeMap.get(undefined))?.(value) ?? "");
|
|
3
17
|
var escape = (escapeMap, template, ...values) => String.raw(template, ...escapeValues(escapeMap, values));
|
|
@@ -272,5 +286,6 @@ export {
|
|
|
272
286
|
h,
|
|
273
287
|
fixTypography,
|
|
274
288
|
escapeValues,
|
|
275
|
-
escape
|
|
289
|
+
escape,
|
|
290
|
+
csvParse
|
|
276
291
|
};
|
package/package.json
CHANGED
|
@@ -4,13 +4,11 @@
|
|
|
4
4
|
"homepage": "https://jackens.github.io/nnn/doc/",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"CSS-in-JS",
|
|
7
|
-
"
|
|
7
|
+
"CSV",
|
|
8
|
+
"csvParse",
|
|
8
9
|
"DOM",
|
|
9
|
-
"eq",
|
|
10
|
-
"equal",
|
|
11
10
|
"escape",
|
|
12
11
|
"Gantt",
|
|
13
|
-
"graph",
|
|
14
12
|
"h",
|
|
15
13
|
"has",
|
|
16
14
|
"highlight",
|
|
@@ -40,5 +38,5 @@
|
|
|
40
38
|
"types": "nnn.d.ts",
|
|
41
39
|
"name": "@jackens/nnn",
|
|
42
40
|
"type": "module",
|
|
43
|
-
"version": "2024.4.
|
|
41
|
+
"version": "2024.4.7"
|
|
44
42
|
}
|
package/readme.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Jackens’ JavaScript helpers.
|
|
4
4
|
|
|
5
|
-
<sub>Version: <code class="version">2024.4.
|
|
5
|
+
<sub>Version: <code class="version">2024.4.7</code></sub>
|
|
6
6
|
|
|
7
7
|
## Examples
|
|
8
8
|
|
|
@@ -44,6 +44,7 @@ import { «something» } from './node_modules/@jackens/nnn/nnn.js'
|
|
|
44
44
|
- `HArgs1`: The type of arguments of the `h` and `s` helpers.
|
|
45
45
|
- `JcNode`: The type of arguments of the `jc` helper.
|
|
46
46
|
- `JcRoot`: The type of arguments of the `jc` helper.
|
|
47
|
+
- `csvParse`: A tiny helper for CSV parsing.
|
|
47
48
|
- `escape`: A generic helper for escaping `values` by given `escapeMap` (in *TemplateStrings* flavor).
|
|
48
49
|
- `escapeValues`: A generic helper for escaping `values` by given `escapeMap`.
|
|
49
50
|
- `fixTypography`: A helper that implements typographic corrections specific to Polish typography.
|
|
@@ -106,6 +107,49 @@ type JcRoot = Partial<Record<PropertyKey, JcNode>>;
|
|
|
106
107
|
|
|
107
108
|
The type of arguments of the `jc` helper.
|
|
108
109
|
|
|
110
|
+
### csvParse
|
|
111
|
+
|
|
112
|
+
```ts
|
|
113
|
+
const csvParse: (text: string, { header, separator }?: {
|
|
114
|
+
header?: boolean | undefined;
|
|
115
|
+
separator?: string | undefined;
|
|
116
|
+
}) => Partial<Array<Partial<Record<PropertyKey, string>>>> | Partial<Array<Partial<Array<string>>>>;
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
A tiny helper for CSV parsing.
|
|
120
|
+
|
|
121
|
+
Options:
|
|
122
|
+
- `header`: flag indicating that the parsed CSV has a header row (default: `true`)
|
|
123
|
+
- `separator`: field separator (default: `','`)
|
|
124
|
+
|
|
125
|
+
#### Usage Examples
|
|
126
|
+
|
|
127
|
+
```js
|
|
128
|
+
const text = `"aaa
|
|
129
|
+
""aaa""
|
|
130
|
+
aaa",bbb, "ccc,ccc"
|
|
131
|
+
"xxx,xxx", "yyy
|
|
132
|
+
yyy",zzz
|
|
133
|
+
42,"42" , 17
|
|
134
|
+
|
|
135
|
+
`
|
|
136
|
+
expect(csvParse(text, { header: false })).toStrictEqual([
|
|
137
|
+
['aaa\n"aaa"\naaa', 'bbb', 'ccc,ccc'],
|
|
138
|
+
['xxx,xxx', 'yyy\nyyy', 'zzz'],
|
|
139
|
+
['42', '42', ' 17']
|
|
140
|
+
])
|
|
141
|
+
|
|
142
|
+
expect(csvParse(text)).toStrictEqual([{
|
|
143
|
+
'aaa\n"aaa"\naaa': 'xxx,xxx',
|
|
144
|
+
bbb: 'yyy\nyyy',
|
|
145
|
+
'ccc,ccc': 'zzz'
|
|
146
|
+
}, {
|
|
147
|
+
'aaa\n"aaa"\naaa': '42',
|
|
148
|
+
bbb: '42',
|
|
149
|
+
'ccc,ccc': ' 17'
|
|
150
|
+
}])
|
|
151
|
+
```
|
|
152
|
+
|
|
109
153
|
### escape
|
|
110
154
|
|
|
111
155
|
```ts
|