Skip to content

Commit

Permalink
Clean unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Namide committed Oct 11, 2024
1 parent f931aa8 commit 5d49601
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 227 deletions.
300 changes: 150 additions & 150 deletions package-lock.json

Large diffs are not rendered by default.

16 changes: 9 additions & 7 deletions src/extract/cleanInputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ export const AVERAGE_HUE_DEFAULT = 1 / 12;
export const AVERAGE_SATURATION_DEFAULT = 1 / 5;
export const AVERAGE_LIGHTNESS_DEFAULT = 1 / 5;

export function cleanInputsWarnings({
export function testInputs({
pixels = EXTRACTOR_PIXELS_DEFAULT,
distance = EXTRACTOR_DISTANCE_DEFAULT,
colorValidator = (
_red: number,
_green: number,
_blue: number,
_alpha?: number,
_alpha?: number
) => (_alpha ?? 255) > 250,
hueDistance = AVERAGE_HUE_DEFAULT,
saturationDistance = AVERAGE_LIGHTNESS_DEFAULT,
Expand All @@ -35,7 +35,7 @@ export function cleanInputsWarnings({
label: string,
val: number,
min = 0,
max = Number.MAX_SAFE_INTEGER,
max = Number.MAX_SAFE_INTEGER
) => {
if (!Number.isInteger(val)) {
throw new Error(`${label} is not a valid number (${val})`);
Expand All @@ -59,7 +59,7 @@ export function cleanInputsWarnings({
label: string,
val: number,
min = 0,
max = Number.MAX_VALUE,
max = Number.MAX_VALUE
) => {
if (Number(val) !== val) {
throw new Error(`${label} is not a valid number (${val})`);
Expand All @@ -81,7 +81,7 @@ export function cleanInputsWarnings({
*/
const testFunction = <T = () => void>(label: string, val: T) => {
if (!val || {}.toString.call(val) !== "[object Function]") {
console.warn(`${label} is not a function (${val})`);
throw new Error(`${label} is not a function (${val})`);
}

return val;
Expand All @@ -93,7 +93,9 @@ export function cleanInputsWarnings({
const testValueInList = <T>(label: string, val: T, list: T[]) => {
if (list.indexOf(val) < 0) {
console.warn(
`${label} can be one of this values ${list.map((v) => `"${v}"`).join(", ")} (it's "${val}")`,
`${label} can be one of this values ${list
.map((v) => `"${v}"`)
.join(", ")} (it's "${val}")`
);
}
};
Expand Down Expand Up @@ -124,7 +126,7 @@ export default ({
_red: number,
_green: number,
_blue: number,
_alpha?: number,
_alpha?: number
) => (_alpha ?? 255) > 250,
hueDistance = AVERAGE_HUE_DEFAULT,
saturationDistance = AVERAGE_LIGHTNESS_DEFAULT,
Expand Down
55 changes: 26 additions & 29 deletions src/extractColors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import cleanInputs, { cleanInputsWarnings } from "./extract/cleanInputs";
import cleanInputs, { testInputs } from "./extract/cleanInputs";
import extractor from "./extract/extractor";
import {
checkIsBrowser,
Expand Down Expand Up @@ -28,10 +28,10 @@ import { BrowserOptions, ImageDataAlt, NodeOptions } from "./types/Options";
*/
export const extractColorsFromImageData = (
imageData: ImageData | ImageDataAlt,
options: NodeOptions | BrowserOptions = {},
options: NodeOptions | BrowserOptions = {}
) => {
if (__DEV__) {
cleanInputsWarnings(options);
testInputs(options);
}

const [
Expand All @@ -46,14 +46,14 @@ export const extractColorsFromImageData = (
imageData,
_pixels,
_distance,
_colorValidator,
_colorValidator
);
return sortFinalColors(
colors,
count,
_hueDistance,
_saturationDistance,
_lightnessDistance,
_lightnessDistance
);
};

Expand All @@ -78,20 +78,20 @@ export const extractColorsFromImageData = (
// @ts-ignore
export const extractColorsFromImage = async (
image: HTMLImageElement,
options: BrowserOptions = {},
options: BrowserOptions = {}
): Promise<FinalColor[]> => {
// Node.js version
if (checkIsNode()) {
if (__DEV__) {
throw new Error(
"Use extractColors instead extractColorsFromImage for Node.js",
"Use extractColors instead extractColorsFromImage for Node.js"
);
}
return [];
}

if (__DEV__) {
cleanInputsWarnings(options);
testInputs(options);
}

// Browser version
Expand All @@ -112,16 +112,16 @@ export const extractColorsFromImage = async (
imageData,
_pixels,
_distance,
_colorValidator,
_colorValidator
);
resolve(
sortFinalColors(
colors,
count,
_hueDistance,
_saturationDistance,
_lightnessDistance,
),
_lightnessDistance
)
);
};

Expand All @@ -139,20 +139,20 @@ export const extractColorsFromImage = async (

export const extractColorsFromImageBitmap = async (
image: ImageBitmap,
options: BrowserOptions = {},
options: BrowserOptions = {}
): Promise<FinalColor[]> => {
// Node.js version
if (checkIsNode()) {
if (__DEV__) {
throw new Error(
"Use extractColors instead extractColorsFromImageBitmap for Node.js",
"Use extractColors instead extractColorsFromImageBitmap for Node.js"
);
}
return [];
}

if (__DEV__) {
cleanInputsWarnings(options);
testInputs(options);
}

const [
Expand All @@ -169,15 +169,15 @@ export const extractColorsFromImageBitmap = async (
imageData,
_pixels,
_distance,
_colorValidator,
_colorValidator
);

return sortFinalColors(
colors,
count,
_hueDistance,
_saturationDistance,
_lightnessDistance,
_lightnessDistance
);
};

Expand All @@ -202,7 +202,7 @@ export const extractColorsFromImageBitmap = async (
// @ts-ignore
export const extractColorsFromSrc = async (
src: string,
options: BrowserOptions = {},
options: BrowserOptions = {}
): Promise<FinalColor[]> => {
// Node.js version
if (checkIsNode()) {
Expand All @@ -213,7 +213,7 @@ export const extractColorsFromSrc = async (
}

if (__DEV__) {
cleanInputsWarnings(options);
testInputs(options);
}

// Web Worker version
Expand Down Expand Up @@ -251,14 +251,14 @@ export const extractColorsFromSrc = async (
*/
export const extractColors = (
picture: string | HTMLImageElement | ImageData | ImageDataAlt,
options?: BrowserOptions,
options?: BrowserOptions
) => {
// Browser version
if (checkIsBrowser()) {
if (__DEV__) {
if (options?.requestMode) {
console.warn(
"options.requestMode not supported in Browser, use options.crossOrigin instead",
"options.requestMode not supported in Browser, use options.crossOrigin instead"
);
}
}
Expand Down Expand Up @@ -286,7 +286,7 @@ export const extractColors = (
if (__DEV__) {
if (options?.crossOrigin) {
console.warn(
"options.crossOrigin not supported in Web Worker, use options.requestMode instead",
"options.crossOrigin not supported in Web Worker, use options.requestMode instead"
);
}
}
Expand All @@ -299,8 +299,8 @@ export const extractColors = (
resolve(
extractColorsFromImageData(
picture as ImageData | ImageDataAlt,
options,
),
options
)
);
});
}
Expand All @@ -313,7 +313,7 @@ export const extractColors = (
if ((picture as HTMLImageElement).src) {
if (__DEV__) {
console.warn(
"HTMLImageElement not enable on worker, a fallback is used to extract src from your HTMLImageElement, please send 'src' instead HTMLImageElement",
"HTMLImageElement not enable on worker, a fallback is used to extract src from your HTMLImageElement, please send 'src' instead HTMLImageElement"
);
}
return extractColorsFromSrc((picture as HTMLImageElement).src, options);
Expand All @@ -325,7 +325,7 @@ export const extractColors = (
if (__DEV__) {
if (picture instanceof String) {
throw new Error(
"Send imageData to extractColors (Image src or HTMLImageElement not supported in Nodejs)",
"Send imageData to extractColors (Image src or HTMLImageElement not supported in Nodejs)"
);
}

Expand All @@ -340,10 +340,7 @@ export const extractColors = (

return new Promise((resolve: (value: FinalColor[]) => void) => {
resolve(
extractColorsFromImageData(
picture as ImageData | ImageDataAlt,
options,
),
extractColorsFromImageData(picture as ImageData | ImageDataAlt, options)
);
});
}
Expand Down
Loading

0 comments on commit 5d49601

Please sign in to comment.