Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
3ca956e531 | |||
9350c837e5 |
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ztimson/utils",
|
||||
"version": "0.9.0",
|
||||
"version": "0.10.1",
|
||||
"description": "Utility library",
|
||||
"author": "Zak Timson",
|
||||
"license": "MIT",
|
||||
|
@ -1,15 +1,3 @@
|
||||
import {XHR} from './xhr';
|
||||
|
||||
XHR.addInterceptor((resp: Response, next: () => void) => {
|
||||
if(resp.status == 200) return next();
|
||||
if(resp.status == 400) throw new BadRequestError(resp.statusText);
|
||||
if(resp.status == 401) throw new UnauthorizedError(resp.statusText);
|
||||
if(resp.status == 403) throw new ForbiddenError(resp.statusText);
|
||||
if(resp.status == 404) throw new NotFoundError(resp.statusText);
|
||||
if(resp.status == 500) throw new InternalServerError(resp.statusText);
|
||||
throw new CustomError(resp.statusText, resp.status);
|
||||
});
|
||||
|
||||
export class CustomError extends Error {
|
||||
static code = 500;
|
||||
|
||||
|
@ -4,6 +4,7 @@ export * from './download';
|
||||
export * from './emitter';
|
||||
export * from './errors';
|
||||
export * from './logger';
|
||||
export * from './math';
|
||||
export * from './misc';
|
||||
export * from './objects';
|
||||
export * from './promise-progress';
|
||||
|
45
src/math.ts
Normal file
45
src/math.ts
Normal file
@ -0,0 +1,45 @@
|
||||
/**
|
||||
* Convert decimal number to fraction
|
||||
*
|
||||
* @example
|
||||
* ```js
|
||||
* dec2Frac(1.25) // Outputs: "1 1/4"
|
||||
* ```
|
||||
*
|
||||
* @param {number} num Number to convert
|
||||
* @return {string} Fraction with remainder
|
||||
*/
|
||||
export function dec2Frac(num: number) {
|
||||
const gcd = (a: number, b: number): number => {
|
||||
if (b < 0.0000001) return a;
|
||||
return gcd(b, ~~(a % b));
|
||||
};
|
||||
|
||||
const len = num.toString().length - 2;
|
||||
let denominator = Math.pow(10, len);
|
||||
let numerator = num * denominator;
|
||||
const divisor = gcd(numerator, denominator);
|
||||
numerator = ~~(numerator / divisor);
|
||||
denominator = ~~(denominator / divisor)
|
||||
const remainder = ~~(numerator / denominator);
|
||||
numerator -= remainder * denominator;
|
||||
return `${remainder ? remainder + ' ' : ''}${~~(numerator)}/${~~(denominator)}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert fraction to decimal number
|
||||
*
|
||||
* @example
|
||||
* ```js
|
||||
* fracToDec('1 1/4') // Outputs: 1.25
|
||||
* ```
|
||||
*
|
||||
* @param {string} frac Fraction to convert
|
||||
* @return {number} Faction as a decimal
|
||||
*/
|
||||
export function fracToDec(frac: string) {
|
||||
let split = frac.split(' ');
|
||||
const whole = split.length == 2 ? Number(split[0]) : 0;
|
||||
split = (<string>split.pop()).split('/');
|
||||
return whole + (Number(split[0]) / Number(split[1]));
|
||||
}
|
19
src/xhr.ts
19
src/xhr.ts
@ -1,3 +1,11 @@
|
||||
import {
|
||||
BadRequestError,
|
||||
CustomError,
|
||||
ForbiddenError,
|
||||
InternalServerError,
|
||||
NotFoundError,
|
||||
UnauthorizedError
|
||||
} from './errors.ts';
|
||||
import {clean} from './objects';
|
||||
|
||||
export type Interceptor = (request: Response, next: () => void) => void;
|
||||
@ -74,3 +82,14 @@ export class XHR {
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
XHR.addInterceptor((resp: Response, next: () => void) => {
|
||||
const getErr = (e: any) => e.error?.message || e.reason?.message || e.message || e.statusText || e.toString();
|
||||
if(resp.status == 200) return next();
|
||||
if(resp.status == 400) throw new BadRequestError(getErr(resp));
|
||||
if(resp.status == 401) throw new UnauthorizedError(getErr(resp));
|
||||
if(resp.status == 403) throw new ForbiddenError(getErr(resp));
|
||||
if(resp.status == 404) throw new NotFoundError(getErr(resp));
|
||||
if(resp.status == 500) throw new InternalServerError(getErr(resp));
|
||||
throw new CustomError(getErr(resp), resp.status);
|
||||
});
|
||||
|
Reference in New Issue
Block a user