Skip to content

Commit

Permalink
style: eslint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
uwla committed Feb 19, 2025
1 parent ed14e52 commit 83208f9
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/components/DataTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ export default defineComponent({
lastPage,
]
}
throw new Error('INVALID PAGE RANGE')
throw new Error("INVALID PAGE RANGE")
},

// ─────────────────────────────────────────────────────────────────────
Expand Down
10 changes: 5 additions & 5 deletions src/demo/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export default {

methods: {
updateUserField(user: User, field: UserField, value: any) {
const ind = this.data.findIndex((u: any) => u.id === user.id)
const ind = this.data.findIndex((u: User) => u.id === user.id)
if (ind < 0) return
const newUser = { ...this.data[ind] }
newUser[field] = value
Expand All @@ -151,12 +151,12 @@ export default {
this.showUser(user)
break
case "updateCell":
const { key, value } = payload
this.updateUserField(user, key, value)
this.updateUserField(user, payload.key, payload.value)
break
case "select":
const { selected } = payload
this.updateSelection(user, selected)
this.updateSelection(user, payload.selected)
break
default:
}
},
addUser(user: User) {
Expand Down
2 changes: 1 addition & 1 deletion src/demo/CellImage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/>
</template>
<script>
export default { name: "CellImage", props: ["data"] }
export default { name: "CellImage", props: { data: Object } }
</script>
<style>
.cell-img {
Expand Down
2 changes: 1 addition & 1 deletion src/demo/CellList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
</ul>
</template>
<script>
export default { name: "CellList", props: ["data"] }
export default { name: "CellList", props: { data: Object } }
</script>
7 changes: 3 additions & 4 deletions src/parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ export const globalDefaultColumn = {
type: "string",
} as Column

const type2searchFunction = {
string: searchStringColumn as Function,
numeric: searchNumericColumn as Function,
} as Record<ColumnType, Function>
const type2searchFunction: Partial<
Record<ColumnType, Column["searchFunction"]>
> = { string: searchStringColumn, numeric: searchNumericColumn }

export function parseColumnProps(props: any) {
// extract the columns. If not set, columns are derived from columnKeys
Expand Down
5 changes: 3 additions & 2 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
export type VueComponent = string | any
export type VueComponentProps = { [key: string]: any }

export type CompareFn = (a: any, b: any) => number
export type SortingMode = "asc" | "desc" | "none"
export type ColumnType = "numeric" | "string" | "array" | "other"
export type Column = {
compareFunction: Function
compareFunction: CompareFn
component: VueComponent
componentProps: VueComponentProps
collapsed: boolean
Expand All @@ -17,7 +18,7 @@ export type Column = {
key: string
id: number
searchable: boolean
searchFunction: Function
searchFunction: (data: Cell, search: string, key: string) => boolean
sortable: boolean
sortingIndex: number
sortingMode: SortingMode
Expand Down
21 changes: 13 additions & 8 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Cell, Column, Data } from "./types"
import type { Cell, Column, CompareFn, Data } from "./types"

export function toTitleCase(str: string): string {
// convert snake case to title case
Expand Down Expand Up @@ -41,15 +41,15 @@ export function isNullable(variable: any): boolean {
return variable === null || variable === "" || variable === undefined
}

export function stableSort<T>(arr: T[], compare: Function): T[] {
export function stableSort<T>(arr: T[], compare: CompareFn): T[] {
return arr
.map((item, index) => ({ item, index }))
.sort((a, b) => compare(a.item, b.item) || a.index - b.index)
.map(({ item }) => item)
}

// Safely compare two items, which may be nullable
export function safeCompare(compareFunction: Function): Function {
export function safeCompare(compareFunction: CompareFn): CompareFn {
return function (a: any, b: any) {
if (isNullable(a)) return 1
if (isNullable(b)) return -1
Expand All @@ -58,7 +58,7 @@ export function safeCompare(compareFunction: Function): Function {
}

// Safely compare two items by key, which may be nullable
export function safeKeyCompare(compareFunction: Function, key: string) {
export function safeKeyCompare(compareFunction: CompareFn, key: string) {
return function (a: any, b: any) {
if (isNullable(a[key])) return 1
if (isNullable(b[key])) return -1
Expand All @@ -67,7 +67,7 @@ export function safeKeyCompare(compareFunction: Function, key: string) {
}

// Reverse a comparison function
export function reverseCompare(compareFunction: Function): Function {
export function reverseCompare(compareFunction: CompareFn): CompareFn {
return (a: any, b: any) => compareFunction(b, a)
}

Expand All @@ -82,7 +82,7 @@ export function compareNumbers(a: string | number, b: string | number): number {
}

// Safely stable sort an array that may have null elements
export function arraySafeSort<T>(array: T[], compareFunction: Function): T[] {
export function arraySafeSort<T>(array: T[], compareFunction: CompareFn): T[] {
return stableSort(array, safeCompare(compareFunction))
}

Expand All @@ -94,7 +94,8 @@ export function sortDataByColumns(data: Data, columns: Column[]): Data {
let i = 0
while (i < l) {
const c = columns[i]
let { sortingMode, compareFunction: f } = c
const { sortingMode, compareFunction } = c
let f = compareFunction

// reverse comparison
const reverseSearch = sortingMode === "desc"
Expand Down Expand Up @@ -141,7 +142,11 @@ export function getEventTargetValue(event: any = null) {
}

// Performs search on strings
export function searchStringColumn(data: Cell, search: string, key: string) {
export function searchStringColumn(
data: Cell,
search: string,
key: string
): boolean {
return (data[key] || "").toLowerCase().includes(search.toLowerCase())
}

Expand Down
5 changes: 3 additions & 2 deletions tests/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
arraySafeSort,
safeCompare,
} from "../src/utils"
import type { CompareFn } from "../src/types"

test("test string replacement", function () {
const str = "showing :first: to :last: entries of :total: rows"
Expand All @@ -16,8 +17,8 @@ test("test string replacement", function () {
test("test safe sort", function () {
let arr = [2, 45, null, 10, 20, null, 15]
let res: (number | null)[]
let f: Function = (a: any, b: any) => a - b
let g: Function = (a: any, b: any) => b - a
let f: CompareFn = (a: any, b: any) => a - b
let g: CompareFn = (a: any, b: any) => b - a
res = arraySafeSort(arr, f)
expect(res).toEqual([2, 10, 15, 20, 45, null, null])
res = arraySafeSort(arr, g)
Expand Down

0 comments on commit 83208f9

Please sign in to comment.