@cntwg/file-helper 0.0.1 → 0.0.2-b

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/index.d.ts ADDED
@@ -0,0 +1,35 @@
1
+ import {
2
+ checkFsError,
3
+ loadFromFile, loadFromFileSync,
4
+ saveToFile, saveToFileSync,
5
+ } from './lib/base-func';
6
+
7
+ import type {
8
+ fsoDescr, VCOR_evalerrfs,
9
+ } from './lib/base-func';
10
+
11
+ import {
12
+ loadJSONFromFile, loadJSONFromFileSync,
13
+ saveJSONToFile, saveJSONToFileSync,
14
+ } from './lib/json-func';
15
+
16
+ import {
17
+ RVAL_loadjsonff,
18
+ } from './lib/json-func';
19
+
20
+ export {
21
+ checkFsError,
22
+ loadFromFile,
23
+ loadFromFileSync,
24
+ saveToFile,
25
+ saveToFileSync,
26
+ loadJSONFromFile,
27
+ loadJSONFromFileSync,
28
+ saveJSONToFile,
29
+ saveJSONToFileSync
30
+ };
31
+
32
+ export type {
33
+ fsoDescr, VCOR_evalerrfs,
34
+ RVAL_loadjsonff,
35
+ };
@@ -0,0 +1,76 @@
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
+ /**
35
+ * A result of an error check ops.
36
+ */
37
+ export type VCOR_evalerrfs = {
38
+ /**
39
+ * - ops flag
40
+ */
41
+ isSucceed: boolean;
42
+ /**
43
+ * - description
44
+ */
45
+ descr: fsoDescr;
46
+ };
47
+ /**
48
+ * a ref to the `Array.isArray()` method
49
+ */
50
+ declare const isArray: (arg: any) => arg is any[];
51
+ /**
52
+ * Checks if the given value is a plain object
53
+ */
54
+ declare function isPlainObject(value?: any): boolean;
55
+ /**
56
+ * Checks an error code of a fs ops and sets descr info if succeed
57
+ */
58
+ export function checkFsError(descr: fsoDescr, err: Error): VCOR_evalerrfs;
59
+ /**
60
+ * Loads file by a given path
61
+ */
62
+ export function loadFromFile(src: string): Promise<fsoDescr>;
63
+ /**
64
+ * Loads file by a given path
65
+ */
66
+ export function loadFromFileSync(src: string): fsoDescr;
67
+ /**
68
+ * Saves a content to a file
69
+ */
70
+ export function saveToFile(src: string, content?: string, opt?: any): Promise<fsoDescr>;
71
+ /**
72
+ * Saves a content to a file
73
+ */
74
+ export function saveToFileSync(src: string, content?: string, opt?: any): fsoDescr;
75
+
76
+ export {};
package/lib/base-func.js CHANGED
@@ -1,11 +1,11 @@
1
- // [v0.1.030-20250408]
1
+ // [v0.1.032-20251126]
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,16 +15,21 @@ 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
  /***
@@ -36,6 +41,7 @@ function isPlainObject(value = null) {
36
41
  */
37
42
 
38
43
  /**
44
+ * An FS-ops result descriptor.
39
45
  * @typedef {Object} fsoDescr
40
46
  * @property {boolean} isERR - flag
41
47
  * @property {number} [errCode] - error code
@@ -43,22 +49,21 @@ function isPlainObject(value = null) {
43
49
  * @property {string} [errMsg] - event message
44
50
  * @property {string} [source] - path to file
45
51
  * @property {any} [content] - file content
46
- * @description A fs ops description.
47
52
  */
48
53
 
49
54
  /**
55
+ * A result of an error check ops.
50
56
  * @typedef {Object} VCOR_evalerrfs
51
57
  * @property {boolean} isSucceed - ops flag
52
58
  * @property {fsoDescr} descr - description
53
- * @description A result of an error check ops.
54
59
  */
55
60
 
56
61
  /**
62
+ * Checks an error code of a fs ops and sets descr info if succeed
57
63
  * @function checkFsError
58
64
  * @param {fsoDescr} descr - event descriptor
59
65
  * @param {Error} err - error event
60
66
  * @returns {VCOR_evalerrfs}
61
- * @description Checks an error code of a fs ops and sets descr info if succeed
62
67
  */
63
68
  function checkFsError(descr, err) {
64
69
  let isSucceed = false;
@@ -83,10 +88,10 @@ function checkFsError(descr, err) {
83
88
  };
84
89
 
85
90
  /**
91
+ * Loads file by a given path
86
92
  * @function loadFromFileSync
87
93
  * @param {string} src - a path to some file
88
94
  * @returns {fsoDescr}
89
- * @description Loads file by a given path
90
95
  */
91
96
  function loadFromFileSync(src) {
92
97
  /** @type {fsoDescr} */
@@ -124,11 +129,11 @@ function loadFromFileSync(src) {
124
129
  };
125
130
 
126
131
  /**
132
+ * Loads file by a given path
127
133
  * @function loadFromFile
128
134
  * @param {string} src - a path to some file
129
135
  * @returns {Promise<fsoDescr, Error>}
130
136
  * @async
131
- * @description Loads file by a given path
132
137
  */
133
138
  function loadFromFile(src) {
134
139
  /**/// main part that return promise as a result
@@ -172,12 +177,12 @@ function loadFromFile(src) {
172
177
  };
173
178
 
174
179
  /**
180
+ * Saves a content to a file
175
181
  * @function saveToFileSync
176
182
  * @param {string} src - a path to some file
177
183
  * @param {string} content - some file content
178
184
  * @param {any} [opt] - <reserved>
179
185
  * @returns {fsoDescr}
180
- * @description Saves a content to a file
181
186
  */
182
187
  function saveToFileSync(src, content = '', opt) {
183
188
  /** @type {fsoDescr} */
@@ -221,13 +226,13 @@ function saveToFileSync(src, content = '', opt) {
221
226
  };
222
227
 
223
228
  /**
229
+ * Saves a content to a file
224
230
  * @function saveToFile
225
231
  * @param {string} src - a path to some file
226
232
  * @param {string} content - some file content
227
233
  * @param {any} [opt] - <reserved>
228
234
  * @returns {Promise<fsoDescr, Error>}
229
235
  * @async
230
- * @description Saves a content to a file
231
236
  */
232
237
  function saveToFile(src, content = '', opt) {
233
238
  /**/// main part that return promise as a result
@@ -0,0 +1,30 @@
1
+ import type { fsoDescr } from "./base-func";
2
+ /**
3
+ * A result of `loadJSONFromFileSync`
4
+ */
5
+ export type RVAL_loadjsonff = {
6
+ /**
7
+ * - ops description
8
+ */
9
+ descr: fsoDescr;
10
+ /**
11
+ * - loaded content
12
+ */
13
+ obj: any;
14
+ };
15
+ /**
16
+ * Loads a JSON-object from a file
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;
package/lib/json-func.js CHANGED
@@ -1,4 +1,4 @@
1
- // [v0.1.030-20250408]
1
+ // [v0.1.032-20251126]
2
2
 
3
3
  // === module init block ===
4
4
 
@@ -7,25 +7,7 @@ const {
7
7
  saveToFile, saveToFileSync,
8
8
  } = require('./base-func');
9
9
 
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
- };
10
+ // === module inner block ===
29
11
 
30
12
  // === module main block ===
31
13
 
@@ -38,18 +20,18 @@ function isPlainObject(value = null) {
38
20
  */
39
21
 
40
22
  /**
23
+ * A result of `loadJSONFromFileSync`
41
24
  * @typedef {Object} RVAL_loadjsonff
42
25
  * @property {fsoDescr} descr - ops description
43
26
  * @property {any} obj - loaded content
44
- * @description A result of `loadJSONFromFileSync`
45
27
  */
46
28
 
47
29
  /**
30
+ * Loads a JSON-object from a file
48
31
  * @function loadJSONFromFileSync
49
32
  * @param {string} src - a path to some file
50
33
  * @param {any} [opt] - <reserved>
51
34
  * @returns {RVAL_loadjsonff}
52
- * @description Loads a JSON-object from a file
53
35
  */
54
36
  function loadJSONFromFileSync(src, opt) {
55
37
  const data = loadFromFileSync(src);
@@ -77,12 +59,12 @@ function loadJSONFromFileSync(src, opt) {
77
59
  };
78
60
 
79
61
  /**
62
+ * Loads a JSON-object from a file
80
63
  * @function loadJSONFromFile
81
64
  * @param {string} src - a path to some file
82
65
  * @param {any} [opt] - <reserved>
83
66
  * @returns {Promise<RVAL_loadjsonff, Error>}
84
67
  * @async
85
- * @description Loads a JSON-object from a file
86
68
  */
87
69
  function loadJSONFromFile(src, opt) {
88
70
  /**/// main part that return promise as a result
@@ -118,12 +100,12 @@ function loadJSONFromFile(src, opt) {
118
100
  };
119
101
 
120
102
  /**
103
+ * Saves a JSON-object to a file
121
104
  * @function saveJSONToFileSync
122
105
  * @param {string} src - a path to some file
123
106
  * @param {object} obj - some content
124
107
  * @param {any} [opt] - <reserved>
125
108
  * @returns {fsoDescr}
126
- * @description Saves a JSON-object to a file
127
109
  */
128
110
  function saveJSONToFileSync(src, obj, opt) {
129
111
  /** @type {fsoDescr} */
@@ -150,6 +132,7 @@ function saveJSONToFileSync(src, obj, opt) {
150
132
  };
151
133
 
152
134
  /**
135
+ * Saves a JSON-object to a file
153
136
  * @function saveJSONToFile
154
137
  * @param {string} src - a path to some file
155
138
  * @param {object} obj - some content
@@ -157,7 +140,6 @@ function saveJSONToFileSync(src, obj, opt) {
157
140
  * @returns {Promise<fsoDescr, Error>}
158
141
  * @throws {Error}
159
142
  * @async
160
- * @description Saves a JSON-object to a file
161
143
  */
162
144
  function saveJSONToFile(src, obj, opt) {
163
145
  /**/// 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.2-b",
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
  }