From bb840bd42dc27ff0ad10d8c231711104ed3cc5ff Mon Sep 17 00:00:00 2001 From: Cristobal Contreras Rubio Date: Sat, 6 Jul 2024 20:14:54 +0200 Subject: [PATCH] Update schemas-types --- src/image.ts | 4 ++-- src/schemas.ts | 6 +++--- src/splitea.ts | 16 ++++++++-------- src/types.ts | 4 ++-- tests/schemas.test.ts | 8 ++++---- 5 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/image.ts b/src/image.ts index afe9a2f..e18b38a 100644 --- a/src/image.ts +++ b/src/image.ts @@ -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 => { +export const getImage = async (image: ImageSource): Promise => { try { // @ts-expect-error return await Jimp.read(image) diff --git a/src/schemas.ts b/src/schemas.ts index ddc3027..9951892 100644 --- a/src/schemas.ts +++ b/src/schemas.ts @@ -12,13 +12,13 @@ export const NaturalSchema = ValibotValidator(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>(ValibotImageSchema) +const ValibotImageSourceSchema = v.union([ValibotFileExistsSchema, ValibotBufferSchema]) +export const ImageSourceSchema = ValibotValidator>(ValibotImageSourceSchema) // OPTIONS ------------------------------------------------------------------------------------------------------------ const ValibotResponseSchema = v.picklist(['buffer', 'file']) export const ResponseSchema = ValibotValidator>(ValibotResponseSchema) diff --git a/src/splitea.ts b/src/splitea.ts index 78f1953..5765748 100755 --- a/src/splitea.ts +++ b/src/splitea.ts @@ -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 => { +export const horizontalTiles = async (image: ImageSource, options: HorizontalOptions): Promise => { // 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) @@ -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 => { +export const verticalTiles = async (image: ImageSource, options: VerticalOptions): Promise => { // 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) @@ -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 => { +export const gridTiles = async (image: ImageSource, options: GridOptions): Promise => { // 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) diff --git a/src/types.ts b/src/types.ts index 5b865b5..5304d79 100755 --- a/src/types.ts +++ b/src/types.ts @@ -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 // IMAGE ---------------------------------------------------------------------- -export type Image = ReturnType +export type ImageSource = ReturnType export interface Size { width: Natural height: Natural diff --git a/tests/schemas.test.ts b/tests/schemas.test.ts index 9e78f64..3553ec1 100644 --- a/tests/schemas.test.ts +++ b/tests/schemas.test.ts @@ -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) @@ -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 -------------------------------------------------------------