2023-01-24 02:32:31 -08:00
|
|
|
import { IDisplayOptions, INodeProperties } from 'n8n-workflow';
|
|
|
|
|
|
|
|
import { merge } from 'lodash';
|
|
|
|
|
2020-07-08 01:00:13 -07:00
|
|
|
/**
|
|
|
|
* Creates an array of elements split into groups the length of `size`.
|
|
|
|
* If `array` can't be split evenly, the final chunk will be the remaining
|
|
|
|
* elements.
|
|
|
|
*
|
|
|
|
* @param {Array} array The array to process.
|
|
|
|
* @param {number} [size=1] The length of each chunk
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* chunk(['a', 'b', 'c', 'd'], 2)
|
|
|
|
* // => [['a', 'b'], ['c', 'd']]
|
|
|
|
*
|
|
|
|
* chunk(['a', 'b', 'c', 'd'], 3)
|
|
|
|
* // => [['a', 'b', 'c'], ['d']]
|
|
|
|
*/
|
2022-08-17 08:50:24 -07:00
|
|
|
|
|
|
|
export function chunk(array: any[], size = 1) {
|
2020-07-08 01:00:13 -07:00
|
|
|
const length = array == null ? 0 : array.length;
|
|
|
|
if (!length || size < 1) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
let index = 0;
|
|
|
|
let resIndex = 0;
|
|
|
|
const result = new Array(Math.ceil(length / size));
|
|
|
|
|
|
|
|
while (index < length) {
|
|
|
|
result[resIndex++] = array.slice(index, (index += size));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Takes a multidimensional array and converts it to a one-dimensional array.
|
|
|
|
*
|
|
|
|
* @param {Array} nestedArray The array to be flattened.
|
|
|
|
* @example
|
|
|
|
*
|
|
|
|
* flatten([['a', 'b'], ['c', 'd']])
|
|
|
|
* // => ['a', 'b', 'c', 'd']
|
|
|
|
*
|
|
|
|
*/
|
2022-08-17 08:50:24 -07:00
|
|
|
|
|
|
|
export function flatten(nestedArray: any[][]) {
|
2020-07-08 01:00:13 -07:00
|
|
|
const result = [];
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
(function loop(array: any[]) {
|
2020-07-14 04:59:37 -07:00
|
|
|
for (let i = 0; i < array.length; i++) {
|
2020-07-08 01:00:13 -07:00
|
|
|
if (Array.isArray(array[i])) {
|
|
|
|
loop(array[i]);
|
|
|
|
} else {
|
|
|
|
result.push(array[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})(nestedArray);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2023-01-24 02:32:31 -08:00
|
|
|
|
|
|
|
export function updateDisplayOptions(
|
|
|
|
displayOptions: IDisplayOptions,
|
|
|
|
properties: INodeProperties[],
|
|
|
|
) {
|
|
|
|
return properties.map((nodeProperty) => {
|
|
|
|
return {
|
|
|
|
...nodeProperty,
|
|
|
|
displayOptions: merge({}, nodeProperty.displayOptions, displayOptions),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|