@ygracs/xobj-lib-js 0.3.2 → 0.4.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.
@@ -0,0 +1,219 @@
1
+ // [v0.3.141-20260629]
2
+
3
+ // === module init block ===
4
+
5
+ const {
6
+ valueToIndex,
7
+ isInteger,
8
+ isPlainObject,
9
+ } = require('@ygracs/bsfoc-lib-js');
10
+
11
+ const {
12
+ XOBJ_ECODE_TABLE,
13
+ } = require('./xObj-errors');
14
+
15
+ // === module inner block ===
16
+
17
+ const {
18
+ XOBJ_TE_NSTR_EMSG,
19
+ XOBJ_TE_NSTR_ECODE,
20
+ XOBJ_TE_KNES_EMSG,
21
+ XOBJ_TE_KNES_ECODE,
22
+ } = XOBJ_ECODE_TABLE;
23
+
24
+ // === module main block ===
25
+
26
+ /**
27
+ * An error description of a value check ops on fail.
28
+ * @since 0.3.0
29
+ * @typedef {Object} IEvalKeyNameReasonOnFail
30
+ * @property {string} code - message ID
31
+ * @property {string} msg - message text
32
+ */
33
+
34
+ /**
35
+ * An options setting for `evalKeyName` function
36
+ * @since 0.3.0
37
+ * @typedef {Object} IEvalKeyNameOptions
38
+ * @property {boolean} [onlyNES] - a flag to accept only a non-empty string values
39
+ */
40
+
41
+ /**
42
+ * A result of `evalKeyName` function if succeed
43
+ * @since 0.3.0
44
+ * @typedef {Object} IEvalKeyNameResultOnSuccess
45
+ * @property {true} isSucceed - ops flag
46
+ * @property {string} value - result value
47
+ */
48
+
49
+ /**
50
+ * A result of `evalKeyName` function if failed
51
+ * @since 0.3.0
52
+ * @typedef {Object} IEvalKeyNameResultOnFail
53
+ * @property {false} isSucceed - ops flag
54
+ * @property {IEvalKeyNameReasonOnFail} value - reason of fail
55
+ */
56
+
57
+ /**
58
+ * A result of `evalKeyName` function
59
+ * @since 0.3.0
60
+ * @typedef {IEvalKeyNameResultOnSuccess|IEvalKeyNameResultOnFail} IEvalKeyNameResult
61
+ */
62
+ /**
63
+ * A virtual constant meant for support jsdoc notation:
64
+ * @type {IEvalKeyNameResult}
65
+ */
66
+ module.exports.IEvalKeyNameResult = { isSucceed: true, value: '' };
67
+
68
+ /**
69
+ * Tries to convert a value into an ID.
70
+ * @function evalKeyName
71
+ * @param {any} value - key to validate
72
+ * @param {boolean|IEvalKeyNameOptions} [opt=true] - some options
73
+ * @returns {IEvalKeyNameResult}
74
+ * @inner
75
+ */
76
+ function evalKeyName(value, opt = true) {
77
+ let {
78
+ onlyNES,
79
+ } = isPlainObject(opt) ? opt : { onlyNES: opt };
80
+ if (typeof value !== 'string') {
81
+ return {
82
+ isSucceed: false,
83
+ value: {
84
+ code: XOBJ_TE_NSTR_ECODE,
85
+ msg: XOBJ_TE_NSTR_EMSG,
86
+ },
87
+ };
88
+ };
89
+ const key = value.trim();
90
+ if (onlyNES && key === '') {
91
+ return {
92
+ isSucceed: false,
93
+ value: {
94
+ code: XOBJ_TE_KNES_ECODE,
95
+ msg: XOBJ_TE_KNES_EMSG,
96
+ },
97
+ };
98
+ };
99
+ return {
100
+ isSucceed: true,
101
+ value: key,
102
+ };
103
+ };
104
+ module.exports.evalKeyName = evalKeyName;
105
+
106
+ /**
107
+ * Tries to convert a value into an ID.
108
+ * @function evalXObjEName
109
+ * @param {any} value - some value to evaluate
110
+ * @returns {null|number|string}
111
+ */
112
+ function evalXObjEName(value) {
113
+ //return valueToIDString(value); // // TODO: [?]
114
+ let name = value;
115
+ switch (typeof name) {
116
+ case 'number' : {
117
+ if (valueToIndex(name) === -1) name = null;
118
+ break;
119
+ }
120
+ case 'string' : {
121
+ name = name.trim();
122
+ if (name !== '') {
123
+ let value = Number(name);
124
+ if (!Number.isNaN(value)) {
125
+ name = (
126
+ value < 0 || !isInteger(value)
127
+ ? null
128
+ : value
129
+ );
130
+ };
131
+ } else {
132
+ name = null;
133
+ };
134
+ break;
135
+ }
136
+ default: {
137
+ name = null;
138
+ }
139
+ };
140
+ return name;
141
+ };
142
+ module.exports.evalXObjEName = evalXObjEName;
143
+
144
+ /**
145
+ * Some conditions applied to an element as a result of an element name pattern
146
+ * evaluation ops.
147
+ * @since 0.3.0
148
+ * @typedef {Object} IXObjENameDescrConditions
149
+ * @property {any} name - element name
150
+ * @property {any} type - element type
151
+ * @property {any} [value] - some value
152
+ */
153
+
154
+ /**
155
+ * A result of an element name pattern evaluation ops.
156
+ * @typedef {Object} IXObjENameDescr
157
+ * @property {boolean} isERR - ops flag
158
+ * @property {null|number|string} name - element name
159
+ * @property {?IXObjENameDescrConditions} conditions - some conditions applied to an element
160
+ */
161
+
162
+ /**
163
+ * Tries to convert a value into an element name description.
164
+ * @function genXObjENameDescr
165
+ * @param {any} value - element name pattern
166
+ * @returns {IXObjENameDescr}
167
+ * @experimental
168
+ */
169
+ function genXObjENameDescr(value) {
170
+ let result = null;
171
+ let name = null;
172
+ let conditions = null;
173
+ let isERR = false;
174
+ let tail = null;
175
+ if (typeof value === 'string') {
176
+ const re = /^\s*([^\[\]\s]+)?(\s*)(\[.*])?\s*$/;
177
+ tail = value.match(re);
178
+ //console.log('CHECK: '+JSON.stringify(tail, null, 2));
179
+ if (tail) {
180
+ if (tail[1]) {
181
+ name = tail[1];
182
+ if (name) {
183
+ if (tail[2] === '') {
184
+ if (tail[3]) {
185
+ const re = /\[(@{0,1})(?:([^\[=]*?)(?:(?=[=])(=)((?![\"\'])[^\]]*|(?=([\"\']))\5(.*)\5))?)](\s*.*)/;
186
+ tail = tail[3].match(re);
187
+ //console.log('CHECK: '+JSON.stringify(tail, null, 2));
188
+ if (tail) {
189
+ let name = evalXObjEName(tail[2]);
190
+ let type = tail[1] === '@' ? 'attribute' : 'child';
191
+ let value = undefined;
192
+ if (tail[3] === '=') value = tail[5] ? tail[6] : tail[4];
193
+ if (tail[7]) isERR = true;
194
+ conditions = {
195
+ name,
196
+ type,
197
+ value,
198
+ };
199
+ };
200
+ };
201
+ } else if (tail[3]) {
202
+ name = null;
203
+ };
204
+ };
205
+ };
206
+ };
207
+ } else {
208
+ name = value;
209
+ };
210
+ name = evalXObjEName(name);
211
+ if (name === null) isERR = true;
212
+ result = {
213
+ name,
214
+ conditions,
215
+ isERR,
216
+ };
217
+ return result;
218
+ };
219
+ module.exports.genXObjENameDescr = genXObjENameDescr;
@@ -1,69 +1,83 @@
1
+ /**
2
+ * @deprecated
3
+ * @todo \[since `v0.2.1`] deprecated and will be removed
4
+ */
5
+ export type readXObjParamOptionAsStr = string;
6
+ export const readXObjParamOptionAsStr: readXObjParamOptionAsStr;
7
+ /**
8
+ * @deprecated
9
+ * @todo \[since `v0.2.1`] deprecated and will be removed
10
+ */
11
+ export type writeXObjParamOptionAsStr = string;
12
+ export const writeXObjParamOptionAsStr: writeXObjParamOptionAsStr;
1
13
  /**
2
14
  * Extracts a parameter 'AS IS' from a given object.
3
15
  * @throws {TypeError} if `obj` param is not an object
4
16
  * @throws {EvalKeyNameError} if `key` param is not valid identifier
5
17
  */
6
- export function readXObjParamRaw(obj: object, key?: string): any;
18
+ export function readXObjParamRaw(obj: INode, key?: string): any;
7
19
  /**
8
20
  * Writes a parameter 'AS IS' into a given object.
9
21
  * @throws {TypeError} if `obj` param is not an object
10
22
  * @throws {EvalKeyNameError} if `key` param is not valid identifier
11
23
  */
12
- export function writeXObjParamRaw(obj: object, value: any, key?: string): boolean;
24
+ export function writeXObjParamRaw(obj: INode, value: any, key?: string): boolean;
25
+ /**
26
+ * Extracts a parameter from a given object and returns it as a string.
27
+ * @throws {TypeError} if `obj` param is not an object
28
+ */
29
+ export function readXObjParam(obj: INode, key?: string): string;
13
30
  /**
14
31
  * Extracts a parameter from a given object and returns it as a boolean.
15
32
  * @throws {TypeError} if `obj` param is not an object
16
33
  */
17
- export function readXObjParamAsBool(obj: object, defValue?: boolean, key?: string): boolean;
34
+ export function readXObjParamAsBool(obj: INode, defValue?: boolean, key?: string): boolean;
18
35
  /**
19
36
  * Extracts a parameter from a given object and returns it as a number.
20
37
  * @throws {TypeError} if `obj` param is not an object
21
38
  */
22
- export function readXObjParamAsNum(obj: object, defValue?: number, key?: string): number;
39
+ export function readXObjParamAsNum(obj: INode, defValue?: number, key?: string): number;
23
40
  /**
24
41
  * Extracts a parameter from a given object and returns it as 'index' value.
25
42
  * @throws {TypeError} if `obj` param is not an object
26
43
  */
27
- export function readXObjParamAsIndex(obj: object, key?: string): number;
44
+ export function readXObjParamAsIndex(obj: INode, key?: string): number;
28
45
  /**
29
46
  * Extracts a parameter from a given object and returns it as a string.
30
47
  * @throws {TypeError} if `obj` param is not an object
31
48
  * @todo [since `v0.2.1`] deprecate use of `opt` as `string`
32
49
  */
33
- export function readXObjParamEx(obj: object, opt?: object, key?: string): string;
50
+ export function readXObjParamEx(obj: INode, opt?: readXObjParamOptionAsStr | IValueToStringTransformOptions, key?: string): string;
51
+ /**
52
+ * Tries to convert a given value to a string and writes it as a parameter
53
+ * into a given object.
54
+ * @throws {TypeError} if `obj` param is not an object
55
+ */
56
+ export function writeXObjParam(obj: INode, value: any, key?: string): boolean;
34
57
  /**
35
58
  * Tries to convert a given value to a boolean and writes it as a parameter
36
59
  * into a given object.
37
60
  * @throws {TypeError} if `obj` param is not an object
38
61
  */
39
- export function writeXObjParamAsBool(obj: object, value: any, defValue?: boolean, key?: string): boolean;
62
+ export function writeXObjParamAsBool(obj: INode, value: any, defValue?: boolean, key?: string): boolean;
40
63
  /**
41
64
  * Tries to convert a given value to a number and writes it as a parameter
42
65
  * into a given object.
43
66
  * @throws {TypeError} if `obj` param is not an object
44
67
  */
45
- export function writeXObjParamAsNum(obj: object, value: any, defValue?: number, key?: string): boolean;
68
+ export function writeXObjParamAsNum(obj: INode, value: any, defValue?: number, key?: string): boolean;
46
69
  /**
47
70
  * Tries to convert a given value into an 'index' value and writes it
48
71
  * as a parameter into a given object.
49
72
  * @throws {TypeError} if `obj` param is not an object
50
73
  */
51
- export function writeXObjParamAsIndex(obj: object, value: any, key?: string): boolean;
74
+ export function writeXObjParamAsIndex(obj: INode, value: any, key?: string): boolean;
52
75
  /**
53
76
  * Tries to convert a given value to a string and writes it
54
77
  * as a parameter into a given object.
55
78
  * @throws {TypeError} if `obj` param is not an object
56
79
  * @todo [since `v0.2.1`] deprecate use of `opt` as `string`
57
80
  */
58
- export function writeXObjParamEx(obj: object, value: any, opt?: object, key?: string): boolean;
59
- /**
60
- * Extracts a parameter from a given object and returns it as a string.
61
- * @throws {TypeError} if `obj` param is not an object
62
- */
63
- export function readXObjParam(obj: object, key?: string): string;
64
- /**
65
- * Tries to convert a given value to a string and writes it as a parameter
66
- * into a given object.
67
- * @throws {TypeError} if `obj` param is not an object
68
- */
69
- export function writeXObjParam(obj: object, value: any, key?: string): boolean;
81
+ export function writeXObjParamEx(obj: INode, value: any, opt?: writeXObjParamOptionAsStr | IValueToStringTransformOptions, key?: string): boolean;
82
+ import type { INode } from "./xObj-lib";
83
+ import type { IValueToStringTransformOptions } from "@ygracs/bsfoc-lib-js";
package/lib/xObj-param.js CHANGED
@@ -1,4 +1,4 @@
1
- // [v0.3.138-20260530]
1
+ // [v0.3.141-20260629]
2
2
 
3
3
  // === module init block ===
4
4
 
@@ -21,6 +21,11 @@ const {
21
21
 
22
22
  const {
23
23
  evalKeyName,
24
+ } = require('./xObj-names');
25
+
26
+ const {
27
+ // * import types definitions *
28
+ INode,
24
29
  } = require('./xObj-lib');
25
30
 
26
31
  // === module inner block ===
@@ -40,7 +45,7 @@ const {
40
45
  /**
41
46
  * Extracts a parameter 'AS IS' from a given object.
42
47
  * @function readXObjParamRaw
43
- * @param {object} obj - some object
48
+ * @param {INode} obj - some object
44
49
  * @param {string} [key=XOBJ_DEF_PARAM_TNAME] - some key
45
50
  * @returns {any}
46
51
  * @throws {TypeError} if `obj` param is not an object
@@ -65,7 +70,7 @@ module.exports.readXObjParamRaw = readXObjParamRaw;
65
70
  /**
66
71
  * Writes a parameter 'AS IS' into a given object.
67
72
  * @function writeXObjParamRaw
68
- * @param {object} obj - some object
73
+ * @param {INode} obj - some object
69
74
  * @param {any} value - some value
70
75
  * @param {string} [key=XOBJ_DEF_PARAM_TNAME] - some key
71
76
  * @returns {boolean}
@@ -101,7 +106,7 @@ module.exports.writeXObjParamRaw = writeXObjParamRaw;
101
106
  /**
102
107
  * Extracts a parameter from a given object and returns it as a string.
103
108
  * @function readXObjParam
104
- * @param {object} obj - some object
109
+ * @param {INode} obj - some object
105
110
  * @param {string} [key] - some key
106
111
  * @returns {string}
107
112
  * @throws {TypeError} if `obj` param is not an object
@@ -126,7 +131,7 @@ module.exports.readXObjParam = readXObjParam;
126
131
  /**
127
132
  * Extracts a parameter from a given object and returns it as a boolean.
128
133
  * @function readXObjParamAsBool
129
- * @param {object} obj - some object
134
+ * @param {INode} obj - some object
130
135
  * @param {boolean} [defValue] - default value
131
136
  * @param {string} [key] - some key
132
137
  * @returns {boolean}
@@ -154,7 +159,7 @@ module.exports.readXObjParamAsBool = readXObjParamAsBool;
154
159
  /**
155
160
  * Extracts a parameter from a given object and returns it as a number.
156
161
  * @function readXObjParamAsNum
157
- * @param {object} obj - some object
162
+ * @param {INode} obj - some object
158
163
  * @param {number} [defValue] - default value
159
164
  * @param {string} [key] - some key
160
165
  * @returns {number}
@@ -182,7 +187,7 @@ module.exports.readXObjParamAsNum = readXObjParamAsNum;
182
187
  /**
183
188
  * Extracts a parameter from a given object and returns it as 'index' value.
184
189
  * @function readXObjParamAsIndex
185
- * @param {object} obj - some object
190
+ * @param {INode} obj - some object
186
191
  * @param {string} [key] - some key
187
192
  * @returns {number}
188
193
  * @throws {TypeError} if `obj` param is not an object
@@ -205,11 +210,23 @@ function readXObjParamAsIndex(obj, key) {
205
210
  };
206
211
  module.exports.readXObjParamAsIndex = readXObjParamAsIndex;
207
212
 
213
+ /**
214
+ *
215
+ * @typedef {string} readXObjParamOptionAsStr
216
+ * @deprecated
217
+ * @todo \[since `v0.2.1`] deprecated and will be removed
218
+ */
219
+ /**
220
+ * A virtual constant meant for support jsdoc notation:
221
+ * @type {readXObjParamOptionAsStr}
222
+ */
223
+ module.exports.readXObjParamOptionAsStr = '';
224
+
208
225
  /**
209
226
  * Extracts a parameter from a given object and returns it as a string.
210
227
  * @function readXObjParamEx
211
- * @param {object} obj - some object
212
- * @param {object} [opt] - options
228
+ * @param {INode} obj - some object
229
+ * @param {readXObjParamOptionAsStr|IValueToStringTransformOptions} [opt] - options
213
230
  * @param {string} [key] - some key
214
231
  * @returns {string}
215
232
  * @throws {TypeError} if `obj` param is not an object
@@ -229,7 +246,6 @@ function readXObjParamEx(obj, opt, key) {
229
246
  }
230
247
  };
231
248
  };
232
- /** @type {IValueToStringTransformOptions} */
233
249
  const _opt = isPlainObject(opt) ? opt : {
234
250
  useTrim: false,
235
251
  numberToString: true,
@@ -244,7 +260,7 @@ module.exports.readXObjParamEx = readXObjParamEx;
244
260
  * Tries to convert a given value to a string and writes it as a parameter
245
261
  * into a given object.
246
262
  * @function writeXObjParam
247
- * @param {object} obj - some object
263
+ * @param {INode} obj - some object
248
264
  * @param {any} value - some value
249
265
  * @param {string} [key] - some key
250
266
  * @returns {boolean}
@@ -271,7 +287,7 @@ module.exports.writeXObjParam = writeXObjParam;
271
287
  * Tries to convert a given value to a boolean and writes it as a parameter
272
288
  * into a given object.
273
289
  * @function writeXObjParamAsBool
274
- * @param {object} obj - some object
290
+ * @param {INode} obj - some object
275
291
  * @param {any} value - some value
276
292
  * @param {boolean} [defValue] - default value
277
293
  * @param {string} [key] - some key
@@ -303,7 +319,7 @@ module.exports.writeXObjParamAsBool = writeXObjParamAsBool;
303
319
  * Tries to convert a given value to a number and writes it as a parameter
304
320
  * into a given object.
305
321
  * @function writeXObjParamAsNum
306
- * @param {object} obj - some object
322
+ * @param {INode} obj - some object
307
323
  * @param {any} value - some value
308
324
  * @param {number} [defValue] - default value
309
325
  * @param {string} [key] - some key
@@ -335,7 +351,7 @@ module.exports.writeXObjParamAsNum = writeXObjParamAsNum;
335
351
  * Tries to convert a given value into an 'index' value and writes it
336
352
  * as a parameter into a given object.
337
353
  * @function writeXObjParamAsIndex
338
- * @param {object} obj - some object
354
+ * @param {INode} obj - some object
339
355
  * @param {any} value - some value
340
356
  * @param {string} [key] - some key
341
357
  * @returns {boolean}
@@ -362,13 +378,25 @@ function writeXObjParamAsIndex(obj, value, key) {
362
378
  };
363
379
  module.exports.writeXObjParamAsIndex = writeXObjParamAsIndex;
364
380
 
381
+ /**
382
+ *
383
+ * @typedef {string} writeXObjParamOptionAsStr
384
+ * @deprecated
385
+ * @todo \[since `v0.2.1`] deprecated and will be removed
386
+ */
387
+ /**
388
+ * A virtual constant meant for support jsdoc notation:
389
+ * @type {writeXObjParamOptionAsStr}
390
+ */
391
+ module.exports.writeXObjParamOptionAsStr = '';
392
+
365
393
  /**
366
394
  * Tries to convert a given value to a string and writes it
367
395
  * as a parameter into a given object.
368
396
  * @function writeXObjParamEx
369
- * @param {object} obj - some object
397
+ * @param {INode} obj - some object
370
398
  * @param {any} value - some value
371
- * @param {object} [opt] - options
399
+ * @param {writeXObjParamOptionAsStr|IValueToStringTransformOptions} [opt] - options
372
400
  * @param {string} [key] - some key
373
401
  * @returns {boolean}
374
402
  * @throws {TypeError} if `obj` param is not an object
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ygracs/xobj-lib-js",
3
- "version": "0.3.2",
3
+ "version": "0.4.0",
4
4
  "description": "A helper library to work with xml-js module",
5
5
  "author": "ygracs <cs70th-om@rambler.ru>",
6
6
  "license": "MIT",
@@ -14,6 +14,7 @@
14
14
  "doc/*.md",
15
15
  "lib/xObj-lib.js",
16
16
  "lib/xObj-defs.js",
17
+ "lib/xObj-names.js",
17
18
  "lib/xObj-errors.js",
18
19
  "lib/xObj-attr.js",
19
20
  "lib/xObj-param.js",
@@ -33,7 +34,7 @@
33
34
  "#test-dir/*": "./__test__/*"
34
35
  },
35
36
  "dependencies": {
36
- "@ygracs/bsfoc-lib-js": "~0.3.4"
37
+ "@ygracs/bsfoc-lib-js": "~0.4.0"
37
38
  },
38
39
  "devDependencies": {
39
40
  "@ygracs/test-helper": "~0.0.2-b",