@cntwg/file-helper 0.0.2-b → 0.0.3

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 CHANGED
@@ -1,3 +1,10 @@
1
+ #### *v0.0.3*
2
+
3
+ Pre-release version.
4
+
5
+ > - update `file-helper.md`;
6
+ > - some fixes.
7
+
1
8
  #### *v0.0.1*
2
9
 
3
10
  Pre-release version.
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2024-2025 Yuri Grachev
3
+ Copyright (c) 2024-2026 Yuri Grachev
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
6
 
@@ -1,6 +1,6 @@
1
- >|***rev.*:**|0.0.1|
1
+ >|***rev.*:**|0.0.2|
2
2
  >|:---|---:|
3
- >|date:|2025-04-08|
3
+ >|date:|2026-01-05|
4
4
 
5
5
  ## Introduction
6
6
 
@@ -49,7 +49,7 @@ Checks an error code of a fs ops and sets a descriptor if succeed.
49
49
  | parameter name | value type | default value | description |
50
50
  |:---|---|---:|:---|
51
51
  | `descr` | `fsoDescr` | --- | event descriptor |
52
- | `err` | `Error` | --- | error event |
52
+ | `err` | `object` | --- | error event |
53
53
 
54
54
  The returned value is an `object` that contains a properties listed in a table below:
55
55
 
@@ -109,7 +109,7 @@ Saves a content to the file located by a given path.
109
109
  ### Module functions (*async*)
110
110
 
111
111
  <a name="loadFromFile"></a>
112
- #### **loadFromFile(src)** => `Promise<fsoDescr, Error>`
112
+ #### **loadFromFile(src)** => `Promise<fsoDescr>`
113
113
 
114
114
  Loads a content from the file located by a given path.
115
115
 
@@ -117,8 +117,12 @@ Loads a content from the file located by a given path.
117
117
  |:---|---|---:|:---|
118
118
  | `src` | `string` | --- | a path to some file |
119
119
 
120
+ ##### ***exceptions***
121
+
122
+ The function throws an error.
123
+
120
124
  <a name="saveToFile"></a>
121
- #### **saveToFile(src, content, [opt])** => `Promise<fsoDescr, Error>`
125
+ #### **saveToFile(src, content, [opt])** => `Promise<fsoDescr>`
122
126
 
123
127
  Saves a content to the file located by a given path.
124
128
 
@@ -129,7 +133,7 @@ Saves a content to the file located by a given path.
129
133
  | `opt` | `any` | --- | <*reserved*> |
130
134
 
131
135
  <a name="loadJSONFromFile"></a>
132
- #### **loadJSONFromFile(src, [opt])** => `Promise<object, Error>`
136
+ #### **loadJSONFromFile(src, [opt])** => `Promise<object>`
133
137
 
134
138
  Loads a content in JSON-format from the file located by a given path.
135
139
 
@@ -145,8 +149,12 @@ The returned value is an `object` containing a properties listed in a table belo
145
149
  | `descr` | `fsoDescr` | ops descriptor |
146
150
  | `obj` | `any` | loaded content |
147
151
 
152
+ ##### ***exceptions***
153
+
154
+ The function throws an error.
155
+
148
156
  <a name="saveJSONToFile"></a>
149
- #### **saveJSONToFile(src, content, [opt])** => `Promise<fsoDescr, Error>`
157
+ #### **saveJSONToFile(src, content, [opt])** => `Promise<fsoDescr>`
150
158
 
151
159
  Saves a content to the file located by a given path.
152
160
 
package/index.d.ts CHANGED
@@ -2,20 +2,15 @@ import {
2
2
  checkFsError,
3
3
  loadFromFile, loadFromFileSync,
4
4
  saveToFile, saveToFileSync,
5
- } from './lib/base-func';
6
-
7
- import type {
8
- fsoDescr, VCOR_evalerrfs,
9
- } from './lib/base-func';
5
+ type fsoDescr,
6
+ type VCOR_evalerrfs,
7
+ } from "./lib/base-func";
10
8
 
11
9
  import {
12
10
  loadJSONFromFile, loadJSONFromFileSync,
13
11
  saveJSONToFile, saveJSONToFileSync,
14
- } from './lib/json-func';
15
-
16
- import {
17
- RVAL_loadjsonff,
18
- } from './lib/json-func';
12
+ type RVAL_loadjsonff,
13
+ } from "./lib/json-func";
19
14
 
20
15
  export {
21
16
  checkFsError,
@@ -30,6 +25,7 @@ export {
30
25
  };
31
26
 
32
27
  export type {
33
- fsoDescr, VCOR_evalerrfs,
28
+ fsoDescr,
29
+ VCOR_evalerrfs,
34
30
  RVAL_loadjsonff,
35
31
  };
package/index.js CHANGED
@@ -1,4 +1,4 @@
1
- // [v0.1.001-20250408]
1
+ // [v0.1.003-20260105]
2
2
 
3
3
  // === module init block ===
4
4
 
@@ -6,14 +6,16 @@ const {
6
6
  loadFromFile, loadFromFileSync,
7
7
  saveToFile, saveToFileSync,
8
8
  checkFsError,
9
- } = require('#lib/base-func.js');
9
+ // * import types definitions *
10
+ fsoDescr,
11
+ } = require('./lib/base-func');
10
12
 
11
13
  const {
12
14
  loadJSONFromFile, loadJSONFromFileSync,
13
15
  saveJSONToFile, saveJSONToFileSync,
14
- } = require('#lib/json-func.js');
16
+ } = require('./lib/json-func');
15
17
 
16
- // === module extra block (helper functions) ===
18
+ // === module inner block ===
17
19
 
18
20
  // === module main block ===
19
21
 
@@ -28,3 +30,6 @@ module.exports.loadJSONFromFile = loadJSONFromFile;
28
30
  module.exports.loadJSONFromFileSync = loadJSONFromFileSync;
29
31
  module.exports.saveJSONToFile = saveJSONToFile;
30
32
  module.exports.saveJSONToFileSync = saveJSONToFileSync;
33
+
34
+ // * export types definitions *
35
+ module.exports.fsoDescr = fsoDescr;
@@ -31,6 +31,7 @@ export type fsoDescr = {
31
31
  */
32
32
  content?: any;
33
33
  };
34
+ export const fsoDescr: fsoDescr;
34
35
  /**
35
36
  * A result of an error check ops.
36
37
  */
@@ -51,13 +52,14 @@ declare const isArray: (arg: any) => arg is any[];
51
52
  /**
52
53
  * Checks if the given value is a plain object
53
54
  */
54
- declare function isPlainObject(value?: any): boolean;
55
+ declare function isPlainObject(value?: any): value is Record<string, any>;
55
56
  /**
56
57
  * Checks an error code of a fs ops and sets descr info if succeed
57
58
  */
58
- export function checkFsError(descr: fsoDescr, err: Error): VCOR_evalerrfs;
59
+ export function checkFsError(descr: fsoDescr, err: object): VCOR_evalerrfs;
59
60
  /**
60
61
  * Loads file by a given path
62
+ * @throws {Error}
61
63
  */
62
64
  export function loadFromFile(src: string): Promise<fsoDescr>;
63
65
  /**
@@ -66,6 +68,7 @@ export function loadFromFile(src: string): Promise<fsoDescr>;
66
68
  export function loadFromFileSync(src: string): fsoDescr;
67
69
  /**
68
70
  * Saves a content to a file
71
+ * @throws {Error}
69
72
  */
70
73
  export function saveToFile(src: string, content?: string, opt?: any): Promise<fsoDescr>;
71
74
  /**
package/lib/base-func.js CHANGED
@@ -1,4 +1,4 @@
1
- // [v0.1.032-20251126]
1
+ // [v0.1.035-20260105]
2
2
 
3
3
  // === module init block ===
4
4
 
@@ -36,10 +36,6 @@ module.exports.modHelper = {
36
36
  * (* constant definitions *)
37
37
  */
38
38
 
39
- /***
40
- * (* function definitions *)
41
- */
42
-
43
39
  /**
44
40
  * An FS-ops result descriptor.
45
41
  * @typedef {Object} fsoDescr
@@ -50,6 +46,15 @@ module.exports.modHelper = {
50
46
  * @property {string} [source] - path to file
51
47
  * @property {any} [content] - file content
52
48
  */
49
+ /**
50
+ * A virtual constant meant for support jsdoc notation:
51
+ * @type {fsoDescr}
52
+ */
53
+ module.exports.fsoDescr = { isERR: false, errEvent: '' };
54
+
55
+ /***
56
+ * (* function definitions *)
57
+ */
53
58
 
54
59
  /**
55
60
  * A result of an error check ops.
@@ -62,12 +67,16 @@ module.exports.modHelper = {
62
67
  * Checks an error code of a fs ops and sets descr info if succeed
63
68
  * @function checkFsError
64
69
  * @param {fsoDescr} descr - event descriptor
65
- * @param {Error} err - error event
70
+ * @param {Object} err - error event
66
71
  * @returns {VCOR_evalerrfs}
67
72
  */
68
73
  function checkFsError(descr, err) {
69
74
  let isSucceed = false;
70
- if (isPlainObject(err) && isPlainObject(descr)) {
75
+ if (
76
+ isPlainObject(descr)
77
+ && isPlainObject(err)
78
+ && typeof err.code === 'string'
79
+ ) {
71
80
  const id = err.code;
72
81
  switch (id) {
73
82
  case 'ENOENT':
@@ -132,8 +141,9 @@ function loadFromFileSync(src) {
132
141
  * Loads file by a given path
133
142
  * @function loadFromFile
134
143
  * @param {string} src - a path to some file
135
- * @returns {Promise<fsoDescr, Error>}
144
+ * @returns {Promise<fsoDescr>}
136
145
  * @async
146
+ * @throws {Error}
137
147
  */
138
148
  function loadFromFile(src) {
139
149
  /**/// main part that return promise as a result
@@ -231,8 +241,9 @@ function saveToFileSync(src, content = '', opt) {
231
241
  * @param {string} src - a path to some file
232
242
  * @param {string} content - some file content
233
243
  * @param {any} [opt] - <reserved>
234
- * @returns {Promise<fsoDescr, Error>}
244
+ * @returns {Promise<fsoDescr>}
235
245
  * @async
246
+ * @throws {Error}
236
247
  */
237
248
  function saveToFile(src, content = '', opt) {
238
249
  /**/// main part that return promise as a result
@@ -1,4 +1,3 @@
1
- import type { fsoDescr } from "./base-func";
2
1
  /**
3
2
  * A result of `loadJSONFromFileSync`
4
3
  */
@@ -14,6 +13,7 @@ export type RVAL_loadjsonff = {
14
13
  };
15
14
  /**
16
15
  * Loads a JSON-object from a file
16
+ * @throws {Error}
17
17
  */
18
18
  export function loadJSONFromFile(src: string, opt?: any): Promise<RVAL_loadjsonff>;
19
19
  /**
@@ -28,3 +28,5 @@ export function saveJSONToFile(src: string, obj: object, opt?: any): Promise<fso
28
28
  * Saves a JSON-object to a file
29
29
  */
30
30
  export function saveJSONToFileSync(src: string, obj: object, opt?: any): fsoDescr;
31
+
32
+ import { fsoDescr } from "./base-func";
package/lib/json-func.js CHANGED
@@ -1,10 +1,12 @@
1
- // [v0.1.032-20251126]
1
+ // [v0.1.034-20260105]
2
2
 
3
3
  // === module init block ===
4
4
 
5
5
  const {
6
6
  loadFromFile, loadFromFileSync,
7
7
  saveToFile, saveToFileSync,
8
+ // * import types definitions *
9
+ fsoDescr,
8
10
  } = require('./base-func');
9
11
 
10
12
  // === module inner block ===
@@ -63,8 +65,9 @@ function loadJSONFromFileSync(src, opt) {
63
65
  * @function loadJSONFromFile
64
66
  * @param {string} src - a path to some file
65
67
  * @param {any} [opt] - <reserved>
66
- * @returns {Promise<RVAL_loadjsonff, Error>}
68
+ * @returns {Promise<RVAL_loadjsonff>}
67
69
  * @async
70
+ * @throws {Error}
68
71
  */
69
72
  function loadJSONFromFile(src, opt) {
70
73
  /**/// main part that return promise as a result
@@ -137,9 +140,9 @@ function saveJSONToFileSync(src, obj, opt) {
137
140
  * @param {string} src - a path to some file
138
141
  * @param {object} obj - some content
139
142
  * @param {any} [opt] - <reserved>
140
- * @returns {Promise<fsoDescr, Error>}
141
- * @throws {Error}
143
+ * @returns {Promise<fsoDescr>}
142
144
  * @async
145
+ * @throws {Error} (???)
143
146
  */
144
147
  function saveJSONToFile(src, obj, opt) {
145
148
  /**/// main part that return promise as a result
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cntwg/file-helper",
3
- "version": "0.0.2-b",
3
+ "version": "0.0.3",
4
4
  "description": "A small helper library to load/save file content",
5
5
  "author": "ygracs <cs70th-om@rambler.ru>",
6
6
  "license": "MIT",
@@ -30,7 +30,7 @@
30
30
  "#test-dir/*": "./__test__/*"
31
31
  },
32
32
  "devDependencies": {
33
- "@ygracs/test-helper": "^0.0.1-b",
33
+ "@ygracs/test-helper": "~0.0.1-b",
34
34
  "jest": "^30.2.0",
35
35
  "jsdoc-to-markdown": "^9.1.3",
36
36
  "minimist": "^1.2.8",