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 63b9235 commit ed14e52
Show file tree
Hide file tree
Showing 21 changed files with 73 additions and 29 deletions.
10 changes: 9 additions & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@ import globals from "globals"
import typescriptEslint from "typescript-eslint"

export default typescriptEslint.config(
{ ignores: ["*.d.ts", "**/coverage", "**/dist", "assets/", "src/dev.ts"] },
{
ignores: [
"**/*.d.ts",
"**/coverage",
"**/dist",
"assets/",
"src/dev.ts",
],
},
{
extends: [
eslint.configs.recommended,
Expand Down
1 change: 1 addition & 0 deletions src/components/ActionButtons/ActionButtons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default defineComponent({
},
data: Object,
},
emits: ["userEvent"],
methods: {
triggerAction(action: string) {
this.$emit("userEvent", { action: action, data: this.data })
Expand Down
8 changes: 6 additions & 2 deletions src/components/DataTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { parseColumnProps, parseTextProps } from "../parser"

import { defineComponent, reactive } from "vue"
import { SORTING_MODE } from "../const"
import type { Column, Data } from "../types"

export default defineComponent({
name: "VueDataTable",
Expand Down Expand Up @@ -72,6 +73,8 @@ export default defineComponent({
vKey: { type: String, default: "" },
},

emits: ["userEvent"],

data: () => {
return reactive({
dataFetched: [] as Column[],
Expand Down Expand Up @@ -361,6 +364,7 @@ export default defineComponent({
lastPage,
]
}
throw new Error('INVALID PAGE RANGE')
},

// ─────────────────────────────────────────────────────────────────────
Expand Down Expand Up @@ -662,12 +666,12 @@ export default defineComponent({
* Set the current rows per page
*/
setPerPage(value: any) {
let previousFirstEntry, newPerPage, newCurrentPage
let newPerPage, newCurrentPage
const previousFirstEntry = this.firstEntry

// before updating the value of currentPerPage,
// we need to store the current firstEntry.
// We will use it to change the current page.
previousFirstEntry = this.firstEntry
newPerPage = this.currentPerPage

if (!this.perPageSizes.includes(newPerPage)) {
Expand Down
1 change: 1 addition & 0 deletions src/components/Pagination/Pagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default defineComponent({
nextPage: Number,
pagination: Array,
},
emits: ["set-page"],
setup() {
return { pageToGo: 1 }
},
Expand Down
1 change: 1 addition & 0 deletions src/components/PerPage/PerPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default defineComponent({
currentPerPage: { type: [Number, String], required: true },
perPageSizes: { type: Array, required: true },
},
emits: ["set-per-page"],
computed: {
textBeforeOptions() {
return (this.perPageText.split(":entries")[0] || "").trim()
Expand Down
1 change: 1 addition & 0 deletions src/components/SearchFilter/SearchFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ import { defineComponent } from "vue"
export default defineComponent({
name: "VdtSearchFilter",
props: { searchText: String, search: String },
emits: ["set-search"],
})
1 change: 1 addition & 0 deletions src/components/Table/Table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default defineComponent({
sortingIconComponent: [Object, String],
sortingIndexComponent: [Object, String],
},
emits: ["user-event", "sort-column"],
methods: {
// Propagate upwards an event from a user custom component
emitUserEvent(payload: any) {
Expand Down
1 change: 1 addition & 0 deletions src/components/Table/TableCellEditable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export default defineComponent({
data: { type: Object, required: true },
columnKey: { type: String, required: true },
},
emits: ["userEvent"],
data: () => {
return reactive({ isEditing: false, text: "" })
},
Expand Down
1 change: 1 addition & 0 deletions src/components/Table/TableCellSelectable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { defineComponent, reactive } from "vue"
export default defineComponent({
name: "VdtTableCellSelectable",
props: { data: { type: Object, required: true } },
emits: ["userEvent"],
data: () => {
return reactive({ selected: false })
},
Expand Down
2 changes: 2 additions & 0 deletions src/const.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { ColumnType, SortingMode } from "./types"

export const SORTING_MODE = { ASC: "asc", DESC: "desc", NONE: "none" } as {
[key: string]: SortingMode
}
Expand Down
2 changes: 1 addition & 1 deletion 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 => u.id === user.id)
const ind = this.data.findIndex((u: any) => u.id === user.id)
if (ind < 0) return
const newUser = { ...this.data[ind] }
newUser[field] = value
Expand Down
10 changes: 8 additions & 2 deletions src/lang.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
import en from "./lang/en"
import es from "./lang/es"
import ptBr from "./lang/pt-br"
import type {
LanguageDict,
LanguageDictKey,
LanguageDictVal,
LanguageName,
Translation,
} from "./types"

const translations = { "pt-br": ptBr, "en": en, "es": es } as Translation

Expand All @@ -26,5 +33,4 @@ const languageServiceProvider = {
},
}

export default translations
export { languageServiceProvider, translations }
export { languageServiceProvider, translations, translations as default }
2 changes: 2 additions & 0 deletions src/lang/en.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { LanguageDict } from "../types"

export default {
perPageText: "Show :entries entries",
perPageAllText: "ALL",
Expand Down
2 changes: 2 additions & 0 deletions src/lang/es.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { LanguageDict } from "../types"

export default {
perPageText: "Mostrando :entries datos",
perPageAllText: "TODOS",
Expand Down
2 changes: 2 additions & 0 deletions src/lang/pt-br.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { LanguageDict } from "../types"

export default {
perPageText: "Exibindo :entries dados",
perPageAllText: "TODOS",
Expand Down
4 changes: 1 addition & 3 deletions src/parser/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
// import VdtTableCell from "../components/Table/TableCell.vue"
// import VdtTableCellEditable from "../components/Table/TableCellEditable.vue"
import { searchNumericColumn, searchStringColumn, toTitleCase } from "../utils"
import { SORTING_MODE } from "../const"
import translations from "../lang"
import type { Column, ColumnType, LanguageDict, LanguageName } from "../types"

// default column to all instances of VDT
export const globalDefaultColumn = {
// component: VdtTableCell,
component: "vdt-cell",
componentProps: {},
displayIndex: 1000,
Expand Down
24 changes: 12 additions & 12 deletions src/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// ─────────────────────────────────────────────────────────────────────────────
// TYPE DEFINITIONS

type VueComponent = string | any
type VueComponentProps = { [key: string]: any }
export type VueComponent = string | any
export type VueComponentProps = { [key: string]: any }

type SortingMode = "asc" | "desc" | "none"
type ColumnType = "numeric" | "string" | "array" | "other"
type Column = {
export type SortingMode = "asc" | "desc" | "none"
export type ColumnType = "numeric" | "string" | "array" | "other"
export type Column = {
compareFunction: Function
component: VueComponent
componentProps: VueComponentProps
Expand All @@ -25,8 +25,8 @@ type Column = {
type: string
}

type LanguageName = string
type LanguageDictKey =
export type LanguageName = string
export type LanguageDictKey =
| "downloadButtonText"
| "downloadText"
| "emptyTableText"
Expand All @@ -40,9 +40,9 @@ type LanguageDictKey =
| "perPageAllText"
| "previousButtonText"
| "searchText"
type LanguageDictVal = string
type LanguageDict = Record<LanguageDictKey, LanguageDictVal>
type Translation = Record<LanguageName, LanguageDict>
export type LanguageDictVal = string
export type LanguageDict = Record<LanguageDictKey, LanguageDictVal>
export type Translation = Record<LanguageName, LanguageDict>

type Cell = { [key: string]: any }
type Data = Cell[]
export type Cell = { [key: string]: any }
export type Data = Cell[]
10 changes: 8 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { Cell, Column, Data } from "./types"

export function toTitleCase(str: string): string {
// convert snake case to title case
str = str.replace(/_/g, " ")
Expand Down Expand Up @@ -85,7 +87,7 @@ export function arraySafeSort<T>(array: T[], compareFunction: Function): T[] {
}

// Sort an array of objects (representing the table) by the given column
export function sortDataByColumns(data: Data, columns: Column[]) {
export function sortDataByColumns(data: Data, columns: Column[]): Data {
const l = columns.length

const fn = (a: any, b: any) => {
Expand Down Expand Up @@ -144,6 +146,10 @@ export function searchStringColumn(data: Cell, search: string, key: string) {
}

// Performs search on numeric values
export function searchNumericColumn(data: Cell, search: string, key: string) {
export function searchNumericColumn(
data: Cell,
search: string,
key: string
): boolean {
return (data[key] || "").toString().includes(search)
}
4 changes: 3 additions & 1 deletion tests/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ const CustomComponent2 = defineComponent({

// mount the component
export const wrapper = mount(VueDataTable, {
global: { components: { ...components, CustomComponent1 } },
global: {
components: { ...components, CustomComponent1, CustomComponent2 },
},
props: {
data: data,
columns: [{ key: "name" }, { key: "gender" }, { key: "job" }],
Expand Down
13 changes: 9 additions & 4 deletions tests/sorting.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ import {
testRowsMatchData,
wrapper,
} from "./common"
import { DOMWrapper } from "@vue/test-utils"

test("it sorts data", async () => {
const keys = ["name", "gender", "job"] as any
let c, copy, key: any, i
const keys = ["name", "gender", "job"]
let key: string
let copy: any[]
let i: number
let c: DOMWrapper<any>

for (i = 0; i < keys.length; i += 1) {
key = keys[i]
c = col(i + 1)
Expand All @@ -35,7 +40,7 @@ test("it sorts data", async () => {
})

test("it sorts only one column", async () => {
let arr
let arr: any[]

// sets the sorting mode
await wrapper.setProps({ sortingMode: "single" })
Expand Down Expand Up @@ -98,7 +103,7 @@ test("it sorts filtered data", async () => {

test("it sorts multiple rows", async () => {
// copy the data
let copy
let copy: any[]

// sort by second column, then by third column
await click(col(2))
Expand Down
2 changes: 1 addition & 1 deletion tests/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ test("test string replacement", function () {

test("test safe sort", function () {
let arr = [2, 45, null, 10, 20, null, 15]
let res
let res: (number | null)[]
let f: Function = (a: any, b: any) => a - b
let g: Function = (a: any, b: any) => b - a
res = arraySafeSort(arr, f)
Expand Down

0 comments on commit ed14e52

Please sign in to comment.