Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing row to getColumnValue, so column can have a sortBy function #10

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions .github/workflows/release-package.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Node.js Package

on:
release:
types: [created]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 12
- run: npm ci
- run: npm test

publish-gpr:
needs: build
runs-on: ubuntu-latest
permissions:
packages: write
contents: read
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 12
registry-url: https://npm.pkg.github.com/
- run: npm ci
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
node_modules
coverage
dist
coverage
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npm test
8 changes: 8 additions & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export declare type Direction = 'ASC' | 'DESC';
export declare type SortArray<T> = [keyof T, Direction][];
export declare type SortObject<T> = {
[key in keyof T]?: Direction;
};
export declare type GetColumnValue<T> = (column: keyof T, value: T[keyof T], row: T) => any;
declare const multiColumnSort: <T>(arr: T[], sortArrOrObject: SortArray<T> | SortObject<T>, getColumnValue?: GetColumnValue<T> | undefined) => T[];
export default multiColumnSort;
44 changes: 44 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"use strict";
var __spreadArray = (this && this.__spreadArray) || function (to, from) {
for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
to[j] = from[i];
return to;
};
Object.defineProperty(exports, "__esModule", { value: true });
var sort = function (a, b, columns, getColumnValue) {
var item = columns[0], others = columns.slice(1);
var column = item[0], orderBy = item[1];
var valueA = getColumnValue ? getColumnValue(column, a[column], a) : a[column];
var valueB = getColumnValue ? getColumnValue(column, b[column], b) : b[column];
if (orderBy === 'ASC') {
if (valueA > valueB)
return 1;
if (valueA < valueB)
return -1;
if (others.length)
return sort(a, b, others, getColumnValue);
return 0;
}
else {
if (valueB > valueA)
return 1;
if (valueB < valueA)
return -1;
if (others.length)
return sort(a, b, others, getColumnValue);
return 0;
}
};
var sortObjectToArray = function (object) {
return Object.keys(object).reduce(function (arr, column) {
return __spreadArray(__spreadArray([], arr), [[column, object[column]]]);
}, []);
};
var multiColumnSort = function (arr, sortArrOrObject, getColumnValue) {
return __spreadArray([], arr).sort(function (a, b) {
return sort(a, b, Array.isArray(sortArrOrObject)
? sortArrOrObject
: sortObjectToArray(sortArrOrObject), getColumnValue);
});
};
exports.default = multiColumnSort;
6 changes: 3 additions & 3 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export type Direction = 'ASC' | 'DESC'
export type SortArray<T> = [keyof T, Direction][]
export type SortObject<T> = { [key in keyof T]?: Direction }
export type GetColumnValue<T> = (column: keyof T, value: T[keyof T]) => any
export type GetColumnValue<T> = (column: keyof T, value: T[keyof T], row: T) => any

const sort = <T>(
a: T,
Expand All @@ -11,8 +11,8 @@ const sort = <T>(
): -1 | 0 | 1 => {
const [item, ...others] = columns
const [column, orderBy] = item
const valueA = getColumnValue ? getColumnValue(column, a[column]) : a[column]
const valueB = getColumnValue ? getColumnValue(column, b[column]) : b[column]
const valueA = getColumnValue ? getColumnValue(column, a[column], a) : a[column]
const valueB = getColumnValue ? getColumnValue(column, b[column], b) : b[column]

if (orderBy === 'ASC') {
if (valueA > valueB) return 1
Expand Down
Loading