Compare commits
1 Commits
master
...
improve-ts
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0f78b384e0 |
@ -1,5 +1,6 @@
|
||||
import { decode83 } from "./base83";
|
||||
import { sRGBToLinear, signPow, linearTosRGB } from "./utils";
|
||||
import { ValidationError } from "./error";
|
||||
|
||||
const decodeDC = (value: number) => {
|
||||
const intR = value >> 16;
|
||||
@ -31,8 +32,9 @@ const decode = (
|
||||
punch = punch | 1;
|
||||
|
||||
if (blurhash.length < 6) {
|
||||
console.error("too short blurhash");
|
||||
return null;
|
||||
throw new ValidationError(
|
||||
"The blurhash string must be at least 6 characters"
|
||||
);
|
||||
}
|
||||
|
||||
const sizeFlag = decode83(blurhash[0]);
|
||||
@ -43,11 +45,12 @@ const decode = (
|
||||
const maximumValue = (quantisedMaximumValue + 1) / 166;
|
||||
|
||||
if (blurhash.length !== 4 + 2 * numX * numY) {
|
||||
console.error(
|
||||
"blurhash length mismatch",
|
||||
blurhash.length,
|
||||
4 + 2 * numX * numY
|
||||
throw new ValidationError(
|
||||
`blurhash length mismatch: length is ${
|
||||
blurhash.length
|
||||
} but it should be ${4 + 2 * numX * numY}`
|
||||
);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
import { encode83 } from "./base83";
|
||||
import { sRGBToLinear, signPow, linearTosRGB } from "./utils";
|
||||
import { ValidationError } from "./error";
|
||||
|
||||
type NumberTriplet = [number, number, number];
|
||||
|
||||
@ -71,10 +72,10 @@ const encode = (
|
||||
componentY: number
|
||||
): string => {
|
||||
if (componentX < 1 || componentX > 9 || componentY < 1 || componentY > 9) {
|
||||
throw new Error("BlurHash must have between 1 and 9 components");
|
||||
throw new ValidationError("BlurHash must have between 1 and 9 components");
|
||||
}
|
||||
if (width * height * 4 !== pixels.length) {
|
||||
throw new Error("Width and height must match the pixels array");
|
||||
throw new ValidationError("Width and height must match the pixels array");
|
||||
}
|
||||
|
||||
let factors: Array<[number, number, number]> = [];
|
||||
|
||||
7
TypeScript/src/error.ts
Normal file
7
TypeScript/src/error.ts
Normal file
@ -0,0 +1,7 @@
|
||||
export class ValidationError extends Error {
|
||||
constructor(message: string) {
|
||||
super(message);
|
||||
this.name = "ValidationError";
|
||||
this.message = message;
|
||||
}
|
||||
}
|
||||
@ -1,2 +1,3 @@
|
||||
export { default as decode } from "./decode";
|
||||
export { default as encode } from "./encode";
|
||||
export * from "./error";
|
||||
|
||||
Loading…
Reference in New Issue
Block a user