Skip to content

Commit

Permalink
🔥 Delete toNumericRange
Browse files Browse the repository at this point in the history
  • Loading branch information
NatoBoram committed May 4, 2024
1 parent 5d99daa commit cebaf63
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 37 deletions.
24 changes: 1 addition & 23 deletions src/lib/utils/adapters.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, test } from 'vitest'
import { isLessThan, isNumericRange, toLessThan, toNumericRange } from './adapters.js'
import { isLessThan, isNumericRange, toLessThan } from './adapters.js'

describe.concurrent('isLessThan', () => {
test('returns `true` if input is less than max', ({ expect }) => {
Expand Down Expand Up @@ -56,25 +56,3 @@ describe.concurrent('isNumericRange', () => {
expect(result).toBe(true)
})
})

describe.concurrent('toNumericRange', () => {
test('returns min if input is less than min', ({ expect }) => {
const result = toNumericRange(0, 1, 3)
expect(result).toBe(1)
})

test('returns input if input is within range', ({ expect }) => {
const result = toNumericRange(2, 1, 3)
expect(result).toBe(2)
})

test('returns max if input is greater than max', ({ expect }) => {
const result = toNumericRange(4, 1, 3)
expect(result).toBe(3)
})

test('returns input if input is equal to min and max', ({ expect }) => {
const result = toNumericRange(1, 1, 1)
expect(result).toBe(1)
})
})
12 changes: 0 additions & 12 deletions src/lib/utils/adapters.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable @typescript-eslint/no-unsafe-return */
import type { LessThan, NumericRange } from '@sveltejs/kit'

// @ts-expect-error Type instantiation is excessively deep and possibly infinite.
export function isLessThan<Max extends number>(input: number, max: Max): input is LessThan<Max> {
return input < max
}
Expand All @@ -15,17 +14,6 @@ export function isNumericRange<Min extends number, Max extends number>(
}

export function toLessThan<Max extends number>(input: number, max: Max): LessThan<Max> {
// @ts-expect-error Type instantiation is excessively deep and possibly infinite.
if (isLessThan(input, max)) return input
return (max - 1) as LessThan<Max>
}

export function toNumericRange<Min extends number, Max extends number>(
input: number,
min: Min,
max: Max,
): NumericRange<Min, Max> {
if (isLessThan(input, min)) return min as NumericRange<Min, Max>
if (isLessThan(input, max)) return input
return max as NumericRange<Min, Max>
}
3 changes: 1 addition & 2 deletions src/routes/[site]/post/[post]/+page.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { error, type HttpError } from '@sveltejs/kit'
import { LemmyHttp } from 'lemmy-js-client'
import { toNumericRange } from '$lib/utils/adapters.js'
import { formGetComments, headers, serverFetch } from '$lib/utils/index.js'
import type { PageLoad } from './$types.js'

Expand Down Expand Up @@ -29,7 +28,7 @@ export const load = (async ({ params, fetch, url, parent, depends }) => {

default:
console.error(e)
throw error(toNumericRange(e.status, 400, 599), 'Failed to load post.')
throw error(e.status, 'Failed to load post.')
}
}),
client.getComments(formComment).catch((e: Response) => {
Expand Down

0 comments on commit cebaf63

Please sign in to comment.