Skip to content

Commit

Permalink
Update schemas-types
Browse files Browse the repository at this point in the history
  • Loading branch information
crisconru committed Jul 6, 2024
1 parent 543b5bf commit bb840bd
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/image.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as Path from 'node:path'
import Jimp from 'jimp'
import { SpliteaError, ThrowSpliteaError } from './errors'
import type { CropData, Image, Natural, Size, StoreOptions, UniqueImagesOptions, WriteOptions } from './types'
import type { CropData, ImageSource, Natural, Size, StoreOptions, UniqueImagesOptions, WriteOptions } from './types'

// @ts-expect-error
export const getImage = async (image: Image): Promise<Jimp> => {
export const getImage = async (image: ImageSource): Promise<Jimp> => {
try {
// @ts-expect-error
return await Jimp.read(image)
Expand Down
6 changes: 3 additions & 3 deletions src/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ export const NaturalSchema = ValibotValidator<UnsignedInteger>(ValibotNaturalSch

const ValibotBufferSchema = v.instance(Buffer)
// IMAGE --------------------------------------------------------------------------------------------------------------
const ValibotImageExistsSchema = v.pipe(
const ValibotFileExistsSchema = v.pipe(
v.string(),
v.check((file: string) => fs.existsSync(file), 'This image does not exists')
)

const ValibotImageSchema = v.union([ValibotImageExistsSchema, ValibotBufferSchema])
export const ImageSchema = ValibotValidator<v.InferInput<typeof ValibotImageSchema>>(ValibotImageSchema)
const ValibotImageSourceSchema = v.union([ValibotFileExistsSchema, ValibotBufferSchema])
export const ImageSourceSchema = ValibotValidator<v.InferInput<typeof ValibotImageSourceSchema>>(ValibotImageSourceSchema)
// OPTIONS ------------------------------------------------------------------------------------------------------------
const ValibotResponseSchema = v.picklist(['buffer', 'file'])
export const ResponseSchema = ValibotValidator<v.InferInput<typeof ValibotResponseSchema>>(ValibotResponseSchema)
Expand Down
16 changes: 8 additions & 8 deletions src/splitea.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { Difference, Distance, Filename, GridOptions, HorizontalOptions, Image, Output, StoreOptions, UniqueImagesOptions, UniqueRequirement, VerticalOptions } from './types'
import type { Difference, Distance, Filename, GridOptions, HorizontalOptions, ImageSource, Output, StoreOptions, UniqueImagesOptions, UniqueRequirement, VerticalOptions } from './types'
import { getBufferImages, getGridTiles, getHorizontalTiles, getImage, getSize, getUniqueGridTiles, getUniqueHorizontalTiles, getUniqueVerticalTiles, getVerticalTiles, writeImages } from './image'
import { GridOptionsSchema, HorizontalOptionsSchema, ImageSchema, VerticalOptionsSchema } from './schemas'
import { GridOptionsSchema, HorizontalOptionsSchema, ImageSourceSchema, VerticalOptionsSchema } from './schemas'
import { isSubmultiple } from './utils'
import { SpliteaError } from './errors'

export const horizontalTiles = async (image: Image, options: HorizontalOptions): Promise<Output[]> => {
export const horizontalTiles = async (image: ImageSource, options: HorizontalOptions): Promise<Output[]> => {
// 1. Check Image + Get the image + size
const img = await getImage(ImageSchema.parse(image))
const img = await getImage(ImageSourceSchema.parse(image))
const size = getSize(img)
// 2. Check Options
const opt = HorizontalOptionsSchema.parse(options)
Expand Down Expand Up @@ -40,9 +40,9 @@ export const horizontalTiles = async (image: Image, options: HorizontalOptions):
return await getBufferImages(tiles)
}

export const verticalTiles = async (image: Image, options: VerticalOptions): Promise<Output[]> => {
export const verticalTiles = async (image: ImageSource, options: VerticalOptions): Promise<Output[]> => {
// 1. Check Image + Get the image + size
const img = await getImage(ImageSchema.parse(image))
const img = await getImage(ImageSourceSchema.parse(image))
const size = getSize(img)
// 2. Check Options
const opt = VerticalOptionsSchema.parse(options)
Expand Down Expand Up @@ -76,9 +76,9 @@ export const verticalTiles = async (image: Image, options: VerticalOptions): Pro
return await getBufferImages(tiles)
}

export const gridTiles = async (image: Image, options: GridOptions): Promise<Output[]> => {
export const gridTiles = async (image: ImageSource, options: GridOptions): Promise<Output[]> => {
// 1. Check Image + Get the image + size
const img = await getImage(ImageSchema.parse(image))
const img = await getImage(ImageSourceSchema.parse(image))
const size = getSize(img)
// 2. Check Options
const opt = GridOptionsSchema.parse(options)
Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { DifferenceSchema, DistanceSchema, ExtensionSchema, FilenameSchema, GridOptionsSchema, HorizontalOptionsSchema, ImageSchema, NaturalSchema, OptionsSchema, PathSchema, ResponseSchema, UniqueRequirementSchema, VerticalOptionsSchema } from './schemas'
import { DifferenceSchema, DistanceSchema, ExtensionSchema, FilenameSchema, GridOptionsSchema, HorizontalOptionsSchema, ImageSourceSchema, NaturalSchema, OptionsSchema, PathSchema, ResponseSchema, UniqueRequirementSchema, VerticalOptionsSchema } from './schemas'

// PRIMITIVES -----------------------------------------------------------------
export type Natural = ReturnType<typeof NaturalSchema.parse>
// IMAGE ----------------------------------------------------------------------
export type Image = ReturnType<typeof ImageSchema.parse>
export type ImageSource = ReturnType<typeof ImageSourceSchema.parse>
export interface Size {
width: Natural
height: Natural
Expand Down
8 changes: 4 additions & 4 deletions tests/schemas.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from 'node:fs'
import path from 'node:path'
import { describe, test, expect } from 'vitest'
import type { GridOptions, HorizontalOptions, Options, VerticalOptions } from '../src/types'
import { ExtensionSchema, GridOptionsSchema, HorizontalOptionsSchema, ImageSchema, OptionsSchema, PathSchema, ResponseSchema, VerticalOptionsSchema } from '../src/schemas'
import { ExtensionSchema, GridOptionsSchema, HorizontalOptionsSchema, ImageSourceSchema, OptionsSchema, PathSchema, ResponseSchema, VerticalOptionsSchema } from '../src/schemas'
import { EXTENSIONS, MAX_DIFFERENCE, MAX_DISTANCE } from '../src'

const IMG_FOLDER = path.join(__dirname)
Expand Down Expand Up @@ -33,16 +33,16 @@ const bad = {
// ImageSchema ----------------------------------------------------------------
describe('ImageSchema', () => {
test('input non exists image throw an exception', () => {
expect(() => ImageSchema.parse(bad.file)).toThrow()
expect(() => ImageSourceSchema.parse(bad.file)).toThrow()
})

test('input image exists', async () => {
expect(ImageSchema.parse(satie.file)).toBeTypeOf('string')
expect(ImageSourceSchema.parse(satie.file)).toBeTypeOf('string')
})

test('input buffer', () => {
const buffer = fs.readFileSync(forest.file)
expect(ImageSchema.safeParse(buffer).success).toBeTruthy()
expect(ImageSourceSchema.safeParse(buffer).success).toBeTruthy()
})
})
// ResponseSchema -------------------------------------------------------------
Expand Down

0 comments on commit bb840bd

Please sign in to comment.