@thi.ng/csv 2.1.1 → 2.2.2
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/CHANGELOG.md +11 -1
- package/README.md +4 -4
- package/package.json +10 -10
- package/transforms.d.ts +55 -0
- package/transforms.js +66 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
-
- **Last updated**: 2021-
|
|
3
|
+
- **Last updated**: 2021-12-13T10:26:00Z
|
|
4
4
|
- **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
|
|
5
5
|
|
|
6
6
|
All notable changes to this project will be documented in this file.
|
|
@@ -9,6 +9,16 @@ See [Conventional Commits](https://conventionalcommits.org/) for commit guidelin
|
|
|
9
9
|
**Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
|
|
10
10
|
and/or version bumps of transitive dependencies.
|
|
11
11
|
|
|
12
|
+
## [2.2.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/csv@2.2.0) (2021-12-02)
|
|
13
|
+
|
|
14
|
+
#### 🚀 Features
|
|
15
|
+
|
|
16
|
+
- add more cell transforms, add docs ([23646bf](https://github.com/thi-ng/umbrella/commit/23646bf))
|
|
17
|
+
- add epoch(), date(), url() transforms
|
|
18
|
+
- add formatPercent()
|
|
19
|
+
- update readme
|
|
20
|
+
- add currency formatters ([52b7340](https://github.com/thi-ng/umbrella/commit/52b7340))
|
|
21
|
+
|
|
12
22
|
## [2.1.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/csv@2.1.0) (2021-11-17)
|
|
13
23
|
|
|
14
24
|
#### 🚀 Features
|
package/README.md
CHANGED
|
@@ -51,7 +51,7 @@ for more details/ideas.
|
|
|
51
51
|
### Planned features
|
|
52
52
|
|
|
53
53
|
- [x] Simple CSV row parsing w/o object mapping (`parseCSVSimple()`)
|
|
54
|
-
- [
|
|
54
|
+
- [x] CSV output from structured data
|
|
55
55
|
- [ ] CSVW support (#257)
|
|
56
56
|
- [ ] Integration with thi.ng/egf
|
|
57
57
|
|
|
@@ -78,7 +78,7 @@ node --experimental-repl-await
|
|
|
78
78
|
> const csv = await import("@thi.ng/csv");
|
|
79
79
|
```
|
|
80
80
|
|
|
81
|
-
Package sizes (gzipped, pre-treeshake): ESM: 1.
|
|
81
|
+
Package sizes (gzipped, pre-treeshake): ESM: 1.65 KB
|
|
82
82
|
|
|
83
83
|
## Dependencies
|
|
84
84
|
|
|
@@ -103,8 +103,8 @@ import { parseCSV, upper, float } from "@thi.ng/csv";
|
|
|
103
103
|
all: false,
|
|
104
104
|
cols: {
|
|
105
105
|
"country": { tx: upper },
|
|
106
|
-
"latitude": { alias: "lat", tx: float(
|
|
107
|
-
"longitude": { alias: "lon", tx: float(
|
|
106
|
+
"latitude": { alias: "lat", tx: float() },
|
|
107
|
+
"longitude": { alias: "lon", tx: float() },
|
|
108
108
|
}
|
|
109
109
|
},
|
|
110
110
|
[
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thi.ng/csv",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.2.2",
|
|
4
4
|
"description": "Customizable, transducer-based CSV parser/object mapper and transformer",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./index.js",
|
|
@@ -34,18 +34,18 @@
|
|
|
34
34
|
"test": "testament test"
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@thi.ng/api": "^8.3.
|
|
38
|
-
"@thi.ng/checks": "^3.1.
|
|
39
|
-
"@thi.ng/strings": "^3.
|
|
40
|
-
"@thi.ng/transducers": "^8.
|
|
37
|
+
"@thi.ng/api": "^8.3.3",
|
|
38
|
+
"@thi.ng/checks": "^3.1.3",
|
|
39
|
+
"@thi.ng/strings": "^3.3.1",
|
|
40
|
+
"@thi.ng/transducers": "^8.2.0"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
|
-
"@microsoft/api-extractor": "^7.
|
|
44
|
-
"@thi.ng/testament": "^0.2.
|
|
43
|
+
"@microsoft/api-extractor": "^7.19.2",
|
|
44
|
+
"@thi.ng/testament": "^0.2.3",
|
|
45
45
|
"rimraf": "^3.0.2",
|
|
46
46
|
"tools": "^0.0.1",
|
|
47
|
-
"typedoc": "^0.22.
|
|
48
|
-
"typescript": "^4.5.
|
|
47
|
+
"typedoc": "^0.22.10",
|
|
48
|
+
"typescript": "^4.5.3"
|
|
49
49
|
},
|
|
50
50
|
"keywords": [
|
|
51
51
|
"csv",
|
|
@@ -85,5 +85,5 @@
|
|
|
85
85
|
"thi.ng": {
|
|
86
86
|
"year": 2014
|
|
87
87
|
},
|
|
88
|
-
"gitHead": "
|
|
88
|
+
"gitHead": "1ba92c6b9509e74e509b4c0b875fc380a97bbbc1\n"
|
|
89
89
|
}
|
package/transforms.d.ts
CHANGED
|
@@ -1,9 +1,64 @@
|
|
|
1
1
|
import type { CellTransform } from "./api.js";
|
|
2
|
+
/**
|
|
3
|
+
* Cell parse value transform. Returns uppercased version of given input.
|
|
4
|
+
*
|
|
5
|
+
* @param x
|
|
6
|
+
*/
|
|
2
7
|
export declare const upper: CellTransform;
|
|
8
|
+
/**
|
|
9
|
+
* Cell parse value transform. Returns lowercased version of given input.
|
|
10
|
+
*
|
|
11
|
+
* @param x
|
|
12
|
+
*/
|
|
3
13
|
export declare const lower: CellTransform;
|
|
14
|
+
/**
|
|
15
|
+
* Higher-order cell parse value transform. Attempts to parse cell values as
|
|
16
|
+
* floating point number or returns `defaultVal` if not possible.
|
|
17
|
+
*
|
|
18
|
+
* @param defaultVal
|
|
19
|
+
*/
|
|
4
20
|
export declare const float: (defaultVal?: number) => CellTransform;
|
|
21
|
+
/**
|
|
22
|
+
* Higher-order cell parse value transform. Attempts to parse cell values as
|
|
23
|
+
* integer or returns `defaultVal` if not possible.
|
|
24
|
+
*
|
|
25
|
+
* @param defaultVal
|
|
26
|
+
*/
|
|
5
27
|
export declare const int: (defaultVal?: number) => CellTransform;
|
|
28
|
+
/**
|
|
29
|
+
* Higher-order cell parse value transform. Attempts to parse cell values as
|
|
30
|
+
* hexadecimal integer or returns `defaultVal` if not possible.
|
|
31
|
+
*
|
|
32
|
+
* @param defaultVal
|
|
33
|
+
*/
|
|
6
34
|
export declare const hex: (defaultVal?: number) => CellTransform;
|
|
35
|
+
export declare const percent: CellTransform;
|
|
36
|
+
/**
|
|
37
|
+
* Higher-order cell parse value transform. Attempts to parse cell values as
|
|
38
|
+
* Unix epoch/timestamp or returns `defaultVal` if not possible.
|
|
39
|
+
*
|
|
40
|
+
* @param defaultVal
|
|
41
|
+
*/
|
|
42
|
+
export declare const epoch: (defaultVal?: number) => CellTransform;
|
|
43
|
+
/**
|
|
44
|
+
* Higher-order cell parse value transform. Attempts to parse cell values as JS
|
|
45
|
+
* `Date` instance or returns `defaultVal` if not possible.
|
|
46
|
+
*
|
|
47
|
+
* @param defaultVal
|
|
48
|
+
*/
|
|
49
|
+
export declare const date: (defaultVal?: Date | undefined) => CellTransform;
|
|
50
|
+
/**
|
|
51
|
+
* Cell parse value transform. Attempts to parse cell values as JS `URL`
|
|
52
|
+
* instances.
|
|
53
|
+
*
|
|
54
|
+
* @param x
|
|
55
|
+
*/
|
|
56
|
+
export declare const url: CellTransform;
|
|
7
57
|
export declare const zeroPad: (digits: number) => (x: any, length?: number | undefined) => string;
|
|
8
58
|
export declare const formatFloat: (prec?: number) => (x: number) => string;
|
|
59
|
+
/**
|
|
60
|
+
* Higher order cell value formatter. Takes normalized values and formats them
|
|
61
|
+
* as percentage with given `precision` (number of fractional digits).
|
|
62
|
+
*/
|
|
63
|
+
export declare const formatPercent: (prec?: number | undefined) => import("@thi.ng/strings").Stringer<number>;
|
|
9
64
|
//# sourceMappingURL=transforms.d.ts.map
|
package/transforms.js
CHANGED
|
@@ -1,10 +1,76 @@
|
|
|
1
1
|
import { padLeft } from "@thi.ng/strings/pad-left";
|
|
2
|
+
import { percent as $percent } from "@thi.ng/strings/percent";
|
|
2
3
|
import { maybeParseFloat, maybeParseInt } from "@thi.ng/strings/parse";
|
|
4
|
+
/**
|
|
5
|
+
* Cell parse value transform. Returns uppercased version of given input.
|
|
6
|
+
*
|
|
7
|
+
* @param x
|
|
8
|
+
*/
|
|
3
9
|
export const upper = (x) => x.toUpperCase();
|
|
10
|
+
/**
|
|
11
|
+
* Cell parse value transform. Returns lowercased version of given input.
|
|
12
|
+
*
|
|
13
|
+
* @param x
|
|
14
|
+
*/
|
|
4
15
|
export const lower = (x) => x.toLowerCase();
|
|
16
|
+
/**
|
|
17
|
+
* Higher-order cell parse value transform. Attempts to parse cell values as
|
|
18
|
+
* floating point number or returns `defaultVal` if not possible.
|
|
19
|
+
*
|
|
20
|
+
* @param defaultVal
|
|
21
|
+
*/
|
|
5
22
|
export const float = (defaultVal = 0) => (x) => maybeParseFloat(x, defaultVal);
|
|
23
|
+
/**
|
|
24
|
+
* Higher-order cell parse value transform. Attempts to parse cell values as
|
|
25
|
+
* integer or returns `defaultVal` if not possible.
|
|
26
|
+
*
|
|
27
|
+
* @param defaultVal
|
|
28
|
+
*/
|
|
6
29
|
export const int = (defaultVal = 0) => (x) => maybeParseInt(x, defaultVal, 10);
|
|
30
|
+
/**
|
|
31
|
+
* Higher-order cell parse value transform. Attempts to parse cell values as
|
|
32
|
+
* hexadecimal integer or returns `defaultVal` if not possible.
|
|
33
|
+
*
|
|
34
|
+
* @param defaultVal
|
|
35
|
+
*/
|
|
7
36
|
export const hex = (defaultVal = 0) => (x) => maybeParseInt(x, defaultVal, 16);
|
|
37
|
+
export const percent = (x) => maybeParseFloat(x) * 0.01;
|
|
38
|
+
/**
|
|
39
|
+
* Higher-order cell parse value transform. Attempts to parse cell values as
|
|
40
|
+
* Unix epoch/timestamp or returns `defaultVal` if not possible.
|
|
41
|
+
*
|
|
42
|
+
* @param defaultVal
|
|
43
|
+
*/
|
|
44
|
+
export const epoch = (defaultVal = 0) => (x) => {
|
|
45
|
+
const res = Date.parse(x);
|
|
46
|
+
return isNaN(res) ? defaultVal : res;
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Higher-order cell parse value transform. Attempts to parse cell values as JS
|
|
50
|
+
* `Date` instance or returns `defaultVal` if not possible.
|
|
51
|
+
*
|
|
52
|
+
* @param defaultVal
|
|
53
|
+
*/
|
|
54
|
+
export const date = (defaultVal) => (x) => {
|
|
55
|
+
const epoch = Date.parse(x);
|
|
56
|
+
if (isNaN(epoch))
|
|
57
|
+
return defaultVal;
|
|
58
|
+
const res = new Date();
|
|
59
|
+
res.setTime(epoch);
|
|
60
|
+
return res;
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* Cell parse value transform. Attempts to parse cell values as JS `URL`
|
|
64
|
+
* instances.
|
|
65
|
+
*
|
|
66
|
+
* @param x
|
|
67
|
+
*/
|
|
68
|
+
export const url = (x) => new URL(x);
|
|
8
69
|
// formatters
|
|
9
70
|
export const zeroPad = (digits) => padLeft(digits, "0");
|
|
10
71
|
export const formatFloat = (prec = 2) => (x) => x.toFixed(prec);
|
|
72
|
+
/**
|
|
73
|
+
* Higher order cell value formatter. Takes normalized values and formats them
|
|
74
|
+
* as percentage with given `precision` (number of fractional digits).
|
|
75
|
+
*/
|
|
76
|
+
export const formatPercent = $percent;
|