@ygracs/xobj-lib-js 0.3.3 → 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.
@@ -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;
package/lib/xObj-param.js CHANGED
@@ -1,4 +1,4 @@
1
- // [v0.3.140-20260607]
1
+ // [v0.3.141-20260629]
2
2
 
3
3
  // === module init block ===
4
4
 
@@ -21,6 +21,9 @@ const {
21
21
 
22
22
  const {
23
23
  evalKeyName,
24
+ } = require('./xObj-names');
25
+
26
+ const {
24
27
  // * import types definitions *
25
28
  INode,
26
29
  } = require('./xObj-lib');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ygracs/xobj-lib-js",
3
- "version": "0.3.3",
3
+ "version": "0.4.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",
@@ -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",