@cntwg/file-helper 0.0.1 → 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 ADDED
@@ -0,0 +1,31 @@
1
+ import {
2
+ checkFsError,
3
+ loadFromFile, loadFromFileSync,
4
+ saveToFile, saveToFileSync,
5
+ type fsoDescr,
6
+ type VCOR_evalerrfs,
7
+ } from "./lib/base-func";
8
+
9
+ import {
10
+ loadJSONFromFile, loadJSONFromFileSync,
11
+ saveJSONToFile, saveJSONToFileSync,
12
+ type RVAL_loadjsonff,
13
+ } from "./lib/json-func";
14
+
15
+ export {
16
+ checkFsError,
17
+ loadFromFile,
18
+ loadFromFileSync,
19
+ saveToFile,
20
+ saveToFileSync,
21
+ loadJSONFromFile,
22
+ loadJSONFromFileSync,
23
+ saveJSONToFile,
24
+ saveJSONToFileSync
25
+ };
26
+
27
+ export type {
28
+ fsoDescr,
29
+ VCOR_evalerrfs,
30
+ RVAL_loadjsonff,
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;
@@ -0,0 +1,79 @@
1
+ export namespace modHelper {
2
+ export { isArray };
3
+ export { isPlainObject };
4
+ }
5
+ /**
6
+ * An FS-ops result descriptor.
7
+ */
8
+ export type fsoDescr = {
9
+ /**
10
+ * - flag
11
+ */
12
+ isERR: boolean;
13
+ /**
14
+ * - error code
15
+ */
16
+ errCode?: number;
17
+ /**
18
+ * - event ID
19
+ */
20
+ errEvent: string;
21
+ /**
22
+ * - event message
23
+ */
24
+ errMsg?: string;
25
+ /**
26
+ * - path to file
27
+ */
28
+ source?: string;
29
+ /**
30
+ * - file content
31
+ */
32
+ content?: any;
33
+ };
34
+ export const fsoDescr: fsoDescr;
35
+ /**
36
+ * A result of an error check ops.
37
+ */
38
+ export type VCOR_evalerrfs = {
39
+ /**
40
+ * - ops flag
41
+ */
42
+ isSucceed: boolean;
43
+ /**
44
+ * - description
45
+ */
46
+ descr: fsoDescr;
47
+ };
48
+ /**
49
+ * a ref to the `Array.isArray()` method
50
+ */
51
+ declare const isArray: (arg: any) => arg is any[];
52
+ /**
53
+ * Checks if the given value is a plain object
54
+ */
55
+ declare function isPlainObject(value?: any): value is Record<string, any>;
56
+ /**
57
+ * Checks an error code of a fs ops and sets descr info if succeed
58
+ */
59
+ export function checkFsError(descr: fsoDescr, err: object): VCOR_evalerrfs;
60
+ /**
61
+ * Loads file by a given path
62
+ * @throws {Error}
63
+ */
64
+ export function loadFromFile(src: string): Promise<fsoDescr>;
65
+ /**
66
+ * Loads file by a given path
67
+ */
68
+ export function loadFromFileSync(src: string): fsoDescr;
69
+ /**
70
+ * Saves a content to a file
71
+ * @throws {Error}
72
+ */
73
+ export function saveToFile(src: string, content?: string, opt?: any): Promise<fsoDescr>;
74
+ /**
75
+ * Saves a content to a file
76
+ */
77
+ export function saveToFileSync(src: string, content?: string, opt?: any): fsoDescr;
78
+
79
+ export {};
package/lib/base-func.js CHANGED
@@ -1,11 +1,11 @@
1
- // [v0.1.030-20250408]
1
+ // [v0.1.035-20260105]
2
2
 
3
3
  // === module init block ===
4
4
 
5
5
  const fs = require('fs');
6
6
  const fse = fs.promises;
7
7
 
8
- // === module extra block (helper functions) ===
8
+ // === module inner block ===
9
9
 
10
10
  /**
11
11
  * a ref to the `Array.isArray()` method
@@ -15,27 +15,29 @@ const fse = fs.promises;
15
15
  const isArray = Array.isArray;
16
16
 
17
17
  /**
18
+ * Checks if the given value is a plain object
18
19
  * @function isPlainObject
19
20
  * @param {any} value - some value to verify
20
21
  * @returns {boolean}
21
- * @description Checks if the given value is a plain object
22
22
  * @todo consider if an instance of 'Set' or 'Map' can be treated as plain object
23
23
  */
24
24
  function isPlainObject(value = null) {
25
25
  return value !== null && typeof value === 'object' && !isArray(value);
26
26
  };
27
27
 
28
+ module.exports.modHelper = {
29
+ isArray,
30
+ isPlainObject,
31
+ };
32
+
28
33
  // === module main block ===
29
34
 
30
35
  /***
31
36
  * (* constant definitions *)
32
37
  */
33
38
 
34
- /***
35
- * (* function definitions *)
36
- */
37
-
38
39
  /**
40
+ * An FS-ops result descriptor.
39
41
  * @typedef {Object} fsoDescr
40
42
  * @property {boolean} isERR - flag
41
43
  * @property {number} [errCode] - error code
@@ -43,26 +45,38 @@ function isPlainObject(value = null) {
43
45
  * @property {string} [errMsg] - event message
44
46
  * @property {string} [source] - path to file
45
47
  * @property {any} [content] - file content
46
- * @description A fs ops description.
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 *)
47
57
  */
48
58
 
49
59
  /**
60
+ * A result of an error check ops.
50
61
  * @typedef {Object} VCOR_evalerrfs
51
62
  * @property {boolean} isSucceed - ops flag
52
63
  * @property {fsoDescr} descr - description
53
- * @description A result of an error check ops.
54
64
  */
55
65
 
56
66
  /**
67
+ * Checks an error code of a fs ops and sets descr info if succeed
57
68
  * @function checkFsError
58
69
  * @param {fsoDescr} descr - event descriptor
59
- * @param {Error} err - error event
70
+ * @param {Object} err - error event
60
71
  * @returns {VCOR_evalerrfs}
61
- * @description Checks an error code of a fs ops and sets descr info if succeed
62
72
  */
63
73
  function checkFsError(descr, err) {
64
74
  let isSucceed = false;
65
- if (isPlainObject(err) && isPlainObject(descr)) {
75
+ if (
76
+ isPlainObject(descr)
77
+ && isPlainObject(err)
78
+ && typeof err.code === 'string'
79
+ ) {
66
80
  const id = err.code;
67
81
  switch (id) {
68
82
  case 'ENOENT':
@@ -83,10 +97,10 @@ function checkFsError(descr, err) {
83
97
  };
84
98
 
85
99
  /**
100
+ * Loads file by a given path
86
101
  * @function loadFromFileSync
87
102
  * @param {string} src - a path to some file
88
103
  * @returns {fsoDescr}
89
- * @description Loads file by a given path
90
104
  */
91
105
  function loadFromFileSync(src) {
92
106
  /** @type {fsoDescr} */
@@ -124,11 +138,12 @@ function loadFromFileSync(src) {
124
138
  };
125
139
 
126
140
  /**
141
+ * Loads file by a given path
127
142
  * @function loadFromFile
128
143
  * @param {string} src - a path to some file
129
- * @returns {Promise<fsoDescr, Error>}
144
+ * @returns {Promise<fsoDescr>}
130
145
  * @async
131
- * @description Loads file by a given path
146
+ * @throws {Error}
132
147
  */
133
148
  function loadFromFile(src) {
134
149
  /**/// main part that return promise as a result
@@ -172,12 +187,12 @@ function loadFromFile(src) {
172
187
  };
173
188
 
174
189
  /**
190
+ * Saves a content to a file
175
191
  * @function saveToFileSync
176
192
  * @param {string} src - a path to some file
177
193
  * @param {string} content - some file content
178
194
  * @param {any} [opt] - <reserved>
179
195
  * @returns {fsoDescr}
180
- * @description Saves a content to a file
181
196
  */
182
197
  function saveToFileSync(src, content = '', opt) {
183
198
  /** @type {fsoDescr} */
@@ -221,13 +236,14 @@ function saveToFileSync(src, content = '', opt) {
221
236
  };
222
237
 
223
238
  /**
239
+ * Saves a content to a file
224
240
  * @function saveToFile
225
241
  * @param {string} src - a path to some file
226
242
  * @param {string} content - some file content
227
243
  * @param {any} [opt] - <reserved>
228
- * @returns {Promise<fsoDescr, Error>}
244
+ * @returns {Promise<fsoDescr>}
229
245
  * @async
230
- * @description Saves a content to a file
246
+ * @throws {Error}
231
247
  */
232
248
  function saveToFile(src, content = '', opt) {
233
249
  /**/// main part that return promise as a result
@@ -0,0 +1,32 @@
1
+ /**
2
+ * A result of `loadJSONFromFileSync`
3
+ */
4
+ export type RVAL_loadjsonff = {
5
+ /**
6
+ * - ops description
7
+ */
8
+ descr: fsoDescr;
9
+ /**
10
+ * - loaded content
11
+ */
12
+ obj: any;
13
+ };
14
+ /**
15
+ * Loads a JSON-object from a file
16
+ * @throws {Error}
17
+ */
18
+ export function loadJSONFromFile(src: string, opt?: any): Promise<RVAL_loadjsonff>;
19
+ /**
20
+ * Loads a JSON-object from a file
21
+ */
22
+ export function loadJSONFromFileSync(src: string, opt?: any): RVAL_loadjsonff;
23
+ /**
24
+ * Saves a JSON-object to a file
25
+ */
26
+ export function saveJSONToFile(src: string, obj: object, opt?: any): Promise<fsoDescr>;
27
+ /**
28
+ * Saves a JSON-object to a file
29
+ */
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,31 +1,15 @@
1
- // [v0.1.030-20250408]
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
- // === module extra block (helper functions) ===
11
-
12
- /**
13
- * a ref to the `Array.isArray()` method
14
- * @param {any} value - some value to verify
15
- * @returns {boolean}
16
- */
17
- const isArray = Array.isArray;
18
-
19
- /**
20
- * @function isPlainObject
21
- * @param {any} value - some value to verify
22
- * @returns {boolean}
23
- * @description Checks if the given value is a plain object
24
- * @todo consider if an instance of 'Set' or 'Map' can be treated as plain object
25
- */
26
- function isPlainObject(value = null) {
27
- return value !== null && typeof value === 'object' && !isArray(value);
28
- };
12
+ // === module inner block ===
29
13
 
30
14
  // === module main block ===
31
15
 
@@ -38,18 +22,18 @@ function isPlainObject(value = null) {
38
22
  */
39
23
 
40
24
  /**
25
+ * A result of `loadJSONFromFileSync`
41
26
  * @typedef {Object} RVAL_loadjsonff
42
27
  * @property {fsoDescr} descr - ops description
43
28
  * @property {any} obj - loaded content
44
- * @description A result of `loadJSONFromFileSync`
45
29
  */
46
30
 
47
31
  /**
32
+ * Loads a JSON-object from a file
48
33
  * @function loadJSONFromFileSync
49
34
  * @param {string} src - a path to some file
50
35
  * @param {any} [opt] - <reserved>
51
36
  * @returns {RVAL_loadjsonff}
52
- * @description Loads a JSON-object from a file
53
37
  */
54
38
  function loadJSONFromFileSync(src, opt) {
55
39
  const data = loadFromFileSync(src);
@@ -77,12 +61,13 @@ function loadJSONFromFileSync(src, opt) {
77
61
  };
78
62
 
79
63
  /**
64
+ * Loads a JSON-object from a file
80
65
  * @function loadJSONFromFile
81
66
  * @param {string} src - a path to some file
82
67
  * @param {any} [opt] - <reserved>
83
- * @returns {Promise<RVAL_loadjsonff, Error>}
68
+ * @returns {Promise<RVAL_loadjsonff>}
84
69
  * @async
85
- * @description Loads a JSON-object from a file
70
+ * @throws {Error}
86
71
  */
87
72
  function loadJSONFromFile(src, opt) {
88
73
  /**/// main part that return promise as a result
@@ -118,12 +103,12 @@ function loadJSONFromFile(src, opt) {
118
103
  };
119
104
 
120
105
  /**
106
+ * Saves a JSON-object to a file
121
107
  * @function saveJSONToFileSync
122
108
  * @param {string} src - a path to some file
123
109
  * @param {object} obj - some content
124
110
  * @param {any} [opt] - <reserved>
125
111
  * @returns {fsoDescr}
126
- * @description Saves a JSON-object to a file
127
112
  */
128
113
  function saveJSONToFileSync(src, obj, opt) {
129
114
  /** @type {fsoDescr} */
@@ -150,14 +135,14 @@ function saveJSONToFileSync(src, obj, opt) {
150
135
  };
151
136
 
152
137
  /**
138
+ * Saves a JSON-object to a file
153
139
  * @function saveJSONToFile
154
140
  * @param {string} src - a path to some file
155
141
  * @param {object} obj - some content
156
142
  * @param {any} [opt] - <reserved>
157
- * @returns {Promise<fsoDescr, Error>}
158
- * @throws {Error}
143
+ * @returns {Promise<fsoDescr>}
159
144
  * @async
160
- * @description Saves a JSON-object to a file
145
+ * @throws {Error} (???)
161
146
  */
162
147
  function saveJSONToFile(src, obj, opt) {
163
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.1",
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",
@@ -9,25 +9,31 @@
9
9
  "url": "git+https://gitlab.com/cntwg/file-helper.git"
10
10
  },
11
11
  "main": "./index.js",
12
+ "types": "./index.d.ts",
12
13
  "files": [
13
14
  "doc/file-helper.md",
14
15
  "lib/base-func.js",
15
16
  "lib/json-func.js",
17
+ "lib/*.d.ts",
16
18
  "index.js",
19
+ "index.d.ts",
17
20
  "CHANGELOG.md"
18
21
  ],
19
22
  "scripts": {
20
23
  "test": "jest",
21
24
  "build-doc-md": "jsdoc2md",
22
- "build-doc-html": "jsdoc"
25
+ "build-doc-html": "jsdoc",
26
+ "gen-dts": "npx -p typescript tsc"
23
27
  },
24
28
  "imports": {
25
29
  "#lib/*": "./lib/*",
26
30
  "#test-dir/*": "./__test__/*"
27
31
  },
28
32
  "devDependencies": {
29
- "jest": "^29.7.0",
30
- "jsdoc-to-markdown": "^9.1.1",
31
- "minimist": "^1.2.8"
33
+ "@ygracs/test-helper": "~0.0.1-b",
34
+ "jest": "^30.2.0",
35
+ "jsdoc-to-markdown": "^9.1.3",
36
+ "minimist": "^1.2.8",
37
+ "typescript": "~5.9.3"
32
38
  }
33
39
  }