@narrative.io/data-collaboration-sdk-ts 0.0.74 → 0.0.75
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.
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { type ObjectAttribute } from 'src/attributes/types';
|
|
2
|
+
import { type Attribute } from '../attributes';
|
|
3
|
+
import type { DataRulesAttributesField, ForecastBody, Nql } from './types';
|
|
2
4
|
/**
|
|
3
5
|
* Convert an NQL object to a Forecast body.
|
|
4
6
|
* @param {Nql} nqlObject - The NQL object to convert.
|
|
@@ -6,3 +8,43 @@ import type { ForecastBody, Nql } from './types';
|
|
|
6
8
|
* @throws {Error} - Throws an error if price filter conditions are not met.
|
|
7
9
|
*/
|
|
8
10
|
export declare function convertNqlObjectToForecastBody(nqlObject: Nql): Promise<ForecastBody>;
|
|
11
|
+
/**
|
|
12
|
+
* Looks for an attribute with the given name and checks if it is of type 'object'.
|
|
13
|
+
*
|
|
14
|
+
* @param {Attribute[]} attributes - An array of attributes to search through.
|
|
15
|
+
* @param {string} name - The name of the attribute to look for.
|
|
16
|
+
* @returns {ObjectAttribute | undefined} Returns the attribute if found and is of type 'object', otherwise returns undefined.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* // Assuming attributes is an array of Attribute objects with properties 'name', 'type', and optionally 'properties'.
|
|
20
|
+
* const attributes = [{ name: 'person', type: 'object', properties: { name: 'John Doe' } }];
|
|
21
|
+
* // Returns { name: 'person', type: 'object', properties: { name: 'John Doe' } }
|
|
22
|
+
* isObject(attributes, 'person');
|
|
23
|
+
*/
|
|
24
|
+
export declare function isObject(attributes: Attribute[], name: string): ObjectAttribute | undefined;
|
|
25
|
+
/**
|
|
26
|
+
* Finds an attribute with the given name, and if it is of type 'object', returns the names of its properties.
|
|
27
|
+
*
|
|
28
|
+
* @param {Attribute[]} attributes - An array of attributes to search through.
|
|
29
|
+
* @param {string} name - The name of the attribute to look for.
|
|
30
|
+
* @returns {string[]} An array containing the names of the properties if the attribute is of type 'object', otherwise an empty array.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* // Assuming attributes is an array of Attribute objects with properties 'name', 'type', and optionally 'properties'.
|
|
34
|
+
* const attributes = [{ name: 'person', type: 'object', properties: { name: 'John Doe' } }];
|
|
35
|
+
* // Returns ['name']
|
|
36
|
+
* findAttributeProperties(attributes, 'person');
|
|
37
|
+
*/
|
|
38
|
+
export declare function findAttributeProperties(attributes: Attribute[], name: string): string[];
|
|
39
|
+
/**
|
|
40
|
+
* Builds an array of field objects with names based on the given attribute and properties.
|
|
41
|
+
*
|
|
42
|
+
* @param {string} attribute - The primary attribute to be prefixed to each field.
|
|
43
|
+
* @param {string[]} properties - An array of properties that will be appended to the attribute to form the field names.
|
|
44
|
+
* @returns {DataRulesAttributesField[]} An array of field objects, where each object has a 'field' property which is a combination of the attribute and property, and an 'exported' property set to true.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* // Returns [{ field: 'person.name', exported: true }, { field: 'person.age', exported: true }]
|
|
48
|
+
* buildField('person', ['name', 'age']);
|
|
49
|
+
*/
|
|
50
|
+
export declare function buildField(attribute: string, properties: string[]): DataRulesAttributesField[];
|
|
@@ -34,13 +34,15 @@ async function getAttributesFromFields(fields) {
|
|
|
34
34
|
const attributes = await attributesApi.getAttributes();
|
|
35
35
|
const uniqueAttributes = [...new Set(fields.map((field) => field.field.split('.')[0]))].filter((att) => !att.startsWith('_'));
|
|
36
36
|
uniqueAttributes.forEach((att) => {
|
|
37
|
+
const isObjectProperty = isObject(attributes.records, att);
|
|
38
|
+
const relevantFields = (isObjectProperty != null) ? fields.filter((field) => field.field.split('.')[0] === att && field.field !== att) : fields.filter((field) => field.field.split('.')[0] === att);
|
|
37
39
|
const attributeId = attributes.records.find((attribute) => attribute.name === att)?.id;
|
|
38
40
|
if (attributeId === undefined) {
|
|
39
41
|
throw new Error(`Attribute ${att} not found`);
|
|
40
42
|
}
|
|
41
43
|
attributesArray.push({
|
|
42
44
|
attribute_id: attributeId,
|
|
43
|
-
fields:
|
|
45
|
+
fields: relevantFields.length > 0 ? relevantFields : buildField(att, findAttributeProperties(attributes.records, att)),
|
|
44
46
|
optional: fields.some(field => field.field.startsWith(att + '.') && field.filter === undefined)
|
|
45
47
|
});
|
|
46
48
|
});
|
|
@@ -212,3 +214,65 @@ function addParentheses(input) {
|
|
|
212
214
|
// Replace matched string with itself followed by ()
|
|
213
215
|
return input.replace(regex, '$1()');
|
|
214
216
|
}
|
|
217
|
+
/**
|
|
218
|
+
* Looks for an attribute with the given name and checks if it is of type 'object'.
|
|
219
|
+
*
|
|
220
|
+
* @param {Attribute[]} attributes - An array of attributes to search through.
|
|
221
|
+
* @param {string} name - The name of the attribute to look for.
|
|
222
|
+
* @returns {ObjectAttribute | undefined} Returns the attribute if found and is of type 'object', otherwise returns undefined.
|
|
223
|
+
*
|
|
224
|
+
* @example
|
|
225
|
+
* // Assuming attributes is an array of Attribute objects with properties 'name', 'type', and optionally 'properties'.
|
|
226
|
+
* const attributes = [{ name: 'person', type: 'object', properties: { name: 'John Doe' } }];
|
|
227
|
+
* // Returns { name: 'person', type: 'object', properties: { name: 'John Doe' } }
|
|
228
|
+
* isObject(attributes, 'person');
|
|
229
|
+
*/
|
|
230
|
+
export function isObject(attributes, name) {
|
|
231
|
+
// Look for the attribute with the given name.
|
|
232
|
+
const attribute = attributes.find(attr => attr.name === name);
|
|
233
|
+
// If no such attribute is found, return an empty array.
|
|
234
|
+
if (attribute == null) {
|
|
235
|
+
return undefined;
|
|
236
|
+
}
|
|
237
|
+
return attribute.type === 'object' && 'properties' in attribute ? attribute : undefined;
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Finds an attribute with the given name, and if it is of type 'object', returns the names of its properties.
|
|
241
|
+
*
|
|
242
|
+
* @param {Attribute[]} attributes - An array of attributes to search through.
|
|
243
|
+
* @param {string} name - The name of the attribute to look for.
|
|
244
|
+
* @returns {string[]} An array containing the names of the properties if the attribute is of type 'object', otherwise an empty array.
|
|
245
|
+
*
|
|
246
|
+
* @example
|
|
247
|
+
* // Assuming attributes is an array of Attribute objects with properties 'name', 'type', and optionally 'properties'.
|
|
248
|
+
* const attributes = [{ name: 'person', type: 'object', properties: { name: 'John Doe' } }];
|
|
249
|
+
* // Returns ['name']
|
|
250
|
+
* findAttributeProperties(attributes, 'person');
|
|
251
|
+
*/
|
|
252
|
+
export function findAttributeProperties(attributes, name) {
|
|
253
|
+
const attribute = isObject(attributes, name);
|
|
254
|
+
// If the attribute is of type 'object', return the names of its properties.
|
|
255
|
+
if (attribute != null) {
|
|
256
|
+
return Object.keys(attribute.properties);
|
|
257
|
+
}
|
|
258
|
+
return [];
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Builds an array of field objects with names based on the given attribute and properties.
|
|
262
|
+
*
|
|
263
|
+
* @param {string} attribute - The primary attribute to be prefixed to each field.
|
|
264
|
+
* @param {string[]} properties - An array of properties that will be appended to the attribute to form the field names.
|
|
265
|
+
* @returns {DataRulesAttributesField[]} An array of field objects, where each object has a 'field' property which is a combination of the attribute and property, and an 'exported' property set to true.
|
|
266
|
+
*
|
|
267
|
+
* @example
|
|
268
|
+
* // Returns [{ field: 'person.name', exported: true }, { field: 'person.age', exported: true }]
|
|
269
|
+
* buildField('person', ['name', 'age']);
|
|
270
|
+
*/
|
|
271
|
+
export function buildField(attribute, properties) {
|
|
272
|
+
return properties.map((property) => {
|
|
273
|
+
return {
|
|
274
|
+
field: `${attribute}.${property}`,
|
|
275
|
+
exported: true
|
|
276
|
+
};
|
|
277
|
+
});
|
|
278
|
+
}
|