@ygracs/xobj-lib-js 0.2.4 → 0.2.6-b

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,71 @@
1
+ // [v0.1.003-20251229]
2
+
3
+ // === module init block ===
4
+
5
+ const {
6
+ isPlainObject,
7
+ } = require('@ygracs/bsfoc-lib-js');
8
+
9
+ // === module inner block ===
10
+
11
+ // === module main block ===
12
+
13
+ const XOBJ_TE_INVARG_EMSG = 'invalid argument';
14
+ const XOBJ_TE_NOBJ_EMSG = `${XOBJ_TE_INVARG_EMSG} (an object expected)`;
15
+ const XOBJ_TE_NOBJ_ECODE = 'ERR_XOBJ_NOBJ';
16
+ const XOBJ_TE_NARR_EMSG = `${XOBJ_TE_INVARG_EMSG} (an array expected)`;
17
+ const XOBJ_TE_NARR_ECODE = 'ERR_XOBJ_NARR';
18
+ const XOBJ_TE_NSTR_EMSG = `${XOBJ_TE_INVARG_EMSG} (a string expected)`;
19
+ const XOBJ_TE_NSTR_ECODE = 'ERR_XOBJ_NSTR';
20
+ const XOBJ_TE_NPOBJ_EMSG = `${XOBJ_TE_INVARG_EMSG} (a plain object expected)`;
21
+ const XOBJ_TE_NPOBJ_ECODE = 'ERR_XOBJ_NPOBJ';
22
+ const XOBJ_TE_ANES_EMSG = '<attr_name> must be a non-empty string';
23
+ const XOBJ_TE_ANES_ECODE = 'ERR_XOBJ_INVARG_ATTR';
24
+ const XOBJ_TE_KNES_EMSG = '<key_name> must be a non-empty string';
25
+ const XOBJ_TE_KNES_ECODE = 'ERR_XOBJ_INVARG_KEY';
26
+
27
+ const XOBJ_ECODE_TABLE = {
28
+ XOBJ_TE_NOBJ_EMSG,
29
+ XOBJ_TE_NOBJ_ECODE,
30
+ XOBJ_TE_NARR_EMSG,
31
+ XOBJ_TE_NARR_ECODE,
32
+ XOBJ_TE_NSTR_EMSG,
33
+ XOBJ_TE_NSTR_ECODE,
34
+ XOBJ_TE_NPOBJ_EMSG,
35
+ XOBJ_TE_NPOBJ_ECODE,
36
+ XOBJ_TE_ANES_EMSG,
37
+ XOBJ_TE_ANES_ECODE,
38
+ XOBJ_TE_KNES_EMSG,
39
+ XOBJ_TE_KNES_ECODE,
40
+ };
41
+ module.exports.XOBJ_ECODE_TABLE = XOBJ_ECODE_TABLE;
42
+
43
+ /**
44
+ * @augments TypeError
45
+ * @classdesc Extends a base `TypeError` object to provide some information
46
+ * on the errors thrown when a key name evaluation failed
47
+ */
48
+ class EvalKeyNameError extends TypeError {
49
+ /**
50
+ * Creates a new instance
51
+ * @param {string} msg - some user error message
52
+ * @param {*} [options] - user options
53
+ */
54
+ constructor(msg, options) {
55
+ super(msg, options);
56
+ /** @type {string} */
57
+ this.name = 'EvalKeyNameError';
58
+ const code = (
59
+ isPlainObject(options) && typeof options.code === 'string'
60
+ ? options.code
61
+ : ''
62
+ );
63
+ /**
64
+ * Represents an error code
65
+ * @type {string}
66
+ */
67
+ this.code = code;
68
+ }
69
+
70
+ };
71
+ module.exports.EvalKeyNameError = EvalKeyNameError;
@@ -0,0 +1,297 @@
1
+ /**
2
+ * A result of a value check ops.
3
+ */
4
+ export type RVAL_reason = {
5
+ /** - message ID */
6
+ code: string;
7
+ /** - message text */
8
+ msg: string;
9
+ };
10
+ /**
11
+ * A result of a value check ops.
12
+ */
13
+ export type VCOR_evalkname = {
14
+ /** - ops flag */
15
+ isSucceed: boolean;
16
+ /** - result value or reson if failed */
17
+ value: string | RVAL_reason;
18
+ };
19
+ /**
20
+ * A result of an element name pattern evaluation ops.
21
+ */
22
+ export type XObjENameDescr = {
23
+ /** - ops flag */
24
+ isERR: boolean;
25
+ /** - element name */
26
+ name: null | number | string;
27
+ /** - some conditions applied to an element */
28
+ conditions: object | null;
29
+ };
30
+ /**
31
+ * A result of an xObj modification ops
32
+ */
33
+ export type RVAL_emodif = {
34
+ /** - flag that indicates whether an ops is succeed or not */
35
+ isSucceed: boolean;
36
+ /** - some element as a result */
37
+ item: object | object[] | null;
38
+ };
39
+ /**
40
+ * An options for an element insertion ops
41
+ */
42
+ export type OPT_inselops_L = {
43
+ force?: boolean;
44
+ ripOldies?: boolean;
45
+ acceptIfList?: boolean;
46
+ };
47
+ /**
48
+ * An options for an element insertion ops
49
+ */
50
+ export type OPT_inselops_S = {
51
+ force?: boolean;
52
+ ripOldies?: boolean;
53
+ };
54
+
55
+ /**
56
+ * Extracts a parameter 'AS IS' from a given object.
57
+ * @throws {TypeError} if first param is not an object
58
+ * @throws {TypeError} if third param is not a string
59
+ */
60
+ export function readXObjParamRaw(obj: object, key?: string): any;
61
+ /**
62
+ * Extracts a parameter from a given object and returns it as a string.
63
+ * @throws {TypeError} if first param is not an object
64
+ */
65
+ export function readXObjParam(obj: object, key?: string): string;
66
+ /**
67
+ * Extracts a parameter from a given object and returns it as a boolean.
68
+ * @throws {TypeError} if first param is not an object
69
+ */
70
+ export function readXObjParamAsBool(obj: object, defValue?: boolean, key?: string): boolean;
71
+ /**
72
+ * Extracts a parameter from a given object and returns it as a number.
73
+ * @throws {TypeError} if first param is not an object
74
+ */
75
+ export function readXObjParamAsNum(obj: object, defValue?: number, key?: string): number;
76
+ /**
77
+ * Extracts a parameter from a given object and returns it as a string.
78
+ * @throws {TypeError} if first param is not an object
79
+ * @todo [since `v0.2.1`] deprecate use of `opt` as `string`
80
+ */
81
+ export function readXObjParamEx(obj: object, opt?: object, key?: string): string;
82
+ /**
83
+ * Extracts a parameter from a given object and returns it as 'index' value.
84
+ * @throws {TypeError} if first param is not an object
85
+ */
86
+ export function readXObjParamAsIndex(obj: object, key?: string): number;
87
+ /**
88
+ * Writes a parameter 'AS IS' into a given object.
89
+ * @throws {TypeError} if first param is not an object
90
+ * @throws {TypeError} if third param is not a string
91
+ */
92
+ export function writeXObjParamRaw(obj: object, value: any, key?: string): boolean;
93
+ /**
94
+ * Tries to convert a given value to a string and writes it as a parameter
95
+ * into a given object.
96
+ * @throws {TypeError} if first param is not an object
97
+ */
98
+ export function writeXObjParam(obj: object, value: any, key?: string): boolean;
99
+ /**
100
+ * Tries to convert a given value to a boolean and writes it as a parameter
101
+ * into a given object.
102
+ * @throws {TypeError} if first param is not an object
103
+ */
104
+ export function writeXObjParamAsBool(obj: object, value: any, defValue?: boolean, key?: string): boolean;
105
+ /**
106
+ * Tries to convert a given value to a number and writes it as a parameter
107
+ * into a given object.
108
+ * @throws {TypeError} if first param is not an object
109
+ */
110
+ export function writeXObjParamAsNum(obj: object, value: any, defValue?: number, key?: string): boolean;
111
+ /**
112
+ * Tries to convert a given value into an 'index' value and writes it
113
+ * as a parameter into a given object.
114
+ * @throws {TypeError} if first param is not an object
115
+ */
116
+ export function writeXObjParamAsIndex(obj: object, value: any, key?: string): boolean;
117
+ /**
118
+ * Tries to convert a given value to a string and writes it
119
+ * as a parameter into a given object.
120
+ * @throws {TypeError} if first param is not an object
121
+ * @todo [since `v0.2.1`] deprecate use of `opt` as `string`
122
+ */
123
+ export function writeXObjParamEx(obj: object, value: any, opt?: object, key?: string): boolean;
124
+ /**
125
+ * Extracts an attribute 'AS IS' from a given object.
126
+ * @throws {TypeError} if first param is not an object
127
+ * @throws {TypeError} if third param is not a string
128
+ */
129
+ export function readXObjAttrRaw(obj: object, attr?: string, key?: string): any;
130
+ /**
131
+ * Extracts an attribute from a given object and returns it as a string.
132
+ * @throws {TypeError} if first param is not an object
133
+ */
134
+ export function readXObjAttr(obj: object, attr: string, key?: string): string;
135
+ /**
136
+ * Extracts an attribute from a given object and returns it as a boolean.
137
+ * @throws {TypeError} if first param is not an object
138
+ */
139
+ export function readXObjAttrAsBool(obj: object, attr: string, defValue?: boolean, key?: string): boolean;
140
+ /**
141
+ * Extracts an attribute from a given object and returns it as a number.
142
+ * @returns {number}
143
+ * @throws {TypeError} if first param is not an object
144
+ */
145
+ export function readXObjAttrAsNum(obj: object, attr: string, defValue?: number, key?: string): number;
146
+ /**
147
+ * Extracts an attribute from a given object and returns it as a string.
148
+ * @throws {TypeError} if first param is not an object
149
+ * @todo [since `v0.2.1`] deprecate use of `opt` as `string`
150
+ */
151
+ export function readXObjAttrEx(obj: object, attr: string, opt?: object, key?: string): string;
152
+ /**
153
+ * Extracts an attribute from a given object and returns it as 'index' value.
154
+ * @throws {TypeError} if first param is not an object
155
+ */
156
+ export function readXObjAttrAsIndex(obj: object, attr: string, key?: string): number;
157
+ /**
158
+ * Writes a parameter into a given object.
159
+ * @throws {TypeError} if first param is not an object
160
+ * @throws {TypeError} if third param is not a string
161
+ */
162
+ export function writeXObjAttrRaw(obj: object, attr: string, value: any, key?: string): boolean;
163
+ /**
164
+ * Tries to convert a given value to a string and writes it as an attribute
165
+ * into a given object.
166
+ * @throws {TypeError} if first param is not an object
167
+ */
168
+ export function writeXObjAttr(obj: object, attr: string, value: any, key?: string): boolean;
169
+ /**
170
+ * Tries to convert a given value to a boolean and writes it as an attribute
171
+ * into a given object.
172
+ * @throws {TypeError} if first param is not an object
173
+ */
174
+ export function writeXObjAttrAsBool(obj: object, attr: string, value: any, defValue?: boolean, key?: string): boolean;
175
+ /**
176
+ * Tries to convert a given value to a number and writes it as an attribute
177
+ * into a given object.
178
+ * @throws {TypeError} if first param is not an object
179
+ */
180
+ export function writeXObjAttrAsNum(obj: object, attr: string, value: any, defValue?: number, key?: string): boolean;
181
+ /**
182
+ * Tries to convert a given value into an 'index' value and writes it
183
+ * as an attribute into a given object.
184
+ * @throws {TypeError} if first param is not an object
185
+ */
186
+ export function writeXObjAttrAsIndex(obj: object, attr: string, value: any, key?: string): boolean;
187
+ /**
188
+ * Tries to convert a given value to a string and writes it as an attribute
189
+ * into a given object.
190
+ * @throws {TypeError} if first param is not an object
191
+ * @todo [since `v0.2.1`] deprecate use of `opt` as `string`
192
+ */
193
+ export function writeXObjAttrEx(obj: object, attr: string, value: any, opt?: object, key?: string): boolean;
194
+ /**
195
+ * Extracts an attributes from a given object by its key.
196
+ * @throws {TypeError} if first param is not an object
197
+ */
198
+ export function getXObjAttributes(obj: object, key?: string): object | null;
199
+ /**
200
+ * Checks whether an attribute is exists.
201
+ * @throws {TypeError} if first param is not an object
202
+ */
203
+ export function checkXObjAttribute(obj: object, attr?: string, key?: string): boolean;
204
+ /**
205
+ * Deletes an attribute addressed by a given name.
206
+ * @throws {TypeError} if first param is not an object
207
+ */
208
+ export function deleteXObjAttribute(obj: object, attr?: string, key?: string): boolean;
209
+ /**
210
+ * Renames an attribute addressed by a given name.
211
+ * @since 0.2.0
212
+ * @throws {TypeError} if first param is not an object
213
+ * @throws {TypeError} if second param is not an object
214
+ * @throws {TypeError} if third param is not an object
215
+ */
216
+ export function renameXObjAttribute(obj: object, attr?: string, value?: string, key?: string): boolean;
217
+ /**
218
+ * Extracts an element from a given object by its key.
219
+ * @throws {TypeError} if first param is not an object
220
+ * @throws {TypeError} if second param is empty string or not a string at all
221
+ */
222
+ export function getXObjElement(obj: object, name: string): any | null;
223
+ /**
224
+ * Adds an element addressed by its key to a given object.
225
+ * @throws {TypeError} if first param is not an object
226
+ * @throws {TypeError} if second param is empty string or not a string at all
227
+ */
228
+ export function addXObjElement(obj: object, name: string): RVAL_emodif;
229
+ /**
230
+ * Inserts an element addressed by its key into a given object.
231
+ * @throws {TypeError} if first param is not an object
232
+ * @see insertXObjElementEx
233
+ */
234
+ export function insertXObjElement(...args: [obj: object, name: string, opt?: OPT_inselops_L]): object | null;
235
+ /**
236
+ * Inserts an element addressed by its key into a given object.
237
+ * @since 0.2.0
238
+ * @throws {TypeError} if first param is not an object
239
+ * @throws {TypeError} if second param is empty string or not a string at all
240
+ */
241
+ export function insertXObjElementEx(obj: object, name: string, opt?: OPT_inselops_L): RVAL_emodif;
242
+ /**
243
+ * Deletes an element addressed by its key from a given object.
244
+ * @throws {TypeError} if first param is not an object
245
+ * @see deleteXObjElementEx
246
+ */
247
+ export function deleteXObjElement(...args: [obj: object, name: string]): boolean;
248
+ /**
249
+ * Deletes an element addressed by its key from a given object.
250
+ * @throws {TypeError} if first param is not an object
251
+ * @throws {TypeError} if second param is empty string or not a string at all
252
+ */
253
+ export function deleteXObjElementEx(obj: object, name: string): RVAL_emodif;
254
+ /**
255
+ * Renames an element addressed by its key.
256
+ * @throws {TypeError} if first param is not an object
257
+ * @throws {TypeError} if second param is not a string
258
+ * @throws {TypeError} if third param is not a string
259
+ */
260
+ export function renameXObjElement(obj: object, name?: string, value?: string): boolean;
261
+
262
+ /**
263
+ * Tries to convert a value into an ID.
264
+ */
265
+ export function evalXObjEName(value: any): null | number | string;
266
+ /**
267
+ * Inserts a list elements into a given object.
268
+ * @throws {TypeError} if first param is not an object
269
+ * @see insertXObjEListEx
270
+ */
271
+ export function insertXObjEList(...args: [obj: object, name: string, opt?: OPT_inselops_S]): object | object[] | null;
272
+ /**
273
+ * Inserts a list elements into a given object.
274
+ * @since 0.2.0
275
+ * @throws {TypeError} if first param is not an object
276
+ * @throws {TypeError} if second param is empty string or not a string at all
277
+ */
278
+ export function insertXObjEListEx(obj: object, name: string, opt?: OPT_inselops_S): RVAL_emodif;
279
+ /**
280
+ * Tries to convert a value into an ID.
281
+ * @inner
282
+ */
283
+ export function evalKeyName(value: any, opt?: boolean): VCOR_evalkname;
284
+ /**
285
+ * Tries to convert a value into an element name description.
286
+ */
287
+ export function genXObjENameDescr(value: any): XObjENameDescr;
288
+ /**
289
+ * Inserts an elements into a given object.
290
+ * @throws {TypeError} if first param is not an object
291
+ */
292
+ export function insertXObjElements(obj: object, ...args: any[]): number;
293
+ /**
294
+ * Inserts a chain of an elements into a given object.
295
+ * @throws {TypeError} if first param is not an object
296
+ */
297
+ export function insertXObjEChain(obj: object, ...args: any[]): any | null;