@thi.ng/csv 2.0.8 → 2.2.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/CHANGELOG.md +72 -38
- package/README.md +4 -4
- package/package.json +83 -78
- package/transforms.d.ts +55 -0
- package/transforms.js +66 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,70 +1,104 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
## [2.0.8](https://github.com/thi-ng/umbrella/compare/@thi.ng/csv@2.0.7...@thi.ng/csv@2.0.8) (2021-11-10)
|
|
7
|
-
|
|
8
|
-
**Note:** Version bump only for package @thi.ng/csv
|
|
9
|
-
|
|
3
|
+
- **Last updated**: 2021-12-02T00:12:39Z
|
|
4
|
+
- **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
|
|
10
5
|
|
|
6
|
+
All notable changes to this project will be documented in this file.
|
|
7
|
+
See [Conventional Commits](https://conventionalcommits.org/) for commit guidelines.
|
|
11
8
|
|
|
9
|
+
**Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
|
|
10
|
+
and/or version bumps of transitive dependencies.
|
|
12
11
|
|
|
12
|
+
## [2.2.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/csv@2.2.0) (2021-12-02)
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
**Note:** Version bump only for package @thi.ng/csv
|
|
17
|
-
|
|
18
|
-
|
|
14
|
+
#### 🚀 Features
|
|
19
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))
|
|
20
21
|
|
|
22
|
+
## [2.1.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/csv@2.1.0) (2021-11-17)
|
|
21
23
|
|
|
22
|
-
|
|
24
|
+
#### 🚀 Features
|
|
23
25
|
|
|
26
|
+
- Using workspaces for local tools ([bf7a404](https://github.com/thi-ng/umbrella/commit/bf7a404))
|
|
27
|
+
Improving the overall build ergonomics
|
|
28
|
+
- introduced a tools workspaces
|
|
29
|
+
- imported it in all needed packages/examples
|
|
30
|
+
- inclusive project root
|
|
24
31
|
|
|
25
|
-
|
|
32
|
+
#### ♻️ Refactoring
|
|
26
33
|
|
|
27
|
-
|
|
34
|
+
- testrunner to binary ([4ebbbb2](https://github.com/thi-ng/umbrella/commit/4ebbbb2))
|
|
35
|
+
this commit reverts (partly) changes made in:
|
|
36
|
+
ef346d7a8753590dc9094108a3d861a8dbd5dd2c
|
|
37
|
+
overall purpose is better testament ergonomics:
|
|
38
|
+
instead of having to pass NODE_OPTIONS with every invocation
|
|
39
|
+
having a binary to handle this for us.
|
|
28
40
|
|
|
41
|
+
### [2.0.1](https://github.com/thi-ng/umbrella/tree/@thi.ng/csv@2.0.1) (2021-10-13)
|
|
29
42
|
|
|
30
|
-
|
|
43
|
+
#### ♻️ Refactoring
|
|
31
44
|
|
|
32
|
-
|
|
45
|
+
- update imports in all tests/pkgs ([effd591](https://github.com/thi-ng/umbrella/commit/effd591))
|
|
46
|
+
- update imports in all pkgs ([5fa2b6f](https://github.com/thi-ng/umbrella/commit/5fa2b6f))
|
|
47
|
+
- add .js suffix for all relative imports
|
|
33
48
|
|
|
49
|
+
# [2.0.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/csv@2.0.0) (2021-10-12)
|
|
34
50
|
|
|
35
|
-
|
|
51
|
+
#### 🛑 Breaking changes
|
|
36
52
|
|
|
37
|
-
|
|
53
|
+
- major update of ALL pkgs (export maps, ESM only) ([0d1d6ea](https://github.com/thi-ng/umbrella/commit/0d1d6ea))
|
|
54
|
+
- BREAKING CHANGE: discontinue CommonJS & UMD versions
|
|
55
|
+
- only ESM modules will be published from now on
|
|
56
|
+
- CJS obsolete due to ESM support in recent versions of node:
|
|
57
|
+
- i.e. launch NodeJS via:
|
|
58
|
+
- `node --experimental-specifier-resolution=node --experimental-repl-await`
|
|
59
|
+
- in the node REPL use `await import(...)` instead of `require()`
|
|
60
|
+
- UMD obsolete due to widespread browser support for ESM
|
|
61
|
+
Also:
|
|
62
|
+
- normalize/restructure/reorg all package.json files
|
|
63
|
+
- cleanup all build scripts, remove obsolete
|
|
64
|
+
- switch from mocha to [@thi.ng/testament](https://github.com/thi-ng/umbrella/tree/main/packages/testament) for all tests
|
|
38
65
|
|
|
39
|
-
|
|
40
|
-
- CJS obsolete due to ESM support in recent versions of node:
|
|
41
|
-
- i.e. launch NodeJS via:
|
|
42
|
-
- `node --experimental-specifier-resolution=node --experimental-repl-await`
|
|
43
|
-
- in the node REPL use `await import(...)` instead of `require()`
|
|
44
|
-
- UMD obsolete due to widespread browser support for ESM
|
|
66
|
+
#### 🚀 Features
|
|
45
67
|
|
|
46
|
-
|
|
47
|
-
- normalize/restructure/reorg all package.json files
|
|
48
|
-
- cleanup all build scripts, remove obsolete
|
|
49
|
-
- switch from mocha to @thi.ng/testament for all tests
|
|
68
|
+
- add formatCSV(), types, tests ([a2ca1b6](https://github.com/thi-ng/umbrella/commit/a2ca1b6))
|
|
50
69
|
|
|
70
|
+
#### ♻️ Refactoring
|
|
51
71
|
|
|
72
|
+
- update imports (transducers) ([4bdecc9](https://github.com/thi-ng/umbrella/commit/4bdecc9))
|
|
73
|
+
- update imports ([6b102d0](https://github.com/thi-ng/umbrella/commit/6b102d0))
|
|
74
|
+
- update all tests in _all_ pkgs ([8b582bc](https://github.com/thi-ng/umbrella/commit/8b582bc))
|
|
75
|
+
- update all to use [@thi.ng/testament](https://github.com/thi-ng/umbrella/tree/main/packages/testament)
|
|
52
76
|
|
|
77
|
+
### [0.1.2](https://github.com/thi-ng/umbrella/tree/@thi.ng/csv@0.1.2) (2020-12-07)
|
|
53
78
|
|
|
79
|
+
#### ♻️ Refactoring
|
|
54
80
|
|
|
81
|
+
- update type-only imports ([32cfc25](https://github.com/thi-ng/umbrella/commit/32cfc25))
|
|
55
82
|
|
|
56
|
-
##
|
|
83
|
+
## [0.1.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/csv@0.1.0) (2020-11-24)
|
|
57
84
|
|
|
58
|
-
|
|
85
|
+
#### 🚀 Features
|
|
59
86
|
|
|
60
|
-
|
|
87
|
+
- add/update CSVOpts, cell transforms, docs ([282e85c](https://github.com/thi-ng/umbrella/commit/282e85c))
|
|
88
|
+
- allow arrays for `cols` option
|
|
89
|
+
- update initIndex(), add column autonaming fallback
|
|
90
|
+
- refactor parseCSV()
|
|
91
|
+
- rename CoercionFn => CellTransform
|
|
92
|
+
- add upper/lower() transforms
|
|
93
|
+
- add/update tests
|
|
94
|
+
- add coercions, restructure ([93d79ec](https://github.com/thi-ng/umbrella/commit/93d79ec))
|
|
95
|
+
- import as new package ([2b07100](https://github.com/thi-ng/umbrella/commit/2b07100))
|
|
61
96
|
|
|
62
|
-
|
|
97
|
+
#### 🩹 Bug fixes
|
|
63
98
|
|
|
64
|
-
-
|
|
99
|
+
- add quoting/newline support in header fields ([28cac18](https://github.com/thi-ng/umbrella/commit/28cac18))
|
|
100
|
+
- add tests
|
|
65
101
|
|
|
66
|
-
|
|
102
|
+
#### ♻️ Refactoring
|
|
67
103
|
|
|
68
|
-
-
|
|
69
|
-
- **csv:** add/update CSVOpts, cell transforms, docs ([282e85c](https://github.com/thi-ng/umbrella/commit/282e85cf9c1a9aae704d918218f8c143b51a88df))
|
|
70
|
-
- **csv:** import as new package ([2b07100](https://github.com/thi-ng/umbrella/commit/2b07100f27bb9fb1f934901aec7c9fc1fab67fbf))
|
|
104
|
+
- update parseLine() ([4ae49e6](https://github.com/thi-ng/umbrella/commit/4ae49e6))
|
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,84 +1,89 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
2
|
+
"name": "@thi.ng/csv",
|
|
3
|
+
"version": "2.2.0",
|
|
4
|
+
"description": "Customizable, transducer-based CSV parser/object mapper and transformer",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"module": "./index.js",
|
|
7
|
+
"typings": "./index.d.ts",
|
|
8
|
+
"sideEffects": false,
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "https://github.com/thi-ng/umbrella.git"
|
|
12
|
+
},
|
|
13
|
+
"homepage": "https://github.com/thi-ng/umbrella/tree/master/packages/csv#readme",
|
|
14
|
+
"funding": [
|
|
15
|
+
{
|
|
16
|
+
"type": "github",
|
|
17
|
+
"url": "https://github.com/sponsors/postspectacular"
|
|
12
18
|
},
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
"
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
19
|
+
{
|
|
20
|
+
"type": "patreon",
|
|
21
|
+
"url": "https://patreon.com/thing_umbrella"
|
|
22
|
+
}
|
|
23
|
+
],
|
|
24
|
+
"author": "Karsten Schmidt <k+npm@thi.ng>",
|
|
25
|
+
"license": "Apache-2.0",
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "yarn clean && tsc --declaration",
|
|
28
|
+
"clean": "rimraf '*.js' '*.d.ts' '*.map' doc",
|
|
29
|
+
"doc": "typedoc --excludePrivate --excludeInternal --out doc src/index.ts",
|
|
30
|
+
"doc:ae": "mkdir -p .ae/doc .ae/temp && api-extractor run --local --verbose",
|
|
31
|
+
"doc:readme": "yarn doc:stats && tools:readme",
|
|
32
|
+
"doc:stats": "tools:module-stats",
|
|
33
|
+
"pub": "yarn npm publish --access public",
|
|
34
|
+
"test": "testament test"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"@thi.ng/api": "^8.3.2",
|
|
38
|
+
"@thi.ng/checks": "^3.1.2",
|
|
39
|
+
"@thi.ng/strings": "^3.3.0",
|
|
40
|
+
"@thi.ng/transducers": "^8.1.2"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@microsoft/api-extractor": "^7.18.19",
|
|
44
|
+
"@thi.ng/testament": "^0.2.2",
|
|
45
|
+
"rimraf": "^3.0.2",
|
|
46
|
+
"tools": "^0.0.1",
|
|
47
|
+
"typedoc": "^0.22.9",
|
|
48
|
+
"typescript": "^4.5.2"
|
|
49
|
+
},
|
|
50
|
+
"keywords": [
|
|
51
|
+
"csv",
|
|
52
|
+
"fileformat",
|
|
53
|
+
"parser",
|
|
54
|
+
"string",
|
|
55
|
+
"transducer",
|
|
56
|
+
"typescript"
|
|
57
|
+
],
|
|
58
|
+
"publishConfig": {
|
|
59
|
+
"access": "public"
|
|
60
|
+
},
|
|
61
|
+
"engines": {
|
|
62
|
+
"node": ">=12.7"
|
|
63
|
+
},
|
|
64
|
+
"files": [
|
|
65
|
+
"*.js",
|
|
66
|
+
"*.d.ts"
|
|
67
|
+
],
|
|
68
|
+
"exports": {
|
|
69
|
+
".": {
|
|
70
|
+
"import": "./index.js"
|
|
35
71
|
},
|
|
36
|
-
"
|
|
37
|
-
|
|
38
|
-
"@thi.ng/checks": "^3.0.7",
|
|
39
|
-
"@thi.ng/strings": "^3.1.4",
|
|
40
|
-
"@thi.ng/transducers": "^8.0.8"
|
|
72
|
+
"./api": {
|
|
73
|
+
"import": "./api.js"
|
|
41
74
|
},
|
|
42
|
-
"
|
|
43
|
-
|
|
75
|
+
"./format": {
|
|
76
|
+
"import": "./format.js"
|
|
44
77
|
},
|
|
45
|
-
"
|
|
46
|
-
|
|
47
|
-
"fileformat",
|
|
48
|
-
"parser",
|
|
49
|
-
"string",
|
|
50
|
-
"transducer",
|
|
51
|
-
"typescript"
|
|
52
|
-
],
|
|
53
|
-
"publishConfig": {
|
|
54
|
-
"access": "public"
|
|
78
|
+
"./parse": {
|
|
79
|
+
"import": "./parse.js"
|
|
55
80
|
},
|
|
56
|
-
"
|
|
57
|
-
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
"import": "./index.js"
|
|
66
|
-
},
|
|
67
|
-
"./api": {
|
|
68
|
-
"import": "./api.js"
|
|
69
|
-
},
|
|
70
|
-
"./format": {
|
|
71
|
-
"import": "./format.js"
|
|
72
|
-
},
|
|
73
|
-
"./parse": {
|
|
74
|
-
"import": "./parse.js"
|
|
75
|
-
},
|
|
76
|
-
"./transforms": {
|
|
77
|
-
"import": "./transforms.js"
|
|
78
|
-
}
|
|
79
|
-
},
|
|
80
|
-
"thi.ng": {
|
|
81
|
-
"year": 2014
|
|
82
|
-
},
|
|
83
|
-
"gitHead": "5fe52419af63984ebe53032201b2a6174b9cb159"
|
|
84
|
-
}
|
|
81
|
+
"./transforms": {
|
|
82
|
+
"import": "./transforms.js"
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
"thi.ng": {
|
|
86
|
+
"year": 2014
|
|
87
|
+
},
|
|
88
|
+
"gitHead": "32cf1a96854f9bb97aca65ffa05ca862ea377059\n"
|
|
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;
|