@ygracs/xobj-lib-js 0.3.1 → 0.3.2

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