n8n-nodes-base
Version:
Base nodes of n8n
257 lines (255 loc) • 10.7 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Sort = void 0;
const get_1 = __importDefault(require("lodash/get"));
const isEqual_1 = __importDefault(require("lodash/isEqual"));
const lt_1 = __importDefault(require("lodash/lt"));
const n8n_workflow_1 = require("n8n-workflow");
const utilities_1 = require("../../../utils/utilities");
const utils_1 = require("./utils");
class Sort {
description = {
displayName: 'Sort',
name: 'sort',
icon: 'file:sort.svg',
group: ['transform'],
subtitle: '',
version: 1,
description: 'Change items order',
defaults: {
name: 'Sort',
},
inputs: [n8n_workflow_1.NodeConnectionTypes.Main],
outputs: [n8n_workflow_1.NodeConnectionTypes.Main],
properties: [
{
displayName: 'Type',
name: 'type',
type: 'options',
options: [
{
name: 'Simple',
value: 'simple',
},
{
name: 'Random',
value: 'random',
},
{
name: 'Code',
value: 'code',
},
],
default: 'simple',
description: 'The type of sorting to perform',
},
{
displayName: 'Fields To Sort By',
name: 'sortFieldsUi',
type: 'fixedCollection',
typeOptions: {
multipleValues: true,
},
placeholder: 'Add Field To Sort By',
options: [
{
displayName: '',
name: 'sortField',
values: [
{
displayName: 'Field Name',
name: 'fieldName',
type: 'string',
required: true,
default: '',
description: 'The field to sort by',
// eslint-disable-next-line n8n-nodes-base/node-param-placeholder-miscased-id
placeholder: 'e.g. id',
hint: ' Enter the field name as text',
requiresDataPath: 'single',
},
{
displayName: 'Order',
name: 'order',
type: 'options',
options: [
{
name: 'Ascending',
value: 'ascending',
},
{
name: 'Descending',
value: 'descending',
},
],
default: 'ascending',
description: 'The order to sort by',
},
],
},
],
default: {},
description: 'The fields of the input items to sort by',
displayOptions: {
show: {
type: ['simple'],
},
},
},
{
displayName: 'Code',
name: 'code',
type: 'string',
typeOptions: {
alwaysOpenEditWindow: true,
editor: 'jsEditor',
rows: 10,
},
default: `// The two items to compare are in the variables a and b
// Access the fields in a.json and b.json
// Return -1 if a should go before b
// Return 1 if b should go before a
// Return 0 if there's no difference
fieldName = 'myField';
if (a.json[fieldName] < b.json[fieldName]) {
return -1;
}
if (a.json[fieldName] > b.json[fieldName]) {
return 1;
}
return 0;`,
description: 'Javascript code to determine the order of any two items',
displayOptions: {
show: {
type: ['code'],
},
},
},
{
displayName: 'Options',
name: 'options',
type: 'collection',
placeholder: 'Add Field',
default: {},
displayOptions: {
show: {
type: ['simple'],
},
},
options: [
{
displayName: 'Disable Dot Notation',
name: 'disableDotNotation',
type: 'boolean',
default: false,
description: 'Whether to disallow referencing child fields using `parent.child` in the field name',
},
],
},
],
};
async execute() {
const items = this.getInputData();
let returnData = [...items];
const type = this.getNodeParameter('type', 0);
const disableDotNotation = this.getNodeParameter('options.disableDotNotation', 0, false);
if (type === 'random') {
(0, utilities_1.shuffleArray)(returnData);
return [returnData];
}
if (type === 'simple') {
const sortFieldsUi = this.getNodeParameter('sortFieldsUi', 0);
const sortFields = sortFieldsUi.sortField;
if (!sortFields?.length) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'No sorting specified. Please add a field to sort by');
}
for (const { fieldName } of sortFields) {
let found = false;
for (const item of items) {
if (!disableDotNotation) {
if ((0, get_1.default)(item.json, fieldName) !== undefined) {
found = true;
}
}
else if (item.json.hasOwnProperty(fieldName)) {
found = true;
}
}
if (!found && disableDotNotation && fieldName.includes('.')) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Couldn't find the field '${fieldName}' in the input data`, {
description: "If you're trying to use a nested field, make sure you turn off 'disable dot notation' in the node options",
});
}
else if (!found) {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Couldn't find the field '${fieldName}' in the input data`);
}
}
const sortFieldsWithDirection = sortFields.map((field) => ({
name: field.fieldName,
dir: field.order === 'ascending' ? 1 : -1,
}));
returnData.sort((a, b) => {
let result = 0;
for (const field of sortFieldsWithDirection) {
let equal;
if (!disableDotNotation) {
const _a = typeof (0, get_1.default)(a.json, field.name) === 'string'
? (0, get_1.default)(a.json, field.name).toLowerCase()
: (0, get_1.default)(a.json, field.name);
const _b = typeof (0, get_1.default)(b.json, field.name) === 'string'
? (0, get_1.default)(b.json, field.name).toLowerCase()
: (0, get_1.default)(b.json, field.name);
equal = (0, isEqual_1.default)(_a, _b);
}
else {
const _a = typeof a.json[field.name] === 'string'
? a.json[field.name].toLowerCase()
: a.json[field.name];
const _b = typeof b.json[field.name] === 'string'
? b.json[field.name].toLowerCase()
: b.json[field.name];
equal = (0, isEqual_1.default)(_a, _b);
}
if (!equal) {
let lessThan;
if (!disableDotNotation) {
const _a = typeof (0, get_1.default)(a.json, field.name) === 'string'
? (0, get_1.default)(a.json, field.name).toLowerCase()
: (0, get_1.default)(a.json, field.name);
const _b = typeof (0, get_1.default)(b.json, field.name) === 'string'
? (0, get_1.default)(b.json, field.name).toLowerCase()
: (0, get_1.default)(b.json, field.name);
lessThan = (0, lt_1.default)(_a, _b);
}
else {
const _a = typeof a.json[field.name] === 'string'
? a.json[field.name].toLowerCase()
: a.json[field.name];
const _b = typeof b.json[field.name] === 'string'
? b.json[field.name].toLowerCase()
: b.json[field.name];
lessThan = (0, lt_1.default)(_a, _b);
}
if (lessThan) {
result = -1 * field.dir;
}
else {
result = 1 * field.dir;
}
break;
}
}
return result;
});
}
else {
returnData = await utils_1.sortByCode.call(this);
}
return [returnData];
}
}
exports.Sort = Sort;
//# sourceMappingURL=Sort.node.js.map