@brianbuie/node-kit 0.3.0 → 0.5.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/README.md CHANGED
@@ -1,4 +1,6 @@
1
- # Node Kit • ![NPM Version](https://img.shields.io/npm/v/%40brianbuie%2Fnode-kit)
1
+ [![NPM Version](https://img.shields.io/npm/v/%40brianbuie%2Fnode-kit)](https://www.npmjs.com/package/@brianbuie/node-kit)
2
+
3
+ # Node Kit
2
4
 
3
5
  Basic tools for quick node.js projects
4
6
 
@@ -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;
@@ -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);
@@ -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 {};
@@ -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
  }
@@ -23,7 +23,7 @@ export class Log {
23
23
  static #toConsole(entry, color) {
24
24
  if (entry.message)
25
25
  console.log(color(`[${entry.severity}] ${entry.message}`));
26
- entry.details?.forEach(detail => {
26
+ entry.details?.forEach((detail) => {
27
27
  console.log(inspect(detail, { depth: 10, breakLength: 100, compact: true, colors: true }));
28
28
  });
29
29
  }
@@ -46,7 +46,7 @@ export class Log {
46
46
  * Also snapshots special objects (eg Error, Response) to keep props in later JSON.stringify output
47
47
  */
48
48
  static prepare(...input) {
49
- let [first, ...rest] = input.map(snapshot);
49
+ let [first, ...rest] = input.map((i) => snapshot(i));
50
50
  if (typeof first === 'string')
51
51
  return { message: first, details: rest };
52
52
  // @ts-ignore
@@ -73,7 +73,7 @@ export class Log {
73
73
  return this.#log({ severity: 'INFO', color: chalk.white }, ...input);
74
74
  }
75
75
  static debug(...input) {
76
- const debugging = process.argv.some(arg => arg.includes('--debug')) || process.env.DEBUG !== undefined;
76
+ const debugging = process.argv.some((arg) => arg.includes('--debug')) || process.env.DEBUG !== undefined;
77
77
  if (debugging || process.env.NODE_ENV !== 'production') {
78
78
  return this.#log({ severity: 'DEBUG', color: chalk.gray }, ...input);
79
79
  }
@@ -2,4 +2,4 @@
2
2
  * Allows special objects (Error, Headers, Set) to be included in JSON.stringify output
3
3
  * functions are removed
4
4
  */
5
- export declare function snapshot(i: unknown): any;
5
+ export declare function snapshot(i: unknown, max?: number, depth?: number): any;
@@ -3,19 +3,24 @@ import { isObjectLike } from 'lodash-es';
3
3
  * Allows special objects (Error, Headers, Set) to be included in JSON.stringify output
4
4
  * functions are removed
5
5
  */
6
- export function snapshot(i) {
7
- if (Array.isArray(i))
8
- return i.map(snapshot);
6
+ export function snapshot(i, max = 50, depth = 0) {
7
+ if (Array.isArray(i)) {
8
+ if (depth === max)
9
+ return [];
10
+ return i.map((c) => snapshot(c, max, depth + 1));
11
+ }
9
12
  if (typeof i === 'function')
10
13
  return undefined;
11
14
  if (!isObjectLike(i))
12
15
  return i;
16
+ if (depth === max)
17
+ return {};
13
18
  let output = {};
14
19
  // @ts-ignore If it has an 'entries' function, use that for looping (eg. Set, Map, Headers)
15
20
  if (typeof i.entries === 'function') {
16
21
  // @ts-ignore
17
22
  for (let [k, v] of i.entries()) {
18
- output[k] = snapshot(v);
23
+ output[k] = snapshot(v, max, depth + 1);
19
24
  }
20
25
  return output;
21
26
  }
@@ -23,11 +28,11 @@ export function snapshot(i) {
23
28
  // Get Enumerable, inherited properties
24
29
  const obj = i;
25
30
  for (let key in obj) {
26
- output[key] = snapshot(obj[key]);
31
+ output[key] = snapshot(obj[key], max, depth + 1);
27
32
  }
28
33
  // Get Non-enumberable, own properties
29
- Object.getOwnPropertyNames(obj).forEach(key => {
30
- output[key] = snapshot(obj[key]);
34
+ Object.getOwnPropertyNames(obj).forEach((key) => {
35
+ output[key] = snapshot(obj[key], max, depth + 1);
31
36
  });
32
37
  return output;
33
38
  }
package/package.json CHANGED
@@ -1,24 +1,24 @@
1
1
  {
2
2
  "name": "@brianbuie/node-kit",
3
- "version": "0.3.0",
3
+ "version": "0.5.0",
4
4
  "license": "ISC",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/brianbuie/node-kit.git"
8
8
  },
9
9
  "scripts": {
10
- "test": "tsc && node --test \".dist/**/*.test.js\" --quiet",
10
+ "test": "tsc && node --test \"dist/**/*.test.js\" --quiet",
11
11
  "preversion": "npm test",
12
12
  "postversion": "git push --follow-tags"
13
13
  },
14
14
  "type": "module",
15
15
  "exports": {
16
- ".": "./.dist/_index.js"
16
+ ".": "./dist/_index.js"
17
17
  },
18
18
  "files": [
19
19
  "src",
20
- ".dist",
21
- "!.dist/**/*.test.*",
20
+ "dist",
21
+ "!dist/**/*.test.*",
22
22
  "README.md"
23
23
  ],
24
24
  "engines": {
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
  }
package/src/Log.ts CHANGED
@@ -38,7 +38,7 @@ export class Log {
38
38
  */
39
39
  static #toConsole(entry: Entry, color: ChalkInstance) {
40
40
  if (entry.message) console.log(color(`[${entry.severity}] ${entry.message}`));
41
- entry.details?.forEach(detail => {
41
+ entry.details?.forEach((detail) => {
42
42
  console.log(inspect(detail, { depth: 10, breakLength: 100, compact: true, colors: true }));
43
43
  });
44
44
  }
@@ -63,7 +63,7 @@ export class Log {
63
63
  * Also snapshots special objects (eg Error, Response) to keep props in later JSON.stringify output
64
64
  */
65
65
  static prepare(...input: unknown[]): { message?: string; details: unknown[] } {
66
- let [first, ...rest] = input.map(snapshot);
66
+ let [first, ...rest] = input.map((i) => snapshot(i));
67
67
  if (typeof first === 'string') return { message: first, details: rest };
68
68
  // @ts-ignore
69
69
  if (isObjectLike(first) && typeof first['message'] === 'string') {
@@ -94,7 +94,7 @@ export class Log {
94
94
  }
95
95
 
96
96
  static debug(...input: unknown[]) {
97
- const debugging = process.argv.some(arg => arg.includes('--debug')) || process.env.DEBUG !== undefined;
97
+ const debugging = process.argv.some((arg) => arg.includes('--debug')) || process.env.DEBUG !== undefined;
98
98
  if (debugging || process.env.NODE_ENV !== 'production') {
99
99
  return this.#log({ severity: 'DEBUG', color: chalk.gray }, ...input);
100
100
  }
@@ -1,6 +1,7 @@
1
1
  import { describe, it } from 'node:test';
2
2
  import assert from 'node:assert';
3
3
  import { snapshot } from './snapshot.js';
4
+ import { temp } from './Dir.js';
4
5
 
5
6
  describe('snapshot', () => {
6
7
  it('Captures Error details', () => {
@@ -32,4 +33,22 @@ describe('snapshot', () => {
32
33
  const shot = snapshot(test) as Record<string, any>;
33
34
  assert(shot.func === undefined);
34
35
  });
36
+
37
+ it('Handles recursive references', () => {
38
+ type Thing = { id: number; thing?: Thing; things: Thing[] };
39
+ function createThing(id: number, thing?: Thing) {
40
+ const newThing: Thing = { id, thing, things: [] };
41
+ if (thing) {
42
+ thing.things.push(newThing);
43
+ }
44
+ return newThing;
45
+ }
46
+ const t1 = createThing(1);
47
+ const t2 = createThing(2, t1);
48
+ const result = snapshot(t1, 20) as Thing;
49
+ const f1 = temp.file('recursive').json(result);
50
+ const parsed = f1.read();
51
+ const f2 = temp.file('recursive2').json(parsed);
52
+ assert.deepEqual(f1.read(), f2.read());
53
+ });
35
54
  });
package/src/snapshot.ts CHANGED
@@ -4,17 +4,21 @@ import { isObjectLike } from 'lodash-es';
4
4
  * Allows special objects (Error, Headers, Set) to be included in JSON.stringify output
5
5
  * functions are removed
6
6
  */
7
- export function snapshot(i: unknown): any {
8
- if (Array.isArray(i)) return i.map(snapshot);
7
+ export function snapshot(i: unknown, max = 50, depth = 0): any {
8
+ if (Array.isArray(i)) {
9
+ if (depth === max) return [];
10
+ return i.map((c) => snapshot(c, max, depth + 1));
11
+ }
9
12
  if (typeof i === 'function') return undefined;
10
13
  if (!isObjectLike(i)) return i;
11
14
 
15
+ if (depth === max) return {};
12
16
  let output: Record<string, any> = {};
13
17
  // @ts-ignore If it has an 'entries' function, use that for looping (eg. Set, Map, Headers)
14
18
  if (typeof i.entries === 'function') {
15
19
  // @ts-ignore
16
20
  for (let [k, v] of i.entries()) {
17
- output[k] = snapshot(v);
21
+ output[k] = snapshot(v, max, depth + 1);
18
22
  }
19
23
  return output;
20
24
  }
@@ -24,12 +28,12 @@ export function snapshot(i: unknown): any {
24
28
  // Get Enumerable, inherited properties
25
29
  const obj: Record<string, any> = i!;
26
30
  for (let key in obj) {
27
- output[key] = snapshot(obj[key]);
31
+ output[key] = snapshot(obj[key], max, depth + 1);
28
32
  }
29
33
 
30
34
  // Get Non-enumberable, own properties
31
- Object.getOwnPropertyNames(obj).forEach(key => {
32
- output[key] = snapshot(obj[key]);
35
+ Object.getOwnPropertyNames(obj).forEach((key) => {
36
+ output[key] = snapshot(obj[key], max, depth + 1);
33
37
  });
34
38
 
35
39
  return output;
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes