@naturalcycles/nodejs-lib 15.133.0 → 15.135.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/fs/fs2.d.ts CHANGED
@@ -25,15 +25,15 @@ declare class FS2 {
25
25
  writeFile(filePath: string, data: string | Uint8Array, opt?: WriteFileOptions): void;
26
26
  writeFileAsync(filePath: string, data: string | Uint8Array, opt?: WriteFileOptions): Promise<void>;
27
27
  writePrettyJson(filePath: string, data: any): void;
28
- writeJson(filePath: string, data: any, opt?: JsonOptions): void;
28
+ writeJson(filePath: string, data: any): void;
29
29
  writePrettyJsonAsync(filePath: string, data: any): Promise<void>;
30
- writeJsonAsync(filePath: string, data: any, opt?: JsonOptions): Promise<void>;
30
+ writeJsonAsync(filePath: string, data: any): Promise<void>;
31
31
  appendFile(filePath: string, data: string | Uint8Array): void;
32
32
  appendFileAsync(filePath: string, data: string | Uint8Array): Promise<void>;
33
33
  outputPrettyJson(filePath: string, data: any): void;
34
- outputJson(filePath: string, data: any, opt?: JsonOptions): void;
34
+ outputJson(filePath: string, data: any): void;
35
35
  outputPrettyJsonAsync(filePath: string, data: any): Promise<void>;
36
- outputJsonAsync(filePath: string, data: any, opt?: JsonOptions): Promise<void>;
36
+ outputJsonAsync(filePath: string, data: any): Promise<void>;
37
37
  outputFile(filePath: string, data: string | Uint8Array): void;
38
38
  outputFileAsync(filePath: string, data: string | Uint8Array): Promise<void>;
39
39
  pathExists(filePath: string): boolean;
@@ -79,7 +79,4 @@ declare class FS2 {
79
79
  createReadStream: typeof fs.createReadStream;
80
80
  }
81
81
  export declare const fs2: FS2;
82
- export interface JsonOptions {
83
- spaces?: number;
84
- }
85
82
  export {};
package/dist/fs/fs2.js CHANGED
@@ -62,17 +62,19 @@ class FS2 {
62
62
  await fsp.writeFile(filePath, data, opt);
63
63
  }
64
64
  writePrettyJson(filePath, data) {
65
- this.writeJson(filePath, data, prettyJsonOptions);
65
+ const str = stringifyPretty(data);
66
+ fs.writeFileSync(filePath, str);
66
67
  }
67
- writeJson(filePath, data, opt) {
68
- const str = stringify(data, opt);
68
+ writeJson(filePath, data) {
69
+ const str = JSON.stringify(data);
69
70
  fs.writeFileSync(filePath, str);
70
71
  }
71
72
  async writePrettyJsonAsync(filePath, data) {
72
- await this.writeJsonAsync(filePath, data, prettyJsonOptions);
73
+ const str = stringifyPretty(data);
74
+ await fsp.writeFile(filePath, str);
73
75
  }
74
- async writeJsonAsync(filePath, data, opt) {
75
- const str = stringify(data, opt);
76
+ async writeJsonAsync(filePath, data) {
77
+ const str = JSON.stringify(data);
76
78
  await fsp.writeFile(filePath, str);
77
79
  }
78
80
  appendFile(filePath, data) {
@@ -82,17 +84,19 @@ class FS2 {
82
84
  await fsp.appendFile(filePath, data);
83
85
  }
84
86
  outputPrettyJson(filePath, data) {
85
- this.outputJson(filePath, data, prettyJsonOptions);
87
+ const str = stringifyPretty(data);
88
+ this.outputFile(filePath, str);
86
89
  }
87
- outputJson(filePath, data, opt) {
88
- const str = stringify(data, opt);
90
+ outputJson(filePath, data) {
91
+ const str = JSON.stringify(data);
89
92
  this.outputFile(filePath, str);
90
93
  }
91
94
  async outputPrettyJsonAsync(filePath, data) {
92
- await this.outputJsonAsync(filePath, data, prettyJsonOptions);
95
+ const str = stringifyPretty(data);
96
+ await this.outputFileAsync(filePath, str);
93
97
  }
94
- async outputJsonAsync(filePath, data, opt) {
95
- const str = stringify(data, opt);
98
+ async outputJsonAsync(filePath, data) {
99
+ const str = JSON.stringify(data);
96
100
  await this.outputFileAsync(filePath, str);
97
101
  }
98
102
  outputFile(filePath, data) {
@@ -276,8 +280,7 @@ class FS2 {
276
280
  createReadStream = fs.createReadStream;
277
281
  }
278
282
  export const fs2 = new FS2();
279
- const prettyJsonOptions = { spaces: 2 };
280
- function stringify(data, opt) {
281
- // If pretty-printing is enabled (spaces) - also add a newline at the end (to match our prettier config)
282
- return JSON.stringify(data, null, opt?.spaces) + (opt?.spaces ? '\n' : '');
283
+ function stringifyPretty(data) {
284
+ // Add a newline at the end (to match our prettier config)
285
+ return JSON.stringify(data, null, 2) + '\n';
283
286
  }
@@ -1,6 +1,6 @@
1
1
  import type { ValidationFunction, ValidationFunctionResult } from '@naturalcycles/js-lib';
2
2
  import type { Set2 } from '@naturalcycles/js-lib/object';
3
- import type { AnyObject, BaseDBEntity, IANATimezone, Inclusiveness, IsoDate, IsoDateTime, IsoMonth, Enum, NumberEnum, StringEnum, StringMap, UnixTimestamp, UnixTimestampMillis } from '@naturalcycles/js-lib/types';
3
+ import type { AnyObject, BaseDBEntity, IANATimezone, Inclusiveness, IsoDate, IsoDateTime, IsoMonth, Enum, NonEmptyArray, NumberEnum, StringEnum, StringMap, UnixTimestamp, UnixTimestampMillis } from '@naturalcycles/js-lib/types';
4
4
  import type { StandardJSONSchemaV1, StandardSchemaV1 } from '@standard-schema/spec';
5
5
  import type { Ajv, ValidateFunction } from 'ajv';
6
6
  import { AjvValidationError } from './ajvValidationError.js';
@@ -408,13 +408,17 @@ export declare class JObjectInfer<PROPS extends Record<string, JSchema<any, any>
408
408
  updated: JNumber<UnixTimestamp, false>;
409
409
  }[K] : K extends keyof PROPS ? PROPS[K] : never; }, Opt>;
410
410
  }
411
- export declare class JArray<OUT, Opt> extends JBuilder<OUT[], Opt> {
411
+ export declare class JArray<OUT, Opt, ARR extends OUT[] = OUT[]> extends JBuilder<ARR, Opt> {
412
412
  constructor(itemsSchema: JSchema<OUT, Opt>);
413
413
  minLength(minItems: number): this;
414
414
  maxLength(maxItems: number): this;
415
415
  length(exactLength: number): this;
416
416
  length(minItems: number, maxItems: number): this;
417
417
  exactLength(length: number): this;
418
+ /**
419
+ * Same as `minLength(1)`, but also narrows the output type to `NonEmptyArray`.
420
+ */
421
+ nonEmpty(): JArray<OUT, Opt, NonEmptyArray<OUT>>;
418
422
  unique(): this;
419
423
  }
420
424
  declare class JSet2Builder<OUT, Opt> extends JBuilder<Set2<OUT>, Opt> {
@@ -942,6 +942,12 @@ export class JArray extends JBuilder {
942
942
  exactLength(length) {
943
943
  return this.minLength(length).maxLength(length);
944
944
  }
945
+ /**
946
+ * Same as `minLength(1)`, but also narrows the output type to `NonEmptyArray`.
947
+ */
948
+ nonEmpty() {
949
+ return this.minLength(1);
950
+ }
945
951
  unique() {
946
952
  return this.cloneAndUpdateSchema({ uniqueItems: true });
947
953
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/nodejs-lib",
3
3
  "type": "module",
4
- "version": "15.133.0",
4
+ "version": "15.135.0",
5
5
  "dependencies": {
6
6
  "@naturalcycles/js-lib": "^15",
7
7
  "@standard-schema/spec": "^1",
@@ -14,7 +14,7 @@
14
14
  "yaml": "^2"
15
15
  },
16
16
  "devDependencies": {
17
- "@naturalcycles/dev-lib": "20.68.0",
17
+ "@naturalcycles/dev-lib": "0.0.0",
18
18
  "typescript": "^7"
19
19
  },
20
20
  "exports": {
package/src/fs/fs2.ts CHANGED
@@ -78,20 +78,22 @@ class FS2 {
78
78
  }
79
79
 
80
80
  writePrettyJson(filePath: string, data: any): void {
81
- this.writeJson(filePath, data, prettyJsonOptions)
81
+ const str = stringifyPretty(data)
82
+ fs.writeFileSync(filePath, str)
82
83
  }
83
84
 
84
- writeJson(filePath: string, data: any, opt?: JsonOptions): void {
85
- const str = stringify(data, opt)
85
+ writeJson(filePath: string, data: any): void {
86
+ const str = JSON.stringify(data)
86
87
  fs.writeFileSync(filePath, str)
87
88
  }
88
89
 
89
90
  async writePrettyJsonAsync(filePath: string, data: any): Promise<void> {
90
- await this.writeJsonAsync(filePath, data, prettyJsonOptions)
91
+ const str = stringifyPretty(data)
92
+ await fsp.writeFile(filePath, str)
91
93
  }
92
94
 
93
- async writeJsonAsync(filePath: string, data: any, opt?: JsonOptions): Promise<void> {
94
- const str = stringify(data, opt)
95
+ async writeJsonAsync(filePath: string, data: any): Promise<void> {
96
+ const str = JSON.stringify(data)
95
97
  await fsp.writeFile(filePath, str)
96
98
  }
97
99
 
@@ -104,20 +106,22 @@ class FS2 {
104
106
  }
105
107
 
106
108
  outputPrettyJson(filePath: string, data: any): void {
107
- this.outputJson(filePath, data, prettyJsonOptions)
109
+ const str = stringifyPretty(data)
110
+ this.outputFile(filePath, str)
108
111
  }
109
112
 
110
- outputJson(filePath: string, data: any, opt?: JsonOptions): void {
111
- const str = stringify(data, opt)
113
+ outputJson(filePath: string, data: any): void {
114
+ const str = JSON.stringify(data)
112
115
  this.outputFile(filePath, str)
113
116
  }
114
117
 
115
118
  async outputPrettyJsonAsync(filePath: string, data: any): Promise<void> {
116
- await this.outputJsonAsync(filePath, data, prettyJsonOptions)
119
+ const str = stringifyPretty(data)
120
+ await this.outputFileAsync(filePath, str)
117
121
  }
118
122
 
119
- async outputJsonAsync(filePath: string, data: any, opt?: JsonOptions): Promise<void> {
120
- const str = stringify(data, opt)
123
+ async outputJsonAsync(filePath: string, data: any): Promise<void> {
124
+ const str = JSON.stringify(data)
121
125
  await this.outputFileAsync(filePath, str)
122
126
  }
123
127
 
@@ -324,13 +328,7 @@ class FS2 {
324
328
 
325
329
  export const fs2 = new FS2()
326
330
 
327
- const prettyJsonOptions: JsonOptions = { spaces: 2 }
328
-
329
- export interface JsonOptions {
330
- spaces?: number
331
- }
332
-
333
- function stringify(data: any, opt?: JsonOptions): string {
334
- // If pretty-printing is enabled (spaces) - also add a newline at the end (to match our prettier config)
335
- return JSON.stringify(data, null, opt?.spaces) + (opt?.spaces ? '\n' : '')
331
+ function stringifyPretty(data: any): string {
332
+ // Add a newline at the end (to match our prettier config)
333
+ return JSON.stringify(data, null, 2) + '\n'
336
334
  }
@@ -20,6 +20,7 @@ import type {
20
20
  IsoDateTime,
21
21
  IsoMonth,
22
22
  Enum,
23
+ NonEmptyArray,
23
24
  NumberEnum,
24
25
  StringEnum,
25
26
  StringMap,
@@ -1290,7 +1291,7 @@ export class JObjectInfer<
1290
1291
  }
1291
1292
  }
1292
1293
 
1293
- export class JArray<OUT, Opt> extends JBuilder<OUT[], Opt> {
1294
+ export class JArray<OUT, Opt, ARR extends OUT[] = OUT[]> extends JBuilder<ARR, Opt> {
1294
1295
  constructor(itemsSchema: JSchema<OUT, Opt>) {
1295
1296
  super({
1296
1297
  type: 'array',
@@ -1317,6 +1318,13 @@ export class JArray<OUT, Opt> extends JBuilder<OUT[], Opt> {
1317
1318
  return this.minLength(length).maxLength(length)
1318
1319
  }
1319
1320
 
1321
+ /**
1322
+ * Same as `minLength(1)`, but also narrows the output type to `NonEmptyArray`.
1323
+ */
1324
+ nonEmpty(): JArray<OUT, Opt, NonEmptyArray<OUT>> {
1325
+ return this.minLength(1) as any as JArray<OUT, Opt, NonEmptyArray<OUT>>
1326
+ }
1327
+
1320
1328
  unique(): this {
1321
1329
  return this.cloneAndUpdateSchema({ uniqueItems: true })
1322
1330
  }