Skip to content

Commit

Permalink
add percent aligment option
Browse files Browse the repository at this point in the history
  • Loading branch information
nikrowell committed Feb 27, 2020
1 parent 4ff4759 commit 6337355
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/__tests__/alignment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,10 @@ describe('Alignment', () => {
const measure = endAlign.measure(itemSize)
expect(measure).toBe(900)
})

test('Measures percent alignment for given number correctly', () => {
const percentAlign = Alignment({ align: 0.3, viewSize })
const measure = percentAlign.measure(itemSize)
expect(measure).toBe(300)
})
})
11 changes: 8 additions & 3 deletions src/components/alignment.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type Alignments = 'start' | 'center' | 'end'
export type Alignments = 'start' | 'center' | 'end' | number

type Params = {
viewSize: number
Expand All @@ -11,7 +11,7 @@ export type Alignment = {

export function Alignment(params: Params): Alignment {
const { viewSize, align } = params
const alignment = { start, center, end }
const alignment = { start, center, end, percent }

function start(n: number): number {
return n * 0
Expand All @@ -25,8 +25,13 @@ export function Alignment(params: Params): Alignment {
return viewSize - n
}

function percent(n: number): number {
return viewSize * Number(align)
}

function measure(n: number): number {
return alignment[align](n)
const method = typeof align === 'number' ? 'percent' : align
return alignment[method](n)
}

const self: Alignment = {
Expand Down

0 comments on commit 6337355

Please sign in to comment.