@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 CHANGED
@@ -1,6 +1,5 @@
1
1
  export declare class Cache<T> {
2
2
  file: {
3
- addExt(filepath: string): string;
4
3
  read(): {
5
4
  createdAt: number;
6
5
  value: T;
package/.dist/Fetcher.js CHANGED
@@ -10,7 +10,7 @@ export class Fetcher {
10
10
  constructor(opts = {}) {
11
11
  const defaultOptions = {
12
12
  timeout: 60000,
13
- retries: 3,
13
+ retries: 0,
14
14
  retryDelay: 3000,
15
15
  };
16
16
  this.defaultOptions = merge(defaultOptions, opts);
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(file: string | File);
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
- addExt(filepath: string): string;
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
- addExt(filepath: string): string;
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(file) {
55
- if (file instanceof File) {
56
- const withExt = this.addExt(file.path);
57
- if (withExt === file.path) {
58
- this.file = file;
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
- else {
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
- addExt(filepath) {
80
- return filepath.endsWith('.json') ? filepath : filepath + '.json';
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
- addExt(filepath) {
92
- return filepath.endsWith('.ndjson') ? filepath : filepath + '.ndjson';
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@brianbuie/node-kit",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "license": "ISC",
5
5
  "repository": {
6
6
  "type": "git",
package/src/Fetcher.ts CHANGED
@@ -27,7 +27,7 @@ export class Fetcher {
27
27
  constructor(opts: FetchOptions = {}) {
28
28
  const defaultOptions = {
29
29
  timeout: 60000,
30
- retries: 3,
30
+ retries: 0,
31
31
  retryDelay: 3000,
32
32
  };
33
33
  this.defaultOptions = merge(defaultOptions, opts);
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<typeof thing>();
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(file: string | File) {
68
- if (file instanceof File) {
69
- const withExt = this.addExt(file.path);
70
- if (withExt === file.path) {
71
- this.file = file;
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
- } else {
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
- addExt(filepath: string) {
95
- return filepath.endsWith('.json') ? filepath : filepath + '.json';
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
- addExt(filepath: string) {
110
- return filepath.endsWith('.ndjson') ? filepath : filepath + '.ndjson';
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
- return this.file.lines().map((l) => JSON.parse(l) as T);
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
- append(lines: T | T[]) {
118
- const contents = Array.isArray(lines)
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
  }