n8n/packages/nodes-base/nodes/utils/utilities.ts
Ben Hesseldieck b1035d539d
MSSQL Node Integration (#729)
* basic setup mssql node

* executeQuery for MSSQL working

* work on insert MSSQL, incomplete

* 🚧 basic setup update functionality

* 🔨 refactor insert for handling >1000 values

*  complete MSSQL node

*  add delete action to node

* 🚧 handling multiple tables and column sets

* 🐛 enabling usage of expression for every field

* 🔨 remove lodash dependency

*  enable continue on fail option

* 🐎 minify icon

* 🔨 improve table creation, item copying, 🐛 correct output of node when active continue on fail

* 🐛 move mssql types to dev dependencies

* 🎨 remove auto formatting from redis

* 🎨 apply corrected syntax format

*  reset Redis node to master stage

* 🐛 fix building issue
2020-07-08 10:00:13 +02:00

58 lines
1.4 KiB
TypeScript

/**
* 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
* @returns {Array} Returns the new array of chunks.
* @example
*
* chunk(['a', 'b', 'c', 'd'], 2)
* // => [['a', 'b'], ['c', 'd']]
*
* chunk(['a', 'b', 'c', 'd'], 3)
* // => [['a', 'b', 'c'], ['d']]
*/
export function chunk(array: any[], size: number = 1) {
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.
* @returns {Array} Returns the new flattened array.
* @example
*
* flatten([['a', 'b'], ['c', 'd']])
* // => ['a', 'b', 'c', 'd']
*
*/
export function flatten(nestedArray: any[][]) {
const result = [];
(function loop(array: any[]) {
for (var i = 0; i < array.length; i++) {
if (Array.isArray(array[i])) {
loop(array[i]);
} else {
result.push(array[i]);
}
}
})(nestedArray);
return result;
}