Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
67b314b507 | |||
f5d66f0d8f | |||
18c4366866 |
4
package-lock.json
generated
4
package-lock.json
generated
@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/utils",
|
"name": "@ztimson/utils",
|
||||||
"version": "0.3.0",
|
"version": "0.4.0",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "@ztimson/utils",
|
"name": "@ztimson/utils",
|
||||||
"version": "0.3.0",
|
"version": "0.4.0",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/jest": "^29.5.12",
|
"@types/jest": "^29.5.12",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/utils",
|
"name": "@ztimson/utils",
|
||||||
"version": "0.3.0",
|
"version": "0.5.0",
|
||||||
"description": "Utility library",
|
"description": "Utility library",
|
||||||
"author": "Zak Timson",
|
"author": "Zak Timson",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
58
src/aset.ts
Normal file
58
src/aset.ts
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
export class ASet<T> extends Array {
|
||||||
|
get size() {
|
||||||
|
return this.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
constructor(elements: T[] = []) {
|
||||||
|
super();
|
||||||
|
if(!!elements?.['forEach'])
|
||||||
|
elements.forEach(el => this.add(el));
|
||||||
|
}
|
||||||
|
|
||||||
|
add(el: T) {
|
||||||
|
if(!this.has(el)) this.push(el);
|
||||||
|
}
|
||||||
|
|
||||||
|
delete(el: T) {
|
||||||
|
const index = this.indexOf(el);
|
||||||
|
if(index != -1) this.slice(index, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
difference(set: ASet<T>) {
|
||||||
|
return new ASet<T>(this.reduce((acc, el) => {
|
||||||
|
if(!set.has(el)) acc.push(el);
|
||||||
|
return acc;
|
||||||
|
}, []));
|
||||||
|
}
|
||||||
|
|
||||||
|
has(el: T) {
|
||||||
|
return this.indexOf(el) != -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
intersection(set: ASet<T>) {
|
||||||
|
return new ASet<T>(this.reduce((acc, el) => {
|
||||||
|
if(set.has(el)) acc.push(el);
|
||||||
|
return acc;
|
||||||
|
}, []));
|
||||||
|
}
|
||||||
|
|
||||||
|
isDisjointFrom(set: ASet<T>) {
|
||||||
|
return this.intersection(set).size == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
isSubsetOf(set: ASet<T>) {
|
||||||
|
return this.findIndex(el => !set.has(el)) == -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
isSuperset(set: ASet<T>) {
|
||||||
|
return set.findIndex(el => !this.has(el)) == -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
symmetricDifference(set: ASet<T>) {
|
||||||
|
return new ASet([...this.difference(set), ...set.difference(this)]);
|
||||||
|
}
|
||||||
|
|
||||||
|
union(set: ASet<T> | Array<T>) {
|
||||||
|
return new ASet([...this, ...set]);
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,5 @@
|
|||||||
export * from './array';
|
export * from './array';
|
||||||
|
export * from './aset';
|
||||||
export * from './emitter';
|
export * from './emitter';
|
||||||
export * from './errors';
|
export * from './errors';
|
||||||
export * from './logger';
|
export * from './logger';
|
||||||
|
22
src/xhr.ts
22
src/xhr.ts
@ -7,6 +7,7 @@ export type RequestOptions = {
|
|||||||
method?: 'GET' | 'POST' | 'PATCH' | 'DELETE';
|
method?: 'GET' | 'POST' | 'PATCH' | 'DELETE';
|
||||||
body?: any;
|
body?: any;
|
||||||
headers?: {[key: string | symbol]: string | null | undefined};
|
headers?: {[key: string | symbol]: string | null | undefined};
|
||||||
|
skipConverting?: boolean;
|
||||||
[key: string]: any;
|
[key: string]: any;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -44,6 +45,14 @@ export class XHR {
|
|||||||
return () => { this.interceptors[key] = <any>null; }
|
return () => { this.interceptors[key] = <any>null; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
download(opts: RequestOptions & {url: string}) {
|
||||||
|
this.request<Response>({...opts, skipConverting: true}).then(async resp => {
|
||||||
|
const blob = await resp.blob();
|
||||||
|
download(URL.createObjectURL(blob), <string>opts.url.split('/').pop());
|
||||||
|
URL.revokeObjectURL(opts.url);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
async request<T>(opts: RequestOptions = {}): Promise<T> {
|
async request<T>(opts: RequestOptions = {}): Promise<T> {
|
||||||
if(!this.opts.url && !opts.url) throw new Error('URL needs to be set');
|
if(!this.opts.url && !opts.url) throw new Error('URL needs to be set');
|
||||||
const url = (opts.url?.startsWith('http') ? opts.url : (this.opts.url || '') + (opts.url || '')).replace(/([^:]\/)\/+/g, '$1');
|
const url = (opts.url?.startsWith('http') ? opts.url : (this.opts.url || '') + (opts.url || '')).replace(/([^:]\/)\/+/g, '$1');
|
||||||
@ -67,9 +76,18 @@ export class XHR {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if(!resp.ok) throw new Error(resp.statusText);
|
if(!resp.ok) throw new Error(resp.statusText);
|
||||||
if(resp.headers.get('Content-Type')?.startsWith('application/json')) return await resp.json();
|
if(!opts.skipConverting && resp.headers.get('Content-Type')?.startsWith('application/json')) return await resp.json();
|
||||||
if(resp.headers.get('Content-Type')?.startsWith('text/plain')) return await <any>resp.text();
|
if(!opts.skipConverting && resp.headers.get('Content-Type')?.startsWith('text/plain')) return await <any>resp.text();
|
||||||
return resp;
|
return resp;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function download(href: any, name: string) {
|
||||||
|
const a = document.createElement('a');
|
||||||
|
a.href = href;
|
||||||
|
a.download = name;
|
||||||
|
document.body.appendChild(a);
|
||||||
|
a.click();
|
||||||
|
document.body.removeChild(a);
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user