@terzogenito/json-utils 1.0.9 → 1.0.10
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/README.md +242 -3
- package/index.js +367 -1
- package/package.json +15 -4
package/README.md
CHANGED
|
@@ -3,6 +3,11 @@
|
|
|
3
3
|
## Overview
|
|
4
4
|
This module provides various functions for reading, validating, and processing JSON files and URL content.
|
|
5
5
|
|
|
6
|
+
## Installation
|
|
7
|
+
```bash
|
|
8
|
+
npm install @terzogenito/json-utils
|
|
9
|
+
```
|
|
10
|
+
|
|
6
11
|
## Function List
|
|
7
12
|
|
|
8
13
|
### 1. `getData(path)`
|
|
@@ -182,9 +187,243 @@ app.getJSON("https://api.example.com/data", jsonData => {
|
|
|
182
187
|
});
|
|
183
188
|
```
|
|
184
189
|
|
|
185
|
-
|
|
186
|
-
```
|
|
187
|
-
|
|
190
|
+
### 13. getAttributes(jsonObject)
|
|
191
|
+
```javascript
|
|
192
|
+
const dataJSON = app.readJSON(data);
|
|
193
|
+
const attributes = app.getAttributes(dataJSON);
|
|
194
|
+
// Output: ['name', 'age', 'isActive', 'address', 'hobbies']
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
**Description**: Extracts all attribute/keys from a JSON object and returns them as an array.
|
|
198
|
+
|
|
199
|
+
**Parameters**:
|
|
200
|
+
- jsonObject (Object|String): JSON object or JSON string
|
|
201
|
+
|
|
202
|
+
Returns: Array of attribute names
|
|
203
|
+
|
|
204
|
+
### 14. getMeta(jsonObject)
|
|
205
|
+
```javascript
|
|
206
|
+
const meta = app.getMeta(dataJSON);
|
|
207
|
+
// Output: {"name": "string", "age": "integer", "isActive": "boolean"}
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
**Description**: Generates metadata showing attribute names and their data types.
|
|
211
|
+
|
|
212
|
+
**Parameters**:
|
|
213
|
+
- jsonObject (Object|String): JSON object or JSON string
|
|
214
|
+
|
|
215
|
+
Returns: Object with attribute names as keys and data types as values
|
|
216
|
+
|
|
217
|
+
Data Types Identified:
|
|
218
|
+
- string, integer, float, boolean, array, object, null, date
|
|
219
|
+
|
|
220
|
+
### 15. getMetaDetail(jsonObject)
|
|
221
|
+
```javascript
|
|
222
|
+
const metaDetail = app.getMetaDetail(dataJSON);
|
|
223
|
+
// Output: Detailed metadata including nested structures
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
**Description**: Generates comprehensive metadata including nested attributes, data types, paths, and structural information.
|
|
227
|
+
|
|
228
|
+
**Parameters**:
|
|
229
|
+
- jsonObject (Object|String): JSON object or JSON string
|
|
230
|
+
|
|
231
|
+
Returns: Object with detailed metadata for each attribute
|
|
232
|
+
|
|
233
|
+
Metadata Includes:
|
|
234
|
+
- type: Data type
|
|
235
|
+
- isRequired: Whether attribute exists
|
|
236
|
+
- path: Full path to attribute
|
|
237
|
+
- children: Nested attributes (for objects)
|
|
238
|
+
- length: Array length (for arrays)
|
|
239
|
+
- elementType: Type of array elements
|
|
240
|
+
|
|
241
|
+
### 16. getMetaCompact(jsonObject)
|
|
242
|
+
```javascript
|
|
243
|
+
const metaCompact = app.getMetaCompact(dataJSON);
|
|
244
|
+
// Output: Compact metadata format
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
**Description**: Creates a compact metadata representation showing the structure hierarchy.
|
|
248
|
+
|
|
249
|
+
**Parameters**:
|
|
250
|
+
- jsonObject (Object|String): JSON object or JSON string
|
|
251
|
+
|
|
252
|
+
Returns: Compact representation of JSON structure
|
|
253
|
+
|
|
254
|
+
Format Examples:
|
|
255
|
+
- "array[string]": Array of strings
|
|
256
|
+
- "array[object]": Array of objects
|
|
257
|
+
- Nested objects shown as nested objects
|
|
258
|
+
|
|
259
|
+
### 17. getPartial(jsonObject, attributes)
|
|
260
|
+
```javascript
|
|
261
|
+
const partial = app.getPartial(dataJSON, ["name", "age"]);
|
|
262
|
+
// Output: {"name": "John", "age": 30}
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
**Description**: Extracts specific attributes from a JSON object.
|
|
266
|
+
|
|
267
|
+
**Parameters**:
|
|
268
|
+
- jsonObject (Object|String): JSON object or JSON string
|
|
269
|
+
- attributes (Array|String|Object): Attributes to extract
|
|
270
|
+
|
|
271
|
+
attribute Parameter Types:
|
|
272
|
+
- Array: List of attribute names to extract
|
|
273
|
+
```javascript
|
|
274
|
+
app.getPartial(data, ["name", "age"])
|
|
275
|
+
```
|
|
276
|
+
- String: Single attribute name
|
|
277
|
+
```javascript
|
|
278
|
+
app.getPartial(data, "name")
|
|
279
|
+
```
|
|
280
|
+
- Object: Mapping of new names to original attributes
|
|
281
|
+
```javascript
|
|
282
|
+
app.getPartial(data, {"fullName": "name", "yearsOld": "age"})
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
Returns: Object containing only the specified attributes
|
|
286
|
+
|
|
287
|
+
### 18. getPartialDeep(jsonObject, attributePaths)
|
|
288
|
+
```javascript
|
|
289
|
+
const deepPartial = app.getPartialDeep(dataJSON, ["name", "address.city", "hobbies.length"]);
|
|
290
|
+
// Output: {"name": "John", "city": "Jakarta", "length": 2}
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
**Description**: Extracts attributes using nested paths (dot notation).
|
|
294
|
+
|
|
295
|
+
**Parameters**:
|
|
296
|
+
- jsonObject (Object|String): JSON object or JSON string
|
|
297
|
+
- attributePaths (Array|Object): Paths to extract
|
|
298
|
+
|
|
299
|
+
attributePaths Parameter Types:
|
|
300
|
+
- Array: List of dot-notated paths
|
|
301
|
+
```javascript
|
|
302
|
+
app.getPartialDeep(data, ["user.profile.name", "user.contact.email"])
|
|
303
|
+
```
|
|
304
|
+
- Object: Mapping of new names to paths
|
|
305
|
+
```javascript
|
|
306
|
+
app.getPartialDeep(data, {"userName": "user.profile.name", "userEmail": "user.contact.email"})
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
Returns: Object with extracted values (uses last path segment as key for array input)
|
|
310
|
+
|
|
311
|
+
### 19. getPartialWithDefaults(jsonObject, attributesConfig)
|
|
312
|
+
```javascript
|
|
313
|
+
const partialWithDefaults = app.getPartialWithDefaults(dataJSON, {
|
|
314
|
+
"name": "name",
|
|
315
|
+
"status": {
|
|
316
|
+
path: "isActive",
|
|
317
|
+
transform: (val) => val ? "Active" : "Inactive"
|
|
318
|
+
},
|
|
319
|
+
"email": {
|
|
320
|
+
path: "contact.email",
|
|
321
|
+
default: "no-email@example.com"
|
|
322
|
+
}
|
|
323
|
+
});
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
**Description**: Extracts attributes with advanced configuration including default values and transformations.
|
|
327
|
+
|
|
328
|
+
**Parameters**:
|
|
329
|
+
- jsonObject (Object|String): JSON object or JSON string
|
|
330
|
+
- attributesConfig (Object): Configuration object
|
|
331
|
+
|
|
332
|
+
Configuration Options:
|
|
333
|
+
- String: Simple path extraction
|
|
334
|
+
```javascript
|
|
335
|
+
"name": "user.fullName"
|
|
336
|
+
```
|
|
337
|
+
- Object: Advanced configuration
|
|
338
|
+
```javascript
|
|
339
|
+
"formattedAge": {
|
|
340
|
+
path: "age", // Required: Path to attribute
|
|
341
|
+
default: 0, // Optional: Default value if path doesn't exist
|
|
342
|
+
transform: (val) => ${val} years old // Optional: Transformation function
|
|
343
|
+
}
|
|
344
|
+
```
|
|
345
|
+
|
|
346
|
+
Returns: Object with extracted and processed values
|
|
347
|
+
|
|
348
|
+
### 20. excludeAttributes(jsonObject, attributesToExclude)
|
|
349
|
+
```javascript
|
|
350
|
+
const filtered = app.excludeAttributes(dataJSON, ["isActive", "address"]);
|
|
351
|
+
// Output: {"name": "John", "age": 30, "hobbies": ["reading", "coding"]}
|
|
352
|
+
```
|
|
353
|
+
|
|
354
|
+
**Description**: Creates a new JSON object excluding specified attributes.
|
|
355
|
+
|
|
356
|
+
**Parameters**:
|
|
357
|
+
- jsonObject (Object|String): JSON object or JSON string
|
|
358
|
+
- attributesToExclude (Array|String): Attributes to remove
|
|
359
|
+
|
|
360
|
+
attributesToExclude Parameter Types:
|
|
361
|
+
- Array: List of attribute names to exclude
|
|
362
|
+
```javascript
|
|
363
|
+
app.excludeAttributes(data, ["password", "secretKey"])
|
|
364
|
+
```
|
|
365
|
+
- String: Single attribute name to exclude
|
|
366
|
+
```javascript
|
|
367
|
+
app.excludeAttributes(data, "password")
|
|
368
|
+
```
|
|
369
|
+
|
|
370
|
+
Returns: New object without the excluded attributes
|
|
371
|
+
|
|
372
|
+
### 21. getAttributeValue(jsonObject, attributeName, defaultValue)
|
|
373
|
+
```javascript
|
|
374
|
+
const name = app.getAttributeValue(dataJSON, "name");
|
|
375
|
+
const email = app.getAttributeValue(dataJSON, "email", "default@email.com");
|
|
376
|
+
```
|
|
377
|
+
|
|
378
|
+
**Description**: Gets the value of a specific attribute with optional default value.
|
|
379
|
+
|
|
380
|
+
**Parameters**:
|
|
381
|
+
- jsonObject (Object|String): JSON object or JSON string
|
|
382
|
+
- attributeName (String): Name of the attribute to retrieve
|
|
383
|
+
- defaultValue (Any, optional): Default value if attribute doesn't exist
|
|
384
|
+
|
|
385
|
+
Returns: Attribute value or default value
|
|
386
|
+
|
|
387
|
+
Advanced Usage Examples
|
|
388
|
+
```javascript
|
|
389
|
+
const app = require('./index');
|
|
390
|
+
|
|
391
|
+
// Get all attributes from JSON
|
|
392
|
+
const data = await app.getData('./data.json');
|
|
393
|
+
const jsonData = app.readJSON(data);
|
|
394
|
+
const attributes = app.getAttributes(jsonData);
|
|
395
|
+
|
|
396
|
+
// Get metadata information
|
|
397
|
+
const meta = app.getMeta(jsonData);
|
|
398
|
+
const metaDetail = app.getMetaDetail(jsonData);
|
|
399
|
+
|
|
400
|
+
// Extract specific data
|
|
401
|
+
const userInfo = app.getPartial(jsonData, ["name", "email", "phone"]);
|
|
402
|
+
const nestedData = app.getPartialDeep(jsonData, ["user.profile.name", "user.contact.email"]);
|
|
403
|
+
|
|
404
|
+
// Extract with transformations and defaults
|
|
405
|
+
const processedData = app.getPartialWithDefaults(jsonData, {
|
|
406
|
+
"fullName": "user.name",
|
|
407
|
+
"ageFormatted": {
|
|
408
|
+
path: "user.age",
|
|
409
|
+
transform: (age) => ${age} years old
|
|
410
|
+
},
|
|
411
|
+
"country": {
|
|
412
|
+
path: "user.address.country",
|
|
413
|
+
default: "Unknown"
|
|
414
|
+
}
|
|
415
|
+
});
|
|
416
|
+
|
|
417
|
+
// Exclude sensitive information
|
|
418
|
+
const safeData = app.excludeAttributes(jsonData, ["password", "ssn", "creditCard"]);
|
|
419
|
+
|
|
420
|
+
// Analyze complex nested structures
|
|
421
|
+
const complexMeta = app.getMetaDetail(complexJSON);
|
|
422
|
+
console.log(complexMeta.user?.children?.contact?.children?.email?.type); // "string"
|
|
423
|
+
|
|
424
|
+
// Get single attribute value
|
|
425
|
+
const userName = app.getAttributeValue(jsonData, "name");
|
|
426
|
+
const userEmail = app.getAttributeValue(jsonData, "email", "no-email@example.com");
|
|
188
427
|
```
|
|
189
428
|
|
|
190
429
|
## Requirements
|
package/index.js
CHANGED
|
@@ -132,6 +132,364 @@ function beautify(jsonObject, indent) {
|
|
|
132
132
|
}
|
|
133
133
|
}
|
|
134
134
|
|
|
135
|
+
function getAttributes(jsonObject) {
|
|
136
|
+
try {
|
|
137
|
+
if (typeof jsonObject === 'string') {
|
|
138
|
+
jsonObject = JSON.parse(jsonObject);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (typeof jsonObject !== 'object' || jsonObject === null) {
|
|
142
|
+
return [];
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return Object.keys(jsonObject);
|
|
146
|
+
} catch (error) {
|
|
147
|
+
return [];
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function getMeta(jsonObject) {
|
|
152
|
+
try {
|
|
153
|
+
if (typeof jsonObject === 'string') {
|
|
154
|
+
jsonObject = JSON.parse(jsonObject);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if (typeof jsonObject !== 'object' || jsonObject === null) {
|
|
158
|
+
return {};
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const meta = {};
|
|
162
|
+
|
|
163
|
+
for (const [key, value] of Object.entries(jsonObject)) {
|
|
164
|
+
let type = typeof value;
|
|
165
|
+
|
|
166
|
+
if (type === 'object') {
|
|
167
|
+
if (value === null) {
|
|
168
|
+
type = 'null';
|
|
169
|
+
} else if (Array.isArray(value)) {
|
|
170
|
+
type = 'array';
|
|
171
|
+
} else if (value instanceof Date) {
|
|
172
|
+
type = 'date';
|
|
173
|
+
} else {
|
|
174
|
+
type = 'object';
|
|
175
|
+
}
|
|
176
|
+
} else if (type === 'number') {
|
|
177
|
+
type = Number.isInteger(value) ? 'integer' : 'float';
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
meta[key] = type;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
return meta;
|
|
184
|
+
} catch (error) {
|
|
185
|
+
return {};
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function getMetaDetail(jsonObject) {
|
|
190
|
+
try {
|
|
191
|
+
if (typeof jsonObject === 'string') {
|
|
192
|
+
jsonObject = JSON.parse(jsonObject);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
if (typeof jsonObject !== 'object' || jsonObject === null) {
|
|
196
|
+
return {};
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function getNestedMeta(obj, path = '') {
|
|
200
|
+
const meta = {};
|
|
201
|
+
|
|
202
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
203
|
+
const currentPath = path ? `${path}.${key}` : key;
|
|
204
|
+
const attributeMeta = {};
|
|
205
|
+
|
|
206
|
+
let type = typeof value;
|
|
207
|
+
|
|
208
|
+
if (value === null) {
|
|
209
|
+
type = 'null';
|
|
210
|
+
attributeMeta.type = type;
|
|
211
|
+
attributeMeta.isRequired = false;
|
|
212
|
+
attributeMeta.path = currentPath;
|
|
213
|
+
} else if (Array.isArray(value)) {
|
|
214
|
+
type = 'array';
|
|
215
|
+
attributeMeta.type = type;
|
|
216
|
+
attributeMeta.length = value.length;
|
|
217
|
+
attributeMeta.isRequired = true;
|
|
218
|
+
attributeMeta.path = currentPath;
|
|
219
|
+
|
|
220
|
+
if (value.length > 0) {
|
|
221
|
+
const firstElement = value[0];
|
|
222
|
+
const elementType = typeof firstElement;
|
|
223
|
+
|
|
224
|
+
if (elementType === 'object' && firstElement !== null) {
|
|
225
|
+
if (Array.isArray(firstElement)) {
|
|
226
|
+
attributeMeta.elementType = 'array';
|
|
227
|
+
} else {
|
|
228
|
+
attributeMeta.elementType = 'object';
|
|
229
|
+
attributeMeta.children = getNestedMeta(firstElement, `${currentPath}[0]`);
|
|
230
|
+
}
|
|
231
|
+
} else {
|
|
232
|
+
attributeMeta.elementType = elementType;
|
|
233
|
+
}
|
|
234
|
+
} else {
|
|
235
|
+
attributeMeta.elementType = 'unknown';
|
|
236
|
+
}
|
|
237
|
+
} else if (value instanceof Date) {
|
|
238
|
+
type = 'date';
|
|
239
|
+
attributeMeta.type = type;
|
|
240
|
+
attributeMeta.isRequired = true;
|
|
241
|
+
attributeMeta.path = currentPath;
|
|
242
|
+
} else if (type === 'object') {
|
|
243
|
+
type = 'object';
|
|
244
|
+
attributeMeta.type = type;
|
|
245
|
+
attributeMeta.isRequired = true;
|
|
246
|
+
attributeMeta.path = currentPath;
|
|
247
|
+
attributeMeta.keysCount = Object.keys(value).length;
|
|
248
|
+
|
|
249
|
+
attributeMeta.children = getNestedMeta(value, currentPath);
|
|
250
|
+
} else {
|
|
251
|
+
attributeMeta.type = type;
|
|
252
|
+
attributeMeta.isRequired = true;
|
|
253
|
+
attributeMeta.path = currentPath;
|
|
254
|
+
|
|
255
|
+
if (type === 'number') {
|
|
256
|
+
attributeMeta.numberType = Number.isInteger(value) ? 'integer' : 'float';
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
meta[key] = attributeMeta;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
return meta;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
return getNestedMeta(jsonObject);
|
|
267
|
+
} catch (error) {
|
|
268
|
+
console.error('Error in getMetaDetail:', error);
|
|
269
|
+
return {};
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
function getMetaCompact(jsonObject) {
|
|
274
|
+
try {
|
|
275
|
+
if (typeof jsonObject === 'string') {
|
|
276
|
+
jsonObject = JSON.parse(jsonObject);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
if (typeof jsonObject !== 'object' || jsonObject === null) {
|
|
280
|
+
return {};
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
function getCompactMeta(obj) {
|
|
284
|
+
const meta = {};
|
|
285
|
+
|
|
286
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
287
|
+
let type = typeof value;
|
|
288
|
+
|
|
289
|
+
if (value === null) {
|
|
290
|
+
meta[key] = 'null';
|
|
291
|
+
} else if (Array.isArray(value)) {
|
|
292
|
+
if (value.length > 0) {
|
|
293
|
+
const firstElement = value[0];
|
|
294
|
+
const elementType = typeof firstElement;
|
|
295
|
+
if (elementType === 'object' && firstElement !== null) {
|
|
296
|
+
meta[key] = `array[${Array.isArray(firstElement) ? 'array' : 'object'}]`;
|
|
297
|
+
} else {
|
|
298
|
+
meta[key] = `array[${elementType}]`;
|
|
299
|
+
}
|
|
300
|
+
} else {
|
|
301
|
+
meta[key] = 'array[]';
|
|
302
|
+
}
|
|
303
|
+
} else if (value instanceof Date) {
|
|
304
|
+
meta[key] = 'date';
|
|
305
|
+
} else if (type === 'object') {
|
|
306
|
+
meta[key] = getCompactMeta(value);
|
|
307
|
+
} else if (type === 'number') {
|
|
308
|
+
meta[key] = Number.isInteger(value) ? 'integer' : 'float';
|
|
309
|
+
} else {
|
|
310
|
+
meta[key] = type;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
return meta;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
return getCompactMeta(jsonObject);
|
|
318
|
+
} catch (error) {
|
|
319
|
+
console.error('Error in getMetaCompact:', error);
|
|
320
|
+
return {};
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
function getPartial(jsonObject, attributes) {
|
|
325
|
+
try {
|
|
326
|
+
if (typeof jsonObject === 'string') {
|
|
327
|
+
jsonObject = JSON.parse(jsonObject);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
if (typeof jsonObject !== 'object' || jsonObject === null) {
|
|
331
|
+
return {};
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
const result = {};
|
|
335
|
+
|
|
336
|
+
if (Array.isArray(attributes)) {
|
|
337
|
+
for (const attr of attributes) {
|
|
338
|
+
if (attr in jsonObject) {
|
|
339
|
+
result[attr] = jsonObject[attr];
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
else if (typeof attributes === 'string') {
|
|
344
|
+
if (attributes in jsonObject) {
|
|
345
|
+
result[attributes] = jsonObject[attributes];
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
else if (typeof attributes === 'object' && attributes !== null) {
|
|
349
|
+
for (const [newKey, originalKey] of Object.entries(attributes)) {
|
|
350
|
+
if (originalKey in jsonObject) {
|
|
351
|
+
result[newKey] = jsonObject[originalKey];
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
return result;
|
|
357
|
+
} catch (error) {
|
|
358
|
+
console.error('Error in getPartial:', error);
|
|
359
|
+
return {};
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
function getPartialDeep(jsonObject, attributePaths) {
|
|
364
|
+
try {
|
|
365
|
+
if (typeof jsonObject === 'string') {
|
|
366
|
+
jsonObject = JSON.parse(jsonObject);
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
if (typeof jsonObject !== 'object' || jsonObject === null) {
|
|
370
|
+
return {};
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
const result = {};
|
|
374
|
+
|
|
375
|
+
function getValueFromPath(obj, path) {
|
|
376
|
+
const parts = path.split('.');
|
|
377
|
+
let current = obj;
|
|
378
|
+
|
|
379
|
+
for (const part of parts) {
|
|
380
|
+
if (current && typeof current === 'object' && part in current) {
|
|
381
|
+
current = current[part];
|
|
382
|
+
} else {
|
|
383
|
+
return undefined;
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
return current;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
if (Array.isArray(attributePaths)) {
|
|
391
|
+
for (const path of attributePaths) {
|
|
392
|
+
const value = getValueFromPath(jsonObject, path);
|
|
393
|
+
if (value !== undefined) {
|
|
394
|
+
const key = path.split('.').pop();
|
|
395
|
+
result[key] = value;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
else if (typeof attributePaths === 'object' && attributePaths !== null) {
|
|
400
|
+
for (const [newKey, path] of Object.entries(attributePaths)) {
|
|
401
|
+
const value = getValueFromPath(jsonObject, path);
|
|
402
|
+
if (value !== undefined) {
|
|
403
|
+
result[newKey] = value;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
return result;
|
|
409
|
+
} catch (error) {
|
|
410
|
+
console.error('Error in getPartialDeep:', error);
|
|
411
|
+
return {};
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
function getPartialWithDefaults(jsonObject, attributesConfig) {
|
|
416
|
+
try {
|
|
417
|
+
if (typeof jsonObject === 'string') {
|
|
418
|
+
jsonObject = JSON.parse(jsonObject);
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
if (typeof jsonObject !== 'object' || jsonObject === null) {
|
|
422
|
+
return {};
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
const result = {};
|
|
426
|
+
|
|
427
|
+
function getValueFromPath(obj, path, defaultValue) {
|
|
428
|
+
const parts = path.split('.');
|
|
429
|
+
let current = obj;
|
|
430
|
+
|
|
431
|
+
for (const part of parts) {
|
|
432
|
+
if (current && typeof current === 'object' && part in current) {
|
|
433
|
+
current = current[part];
|
|
434
|
+
} else {
|
|
435
|
+
return defaultValue;
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
return current;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
for (const [outputKey, config] of Object.entries(attributesConfig)) {
|
|
443
|
+
if (typeof config === 'string') {
|
|
444
|
+
result[outputKey] = getValueFromPath(jsonObject, config, undefined);
|
|
445
|
+
} else if (typeof config === 'object' && config !== null) {
|
|
446
|
+
const path = config.path || outputKey;
|
|
447
|
+
const defaultValue = config.default;
|
|
448
|
+
let value = getValueFromPath(jsonObject, path, defaultValue);
|
|
449
|
+
|
|
450
|
+
if (config.transform && typeof config.transform === 'function') {
|
|
451
|
+
value = config.transform(value);
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
result[outputKey] = value;
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
return result;
|
|
459
|
+
} catch (error) {
|
|
460
|
+
console.error('Error in getPartialWithDefaults:', error);
|
|
461
|
+
return {};
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
function excludeAttributes(jsonObject, attributesToExclude) {
|
|
466
|
+
try {
|
|
467
|
+
if (typeof jsonObject === 'string') {
|
|
468
|
+
jsonObject = JSON.parse(jsonObject);
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
if (typeof jsonObject !== 'object' || jsonObject === null) {
|
|
472
|
+
return {};
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
const result = { ...jsonObject };
|
|
476
|
+
|
|
477
|
+
if (Array.isArray(attributesToExclude)) {
|
|
478
|
+
for (const attr of attributesToExclude) {
|
|
479
|
+
delete result[attr];
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
else if (typeof attributesToExclude === 'string') {
|
|
483
|
+
delete result[attributesToExclude];
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
return result;
|
|
487
|
+
} catch (error) {
|
|
488
|
+
console.error('Error in excludeAttributes:', error);
|
|
489
|
+
return {};
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
|
|
135
493
|
module.exports = {
|
|
136
494
|
getString,
|
|
137
495
|
getFile,
|
|
@@ -146,4 +504,12 @@ module.exports = {
|
|
|
146
504
|
getJSON,
|
|
147
505
|
beautifyJSON,
|
|
148
506
|
beautify,
|
|
149
|
-
|
|
507
|
+
getAttributes,
|
|
508
|
+
getMeta,
|
|
509
|
+
getMetaDetail,
|
|
510
|
+
getMetaCompact,
|
|
511
|
+
getPartial,
|
|
512
|
+
getPartialDeep,
|
|
513
|
+
getPartialWithDefaults,
|
|
514
|
+
excludeAttributes,
|
|
515
|
+
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@terzogenito/json-utils",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.0.10",
|
|
4
|
+
"description": "JSON data utilities",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"files": [
|
|
7
7
|
"index.js"
|
|
@@ -19,10 +19,21 @@
|
|
|
19
19
|
"keywords": [
|
|
20
20
|
"nodejs",
|
|
21
21
|
"module",
|
|
22
|
+
"utils",
|
|
23
|
+
"tools",
|
|
22
24
|
"json",
|
|
23
25
|
"check",
|
|
24
|
-
"
|
|
26
|
+
"beautify",
|
|
27
|
+
"analysis"
|
|
25
28
|
],
|
|
26
29
|
"author": "Terzogenito",
|
|
27
|
-
"license": "MIT"
|
|
30
|
+
"license": "MIT",
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "https://github.com/terzogenito/json-utils.git"
|
|
34
|
+
},
|
|
35
|
+
"homepage": "https://github.com/terzogenito/json-utils#readme",
|
|
36
|
+
"bugs": {
|
|
37
|
+
"url": "https://github.com/terzogenito/json-utils/issues"
|
|
38
|
+
}
|
|
28
39
|
}
|