@brianbuie/node-kit 0.3.0 → 0.4.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/.dist/Cache.d.ts +0 -1
- package/.dist/Fetcher.js +1 -1
- package/.dist/File.d.ts +7 -8
- package/.dist/File.js +21 -28
- package/package.json +1 -1
- package/src/Fetcher.ts +1 -1
- package/src/File.test.ts +1 -2
- package/src/File.ts +23 -29
package/.dist/Cache.d.ts
CHANGED
package/.dist/Fetcher.js
CHANGED
package/.dist/File.d.ts
CHANGED
|
@@ -14,26 +14,25 @@ export declare class File {
|
|
|
14
14
|
*/
|
|
15
15
|
lines(): string[];
|
|
16
16
|
static get Adaptor(): typeof FileAdaptor;
|
|
17
|
-
json<T>(): JsonFile<T>;
|
|
17
|
+
json<T>(contents?: T): JsonFile<T>;
|
|
18
18
|
static get json(): typeof JsonFile;
|
|
19
|
-
ndjson<T>(): NdjsonFile<T>;
|
|
19
|
+
ndjson<T>(contents?: T): NdjsonFile<T>;
|
|
20
20
|
static get ndjson(): typeof NdjsonFile;
|
|
21
21
|
}
|
|
22
|
-
declare class FileAdaptor {
|
|
22
|
+
declare class FileAdaptor<T = string> {
|
|
23
23
|
file: File;
|
|
24
|
-
constructor(
|
|
25
|
-
addExt(filepath: string): string;
|
|
24
|
+
constructor(filepath: string, contents?: T);
|
|
26
25
|
get exists(): boolean;
|
|
27
26
|
get path(): string;
|
|
28
27
|
}
|
|
29
28
|
declare class JsonFile<T> extends FileAdaptor {
|
|
30
|
-
|
|
29
|
+
constructor(filepath: string, contents?: T);
|
|
31
30
|
read(): T | undefined;
|
|
32
31
|
write(contents: T): void;
|
|
33
32
|
}
|
|
34
33
|
declare class NdjsonFile<T> extends FileAdaptor {
|
|
35
|
-
|
|
36
|
-
lines(): T[];
|
|
34
|
+
constructor(filepath: string, contents?: T);
|
|
37
35
|
append(lines: T | T[]): void;
|
|
36
|
+
lines(): T[];
|
|
38
37
|
}
|
|
39
38
|
export {};
|
package/.dist/File.js
CHANGED
|
@@ -36,14 +36,14 @@ export class File {
|
|
|
36
36
|
static get Adaptor() {
|
|
37
37
|
return FileAdaptor;
|
|
38
38
|
}
|
|
39
|
-
json() {
|
|
40
|
-
return new JsonFile(this);
|
|
39
|
+
json(contents) {
|
|
40
|
+
return new JsonFile(this.path, contents);
|
|
41
41
|
}
|
|
42
42
|
static get json() {
|
|
43
43
|
return JsonFile;
|
|
44
44
|
}
|
|
45
|
-
ndjson() {
|
|
46
|
-
return new NdjsonFile(this);
|
|
45
|
+
ndjson(contents) {
|
|
46
|
+
return new NdjsonFile(this.path, contents);
|
|
47
47
|
}
|
|
48
48
|
static get ndjson() {
|
|
49
49
|
return NdjsonFile;
|
|
@@ -51,23 +51,15 @@ export class File {
|
|
|
51
51
|
}
|
|
52
52
|
class FileAdaptor {
|
|
53
53
|
file;
|
|
54
|
-
constructor(
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
if (
|
|
58
|
-
|
|
54
|
+
constructor(filepath, contents) {
|
|
55
|
+
this.file = new File(filepath);
|
|
56
|
+
if (contents) {
|
|
57
|
+
if (typeof contents !== 'string') {
|
|
58
|
+
throw new Error('File contents must be a string');
|
|
59
59
|
}
|
|
60
|
-
|
|
61
|
-
this.file = new File(withExt);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
else {
|
|
65
|
-
this.file = new File(this.addExt(file));
|
|
60
|
+
this.file.write(contents);
|
|
66
61
|
}
|
|
67
62
|
}
|
|
68
|
-
addExt(filepath) {
|
|
69
|
-
return filepath;
|
|
70
|
-
}
|
|
71
63
|
get exists() {
|
|
72
64
|
return this.file.exists;
|
|
73
65
|
}
|
|
@@ -76,8 +68,10 @@ class FileAdaptor {
|
|
|
76
68
|
}
|
|
77
69
|
}
|
|
78
70
|
class JsonFile extends FileAdaptor {
|
|
79
|
-
|
|
80
|
-
|
|
71
|
+
constructor(filepath, contents) {
|
|
72
|
+
super(filepath.endsWith('.json') ? filepath : filepath + '.json');
|
|
73
|
+
if (contents)
|
|
74
|
+
this.write(contents);
|
|
81
75
|
}
|
|
82
76
|
read() {
|
|
83
77
|
const contents = this.file.read();
|
|
@@ -88,16 +82,15 @@ class JsonFile extends FileAdaptor {
|
|
|
88
82
|
}
|
|
89
83
|
}
|
|
90
84
|
class NdjsonFile extends FileAdaptor {
|
|
91
|
-
|
|
92
|
-
|
|
85
|
+
constructor(filepath, contents) {
|
|
86
|
+
super(filepath.endsWith('.ndjson') ? filepath : filepath + '.ndjson');
|
|
87
|
+
if (contents)
|
|
88
|
+
this.append(contents);
|
|
89
|
+
}
|
|
90
|
+
append(lines) {
|
|
91
|
+
this.file.append(Array.isArray(lines) ? lines.map((l) => JSON.stringify(snapshot(l))) : JSON.stringify(snapshot(lines)));
|
|
93
92
|
}
|
|
94
93
|
lines() {
|
|
95
94
|
return this.file.lines().map((l) => JSON.parse(l));
|
|
96
95
|
}
|
|
97
|
-
append(lines) {
|
|
98
|
-
const contents = Array.isArray(lines)
|
|
99
|
-
? lines.map((l) => JSON.stringify(snapshot(l)))
|
|
100
|
-
: JSON.stringify(snapshot(lines));
|
|
101
|
-
this.file.append(contents);
|
|
102
|
-
}
|
|
103
96
|
}
|
package/package.json
CHANGED
package/src/Fetcher.ts
CHANGED
package/src/File.test.ts
CHANGED
|
@@ -51,8 +51,7 @@ describe('File.ndjson', () => {
|
|
|
51
51
|
|
|
52
52
|
describe('File.json', () => {
|
|
53
53
|
it('Saves data as json', () => {
|
|
54
|
-
const file = testDir.file('jsonfile-data').json
|
|
55
|
-
file.write(thing);
|
|
54
|
+
const file = testDir.file('jsonfile-data').json(thing);
|
|
56
55
|
assert(isEqual(file.read(), thing));
|
|
57
56
|
file.write(thing);
|
|
58
57
|
assert(isEqual(file.read(), thing));
|
package/src/File.ts
CHANGED
|
@@ -44,16 +44,16 @@ export class File {
|
|
|
44
44
|
return FileAdaptor;
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
json<T>() {
|
|
48
|
-
return new JsonFile<T>(this);
|
|
47
|
+
json<T>(contents?: T) {
|
|
48
|
+
return new JsonFile<T>(this.path, contents);
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
static get json() {
|
|
52
52
|
return JsonFile;
|
|
53
53
|
}
|
|
54
54
|
|
|
55
|
-
ndjson<T>() {
|
|
56
|
-
return new NdjsonFile<T>(this);
|
|
55
|
+
ndjson<T>(contents?: T) {
|
|
56
|
+
return new NdjsonFile<T>(this.path, contents);
|
|
57
57
|
}
|
|
58
58
|
|
|
59
59
|
static get ndjson() {
|
|
@@ -61,26 +61,19 @@ export class File {
|
|
|
61
61
|
}
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
-
class FileAdaptor {
|
|
64
|
+
class FileAdaptor<T = string> {
|
|
65
65
|
file;
|
|
66
66
|
|
|
67
|
-
constructor(
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
if (
|
|
71
|
-
|
|
72
|
-
} else {
|
|
73
|
-
this.file = new File(withExt);
|
|
67
|
+
constructor(filepath: string, contents?: T) {
|
|
68
|
+
this.file = new File(filepath);
|
|
69
|
+
if (contents) {
|
|
70
|
+
if (typeof contents !== 'string') {
|
|
71
|
+
throw new Error('File contents must be a string');
|
|
74
72
|
}
|
|
75
|
-
|
|
76
|
-
this.file = new File(this.addExt(file));
|
|
73
|
+
this.file.write(contents);
|
|
77
74
|
}
|
|
78
75
|
}
|
|
79
76
|
|
|
80
|
-
addExt(filepath: string) {
|
|
81
|
-
return filepath;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
77
|
get exists() {
|
|
85
78
|
return this.file.exists;
|
|
86
79
|
}
|
|
@@ -91,8 +84,9 @@ class FileAdaptor {
|
|
|
91
84
|
}
|
|
92
85
|
|
|
93
86
|
class JsonFile<T> extends FileAdaptor {
|
|
94
|
-
|
|
95
|
-
|
|
87
|
+
constructor(filepath: string, contents?: T) {
|
|
88
|
+
super(filepath.endsWith('.json') ? filepath : filepath + '.json');
|
|
89
|
+
if (contents) this.write(contents);
|
|
96
90
|
}
|
|
97
91
|
|
|
98
92
|
read() {
|
|
@@ -106,18 +100,18 @@ class JsonFile<T> extends FileAdaptor {
|
|
|
106
100
|
}
|
|
107
101
|
|
|
108
102
|
class NdjsonFile<T> extends FileAdaptor {
|
|
109
|
-
|
|
110
|
-
|
|
103
|
+
constructor(filepath: string, contents?: T) {
|
|
104
|
+
super(filepath.endsWith('.ndjson') ? filepath : filepath + '.ndjson');
|
|
105
|
+
if (contents) this.append(contents);
|
|
111
106
|
}
|
|
112
107
|
|
|
113
|
-
lines
|
|
114
|
-
|
|
108
|
+
append(lines: T | T[]) {
|
|
109
|
+
this.file.append(
|
|
110
|
+
Array.isArray(lines) ? lines.map((l) => JSON.stringify(snapshot(l))) : JSON.stringify(snapshot(lines))
|
|
111
|
+
);
|
|
115
112
|
}
|
|
116
113
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
? lines.map((l) => JSON.stringify(snapshot(l)))
|
|
120
|
-
: JSON.stringify(snapshot(lines));
|
|
121
|
-
this.file.append(contents);
|
|
114
|
+
lines() {
|
|
115
|
+
return this.file.lines().map((l) => JSON.parse(l) as T);
|
|
122
116
|
}
|
|
123
117
|
}
|