@ygracs/xobj-lib-js 0.2.8-b → 0.2.9-b.1
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 +2 -0
- package/doc/xObj.md +62 -6
- package/lib/xObj-defs.js +1 -19
- package/lib/xObj-lib.d.ts +66 -21
- package/lib/xObj-lib.js +230 -177
- package/package.json +4 -4
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,8 @@ Release version.
|
|
|
4
4
|
|
|
5
5
|
> - update `xObj.md`;
|
|
6
6
|
> - updated dependency on `@ygracs/bsfoc-lib-js` module to v0.3.3;
|
|
7
|
+
> - change behavior for function: `evalKeyName` on handling `opt` parameter;
|
|
8
|
+
> - change behavior for function: `writeXObjParamRaw`, `writeXObjAttrRaw`;
|
|
7
9
|
> - add a new `EvalKeyNameError` class;
|
|
8
10
|
> - some fixes.
|
|
9
11
|
|
package/doc/xObj.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
>|***rev.*:**|0.1.
|
|
1
|
+
>|***rev.*:**|0.1.47|
|
|
2
2
|
>|:---|---:|
|
|
3
|
-
>|date:|
|
|
3
|
+
>|date:|2026-02-14|
|
|
4
4
|
|
|
5
5
|
## Introduction
|
|
6
6
|
|
|
@@ -10,6 +10,16 @@ This paper describes a functions provided by `xObj.js` module.
|
|
|
10
10
|
|
|
11
11
|
## Content
|
|
12
12
|
|
|
13
|
+
- Base types
|
|
14
|
+
- <a href="#typedef+xml2jsParseOptions">xml2jsParseOptions</a>
|
|
15
|
+
- <a href="#typedef+js2xmlParseOptions">js2xmlParseOptions</a>
|
|
16
|
+
|
|
17
|
+
- Classes
|
|
18
|
+
- <a href="#TXmlContentParseOptions">TXmlContentParseOptions</a>
|
|
19
|
+
- <a href="#EvalKeyNameError">EvalKeyNameError</a>
|
|
20
|
+
|
|
21
|
+
- Functions
|
|
22
|
+
|
|
13
23
|
### Base type definitions
|
|
14
24
|
|
|
15
25
|
This section contains some definitions for a general types of the objects (e.g. options set) that frequently used in a function descriptions.
|
|
@@ -116,10 +126,8 @@ The settings listed in the table below:
|
|
|
116
126
|
| `elementsKey` | `string` | `items` ||
|
|
117
127
|
| `ignoreDeclaration` | `boolean` | `false` ||
|
|
118
128
|
| `ignoreDocType` | `boolean` | `false` ||
|
|
119
|
-
| `ignoreInstractions` | `boolean` | `false` | \<*deprecated*> (*use `ignoreInstraction` instead*) |
|
|
120
129
|
| `ignoreInstraction` | `boolean` | `false` ||
|
|
121
130
|
| `ignoreText` | `boolean` | `false` ||
|
|
122
|
-
| `ignoreComments` | `boolean` | `false` | \<*deprecated*> (*use `ignoreComment` instead*) |
|
|
123
131
|
| `ignoreComment` | `boolean` | `false` ||
|
|
124
132
|
| `ignoreCData` | `boolean` | `false` ||
|
|
125
133
|
| `fullTagEmptyElement` | `boolean` | `true` ||
|
|
@@ -1214,7 +1222,7 @@ The class constructor receives an arguments listed below:
|
|
|
1214
1222
|
|
|
1215
1223
|
| parameter name | value type | default value | description |
|
|
1216
1224
|
|:---|---|---:|:---|
|
|
1217
|
-
| `options` | `object` | --- |
|
|
1225
|
+
| `options` | `object` | --- | some initial options settings |
|
|
1218
1226
|
|
|
1219
1227
|
##### class properties
|
|
1220
1228
|
|
|
@@ -1232,6 +1240,8 @@ The class constructor receives an arguments listed below:
|
|
|
1232
1240
|
|---|---|:---|
|
|
1233
1241
|
| `xml2jsParseOptions` | yes | returns an options for an XML-to-JS converter |
|
|
1234
1242
|
|
|
1243
|
+
for more details see [`xml2jsParseOptions`](#typedef+xml2jsParseOptions).
|
|
1244
|
+
|
|
1235
1245
|
<a name="TXmlContentParseOptions+js2xml"></a>
|
|
1236
1246
|
###### **js2xml**
|
|
1237
1247
|
|
|
@@ -1239,12 +1249,14 @@ The class constructor receives an arguments listed below:
|
|
|
1239
1249
|
|---|---|:---|
|
|
1240
1250
|
| `js2xmlParseOptions` | yes | returns an options for a JS-to-XML converter |
|
|
1241
1251
|
|
|
1252
|
+
for more details see [`js2xmlParseOptions`](#typedef+js2xmlParseOptions).
|
|
1253
|
+
|
|
1242
1254
|
<a name="TXmlContentParseOptions+reservedKeys"></a>
|
|
1243
1255
|
###### **reservedKeys**
|
|
1244
1256
|
|
|
1245
1257
|
| property type | read only | description |
|
|
1246
1258
|
|---|---|:---|
|
|
1247
|
-
| `Set<string>` | yes | returns a set of the reserved key words those must not be used as an element names
|
|
1259
|
+
| `Set<string>` | yes | returns a set of the reserved key words those must not be used as an element names |
|
|
1248
1260
|
|
|
1249
1261
|
##### class methods (*static*)
|
|
1250
1262
|
|
|
@@ -1252,3 +1264,47 @@ The class constructor receives an arguments listed below:
|
|
|
1252
1264
|
###### **createNewOptionsSet(obj)** => `object`
|
|
1253
1265
|
|
|
1254
1266
|
This method transforms a given object to a set of accepted parser options.
|
|
1267
|
+
|
|
1268
|
+
| parameter name | value type | default value | description |
|
|
1269
|
+
|:---|---|---:|:---|
|
|
1270
|
+
| `obj` | `object` | --- | some initial options settings |
|
|
1271
|
+
|
|
1272
|
+
### Special class
|
|
1273
|
+
|
|
1274
|
+
<a name="EvalKeyNameError"></a>
|
|
1275
|
+
#### **EvalKeyNameError**
|
|
1276
|
+
|
|
1277
|
+
> since: \[v0.2.x]
|
|
1278
|
+
|
|
1279
|
+
This class extends a functionality of a `TypeError`-object to provide more accurate information when an error happened through a key evaluation.
|
|
1280
|
+
|
|
1281
|
+
> NOTE: the class is in a `beta`-stage.
|
|
1282
|
+
|
|
1283
|
+
#### class constructor
|
|
1284
|
+
|
|
1285
|
+
The class constructor creates a new instance of the class.
|
|
1286
|
+
|
|
1287
|
+
##### constructor parameters
|
|
1288
|
+
|
|
1289
|
+
The class constructor receives an arguments listed below:
|
|
1290
|
+
|
|
1291
|
+
| parameter name | value type | default value | description |
|
|
1292
|
+
|:---|---|---:|:---|
|
|
1293
|
+
| `msg` | `string` | --- | some user error message |
|
|
1294
|
+
| `options` | `any` | --- | user options |
|
|
1295
|
+
|
|
1296
|
+
#### class properties
|
|
1297
|
+
|
|
1298
|
+
<a name="EvalKeyNameError+name"></a>
|
|
1299
|
+
##### **name**
|
|
1300
|
+
|
|
1301
|
+
| property type | read only | description |
|
|
1302
|
+
|---|---|:---|
|
|
1303
|
+
| `string` | yes | represents the name for the type of error |
|
|
1304
|
+
|
|
1305
|
+
<a name="EvalKeyNameError+code"></a>
|
|
1306
|
+
##### **code**
|
|
1307
|
+
|
|
1308
|
+
| property type | read only | description |
|
|
1309
|
+
|---|---|:---|
|
|
1310
|
+
| `string` | yes | represents an error code |
|
package/lib/xObj-defs.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// [v0.1.
|
|
1
|
+
// [v0.1.068-20260203]
|
|
2
2
|
|
|
3
3
|
// === module init block ===
|
|
4
4
|
|
|
@@ -10,10 +10,6 @@ const {
|
|
|
10
10
|
|
|
11
11
|
// === module main block ===
|
|
12
12
|
|
|
13
|
-
/***
|
|
14
|
-
* (* constant definitions *)
|
|
15
|
-
*/
|
|
16
|
-
|
|
17
13
|
const XOBJ_DEF_PARAM_TNAME = '__text';
|
|
18
14
|
const XOBJ_DEF_ATTR_TNAME = '__attr';
|
|
19
15
|
const XOBJ_DEF_DECL_TNAME = '__decl';
|
|
@@ -25,8 +21,6 @@ const XOBJ_DEF_TNAMES = {
|
|
|
25
21
|
XOBJ_DEF_DECL_TNAME,
|
|
26
22
|
XOBJ_DEF_CDATA_TNAME,
|
|
27
23
|
};
|
|
28
|
-
//module.exports.XOBJ_DEF_PARAM_TNAME = XOBJ_DEF_PARAM_TNAME;
|
|
29
|
-
//module.exports.XOBJ_DEF_ATTR_TNAME = XOBJ_DEF_ATTR_TNAME;
|
|
30
24
|
module.exports.XOBJ_DEF_TNAMES = XOBJ_DEF_TNAMES;
|
|
31
25
|
|
|
32
26
|
/**
|
|
@@ -68,12 +62,8 @@ const DEF_XML_PARSE_OPTIONS = {
|
|
|
68
62
|
elementsKey: 'items',
|
|
69
63
|
ignoreDeclaration: false,
|
|
70
64
|
ignoreDocType: false,
|
|
71
|
-
/** @deprecated */
|
|
72
|
-
ignoreInstractions: false,
|
|
73
65
|
ignoreInstraction: false,
|
|
74
66
|
ignoreText: false,
|
|
75
|
-
/** @deprecated */
|
|
76
|
-
ignoreComments: false,
|
|
77
67
|
ignoreComment: false,
|
|
78
68
|
ignoreCData: false,
|
|
79
69
|
fullTagEmptyElement: true,
|
|
@@ -83,14 +73,6 @@ const DEF_XML_PARSE_OPTIONS = {
|
|
|
83
73
|
};
|
|
84
74
|
module.exports.DEF_XML_PARSE_OPTIONS = DEF_XML_PARSE_OPTIONS;
|
|
85
75
|
|
|
86
|
-
/***
|
|
87
|
-
* (* function definitions *)
|
|
88
|
-
*/
|
|
89
|
-
|
|
90
|
-
/***
|
|
91
|
-
* (* class definitions *)
|
|
92
|
-
*/
|
|
93
|
-
|
|
94
76
|
/**
|
|
95
77
|
* A parser options to convert an 'XML'-string into a 'JS'-object
|
|
96
78
|
* @typedef {Object} xml2jsParseOptions
|
package/lib/xObj-lib.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { EvalKeyNameError } from "./xObj-errors";
|
|
1
2
|
/**
|
|
2
3
|
* A result of a value check ops.
|
|
3
4
|
*/
|
|
@@ -11,6 +12,45 @@ export type RVAL_reason = {
|
|
|
11
12
|
*/
|
|
12
13
|
msg: string;
|
|
13
14
|
};
|
|
15
|
+
/**
|
|
16
|
+
* An options setting for `evalKeyName` function
|
|
17
|
+
*/
|
|
18
|
+
export type IEvalKeyNameOptions = {
|
|
19
|
+
/**
|
|
20
|
+
* - a flag to accept only a non-empty string values
|
|
21
|
+
*/
|
|
22
|
+
onlyNES?: boolean | undefined;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* A result of `evalKeyName` function if succeed
|
|
26
|
+
*/
|
|
27
|
+
export type IEvalKeyNameResultOnSuccess = {
|
|
28
|
+
/**
|
|
29
|
+
* - ops flag
|
|
30
|
+
*/
|
|
31
|
+
isSucceed: true;
|
|
32
|
+
/**
|
|
33
|
+
* - result value
|
|
34
|
+
*/
|
|
35
|
+
value: string;
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* A result of `evalKeyName` function if failed
|
|
39
|
+
*/
|
|
40
|
+
export type IEvalKeyNameResultOnFail = {
|
|
41
|
+
/**
|
|
42
|
+
* - ops flag
|
|
43
|
+
*/
|
|
44
|
+
isSucceed: false;
|
|
45
|
+
/**
|
|
46
|
+
* - reason of fail
|
|
47
|
+
*/
|
|
48
|
+
value: RVAL_reason;
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* A result of `evalKeyName` function
|
|
52
|
+
*/
|
|
53
|
+
export type IEvalKeyNameResult = IEvalKeyNameResultOnSuccess | IEvalKeyNameResultOnFail;
|
|
14
54
|
/**
|
|
15
55
|
* A result of a value check ops.
|
|
16
56
|
*/
|
|
@@ -73,7 +113,7 @@ export type OPT_inselops_S = {
|
|
|
73
113
|
/**
|
|
74
114
|
* Extracts a parameter 'AS IS' from a given object.
|
|
75
115
|
* @throws {TypeError} if first param is not an object
|
|
76
|
-
* @throws {
|
|
116
|
+
* @throws {EvalKeyNameError} if second param is not valid identifier
|
|
77
117
|
*/
|
|
78
118
|
export function readXObjParamRaw(obj: object, key?: string): any;
|
|
79
119
|
/**
|
|
@@ -105,7 +145,7 @@ export function readXObjParamAsIndex(obj: object, key?: string): number;
|
|
|
105
145
|
/**
|
|
106
146
|
* Writes a parameter 'AS IS' into a given object.
|
|
107
147
|
* @throws {TypeError} if first param is not an object
|
|
108
|
-
* @throws {
|
|
148
|
+
* @throws {EvalKeyNameError} if third param is not valid identifier
|
|
109
149
|
*/
|
|
110
150
|
export function writeXObjParamRaw(obj: object, value: any, key?: string): boolean;
|
|
111
151
|
/**
|
|
@@ -142,7 +182,7 @@ export function writeXObjParamEx(obj: object, value: any, opt?: object, key?: st
|
|
|
142
182
|
/**
|
|
143
183
|
* Extracts an attribute 'AS IS' from a given object.
|
|
144
184
|
* @throws {TypeError} if first param is not an object
|
|
145
|
-
* @throws {
|
|
185
|
+
* @throws {EvalKeyNameError} if second param is not valid identifier
|
|
146
186
|
*/
|
|
147
187
|
export function readXObjAttrRaw(obj: object, attr?: string, key?: string): any;
|
|
148
188
|
/**
|
|
@@ -175,7 +215,7 @@ export function readXObjAttrAsIndex(obj: object, attr: string, key?: string): nu
|
|
|
175
215
|
/**
|
|
176
216
|
* Writes a parameter into a given object.
|
|
177
217
|
* @throws {TypeError} if first param is not an object
|
|
178
|
-
* @throws {
|
|
218
|
+
* @throws {EvalKeyNameError} if second param is not valid identifier
|
|
179
219
|
*/
|
|
180
220
|
export function writeXObjAttrRaw(obj: object, attr: string, value: any, key?: string): boolean;
|
|
181
221
|
/**
|
|
@@ -217,66 +257,70 @@ export function getXObjAttributes(obj: object, key?: string): object | null;
|
|
|
217
257
|
/**
|
|
218
258
|
* Checks whether an attribute is exists.
|
|
219
259
|
* @throws {TypeError} if first param is not an object
|
|
260
|
+
* @throws {EvalKeyNameError} if second param is not valid identifier
|
|
220
261
|
*/
|
|
221
262
|
export function checkXObjAttribute(obj: object, attr?: string, key?: string): boolean;
|
|
222
263
|
/**
|
|
223
264
|
* Deletes an attribute addressed by a given name.
|
|
224
265
|
* @throws {TypeError} if first param is not an object
|
|
266
|
+
* @throws {EvalKeyNameError} if second param is not valid identifier
|
|
225
267
|
*/
|
|
226
268
|
export function deleteXObjAttribute(obj: object, attr?: string, key?: string): boolean;
|
|
227
269
|
/**
|
|
228
270
|
* Renames an attribute addressed by a given name.
|
|
229
271
|
* @since 0.2.0
|
|
230
272
|
* @throws {TypeError} if first param is not an object
|
|
231
|
-
* @throws {
|
|
232
|
-
* @throws {
|
|
273
|
+
* @throws {EvalKeyNameError} if second param is not valid identifier
|
|
274
|
+
* @throws {EvalKeyNameError} if third param is not valid identifier
|
|
275
|
+
* @throws {EvalKeyNameError} if forth param is not valid identifier
|
|
233
276
|
*/
|
|
234
277
|
export function renameXObjAttribute(obj: object, attr?: string, value?: string, key?: string): boolean;
|
|
235
278
|
/**
|
|
236
279
|
* Extracts an element from a given object by its key.
|
|
237
280
|
* @throws {TypeError} if first param is not an object
|
|
238
|
-
* @throws {
|
|
281
|
+
* @throws {EvalKeyNameError} if second param is not valid identifier
|
|
239
282
|
*/
|
|
240
283
|
export function getXObjElement(obj: object, name: string): any | null;
|
|
241
284
|
/**
|
|
242
285
|
* Adds an element addressed by its key to a given object.
|
|
243
286
|
* @throws {TypeError} if first param is not an object
|
|
244
|
-
* @throws {
|
|
287
|
+
* @throws {EvalKeyNameError} if second param is not valid identifier
|
|
245
288
|
*/
|
|
246
289
|
export function addXObjElement(obj: object, name: string): RVAL_emodif;
|
|
247
290
|
/**
|
|
248
291
|
* Inserts an element addressed by its key into a given object.
|
|
249
292
|
* @throws {TypeError} if first param is not an object
|
|
250
|
-
* @see insertXObjElementEx
|
|
293
|
+
* @see {@link insertXObjElementEx} for param details
|
|
251
294
|
*/
|
|
252
|
-
export function insertXObjElement(...args:
|
|
295
|
+
export function insertXObjElement(...args: insertXObjElementParams): object | null;
|
|
296
|
+
type insertXObjElementParams = Parameters<typeof insertXObjElementEx>;
|
|
253
297
|
/**
|
|
254
298
|
* Inserts an element addressed by its key into a given object.
|
|
255
299
|
* @since 0.2.0
|
|
256
300
|
* @throws {TypeError} if first param is not an object
|
|
257
|
-
* @throws {
|
|
301
|
+
* @throws {EvalKeyNameError} if second param is not valid identifier
|
|
258
302
|
*/
|
|
259
303
|
export function insertXObjElementEx(obj: object, name: string, opt?: OPT_inselops_L): RVAL_emodif;
|
|
260
304
|
/**
|
|
261
305
|
* Deletes an element addressed by its key from a given object.
|
|
262
306
|
* @throws {TypeError} if first param is not an object
|
|
263
|
-
* @see deleteXObjElementEx
|
|
307
|
+
* @see {@link deleteXObjElementEx} for param details
|
|
264
308
|
*/
|
|
265
|
-
export function deleteXObjElement(...args:
|
|
309
|
+
export function deleteXObjElement(...args: deleteXObjElementParams): boolean;
|
|
310
|
+
type deleteXObjElementParams = Parameters<typeof deleteXObjElementEx>;
|
|
266
311
|
/**
|
|
267
312
|
* Deletes an element addressed by its key from a given object.
|
|
268
313
|
* @throws {TypeError} if first param is not an object
|
|
269
|
-
* @throws {
|
|
314
|
+
* @throws {EvalKeyNameError} if second param is not valid identifier
|
|
270
315
|
*/
|
|
271
316
|
export function deleteXObjElementEx(obj: object, name: string): RVAL_emodif;
|
|
272
317
|
/**
|
|
273
318
|
* Renames an element addressed by its key.
|
|
274
319
|
* @throws {TypeError} if first param is not an object
|
|
275
|
-
* @throws {
|
|
276
|
-
* @throws {
|
|
320
|
+
* @throws {EvalKeyNameError} if second param is not valid identifier
|
|
321
|
+
* @throws {EvalKeyNameError} if third param is not valid identifier
|
|
277
322
|
*/
|
|
278
323
|
export function renameXObjElement(obj: object, name?: string, value?: string): boolean;
|
|
279
|
-
|
|
280
324
|
/**
|
|
281
325
|
* Tries to convert a value into an ID.
|
|
282
326
|
*/
|
|
@@ -286,19 +330,20 @@ export function evalXObjEName(value: any): null | number | string;
|
|
|
286
330
|
* @throws {TypeError} if first param is not an object
|
|
287
331
|
* @see insertXObjEListEx
|
|
288
332
|
*/
|
|
289
|
-
export function insertXObjEList(...args:
|
|
333
|
+
export function insertXObjEList(...args: insertXObjEListParams): object | object[] | null;
|
|
334
|
+
type insertXObjEListParams = Parameters<typeof insertXObjEListEx>;
|
|
290
335
|
/**
|
|
291
336
|
* Inserts a list elements into a given object.
|
|
292
337
|
* @since 0.2.0
|
|
293
338
|
* @throws {TypeError} if first param is not an object
|
|
294
|
-
* @throws {
|
|
339
|
+
* @throws {EvalKeyNameError} if second param is not valid identifier
|
|
295
340
|
*/
|
|
296
341
|
export function insertXObjEListEx(obj: object, name: string, opt?: OPT_inselops_S): RVAL_emodif;
|
|
297
342
|
/**
|
|
298
343
|
* Tries to convert a value into an ID.
|
|
299
344
|
* @inner
|
|
300
345
|
*/
|
|
301
|
-
export function evalKeyName(value: any, opt?: boolean):
|
|
346
|
+
export function evalKeyName(value: any, opt?: boolean | IEvalKeyNameOptions): IEvalKeyNameResult;
|
|
302
347
|
/**
|
|
303
348
|
* Tries to convert a value into an element name description.
|
|
304
349
|
*/
|
|
@@ -313,4 +358,4 @@ export function insertXObjElements(obj: object, ...args: any[]): number;
|
|
|
313
358
|
* @throws {TypeError} if first param is not an object
|
|
314
359
|
*/
|
|
315
360
|
export function insertXObjEChain(obj: object, ...args: any[]): any | null;
|
|
316
|
-
export {}
|
|
361
|
+
export {}
|
package/lib/xObj-lib.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// [v0.3.
|
|
1
|
+
// [v0.3.120-20260213]
|
|
2
2
|
|
|
3
3
|
// === module init block ===
|
|
4
4
|
|
|
@@ -42,22 +42,55 @@ const {
|
|
|
42
42
|
* @property {string} msg - message text
|
|
43
43
|
*/
|
|
44
44
|
|
|
45
|
+
/**
|
|
46
|
+
* An options setting for `evalKeyName` function
|
|
47
|
+
* @since 0.2.x
|
|
48
|
+
* @typedef {Object} IEvalKeyNameOptions
|
|
49
|
+
* @property {boolean} [onlyNES] - a flag to accept only a non-empty string values
|
|
50
|
+
*/
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* A result of `evalKeyName` function if succeed
|
|
54
|
+
* @since 0.2.x
|
|
55
|
+
* @typedef {Object} IEvalKeyNameResultOnSuccess
|
|
56
|
+
* @property {true} isSucceed - ops flag
|
|
57
|
+
* @property {string} value - result value
|
|
58
|
+
*/
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* A result of `evalKeyName` function if failed
|
|
62
|
+
* @since 0.2.x
|
|
63
|
+
* @typedef {Object} IEvalKeyNameResultOnFail
|
|
64
|
+
* @property {false} isSucceed - ops flag
|
|
65
|
+
* @property {RVAL_reason} value - reason of fail
|
|
66
|
+
*/
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* A result of `evalKeyName` function
|
|
70
|
+
* @since 0.2.x
|
|
71
|
+
* @typedef {(IEvalKeyNameResultOnSuccess|IEvalKeyNameResultOnFail)} IEvalKeyNameResult
|
|
72
|
+
*/
|
|
73
|
+
|
|
45
74
|
/**
|
|
46
75
|
* A result of a value check ops.
|
|
47
76
|
* @typedef {Object} VCOR_evalkname
|
|
48
77
|
* @property {boolean} isSucceed - ops flag
|
|
49
78
|
* @property {(string|RVAL_reason)} value - result value or reson if failed
|
|
79
|
+
* @todo \[from v0.2.x] make obsolete. Use `IEvalKeyNameResult` instead.
|
|
50
80
|
*/
|
|
51
81
|
|
|
52
82
|
/**
|
|
53
83
|
* Tries to convert a value into an ID.
|
|
54
84
|
* @function evalKeyName
|
|
55
85
|
* @param {any} value - key to validate
|
|
56
|
-
* @param {boolean} [opt=true]
|
|
57
|
-
* @returns {
|
|
86
|
+
* @param {(boolean|IEvalKeyNameOptions)} [opt=true] - some options
|
|
87
|
+
* @returns {IEvalKeyNameResult}
|
|
58
88
|
* @inner
|
|
59
89
|
*/
|
|
60
90
|
function evalKeyName(value, opt = true) {
|
|
91
|
+
let {
|
|
92
|
+
onlyNES,
|
|
93
|
+
} = isPlainObject(opt) ? opt : { onlyNES: opt };
|
|
61
94
|
if (typeof value !== 'string') {
|
|
62
95
|
return {
|
|
63
96
|
isSucceed: false,
|
|
@@ -68,7 +101,7 @@ function evalKeyName(value, opt = true) {
|
|
|
68
101
|
};
|
|
69
102
|
};
|
|
70
103
|
const key = value.trim();
|
|
71
|
-
if (
|
|
104
|
+
if (onlyNES && key === '') {
|
|
72
105
|
return {
|
|
73
106
|
isSucceed: false,
|
|
74
107
|
value: {
|
|
@@ -202,7 +235,7 @@ function genXObjENameDescr(value) {
|
|
|
202
235
|
* @param {string} name - some child element
|
|
203
236
|
* @returns {?any}
|
|
204
237
|
* @throws {TypeError} if first param is not an object
|
|
205
|
-
* @throws {
|
|
238
|
+
* @throws {EvalKeyNameError} if second param is not valid identifier
|
|
206
239
|
*/
|
|
207
240
|
function getXObjElement(obj, name) {
|
|
208
241
|
if (!isPlainObject(obj)) {
|
|
@@ -211,7 +244,7 @@ function getXObjElement(obj, name) {
|
|
|
211
244
|
{ code: XOBJ_TE_NPOBJ_ECODE },
|
|
212
245
|
);
|
|
213
246
|
};
|
|
214
|
-
|
|
247
|
+
const { isSucceed, value: key } = evalKeyName(name);
|
|
215
248
|
if (isSucceed) {
|
|
216
249
|
// TODO: [?] check type of obj[key_name]
|
|
217
250
|
return obj[key] !== undefined ? obj[key] : null;
|
|
@@ -260,7 +293,7 @@ function getXObjAttributes(obj, key = XOBJ_DEF_ATTR_TNAME) {
|
|
|
260
293
|
* @param {string} name - some child element
|
|
261
294
|
* @returns {RVAL_emodif}
|
|
262
295
|
* @throws {TypeError} if first param is not an object
|
|
263
|
-
* @throws {
|
|
296
|
+
* @throws {EvalKeyNameError} if second param is not valid identifier
|
|
264
297
|
*/
|
|
265
298
|
function addXObjElement(obj, name) {
|
|
266
299
|
if (!isPlainObject(obj)) {
|
|
@@ -269,31 +302,32 @@ function addXObjElement(obj, name) {
|
|
|
269
302
|
{ code: XOBJ_TE_NPOBJ_ECODE },
|
|
270
303
|
);
|
|
271
304
|
};
|
|
272
|
-
|
|
273
|
-
if (
|
|
305
|
+
const { isSucceed, value: key } = evalKeyName(name);
|
|
306
|
+
if (isSucceed) {
|
|
307
|
+
const item = {};
|
|
308
|
+
let prop = obj[key];
|
|
309
|
+
let isSucceed = false;
|
|
310
|
+
// // TODO: [?] consider whether or not do ops if new_key exists
|
|
311
|
+
if (isNullOrUndef(prop)) {
|
|
312
|
+
obj[key] = prop = item;
|
|
313
|
+
isSucceed = true;
|
|
314
|
+
} else if (isObject(prop)) {
|
|
315
|
+
if (isArray(prop)) {
|
|
316
|
+
prop.push(item);
|
|
317
|
+
} else {
|
|
318
|
+
obj[key] = [ prop, item ];
|
|
319
|
+
};
|
|
320
|
+
prop = item;
|
|
321
|
+
isSucceed = true;
|
|
322
|
+
};
|
|
323
|
+
return {
|
|
324
|
+
isSucceed,
|
|
325
|
+
item: isSucceed ? prop : null,
|
|
326
|
+
};
|
|
327
|
+
} else {
|
|
274
328
|
const { code, msg } = key;
|
|
275
329
|
throw new EvalKeyNameError(msg, { code });
|
|
276
330
|
};
|
|
277
|
-
const item = {};
|
|
278
|
-
let prop = obj[key];
|
|
279
|
-
isSucceed = false;
|
|
280
|
-
// // TODO: [?] consider whether or not do ops if new_key exists
|
|
281
|
-
if (isNullOrUndef(prop)) {
|
|
282
|
-
obj[key] = prop = item;
|
|
283
|
-
isSucceed = true;
|
|
284
|
-
} else if (isObject(prop)) {
|
|
285
|
-
if (isArray(prop)) {
|
|
286
|
-
prop.push(item);
|
|
287
|
-
} else {
|
|
288
|
-
obj[key] = [ prop, item ];
|
|
289
|
-
};
|
|
290
|
-
prop = item;
|
|
291
|
-
isSucceed = true;
|
|
292
|
-
};
|
|
293
|
-
return {
|
|
294
|
-
isSucceed,
|
|
295
|
-
item: isSucceed ? prop : null,
|
|
296
|
-
};
|
|
297
331
|
};
|
|
298
332
|
|
|
299
333
|
/**
|
|
@@ -312,7 +346,7 @@ function addXObjElement(obj, name) {
|
|
|
312
346
|
* @param {OPT_inselops_L} [opt] - options
|
|
313
347
|
* @returns {?object}
|
|
314
348
|
* @throws {TypeError} if first param is not an object
|
|
315
|
-
* @see insertXObjElementEx
|
|
349
|
+
* @see {@link insertXObjElementEx} for param details
|
|
316
350
|
*/
|
|
317
351
|
function insertXObjElement(...args) {
|
|
318
352
|
let item = null;
|
|
@@ -341,7 +375,7 @@ function insertXObjElement(...args) {
|
|
|
341
375
|
* @param {OPT_inselops_L} [opt] - options
|
|
342
376
|
* @returns {RVAL_emodif}
|
|
343
377
|
* @throws {TypeError} if first param is not an object
|
|
344
|
-
* @throws {
|
|
378
|
+
* @throws {EvalKeyNameError} if second param is not valid identifier
|
|
345
379
|
*/
|
|
346
380
|
function insertXObjElementEx(obj, name, opt) {
|
|
347
381
|
if (!isPlainObject(obj)) {
|
|
@@ -350,37 +384,38 @@ function insertXObjElementEx(obj, name, opt) {
|
|
|
350
384
|
{ code: XOBJ_TE_NPOBJ_ECODE },
|
|
351
385
|
);
|
|
352
386
|
};
|
|
353
|
-
|
|
354
|
-
if (
|
|
355
|
-
const
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
let prop = obj[key];
|
|
360
|
-
isSucceed = false;
|
|
361
|
-
if (isNullOrUndef(prop)) {
|
|
362
|
-
obj[key] = prop = item;
|
|
363
|
-
isSucceed = true;
|
|
364
|
-
} else {
|
|
365
|
-
/** @type {OPT_inselops_L} */
|
|
366
|
-
let {
|
|
367
|
-
force,
|
|
368
|
-
ripOldies,
|
|
369
|
-
acceptIfList,
|
|
370
|
-
} = isPlainObject(opt) ? opt : {};
|
|
371
|
-
if (typeof force !== 'boolean') force = false;
|
|
372
|
-
if (typeof ripOldies !== 'boolean') ripOldies = false;
|
|
373
|
-
if (typeof acceptIfList !== 'boolean') acceptIfList = false;
|
|
374
|
-
if (force && (ripOldies || !isObject(prop))) {
|
|
387
|
+
const { isSucceed, value: key } = evalKeyName(name);
|
|
388
|
+
if (isSucceed) {
|
|
389
|
+
const item = {};
|
|
390
|
+
let prop = obj[key];
|
|
391
|
+
let isSucceed = false;
|
|
392
|
+
if (isNullOrUndef(prop)) {
|
|
375
393
|
obj[key] = prop = item;
|
|
376
394
|
isSucceed = true;
|
|
377
395
|
} else {
|
|
378
|
-
|
|
396
|
+
/** @type {OPT_inselops_L} */
|
|
397
|
+
let {
|
|
398
|
+
force,
|
|
399
|
+
ripOldies,
|
|
400
|
+
acceptIfList,
|
|
401
|
+
} = isPlainObject(opt) ? opt : {};
|
|
402
|
+
if (typeof force !== 'boolean') force = false;
|
|
403
|
+
if (typeof ripOldies !== 'boolean') ripOldies = false;
|
|
404
|
+
if (typeof acceptIfList !== 'boolean') acceptIfList = false;
|
|
405
|
+
if (force && (ripOldies || !isObject(prop))) {
|
|
406
|
+
obj[key] = prop = item;
|
|
407
|
+
isSucceed = true;
|
|
408
|
+
} else {
|
|
409
|
+
isSucceed = isPlainObject(prop) || (isArray(prop) && acceptIfList);
|
|
410
|
+
};
|
|
379
411
|
};
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
412
|
+
return {
|
|
413
|
+
isSucceed,
|
|
414
|
+
item: isSucceed ? prop : null,
|
|
415
|
+
};
|
|
416
|
+
} else {
|
|
417
|
+
const { code, msg } = key;
|
|
418
|
+
throw new EvalKeyNameError(msg, { code });
|
|
384
419
|
};
|
|
385
420
|
};
|
|
386
421
|
|
|
@@ -391,7 +426,7 @@ function insertXObjElementEx(obj, name, opt) {
|
|
|
391
426
|
* @param {string} name - some child element
|
|
392
427
|
* @returns {boolean}
|
|
393
428
|
* @throws {TypeError} if first param is not an object
|
|
394
|
-
* @see deleteXObjElementEx
|
|
429
|
+
* @see {@link deleteXObjElementEx} for param details
|
|
395
430
|
*/
|
|
396
431
|
function deleteXObjElement(...args) {
|
|
397
432
|
let isSucceed = false;
|
|
@@ -418,7 +453,7 @@ function deleteXObjElement(...args) {
|
|
|
418
453
|
* @param {string} name - some child element
|
|
419
454
|
* @returns {RVAL_emodif}
|
|
420
455
|
* @throws {TypeError} if first param is not an object
|
|
421
|
-
* @throws {
|
|
456
|
+
* @throws {EvalKeyNameError} if second param is not valid identifier
|
|
422
457
|
*/
|
|
423
458
|
function deleteXObjElementEx(obj, name) {
|
|
424
459
|
if (!isPlainObject(obj)) {
|
|
@@ -427,19 +462,20 @@ function deleteXObjElementEx(obj, name) {
|
|
|
427
462
|
{ code: XOBJ_TE_NPOBJ_ECODE },
|
|
428
463
|
);
|
|
429
464
|
};
|
|
430
|
-
|
|
431
|
-
if (
|
|
465
|
+
const { isSucceed, value: key } = evalKeyName(name);
|
|
466
|
+
if (isSucceed) {
|
|
467
|
+
let prop = obj[key];
|
|
468
|
+
let isSucceed = false;
|
|
469
|
+
// // TODO: catch errors in strict mode
|
|
470
|
+
isSucceed = delete obj[key];
|
|
471
|
+
return {
|
|
472
|
+
isSucceed,
|
|
473
|
+
item: isSucceed && isObject(prop) ? prop : null,
|
|
474
|
+
};
|
|
475
|
+
} else {
|
|
432
476
|
const { code, msg } = key;
|
|
433
477
|
throw new EvalKeyNameError(msg, { code });
|
|
434
478
|
};
|
|
435
|
-
let prop = obj[key];
|
|
436
|
-
isSucceed = false;
|
|
437
|
-
// // TODO: catch errors in strict mode
|
|
438
|
-
isSucceed = delete obj[key];
|
|
439
|
-
return {
|
|
440
|
-
isSucceed,
|
|
441
|
-
item: isSucceed && isObject(prop) ? prop : null,
|
|
442
|
-
};
|
|
443
479
|
};
|
|
444
480
|
|
|
445
481
|
/**
|
|
@@ -450,8 +486,8 @@ function deleteXObjElementEx(obj, name) {
|
|
|
450
486
|
* @param {string} value - new element ID
|
|
451
487
|
* @returns {boolean}
|
|
452
488
|
* @throws {TypeError} if first param is not an object
|
|
453
|
-
* @throws {
|
|
454
|
-
* @throws {
|
|
489
|
+
* @throws {EvalKeyNameError} if second param is not valid identifier
|
|
490
|
+
* @throws {EvalKeyNameError} if third param is not valid identifier
|
|
455
491
|
*/
|
|
456
492
|
function renameXObjElement(obj, name = '', value = '') {
|
|
457
493
|
if (!isPlainObject(obj)) {
|
|
@@ -461,12 +497,12 @@ function renameXObjElement(obj, name = '', value = '') {
|
|
|
461
497
|
);
|
|
462
498
|
};
|
|
463
499
|
const opt = false;
|
|
464
|
-
|
|
500
|
+
const { isSucceed: isAcceptON, value: oname } = evalKeyName(name, opt);
|
|
465
501
|
if (!isAcceptON) {
|
|
466
502
|
const { code, msg } = oname;
|
|
467
503
|
throw new EvalKeyNameError(msg, { code });
|
|
468
504
|
};
|
|
469
|
-
|
|
505
|
+
const { isSucceed: isAcceptNN, value: nname } = evalKeyName(value, opt);
|
|
470
506
|
if (!isAcceptNN) {
|
|
471
507
|
const { code, msg } = nname;
|
|
472
508
|
throw new EvalKeyNameError(msg, { code });
|
|
@@ -495,6 +531,7 @@ function renameXObjElement(obj, name = '', value = '') {
|
|
|
495
531
|
* @param {string} [key] - some key
|
|
496
532
|
* @returns {boolean}
|
|
497
533
|
* @throws {TypeError} if first param is not an object
|
|
534
|
+
* @throws {EvalKeyNameError} if second param is not valid identifier
|
|
498
535
|
*/
|
|
499
536
|
function checkXObjAttribute(obj, attr = '', key) {
|
|
500
537
|
let _obj = null;
|
|
@@ -504,7 +541,7 @@ function checkXObjAttribute(obj, attr = '', key) {
|
|
|
504
541
|
throw err;
|
|
505
542
|
};
|
|
506
543
|
if (_obj === null) return false;
|
|
507
|
-
|
|
544
|
+
const { isSucceed, value: name } = evalKeyName(attr, false);
|
|
508
545
|
if (isSucceed) {
|
|
509
546
|
if (name === '') return false;
|
|
510
547
|
} else {
|
|
@@ -522,6 +559,7 @@ function checkXObjAttribute(obj, attr = '', key) {
|
|
|
522
559
|
* @param {string} [key] - some key
|
|
523
560
|
* @returns {boolean}
|
|
524
561
|
* @throws {TypeError} if first param is not an object
|
|
562
|
+
* @throws {EvalKeyNameError} if second param is not valid identifier
|
|
525
563
|
*/
|
|
526
564
|
function deleteXObjAttribute(obj, attr = '', key) {
|
|
527
565
|
let _obj = null;
|
|
@@ -531,17 +569,18 @@ function deleteXObjAttribute(obj, attr = '', key) {
|
|
|
531
569
|
throw err;
|
|
532
570
|
};
|
|
533
571
|
if (_obj === null) return false;
|
|
534
|
-
|
|
535
|
-
if (
|
|
572
|
+
const { isSucceed, value: name } = evalKeyName(attr, false);
|
|
573
|
+
if (isSucceed) {
|
|
574
|
+
let isSucceed = false;
|
|
575
|
+
if (name !== '') {
|
|
576
|
+
// // TODO: catch errors in strict mode
|
|
577
|
+
isSucceed = delete _obj[name];
|
|
578
|
+
};
|
|
579
|
+
return isSucceed;
|
|
580
|
+
} else {
|
|
536
581
|
const { code, msg } = name;
|
|
537
582
|
throw new EvalKeyNameError(msg, { code });
|
|
538
583
|
};
|
|
539
|
-
isSucceed = false;
|
|
540
|
-
if (name !== '') {
|
|
541
|
-
// // TODO: catch errors in strict mode
|
|
542
|
-
isSucceed = delete _obj[name];
|
|
543
|
-
};
|
|
544
|
-
return isSucceed;
|
|
545
584
|
};
|
|
546
585
|
|
|
547
586
|
/**
|
|
@@ -554,8 +593,9 @@ function deleteXObjAttribute(obj, attr = '', key) {
|
|
|
554
593
|
* @param {string} [key] - some key
|
|
555
594
|
* @returns {boolean}
|
|
556
595
|
* @throws {TypeError} if first param is not an object
|
|
557
|
-
* @throws {
|
|
558
|
-
* @throws {
|
|
596
|
+
* @throws {EvalKeyNameError} if second param is not valid identifier
|
|
597
|
+
* @throws {EvalKeyNameError} if third param is not valid identifier
|
|
598
|
+
* @throws {EvalKeyNameError} if forth param is not valid identifier
|
|
559
599
|
*/
|
|
560
600
|
function renameXObjAttribute(obj, attr = '', value = '', key = XOBJ_DEF_ATTR_TNAME) {
|
|
561
601
|
if (!isPlainObject(obj)) {
|
|
@@ -565,37 +605,38 @@ function renameXObjAttribute(obj, attr = '', value = '', key = XOBJ_DEF_ATTR_TNA
|
|
|
565
605
|
);
|
|
566
606
|
};
|
|
567
607
|
const opt = false;
|
|
568
|
-
|
|
569
|
-
if (
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
const {
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
obj[_key] = Object.fromEntries(_obj);
|
|
608
|
+
const { isSucceed, value: _key } = evalKeyName(key, opt);
|
|
609
|
+
if (isSucceed) {
|
|
610
|
+
let isSucceed = false;
|
|
611
|
+
if (_key !== '') {
|
|
612
|
+
let _obj = obj[_key];
|
|
613
|
+
if (isPlainObject(_obj)) {
|
|
614
|
+
const { isSucceed: isAcceptON, value: oname } = evalKeyName(attr, opt);
|
|
615
|
+
if (!isAcceptON) {
|
|
616
|
+
const { code, msg } = oname;
|
|
617
|
+
throw new EvalKeyNameError(msg, { code });
|
|
618
|
+
};
|
|
619
|
+
const { isSucceed: isAcceptNN, value: nname } = evalKeyName(value, opt);
|
|
620
|
+
if (!isAcceptNN) {
|
|
621
|
+
const { code, msg } = nname;
|
|
622
|
+
throw new EvalKeyNameError(msg, { code });
|
|
623
|
+
};
|
|
624
|
+
if (oname !== '' && oname in _obj && nname !== '') {
|
|
625
|
+
if (oname !== nname) {
|
|
626
|
+
_obj = Object.entries(_obj);
|
|
627
|
+
const index = _obj.findIndex((item) => item[0] === oname);
|
|
628
|
+
_obj[index][0] = nname;
|
|
629
|
+
obj[_key] = Object.fromEntries(_obj);
|
|
630
|
+
};
|
|
631
|
+
isSucceed = true;
|
|
593
632
|
};
|
|
594
|
-
isSucceed = true;
|
|
595
633
|
};
|
|
596
634
|
};
|
|
635
|
+
return isSucceed;
|
|
636
|
+
} else {
|
|
637
|
+
const { code, msg } = _key;
|
|
638
|
+
throw new EvalKeyNameError(msg, { code });
|
|
597
639
|
};
|
|
598
|
-
return isSucceed;
|
|
599
640
|
};
|
|
600
641
|
|
|
601
642
|
/**
|
|
@@ -605,7 +646,7 @@ function renameXObjAttribute(obj, attr = '', value = '', key = XOBJ_DEF_ATTR_TNA
|
|
|
605
646
|
* @param {string} [key=XOBJ_DEF_PARAM_TNAME] - some key
|
|
606
647
|
* @returns {any}
|
|
607
648
|
* @throws {TypeError} if first param is not an object
|
|
608
|
-
* @throws {
|
|
649
|
+
* @throws {EvalKeyNameError} if second param is not valid identifier
|
|
609
650
|
*/
|
|
610
651
|
function readXObjParamRaw(obj, key = XOBJ_DEF_PARAM_TNAME) {
|
|
611
652
|
if (!isPlainObject(obj)) {
|
|
@@ -614,7 +655,7 @@ function readXObjParamRaw(obj, key = XOBJ_DEF_PARAM_TNAME) {
|
|
|
614
655
|
{ code: XOBJ_TE_NPOBJ_ECODE },
|
|
615
656
|
);
|
|
616
657
|
};
|
|
617
|
-
|
|
658
|
+
const { isSucceed, value: _key } = evalKeyName(key, false);
|
|
618
659
|
if (!isSucceed) {
|
|
619
660
|
const { code, msg } = _key;
|
|
620
661
|
throw new EvalKeyNameError(msg, { code });
|
|
@@ -630,7 +671,7 @@ function readXObjParamRaw(obj, key = XOBJ_DEF_PARAM_TNAME) {
|
|
|
630
671
|
* @param {string} [key=XOBJ_DEF_PARAM_TNAME] - some key
|
|
631
672
|
* @returns {boolean}
|
|
632
673
|
* @throws {TypeError} if first param is not an object
|
|
633
|
-
* @throws {
|
|
674
|
+
* @throws {EvalKeyNameError} if third param is not valid identifier
|
|
634
675
|
*/
|
|
635
676
|
function writeXObjParamRaw(obj, value, key = XOBJ_DEF_PARAM_TNAME) {
|
|
636
677
|
if (!isPlainObject(obj)) {
|
|
@@ -639,17 +680,22 @@ function writeXObjParamRaw(obj, value, key = XOBJ_DEF_PARAM_TNAME) {
|
|
|
639
680
|
{ code: XOBJ_TE_NPOBJ_ECODE },
|
|
640
681
|
);
|
|
641
682
|
};
|
|
642
|
-
|
|
643
|
-
if (
|
|
683
|
+
const { isSucceed, value: _key } = evalKeyName(key, false);
|
|
684
|
+
if (isSucceed) {
|
|
685
|
+
if (
|
|
686
|
+
_key === ''
|
|
687
|
+
|| value === undefined
|
|
688
|
+
|| isObject(value)
|
|
689
|
+
|| typeof value === 'function'
|
|
690
|
+
) {
|
|
691
|
+
return false;
|
|
692
|
+
};
|
|
693
|
+
obj[_key] = value;
|
|
694
|
+
return true;
|
|
695
|
+
} else {
|
|
644
696
|
const { code, msg } = _key;
|
|
645
697
|
throw new EvalKeyNameError(msg, { code });
|
|
646
698
|
};
|
|
647
|
-
if (_key === '' || value === undefined) {
|
|
648
|
-
isSucceed = false;
|
|
649
|
-
} else {
|
|
650
|
-
obj[_key] = value;
|
|
651
|
-
};
|
|
652
|
-
return isSucceed;
|
|
653
699
|
};
|
|
654
700
|
|
|
655
701
|
/**
|
|
@@ -660,7 +706,7 @@ function writeXObjParamRaw(obj, value, key = XOBJ_DEF_PARAM_TNAME) {
|
|
|
660
706
|
* @param {string} [key] - some key
|
|
661
707
|
* @returns {any}
|
|
662
708
|
* @throws {TypeError} if first param is not an object
|
|
663
|
-
* @throws {
|
|
709
|
+
* @throws {EvalKeyNameError} if second param is not valid identifier
|
|
664
710
|
*/
|
|
665
711
|
function readXObjAttrRaw(obj, attr = '', key) {
|
|
666
712
|
let _obj = null;
|
|
@@ -670,7 +716,7 @@ function readXObjAttrRaw(obj, attr = '', key) {
|
|
|
670
716
|
throw err;
|
|
671
717
|
};
|
|
672
718
|
if (_obj !== null) {
|
|
673
|
-
|
|
719
|
+
const { isSucceed, value: name } = evalKeyName(attr, false);
|
|
674
720
|
if (!isSucceed) {
|
|
675
721
|
const { code, msg } = name;
|
|
676
722
|
throw new EvalKeyNameError(msg, { code });
|
|
@@ -688,38 +734,44 @@ function readXObjAttrRaw(obj, attr = '', key) {
|
|
|
688
734
|
* @param {string} [key=XOBJ_DEF_ATTR_TNAME] - some key
|
|
689
735
|
* @returns {boolean}
|
|
690
736
|
* @throws {TypeError} if first param is not an object
|
|
691
|
-
* @throws {
|
|
737
|
+
* @throws {EvalKeyNameError} if second param is not valid identifier
|
|
692
738
|
*/
|
|
693
739
|
function writeXObjAttrRaw(obj, attr = '', value, key = XOBJ_DEF_ATTR_TNAME) {
|
|
694
|
-
|
|
695
|
-
if (
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
_obj = insertXObjElement(obj, key, opt);
|
|
705
|
-
} catch (err) {
|
|
706
|
-
throw err;
|
|
707
|
-
};
|
|
708
|
-
if (isArray(_obj)) {
|
|
709
|
-
// force a replacement of the old element if it's array
|
|
740
|
+
const { isSucceed, value: name } = evalKeyName(attr, false);
|
|
741
|
+
if (isSucceed) {
|
|
742
|
+
let isSucceed = false;
|
|
743
|
+
if (
|
|
744
|
+
name !== ''
|
|
745
|
+
&& value !== undefined
|
|
746
|
+
&& !isObject(value)
|
|
747
|
+
&& typeof value !== 'function'
|
|
748
|
+
) {
|
|
749
|
+
let _obj = null;
|
|
710
750
|
try {
|
|
711
|
-
const opt = { force: true,
|
|
751
|
+
const opt = { force: true, acceptIfList: true };
|
|
712
752
|
_obj = insertXObjElement(obj, key, opt);
|
|
713
753
|
} catch (err) {
|
|
714
754
|
throw err;
|
|
715
755
|
};
|
|
756
|
+
if (isArray(_obj)) {
|
|
757
|
+
// force a replacement of the old element if it's array
|
|
758
|
+
try {
|
|
759
|
+
const opt = { force: true, ripOldies: true };
|
|
760
|
+
_obj = insertXObjElement(obj, key, opt);
|
|
761
|
+
} catch (err) {
|
|
762
|
+
throw err;
|
|
763
|
+
};
|
|
764
|
+
};
|
|
765
|
+
if (_obj !== null) {
|
|
766
|
+
_obj[name] = value;
|
|
767
|
+
isSucceed = true;
|
|
768
|
+
};
|
|
716
769
|
};
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
};
|
|
770
|
+
return isSucceed;
|
|
771
|
+
} else {
|
|
772
|
+
const { code, msg } = name;
|
|
773
|
+
throw new EvalKeyNameError(msg, { code });
|
|
721
774
|
};
|
|
722
|
-
return isSucceed;
|
|
723
775
|
};
|
|
724
776
|
|
|
725
777
|
/**
|
|
@@ -1406,7 +1458,7 @@ function insertXObjEList(...args) {
|
|
|
1406
1458
|
* @param {OPT_inselops_S} [opt] - options
|
|
1407
1459
|
* @returns {RVAL_emodif}
|
|
1408
1460
|
* @throws {TypeError} if first param is not an object
|
|
1409
|
-
* @throws {
|
|
1461
|
+
* @throws {EvalKeyNameError} if second param is not valid identifier
|
|
1410
1462
|
*/
|
|
1411
1463
|
function insertXObjEListEx(obj, name, opt) {
|
|
1412
1464
|
if (!isPlainObject(obj)) {
|
|
@@ -1415,36 +1467,37 @@ function insertXObjEListEx(obj, name, opt) {
|
|
|
1415
1467
|
{ code: XOBJ_TE_NPOBJ_ECODE },
|
|
1416
1468
|
);
|
|
1417
1469
|
};
|
|
1418
|
-
|
|
1419
|
-
if (
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
force
|
|
1426
|
-
ripOldies
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|| (force && (ripOldies || !isObject(prop)))
|
|
1435
|
-
) {
|
|
1436
|
-
obj[key] = prop = [];
|
|
1437
|
-
isSucceed = true;
|
|
1438
|
-
} else {
|
|
1439
|
-
if (isObject(prop)) {
|
|
1440
|
-
if (!isArray(prop)) prop = [ prop ];
|
|
1441
|
-
obj[key] = prop;
|
|
1470
|
+
const { isSucceed, value: key } = evalKeyName(name);
|
|
1471
|
+
if (isSucceed) {
|
|
1472
|
+
/** @type {OPT_inselops_S} */
|
|
1473
|
+
let {
|
|
1474
|
+
force,
|
|
1475
|
+
ripOldies,
|
|
1476
|
+
} = isPlainObject(opt) ? opt : {};
|
|
1477
|
+
if (typeof force !== 'boolean') force = false;
|
|
1478
|
+
if (typeof ripOldies !== 'boolean') ripOldies = false;
|
|
1479
|
+
let prop = obj[key];
|
|
1480
|
+
let isSucceed = false;
|
|
1481
|
+
if (
|
|
1482
|
+
isNullOrUndef(prop)
|
|
1483
|
+
|| (force && (ripOldies || !isObject(prop)))
|
|
1484
|
+
) {
|
|
1485
|
+
obj[key] = prop = [];
|
|
1442
1486
|
isSucceed = true;
|
|
1487
|
+
} else {
|
|
1488
|
+
if (isObject(prop)) {
|
|
1489
|
+
if (!isArray(prop)) prop = [ prop ];
|
|
1490
|
+
obj[key] = prop;
|
|
1491
|
+
isSucceed = true;
|
|
1492
|
+
};
|
|
1443
1493
|
};
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1494
|
+
return {
|
|
1495
|
+
isSucceed,
|
|
1496
|
+
item: isSucceed ? prop : null,
|
|
1497
|
+
};
|
|
1498
|
+
} else {
|
|
1499
|
+
const { code, msg } = key;
|
|
1500
|
+
throw new EvalKeyNameError(msg, { code });
|
|
1448
1501
|
};
|
|
1449
1502
|
};
|
|
1450
1503
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ygracs/xobj-lib-js",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.9-b.1",
|
|
4
4
|
"description": "A helper library to work with xml-js module",
|
|
5
5
|
"author": "ygracs <cs70th-om@rambler.ru>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"main": "./index.js",
|
|
12
12
|
"types": "./index.d.ts",
|
|
13
13
|
"files": [
|
|
14
|
-
"doc
|
|
14
|
+
"doc/*.md",
|
|
15
15
|
"lib/xObj-lib.js",
|
|
16
16
|
"lib/xObj-defs.js",
|
|
17
17
|
"lib/xObj-errors.js",
|
|
@@ -31,10 +31,10 @@
|
|
|
31
31
|
"#test-dir/*": "./__test__/*"
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@ygracs/bsfoc-lib-js": "
|
|
34
|
+
"@ygracs/bsfoc-lib-js": "~0.3.3"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
|
-
"@ygracs/test-helper": "~0.0.
|
|
37
|
+
"@ygracs/test-helper": "~0.0.2-b",
|
|
38
38
|
"jest": "^30.2.0",
|
|
39
39
|
"jsdoc-to-markdown": "^9.1.3",
|
|
40
40
|
"minimist": "^1.2.8",
|