Skip to content

Commit

Permalink
Fix lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
xiCO2k committed Sep 18, 2021
1 parent f8811b1 commit 8163b08
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 27 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }],
Expand Down
54 changes: 27 additions & 27 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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');
}
}
Expand All @@ -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];
Expand All @@ -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;
}
Expand All @@ -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) {
Expand Down

0 comments on commit 8163b08

Please sign in to comment.