@ygracs/xobj-lib-js 0.4.0 → 0.4.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 +8 -0
- package/doc/xObj.md +34 -2
- package/index.d.ts +2 -0
- package/index.js +5 -1
- package/lib/xObj-attr.js +33 -31
- package/lib/xObj-lib.d.ts +9 -0
- package/lib/xObj-lib.js +33 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
package/doc/xObj.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
>|***rev.*:**|0.1.
|
|
1
|
+
>|***rev.*:**|0.1.54|
|
|
2
2
|
>|:---|---:|
|
|
3
|
-
>|date:|2026-
|
|
3
|
+
>|date:|2026-07-12|
|
|
4
4
|
|
|
5
5
|
## Introduction
|
|
6
6
|
|
|
@@ -24,6 +24,8 @@ This paper describes a functions provided by `xObj.js` module.
|
|
|
24
24
|
- <a href="#func+attribute+write">write element attributes</a>
|
|
25
25
|
- <a href="#func+param+read">read element parameter</a>
|
|
26
26
|
- <a href="#func+param+write">write element parameter</a>
|
|
27
|
+
- <a href="#func+other">other</a>
|
|
28
|
+
- <a href="#func+experimental">experimental</a>
|
|
27
29
|
|
|
28
30
|
### Base type definitions
|
|
29
31
|
|
|
@@ -885,6 +887,7 @@ The function throws a `EvalKeyNameError` exception in case:
|
|
|
885
887
|
+ if `value` parameter given and is not a type of a `string`;
|
|
886
888
|
+ if `key` parameter given and is not a type of a `string`.
|
|
887
889
|
|
|
890
|
+
<a name="func+other"></a>
|
|
888
891
|
### Other functions
|
|
889
892
|
|
|
890
893
|
<a name="getXObjElement"></a>
|
|
@@ -1234,6 +1237,7 @@ The function throws a `EvalKeyNameError` exception in case:
|
|
|
1234
1237
|
- code: `ERR_XOBJ_INVARG_KEY`
|
|
1235
1238
|
+ if `name` parameter given and is an empty string.
|
|
1236
1239
|
|
|
1240
|
+
<a name="func+experimental"></a>
|
|
1237
1241
|
### Experimental functions
|
|
1238
1242
|
|
|
1239
1243
|
> Note: Purpose of those functions will be discussed and some may be deprecate and make obsolete or functionality may be altered. So use it with cautions in mind.
|
|
@@ -1269,6 +1273,34 @@ The object `conditions` property (*if present*) contains following attributes:
|
|
|
1269
1273
|
|`name`|`string`||
|
|
1270
1274
|
|`value`|`any`||
|
|
1271
1275
|
|
|
1276
|
+
<a name="insertXObjNode"></a>
|
|
1277
|
+
#### **insertXObjNode(object, name)** => `null`\|`object`
|
|
1278
|
+
|
|
1279
|
+
> since: \[v0.4.1]
|
|
1280
|
+
|
|
1281
|
+
This function inserts an empty element named by a `name` parameter into a given object. When element exists but it is not an `object`, such element will be replaced. If succeed the element will be returned or `null` in opposite.
|
|
1282
|
+
|
|
1283
|
+
##### ***parameters***
|
|
1284
|
+
|
|
1285
|
+
| parameter name | value type | default value | description |
|
|
1286
|
+
|:---|---|---:|:---|
|
|
1287
|
+
| `object` | `INode` | --- | some object |
|
|
1288
|
+
| `name` | `string` | --- | some child element ID |
|
|
1289
|
+
|
|
1290
|
+
##### ***exceptions***
|
|
1291
|
+
|
|
1292
|
+
The function throws a `TypeError` exception in case:
|
|
1293
|
+
|
|
1294
|
+
- code: `ERR_XOBJ_NPOBJ`
|
|
1295
|
+
+ if `object` parameter is not an `Object` instance;
|
|
1296
|
+
|
|
1297
|
+
The function throws a `EvalKeyNameError` exception in case:
|
|
1298
|
+
|
|
1299
|
+
- code: `ERR_XOBJ_NSTR`
|
|
1300
|
+
+ if `name` parameter given and is not a type of a `string`;
|
|
1301
|
+
- code: `ERR_XOBJ_INVARG_KEY`
|
|
1302
|
+
+ if `name` parameter given and is an empty string.
|
|
1303
|
+
|
|
1272
1304
|
<a name="insertXObjElements"></a>
|
|
1273
1305
|
#### **insertXObjElements(object, name\[...name_N\[, options]])** => `number`
|
|
1274
1306
|
|
package/index.d.ts
CHANGED
|
@@ -59,6 +59,7 @@ import {
|
|
|
59
59
|
insertXObjElements,
|
|
60
60
|
insertXObjEChain,
|
|
61
61
|
clearXObjNode,
|
|
62
|
+
insertXObjNode,
|
|
62
63
|
} from "./lib/xObj-lib";
|
|
63
64
|
|
|
64
65
|
export {
|
|
@@ -93,6 +94,7 @@ export {
|
|
|
93
94
|
insertXObjElements,
|
|
94
95
|
insertXObjEChain,
|
|
95
96
|
clearXObjNode,
|
|
97
|
+
insertXObjNode,
|
|
96
98
|
};
|
|
97
99
|
|
|
98
100
|
import type {
|
package/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// [v0.1.
|
|
1
|
+
// [v0.1.020-20260712]
|
|
2
2
|
|
|
3
3
|
// === module init block ===
|
|
4
4
|
|
|
@@ -84,4 +84,8 @@ module.exports.insertXObjEListEx = xObj.insertXObjEListEx;
|
|
|
84
84
|
module.exports.insertXObjElements = xObj.insertXObjElements;
|
|
85
85
|
module.exports.insertXObjEChain = xObj.insertXObjEChain;
|
|
86
86
|
|
|
87
|
+
module.exports.clearXObjNode = xObj.clearXObjNode;
|
|
88
|
+
module.exports.insertXObjNode = xObj.insertXObjNode;
|
|
89
|
+
|
|
90
|
+
|
|
87
91
|
module.exports.INode = xObj.INode;
|
package/lib/xObj-attr.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// [v0.3.
|
|
1
|
+
// [v0.3.144-20260712]
|
|
2
2
|
|
|
3
3
|
// === module init block ===
|
|
4
4
|
|
|
@@ -24,7 +24,7 @@ const {
|
|
|
24
24
|
} = require('./xObj-names');
|
|
25
25
|
|
|
26
26
|
const {
|
|
27
|
-
getXObjElement,
|
|
27
|
+
getXObjElement, insertXObjNode,
|
|
28
28
|
// * import types definitions *
|
|
29
29
|
INode,
|
|
30
30
|
} = require('./xObj-lib');
|
|
@@ -157,28 +157,32 @@ function renameXObjAttribute(obj, attr = '', value = '', key = XOBJ_DEF_ATTR_TNA
|
|
|
157
157
|
);
|
|
158
158
|
};
|
|
159
159
|
const opt = false;
|
|
160
|
-
const { isSucceed, value:
|
|
160
|
+
const { isSucceed, value: aKey } = evalKeyName(key, opt);
|
|
161
161
|
if (isSucceed) {
|
|
162
162
|
let isSucceed = false;
|
|
163
|
-
if (
|
|
164
|
-
|
|
165
|
-
if (isPlainObject(
|
|
166
|
-
const { isSucceed: isAcceptON, value:
|
|
163
|
+
if (aKey !== '') {
|
|
164
|
+
const aNode = obj[aKey];
|
|
165
|
+
if (isPlainObject(aNode)) {
|
|
166
|
+
const { isSucceed: isAcceptON, value: aname } = evalKeyName(attr, opt);
|
|
167
167
|
if (!isAcceptON) {
|
|
168
|
-
const { code, msg } =
|
|
168
|
+
const { code, msg } = aname;
|
|
169
169
|
throw new EvalKeyNameError(msg, { code });
|
|
170
170
|
};
|
|
171
|
-
const { isSucceed: isAcceptNN, value:
|
|
171
|
+
const { isSucceed: isAcceptNN, value: vname } = evalKeyName(value, opt);
|
|
172
172
|
if (!isAcceptNN) {
|
|
173
|
-
const { code, msg } =
|
|
173
|
+
const { code, msg } = vname;
|
|
174
174
|
throw new EvalKeyNameError(msg, { code });
|
|
175
175
|
};
|
|
176
|
-
if (
|
|
177
|
-
if (
|
|
178
|
-
|
|
179
|
-
const
|
|
180
|
-
|
|
181
|
-
|
|
176
|
+
if (aname !== '' && aname in aNode && vname !== '') {
|
|
177
|
+
if (aname !== vname) {
|
|
178
|
+
const entries = Object.entries(aNode);
|
|
179
|
+
const ai = entries.findIndex((item) => item[0] === aname);
|
|
180
|
+
if (vname in aNode) {
|
|
181
|
+
const vi = entries.findIndex((item) => item[0] === vname);
|
|
182
|
+
if (ai < vi) entries[vi][1] = entries[ai][1];
|
|
183
|
+
};
|
|
184
|
+
entries[ai][0] = vname;
|
|
185
|
+
obj[aKey] = Object.fromEntries(entries);
|
|
182
186
|
};
|
|
183
187
|
isSucceed = true;
|
|
184
188
|
};
|
|
@@ -186,7 +190,7 @@ function renameXObjAttribute(obj, attr = '', value = '', key = XOBJ_DEF_ATTR_TNA
|
|
|
186
190
|
};
|
|
187
191
|
return isSucceed;
|
|
188
192
|
} else {
|
|
189
|
-
const { code, msg } =
|
|
193
|
+
const { code, msg } = aKey;
|
|
190
194
|
throw new EvalKeyNameError(msg, { code });
|
|
191
195
|
};
|
|
192
196
|
};
|
|
@@ -243,24 +247,22 @@ function writeXObjAttrRaw(obj, attr = '', value, key = XOBJ_DEF_ATTR_TNAME) {
|
|
|
243
247
|
&& !isObject(value)
|
|
244
248
|
&& typeof value !== 'function'
|
|
245
249
|
) {
|
|
246
|
-
let
|
|
250
|
+
let node = null;
|
|
247
251
|
try {
|
|
248
|
-
|
|
249
|
-
_obj = insertXObjElement(obj, key, opt);
|
|
252
|
+
node = insertXObjNode(obj, key);
|
|
250
253
|
} catch (err) {
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
throw err;
|
|
254
|
+
switch (err.code) {
|
|
255
|
+
case XOBJ_TE_NSTR_ECODE :
|
|
256
|
+
case XOBJ_TE_KNES_ECODE : {
|
|
257
|
+
break;
|
|
258
|
+
}
|
|
259
|
+
default: {
|
|
260
|
+
throw err;
|
|
261
|
+
}
|
|
260
262
|
};
|
|
261
263
|
};
|
|
262
|
-
if (
|
|
263
|
-
|
|
264
|
+
if (node !== null) {
|
|
265
|
+
node[name] = value;
|
|
264
266
|
isSucceed = true;
|
|
265
267
|
};
|
|
266
268
|
};
|
package/lib/xObj-lib.d.ts
CHANGED
|
@@ -176,6 +176,15 @@ export function insertXObjEChain(obj: INode, ...args: insertListOfElementsParams
|
|
|
176
176
|
* @throws {TypeError} if `node`-param is not an object or array
|
|
177
177
|
*/
|
|
178
178
|
export function clearXObjNode(node: INode | INode[]): boolean;
|
|
179
|
+
/**
|
|
180
|
+
* Inserts an element addressed by its key into a given object. If it exists
|
|
181
|
+
* and it is not an `object` a function forces an element replacement.
|
|
182
|
+
* @since 0.4.1
|
|
183
|
+
* @experimental
|
|
184
|
+
* @throws {TypeError} if `obj` param is not an object
|
|
185
|
+
* @throws {EvalKeyNameError} if `name` param is not valid identifier
|
|
186
|
+
*/
|
|
187
|
+
export function insertXObjNode(obj: INode, name: string): INode | null;
|
|
179
188
|
/**
|
|
180
189
|
* Inserts an elements into a given object.
|
|
181
190
|
* @throws {TypeError} if `obj` param is not an object
|
package/lib/xObj-lib.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// [v0.3.
|
|
1
|
+
// [v0.3.146-20260712]
|
|
2
2
|
|
|
3
3
|
// === module init block ===
|
|
4
4
|
|
|
@@ -627,6 +627,38 @@ function clearXObjNode(node) {
|
|
|
627
627
|
};
|
|
628
628
|
module.exports.clearXObjNode = clearXObjNode;
|
|
629
629
|
|
|
630
|
+
/**
|
|
631
|
+
* Inserts an element addressed by its key into a given object. If it exists
|
|
632
|
+
* and it is not an `object` a function forces an element replacement.
|
|
633
|
+
* @since 0.4.1
|
|
634
|
+
* @param {INode} obj - some object
|
|
635
|
+
* @param {string} name - some child element
|
|
636
|
+
* @returns {?INode}
|
|
637
|
+
* @experimental
|
|
638
|
+
* @throws {TypeError} if `obj` param is not an object
|
|
639
|
+
* @throws {EvalKeyNameError} if `name` param is not valid identifier
|
|
640
|
+
*/
|
|
641
|
+
function insertXObjNode(obj, name) {
|
|
642
|
+
if (!isPlainObject(obj)) {
|
|
643
|
+
throw new EvalKeyNameError(
|
|
644
|
+
XOBJ_TE_NPOBJ_EMSG,
|
|
645
|
+
{ code: XOBJ_TE_NPOBJ_ECODE },
|
|
646
|
+
);
|
|
647
|
+
};
|
|
648
|
+
const { isSucceed, value: key } = evalKeyName(name);
|
|
649
|
+
if (isSucceed) {
|
|
650
|
+
/** @type {?INode} */
|
|
651
|
+
const item = {};
|
|
652
|
+
let prop = obj[key];
|
|
653
|
+
if (!isPlainObject(prop)) prop = obj[key] = item;
|
|
654
|
+
return prop;
|
|
655
|
+
} else {
|
|
656
|
+
const { code, msg } = key;
|
|
657
|
+
throw new EvalKeyNameError(msg, { code });
|
|
658
|
+
};
|
|
659
|
+
};
|
|
660
|
+
module.exports.insertXObjNode = insertXObjNode;
|
|
661
|
+
|
|
630
662
|
// === module exports block ===
|
|
631
663
|
|
|
632
664
|
// * experimental *
|