Skip to content

Commit

Permalink
fix: rename things
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Sep 23, 2024
1 parent 181ab4b commit d32e889
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 31 deletions.
4 changes: 2 additions & 2 deletions examples/basic.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import ansis from 'ansis'
import terminalLink from 'terminal-link'

import {TableProps, printTable} from '../src/index.js'
import {TableOptions, printTable} from '../src/index.js'

const data = [
{
Expand All @@ -25,7 +25,7 @@ const data = [
},
]

const basic: TableProps<(typeof data)[number]> = {
const basic: TableOptions<(typeof data)[number]> = {
borderStyle: 'all',
columns: ['id', {key: 'name', name: 'First Name'}, 'age', 'employed'],
data,
Expand Down
8 changes: 4 additions & 4 deletions examples/multiple.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {TableProps, printTables} from '../src/index.js'
import {TableOptions, printTables} from '../src/index.js'

const description =
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'
Expand Down Expand Up @@ -63,7 +63,7 @@ const projects = [
},
]

const employeesTable: TableProps<(typeof employees)[number]> = {
const employeesTable: TableOptions<(typeof employees)[number]> = {
columns: ['id', 'name', 'age', 'description'],
data: employees,
headerOptions: {
Expand All @@ -79,7 +79,7 @@ const employeesTable: TableProps<(typeof employees)[number]> = {
},
}

const stocksTable: TableProps<(typeof stocks)[number]> = {
const stocksTable: TableOptions<(typeof stocks)[number]> = {
columns: ['ticker', 'name', 'price'],
data: stocks,
headerOptions: {
Expand All @@ -95,7 +95,7 @@ const stocksTable: TableProps<(typeof stocks)[number]> = {
},
}

const projectsTable: TableProps<(typeof projects)[number]> = {
const projectsTable: TableOptions<(typeof projects)[number]> = {
columns: ['id', 'name', 'status', 'description'],
data: projects,
headerOptions: {
Expand Down
4 changes: 2 additions & 2 deletions examples/sf-specific/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {TableProps, printTable} from '../../src'
import {TableOptions, printTable} from '../../src'

const deployResult = [
{
Expand Down Expand Up @@ -940,7 +940,7 @@ const deployResult = [
},
]

const deploy: TableProps<(typeof deployResult)[number]> = {
const deploy: TableOptions<(typeof deployResult)[number]> = {
borderStyle: 'vertical',
columns: [
'state',
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export {printTable, printTables} from './table.js'
export type {ScalarDict, TableProps} from './types.js'
export type {TableOptions} from './types.js'
15 changes: 7 additions & 8 deletions src/table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ import {
Percentage,
RowConfig,
RowProps,
ScalarDict,
TableProps,
TableOptions,
} from './types.js'
import {allKeysInCollection, getColumns, getHeadings, intersperse, maybeStripAnsi, sortData} from './utils.js'

Expand Down Expand Up @@ -62,7 +61,7 @@ function determineWidthToUse<T>(columns: Column<T>[], configuredWidth: number):
return tableWidth < configuredWidth ? configuredWidth : tableWidth
}

export function Table<T extends ScalarDict>(props: TableProps<T>) {
export function Table<T extends Record<string, unknown>>(props: TableOptions<T>) {
const {
data,
filter,
Expand Down Expand Up @@ -211,7 +210,7 @@ export function Table<T extends ScalarDict>(props: TableProps<T>) {
/**
* Constructs a Row element from the configuration.
*/
function row<T extends ScalarDict>(config: RowConfig): (props: RowProps<T>) => React.ReactNode {
function row<T extends Record<string, unknown>>(config: RowConfig): (props: RowProps<T>) => React.ReactNode {
// This is a component builder. We return a function.
const {borderProps, skeleton} = config

Expand Down Expand Up @@ -332,9 +331,9 @@ export function Skeleton(props: React.PropsWithChildren & {readonly height?: num

/**
* Renders a table with the given data.
* @param options see {@link TableProps}
* @param options see {@link TableOptions}
*/
export function printTable<T extends ScalarDict>(options: TableProps<T>): void {
export function printTable<T extends Record<string, unknown>>(options: TableOptions<T>): void {
const instance = render(<Table {...options} />)
instance.unmount()
}
Expand All @@ -347,8 +346,8 @@ function Container(props: ContainerProps) {
)
}

export function printTables<T extends ScalarDict[]>(
tables: {[P in keyof T]: TableProps<T[P]>},
export function printTables<T extends Record<string, unknown>[]>(
tables: {[P in keyof T]: TableOptions<T[P]>},
options?: Omit<ContainerProps, 'children'>,
): void {
const leftMargin = options?.marginLeft ?? options?.margin ?? 0
Expand Down
10 changes: 2 additions & 8 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import {BorderStyle} from './skeletons.js'

export type Scalar = string | number | boolean | null | undefined

export type ScalarDict = {
[key: string]: Scalar
}

export type CellProps = React.PropsWithChildren<{readonly column: number}>

export type HorizontalAlignment = 'left' | 'right' | 'center'
Expand Down Expand Up @@ -99,7 +93,7 @@ export type Sort<T> = {
[K in keyof T]?: SortOrder<T[K]>
}

export type TableProps<T extends ScalarDict> = {
export type TableOptions<T extends Record<string, unknown>> = {
/**
* List of values (rows).
*/
Expand Down Expand Up @@ -250,7 +244,7 @@ export type RowConfig = {
}
}

export type RowProps<T extends ScalarDict> = {
export type RowProps<T extends Record<string, unknown>> = {
readonly key: string
readonly data: Partial<T>
readonly columns: Column<T>[]
Expand Down
12 changes: 6 additions & 6 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {camelCase, capitalCase, constantCase, kebabCase, pascalCase, sentenceCas
import {orderBy} from 'natural-orderby'
import stripAnsi from 'strip-ansi'

import {Column, ColumnProps, Config, ScalarDict, Sort} from './types.js'
import {Column, ColumnProps, Config, Sort} from './types.js'

/**
* Intersperses a list of elements with another element.
Expand All @@ -27,14 +27,14 @@ export function intersperse<T, I>(intersperser: (index: number) => I, elements:
return interspersed
}

export function sortData<T extends ScalarDict>(data: T[], sort?: Sort<T> | undefined): T[] {
export function sortData<T extends Record<string, unknown>>(data: T[], sort?: Sort<T> | undefined): T[] {
if (!sort) return data
const identifiers = Object.keys(sort)
const orders = Object.values(sort)
return orderBy(data, identifiers, orders)
}

export function allKeysInCollection<T extends ScalarDict>(data: T[]): (keyof T)[] {
export function allKeysInCollection<T extends Record<string, unknown>>(data: T[]): (keyof T)[] {
const keys = new Set<keyof T>()
for (const row of data) {
for (const key in row) {
Expand All @@ -45,7 +45,7 @@ export function allKeysInCollection<T extends ScalarDict>(data: T[]): (keyof T)[
return [...keys]
}

export function getColumns<T extends ScalarDict>(config: Config<T>, headings: Partial<T>): Column<T>[] {
export function getColumns<T extends Record<string, unknown>>(config: Config<T>, headings: Partial<T>): Column<T>[] {
const {columns, horizontalAlignment, maxWidth, overflow, verticalAlignment} = config

const widths: Column<T>[] = columns.map((propsOrKey) => {
Expand Down Expand Up @@ -101,7 +101,7 @@ export function getColumns<T extends ScalarDict>(config: Config<T>, headings: Pa
return widths
}

export function getHeadings<T extends ScalarDict>(config: Config<T>): Partial<T> {
export function getHeadings<T extends Record<string, unknown>>(config: Config<T>): Partial<T> {
const {
columns,
headerOptions: {formatter},
Expand Down Expand Up @@ -156,7 +156,7 @@ export function getHeadings<T extends ScalarDict>(config: Config<T>): Partial<T>
) as Partial<T>
}

export function maybeStripAnsi<T extends ScalarDict[]>(data: T, noStyle: boolean): T {
export function maybeStripAnsi<T extends Record<string, unknown>[]>(data: T, noStyle: boolean): T {
if (!noStyle) return data

const newData = []
Expand Down

0 comments on commit d32e889

Please sign in to comment.