From 8163b08031cf995cc33397ff56bf3220c39b99ad Mon Sep 17 00:00:00 2001 From: Francisco Madeira Date: Sat, 18 Sep 2021 22:51:40 +0100 Subject: [PATCH] Fix lint issues --- .eslintrc.js | 2 ++ src/index.ts | 54 ++++++++++++++++++++++++++-------------------------- 2 files changed, 29 insertions(+), 27 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index f54fdb8..4250e1c 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -19,10 +19,12 @@ module.exports = { rules: { 'func-names': ['error', 'never'], 'space-before-function-paren': ['error', 'never'], + 'no-underscore-dangle': 0, 'no-param-reassign': 0, 'arrow-parens': 0, 'dot-notation': 0, 'operator-linebreak': 0, + 'no-nested-ternary': 0, 'import/no-unresolved': 0, 'import/extensions': 0, '@typescript-eslint/type-annotation-spacing': ['error', { before: false, after: false }], diff --git a/src/index.ts b/src/index.ts index efa1ade..3435b74 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,7 @@ function validateData(data) { if (!data || typeof data !== 'object' || - Array.isArray(data) && data.length && - (Array.isArray(data[0]) || typeof data[0] !== 'object')) { + (Array.isArray(data) && data.length && (Array.isArray(data[0]) || typeof data[0] !== 'object')) + ) { throw new Error('It can\'t sort, validate the data sent'); } } @@ -17,24 +17,27 @@ function validateParams(data, columns, orderBy, key) { validateKey(key); } +function isObject(data) { + return data !== null && + typeof data === 'object' && + !Array.isArray(data); +} + function formatArguments(args) { let columns = []; let orderBy = []; let key = '_key'; // If is Object - if (args[0] !== null && - typeof args[0] === 'object' && - !Array.isArray(args[0])) { - for (const column in args[0]) { + if (isObject(args[0])) { + Object.entries(args[0]).forEach(([column, direction]) => { columns.push(column); - orderBy.push(args[0][column]); - } + orderBy.push(direction); + }); key = args[1] || key; } else { - columns = args[0]; - orderBy = args[1]; + [columns, orderBy] = args; if (typeof columns === 'string') { columns = [columns]; @@ -57,9 +60,9 @@ function formatData(arr, key) { const data = []; - for (const i in arr) { + Object.keys(arr).forEach(i => { data.push({ ...arr[i], [key]: i }); - } + }); return data; } @@ -81,31 +84,28 @@ function getValue(column, item) { } function sort(a, b, columns, orderBy, index) { - const direction = orderBy[index] == 'DESC' ? 1 : 0; + const direction = orderBy[index] === 'DESC' ? 1 : 0; const aValue = getValue(columns[index], a); const bValue = getValue(columns[index], b); - const isNumeric = !isNaN(+aValue - +bValue); - const x = isNumeric ? - +aValue : - Array.isArray(aValue) ? - aValue : - (`${aValue}`).toLowerCase(); - const y = isNumeric ? - +bValue : - Array.isArray(bValue) ? - bValue : - (`${bValue}`).toLowerCase(); + const isNumeric = !Number.isNaN(+aValue - +bValue); + const x = isNumeric + ? +aValue : Array.isArray(aValue) + ? aValue : (`${aValue}`).toLowerCase(); + + const y = isNumeric + ? +bValue : Array.isArray(bValue) + ? bValue : (`${bValue}`).toLowerCase(); if (x < y) { - return direction == 0 ? -1 : 1; + return direction === 0 ? -1 : 1; } - if (x == y) { + if (x === y) { return columns.length - 1 > index ? sort(a, b, columns, orderBy, index + 1) : 0; } - return direction == 0 ? 1 : -1; + return direction === 0 ? 1 : -1; } export default function MultiSort(arr = [], ...args) {