From 39ddd9919b523e47df2433c178c44f16856ad6f5 Mon Sep 17 00:00:00 2001 From: Francisco Madeira Date: Sat, 18 Sep 2021 23:15:13 +0100 Subject: [PATCH] Refactor to TypeScript --- src/index.ts | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/index.ts b/src/index.ts index 3435b74..3b0d21c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ -function validateData(data) { +function validateData(data):void { if (!data || typeof data !== 'object' || (Array.isArray(data) && data.length && (Array.isArray(data[0]) || typeof data[0] !== 'object')) ) { @@ -6,24 +6,30 @@ function validateData(data) { } } -function validateKey(key) { +function validateKey(key):void { if (typeof key !== 'string') { throw new Error('The key param needs to be a string'); } } -function validateParams(data, columns, orderBy, key) { +function validateParams(data, columns, orderBy, key):void { validateData(data); validateKey(key); } -function isObject(data) { +function isObject(data):boolean { return data !== null && typeof data === 'object' && !Array.isArray(data); } -function formatArguments(args) { +interface Arguments { + columns:Array, + orderBy:Array, + key:string, +} + +function formatArguments(args:Array):Arguments { let columns = []; let orderBy = []; let key = '_key'; @@ -53,7 +59,7 @@ function formatArguments(args) { return { columns, orderBy, key }; } -function formatData(arr, key) { +function formatData(arr, key):Array { if (Array.isArray(arr)) { return arr; } @@ -67,7 +73,7 @@ function formatData(arr, key) { return data; } -function getValue(column, item) { +function getValue(column, item):any { column = column.split('.'); let value = item; @@ -83,7 +89,7 @@ function getValue(column, item) { return value; } -function sort(a, b, columns, orderBy, index) { +function sort(a, b, columns, orderBy, index):number { const direction = orderBy[index] === 'DESC' ? 1 : 0; const aValue = getValue(columns[index], a); const bValue = getValue(columns[index], b); @@ -108,7 +114,10 @@ function sort(a, b, columns, orderBy, index) { return direction === 0 ? 1 : -1; } -export default function MultiSort(arr = [], ...args) { +export default function MultiSort( + arr:Array = [], + ...args:Array +):Array { const { columns, orderBy, key } = formatArguments(args); validateParams(arr, columns, orderBy, key); // it throws return formatData(arr, key).sort((a, b) => sort(a, b, columns, orderBy, 0));