@ygracs/xobj-lib-js 0.3.1 → 0.3.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.
@@ -0,0 +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;
13
+ /**
14
+ * Extracts a parameter 'AS IS' from a given object.
15
+ * @throws {TypeError} if `obj` param is not an object
16
+ * @throws {EvalKeyNameError} if `key` param is not valid identifier
17
+ */
18
+ export function readXObjParamRaw(obj: INode, key?: string): any;
19
+ /**
20
+ * Writes a parameter 'AS IS' into a given object.
21
+ * @throws {TypeError} if `obj` param is not an object
22
+ * @throws {EvalKeyNameError} if `key` param is not valid identifier
23
+ */
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;
30
+ /**
31
+ * Extracts a parameter from a given object and returns it as a boolean.
32
+ * @throws {TypeError} if `obj` param is not an object
33
+ */
34
+ export function readXObjParamAsBool(obj: INode, defValue?: boolean, key?: string): boolean;
35
+ /**
36
+ * Extracts a parameter from a given object and returns it as a number.
37
+ * @throws {TypeError} if `obj` param is not an object
38
+ */
39
+ export function readXObjParamAsNum(obj: INode, defValue?: number, key?: string): number;
40
+ /**
41
+ * Extracts a parameter from a given object and returns it as 'index' value.
42
+ * @throws {TypeError} if `obj` param is not an object
43
+ */
44
+ export function readXObjParamAsIndex(obj: INode, key?: string): number;
45
+ /**
46
+ * Extracts a parameter from a given object and returns it as a string.
47
+ * @throws {TypeError} if `obj` param is not an object
48
+ * @todo [since `v0.2.1`] deprecate use of `opt` as `string`
49
+ */
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;
57
+ /**
58
+ * Tries to convert a given value to a boolean and writes it as a parameter
59
+ * into a given object.
60
+ * @throws {TypeError} if `obj` param is not an object
61
+ */
62
+ export function writeXObjParamAsBool(obj: INode, value: any, defValue?: boolean, key?: string): boolean;
63
+ /**
64
+ * Tries to convert a given value to a number and writes it as a parameter
65
+ * into a given object.
66
+ * @throws {TypeError} if `obj` param is not an object
67
+ */
68
+ export function writeXObjParamAsNum(obj: INode, value: any, defValue?: number, key?: string): boolean;
69
+ /**
70
+ * Tries to convert a given value into an 'index' value and writes it
71
+ * as a parameter into a given object.
72
+ * @throws {TypeError} if `obj` param is not an object
73
+ */
74
+ export function writeXObjParamAsIndex(obj: INode, value: any, key?: string): boolean;
75
+ /**
76
+ * Tries to convert a given value to a string and writes it
77
+ * as a parameter into a given object.
78
+ * @throws {TypeError} if `obj` param is not an object
79
+ * @todo [since `v0.2.1`] deprecate use of `opt` as `string`
80
+ */
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";
@@ -0,0 +1,435 @@
1
+ // [v0.3.140-20260607]
2
+
3
+ // === module init block ===
4
+
5
+ const {
6
+ valueToIndex,
7
+ readAsString, readAsBoolEx, readAsNumberEx,
8
+ isObject, isPlainObject,
9
+ // * import types definitions *
10
+ IValueToStringTransformOptions,
11
+ } = require('@ygracs/bsfoc-lib-js');
12
+
13
+ const {
14
+ EvalKeyNameError,
15
+ XOBJ_ECODE_TABLE,
16
+ } = require('./xObj-errors');
17
+
18
+ const {
19
+ XOBJ_DEF_TNAMES,
20
+ } = require('./xObj-defs');
21
+
22
+ const {
23
+ evalKeyName,
24
+ // * import types definitions *
25
+ INode,
26
+ } = require('./xObj-lib');
27
+
28
+ // === module inner block ===
29
+
30
+ const {
31
+ XOBJ_TE_NSTR_ECODE,
32
+ XOBJ_TE_NPOBJ_EMSG,
33
+ XOBJ_TE_NPOBJ_ECODE,
34
+ } = XOBJ_ECODE_TABLE;
35
+
36
+ const {
37
+ XOBJ_DEF_PARAM_TNAME,
38
+ } = XOBJ_DEF_TNAMES;
39
+
40
+ // === module main block ===
41
+
42
+ /**
43
+ * Extracts a parameter 'AS IS' from a given object.
44
+ * @function readXObjParamRaw
45
+ * @param {INode} obj - some object
46
+ * @param {string} [key=XOBJ_DEF_PARAM_TNAME] - some key
47
+ * @returns {any}
48
+ * @throws {TypeError} if `obj` param is not an object
49
+ * @throws {EvalKeyNameError} if `key` param is not valid identifier
50
+ */
51
+ function readXObjParamRaw(obj, key = XOBJ_DEF_PARAM_TNAME) {
52
+ if (!isPlainObject(obj)) {
53
+ throw new EvalKeyNameError(
54
+ XOBJ_TE_NPOBJ_EMSG,
55
+ { code: XOBJ_TE_NPOBJ_ECODE },
56
+ );
57
+ };
58
+ const { isSucceed, value: _key } = evalKeyName(key, false);
59
+ if (!isSucceed) {
60
+ const { code, msg } = _key;
61
+ throw new EvalKeyNameError(msg, { code });
62
+ };
63
+ return _key !== '' ? obj[_key] : undefined;
64
+ };
65
+ module.exports.readXObjParamRaw = readXObjParamRaw;
66
+
67
+ /**
68
+ * Writes a parameter 'AS IS' into a given object.
69
+ * @function writeXObjParamRaw
70
+ * @param {INode} obj - some object
71
+ * @param {any} value - some value
72
+ * @param {string} [key=XOBJ_DEF_PARAM_TNAME] - some key
73
+ * @returns {boolean}
74
+ * @throws {TypeError} if `obj` param is not an object
75
+ * @throws {EvalKeyNameError} if `key` param is not valid identifier
76
+ */
77
+ function writeXObjParamRaw(obj, value, key = XOBJ_DEF_PARAM_TNAME) {
78
+ if (!isPlainObject(obj)) {
79
+ throw new EvalKeyNameError(
80
+ XOBJ_TE_NPOBJ_EMSG,
81
+ { code: XOBJ_TE_NPOBJ_ECODE },
82
+ );
83
+ };
84
+ const { isSucceed, value: _key } = evalKeyName(key, false);
85
+ if (isSucceed) {
86
+ if (
87
+ _key === ''
88
+ || value === undefined
89
+ || isObject(value)
90
+ || typeof value === 'function'
91
+ ) {
92
+ return false;
93
+ };
94
+ obj[_key] = value;
95
+ return true;
96
+ } else {
97
+ const { code, msg } = _key;
98
+ throw new EvalKeyNameError(msg, { code });
99
+ };
100
+ };
101
+ module.exports.writeXObjParamRaw = writeXObjParamRaw;
102
+
103
+ /**
104
+ * Extracts a parameter from a given object and returns it as a string.
105
+ * @function readXObjParam
106
+ * @param {INode} obj - some object
107
+ * @param {string} [key] - some key
108
+ * @returns {string}
109
+ * @throws {TypeError} if `obj` param is not an object
110
+ */
111
+ function readXObjParam(obj, key) {
112
+ const opt = {
113
+ useTrim: false,
114
+ numberToString: true,
115
+ boolToString: true,
116
+ defValue: '',
117
+ };
118
+ let result = undefined;
119
+ try {
120
+ result = readXObjParamEx(obj, opt, key);
121
+ } catch (err) {
122
+ throw err;
123
+ };
124
+ return result;
125
+ };
126
+ module.exports.readXObjParam = readXObjParam;
127
+
128
+ /**
129
+ * Extracts a parameter from a given object and returns it as a boolean.
130
+ * @function readXObjParamAsBool
131
+ * @param {INode} obj - some object
132
+ * @param {boolean} [defValue] - default value
133
+ * @param {string} [key] - some key
134
+ * @returns {boolean}
135
+ * @throws {TypeError} if `obj` param is not an object
136
+ * @see readAsBoolEx from `@ygracs/bsfoc-lib-js` on details for result
137
+ */
138
+ function readXObjParamAsBool(obj, defValue, key) {
139
+ let result = undefined;
140
+ try {
141
+ result = readXObjParamRaw(obj, key);
142
+ } catch (err) {
143
+ switch (err.code) {
144
+ case XOBJ_TE_NSTR_ECODE : {
145
+ break;
146
+ }
147
+ default: {
148
+ throw err;
149
+ }
150
+ };
151
+ };
152
+ return readAsBoolEx(result, defValue);
153
+ };
154
+ module.exports.readXObjParamAsBool = readXObjParamAsBool;
155
+
156
+ /**
157
+ * Extracts a parameter from a given object and returns it as a number.
158
+ * @function readXObjParamAsNum
159
+ * @param {INode} obj - some object
160
+ * @param {number} [defValue] - default value
161
+ * @param {string} [key] - some key
162
+ * @returns {number}
163
+ * @throws {TypeError} if `obj` param is not an object
164
+ * @see readAsNumberEx from `@ygracs/bsfoc-lib-js` on details for result
165
+ */
166
+ function readXObjParamAsNum(obj, defValue, key) {
167
+ let result = undefined;
168
+ try {
169
+ result = readXObjParamRaw(obj, key);
170
+ } catch (err) {
171
+ switch (err.code) {
172
+ case XOBJ_TE_NSTR_ECODE : {
173
+ break;
174
+ }
175
+ default: {
176
+ throw err;
177
+ }
178
+ };
179
+ };
180
+ return readAsNumberEx(result, defValue);
181
+ };
182
+ module.exports.readXObjParamAsNum = readXObjParamAsNum;
183
+
184
+ /**
185
+ * Extracts a parameter from a given object and returns it as 'index' value.
186
+ * @function readXObjParamAsIndex
187
+ * @param {INode} obj - some object
188
+ * @param {string} [key] - some key
189
+ * @returns {number}
190
+ * @throws {TypeError} if `obj` param is not an object
191
+ */
192
+ function readXObjParamAsIndex(obj, key) {
193
+ let result = undefined;
194
+ try {
195
+ result = readXObjParamRaw(obj, key);
196
+ } catch (err) {
197
+ switch (err.code) {
198
+ case XOBJ_TE_NSTR_ECODE : {
199
+ break;
200
+ }
201
+ default: {
202
+ throw err;
203
+ }
204
+ };
205
+ };
206
+ return valueToIndex(result);
207
+ };
208
+ module.exports.readXObjParamAsIndex = readXObjParamAsIndex;
209
+
210
+ /**
211
+ *
212
+ * @typedef {string} readXObjParamOptionAsStr
213
+ * @deprecated
214
+ * @todo \[since `v0.2.1`] deprecated and will be removed
215
+ */
216
+ /**
217
+ * A virtual constant meant for support jsdoc notation:
218
+ * @type {readXObjParamOptionAsStr}
219
+ */
220
+ module.exports.readXObjParamOptionAsStr = '';
221
+
222
+ /**
223
+ * Extracts a parameter from a given object and returns it as a string.
224
+ * @function readXObjParamEx
225
+ * @param {INode} obj - some object
226
+ * @param {readXObjParamOptionAsStr|IValueToStringTransformOptions} [opt] - options
227
+ * @param {string} [key] - some key
228
+ * @returns {string}
229
+ * @throws {TypeError} if `obj` param is not an object
230
+ * @todo [since `v0.2.1`] deprecate use of `opt` as `string`
231
+ */
232
+ function readXObjParamEx(obj, opt, key) {
233
+ let result = undefined;
234
+ try {
235
+ result = readXObjParamRaw(obj, key);
236
+ } catch (err) {
237
+ switch (err.code) {
238
+ case XOBJ_TE_NSTR_ECODE : {
239
+ break;
240
+ }
241
+ default: {
242
+ throw err;
243
+ }
244
+ };
245
+ };
246
+ const _opt = isPlainObject(opt) ? opt : {
247
+ useTrim: false,
248
+ numberToString: true,
249
+ boolToString: true,
250
+ defValue: opt,
251
+ };
252
+ return readAsString(result, _opt);
253
+ };
254
+ module.exports.readXObjParamEx = readXObjParamEx;
255
+
256
+ /**
257
+ * Tries to convert a given value to a string and writes it as a parameter
258
+ * into a given object.
259
+ * @function writeXObjParam
260
+ * @param {INode} obj - some object
261
+ * @param {any} value - some value
262
+ * @param {string} [key] - some key
263
+ * @returns {boolean}
264
+ * @throws {TypeError} if `obj` param is not an object
265
+ */
266
+ function writeXObjParam(obj, value, key) {
267
+ let isSucceed = false;
268
+ try {
269
+ const opt = {
270
+ useTrim: false,
271
+ numberToString: true,
272
+ boolToString: true,
273
+ defValue: '',
274
+ };
275
+ isSucceed = writeXObjParamEx(obj, value, opt, key);
276
+ } catch (err) {
277
+ throw err;
278
+ };
279
+ return isSucceed;
280
+ };
281
+ module.exports.writeXObjParam = writeXObjParam;
282
+
283
+ /**
284
+ * Tries to convert a given value to a boolean and writes it as a parameter
285
+ * into a given object.
286
+ * @function writeXObjParamAsBool
287
+ * @param {INode} obj - some object
288
+ * @param {any} value - some value
289
+ * @param {boolean} [defValue] - default value
290
+ * @param {string} [key] - some key
291
+ * @returns {boolean}
292
+ * @throws {TypeError} if `obj` param is not an object
293
+ */
294
+ function writeXObjParamAsBool(obj, value, defValue, key) {
295
+ let isSucceed = false;
296
+ if (value !== undefined || typeof defValue === 'boolean') {
297
+ const _value = readAsBoolEx(value, defValue).toString();
298
+ try {
299
+ isSucceed = writeXObjParamRaw(obj, _value, key);
300
+ } catch (err) {
301
+ switch (err.code) {
302
+ case XOBJ_TE_NSTR_ECODE : {
303
+ break;
304
+ }
305
+ default: {
306
+ throw err;
307
+ }
308
+ };
309
+ };
310
+ };
311
+ return isSucceed;
312
+ };
313
+ module.exports.writeXObjParamAsBool = writeXObjParamAsBool;
314
+
315
+ /**
316
+ * Tries to convert a given value to a number and writes it as a parameter
317
+ * into a given object.
318
+ * @function writeXObjParamAsNum
319
+ * @param {INode} obj - some object
320
+ * @param {any} value - some value
321
+ * @param {number} [defValue] - default value
322
+ * @param {string} [key] - some key
323
+ * @returns {boolean}
324
+ * @throws {TypeError} if `obj` param is not an object
325
+ */
326
+ function writeXObjParamAsNum(obj, value, defValue, key) {
327
+ let isSucceed = false;
328
+ if (value !== undefined || typeof defValue === 'number') {
329
+ const _value = readAsNumberEx(value, defValue).toString();
330
+ try {
331
+ isSucceed = writeXObjParamRaw(obj, _value, key);
332
+ } catch (err) {
333
+ switch (err.code) {
334
+ case XOBJ_TE_NSTR_ECODE : {
335
+ break;
336
+ }
337
+ default: {
338
+ throw err;
339
+ }
340
+ };
341
+ };
342
+ };
343
+ return isSucceed;
344
+ };
345
+ module.exports.writeXObjParamAsNum = writeXObjParamAsNum;
346
+
347
+ /**
348
+ * Tries to convert a given value into an 'index' value and writes it
349
+ * as a parameter into a given object.
350
+ * @function writeXObjParamAsIndex
351
+ * @param {INode} obj - some object
352
+ * @param {any} value - some value
353
+ * @param {string} [key] - some key
354
+ * @returns {boolean}
355
+ * @throws {TypeError} if `obj` param is not an object
356
+ */
357
+ function writeXObjParamAsIndex(obj, value, key) {
358
+ let isSucceed = false;
359
+ if (value !== undefined) {
360
+ const _value = valueToIndex(value).toString();
361
+ try {
362
+ isSucceed = writeXObjParamRaw(obj, _value, key);
363
+ } catch (err) {
364
+ switch (err.code) {
365
+ case XOBJ_TE_NSTR_ECODE : {
366
+ break;
367
+ }
368
+ default: {
369
+ throw err;
370
+ }
371
+ };
372
+ };
373
+ };
374
+ return isSucceed;
375
+ };
376
+ module.exports.writeXObjParamAsIndex = writeXObjParamAsIndex;
377
+
378
+ /**
379
+ *
380
+ * @typedef {string} writeXObjParamOptionAsStr
381
+ * @deprecated
382
+ * @todo \[since `v0.2.1`] deprecated and will be removed
383
+ */
384
+ /**
385
+ * A virtual constant meant for support jsdoc notation:
386
+ * @type {writeXObjParamOptionAsStr}
387
+ */
388
+ module.exports.writeXObjParamOptionAsStr = '';
389
+
390
+ /**
391
+ * Tries to convert a given value to a string and writes it
392
+ * as a parameter into a given object.
393
+ * @function writeXObjParamEx
394
+ * @param {INode} obj - some object
395
+ * @param {any} value - some value
396
+ * @param {writeXObjParamOptionAsStr|IValueToStringTransformOptions} [opt] - options
397
+ * @param {string} [key] - some key
398
+ * @returns {boolean}
399
+ * @throws {TypeError} if `obj` param is not an object
400
+ * @todo [since `v0.2.1`] deprecate use of `opt` as `string`
401
+ */
402
+ function writeXObjParamEx(obj, value, opt, key) {
403
+ let isSucceed = false;
404
+ if (value !== undefined && typeof value !== 'function') {
405
+ let _opt = opt;
406
+ if (!isPlainObject(_opt)) {
407
+ const defValue = readAsString(_opt, {
408
+ useTrim: false,
409
+ numberToString: true,
410
+ boolToString: true,
411
+ });
412
+ _opt = {
413
+ useTrim: false,
414
+ numberToString: true,
415
+ boolToString: true,
416
+ defValue,
417
+ };
418
+ };
419
+ const _value = readAsString(value, _opt);
420
+ try {
421
+ isSucceed = writeXObjParamRaw(obj, _value, key);
422
+ } catch (err) {
423
+ switch (err.code) {
424
+ case XOBJ_TE_NSTR_ECODE : {
425
+ break;
426
+ }
427
+ default: {
428
+ throw err;
429
+ }
430
+ };
431
+ };
432
+ };
433
+ return isSucceed;
434
+ };
435
+ module.exports.writeXObjParamEx = writeXObjParamEx;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ygracs/xobj-lib-js",
3
- "version": "0.3.1",
3
+ "version": "0.3.3",
4
4
  "description": "A helper library to work with xml-js module",
5
5
  "author": "ygracs <cs70th-om@rambler.ru>",
6
6
  "license": "MIT",
@@ -15,6 +15,8 @@
15
15
  "lib/xObj-lib.js",
16
16
  "lib/xObj-defs.js",
17
17
  "lib/xObj-errors.js",
18
+ "lib/xObj-attr.js",
19
+ "lib/xObj-param.js",
18
20
  "lib/*.d.ts",
19
21
  "index.js",
20
22
  "index.d.ts",
@@ -35,7 +37,7 @@
35
37
  },
36
38
  "devDependencies": {
37
39
  "@ygracs/test-helper": "~0.0.2-b",
38
- "jest": "^30.2.0",
40
+ "jest": "^30.4.2",
39
41
  "jsdoc-to-markdown": "^9.1.3",
40
42
  "minimist": "^1.2.8",
41
43
  "typescript": "~5.9.3"