@armniko/assets
Advanced tools
| var s,t={d:(s,e)=>{for(var o in e)t.o(e,o)&&!t.o(s,o)&&Object.defineProperty(s,o,{enumerable:!0,get:e[o]})},o:(s,t)=>Object.prototype.hasOwnProperty.call(s,t)},e={};t.d(e,{sP:()=>h,Gq:()=>d,ke:()=>a});class o{}class r extends o{load(s,t,e){const o=new Image;o.crossOrigin="Anonymous",o.onload=()=>{t(o)},o.onerror=s=>{"string"==typeof s&&e(new Error(s))},o.src=s}}class a{_total;_loaded;_failed;constructor(s,t,e){this._total=s,this._loaded=t,this._failed=e}total(){return this._total}loaded(){return this._loaded}failed(){return this._failed}isCompleted(){return this.loaded()+this.failed()>=this.total()}percents(){return Math.floor(100/this.total()*(this.loaded()+this.failed()))}}!function(s){s.Image="image"}(s||(s={}));class i{_url;constructor(s){this._url=s}url(){return this._url}assetName(){return this.fileName().split(".")[0]}type(){let t;switch(this.fileName().split(".").pop()){case"jpg":case"jpeg":case"png":case"gif":case"svg":t=s.Image;break;default:t=void 0}return t}fileName(){return this._url.split("?")[0].split("/").filter((s=>""!==s)).pop()||""}}class l{_total=0;_loaded=0;_failed=0;_urls=[];_onLoad;_onComplete;_onError;constructor(s,t,e,o){this._urls=s,this._total=this._urls.length,this._onLoad=t,this._onComplete=e,this._onError=o}loadAsset(t,e,o){const a=new i(t);a.type()===s.Image?(new r).load(a.url(),(t=>{e({name:a.assetName(),url:a.url(),asset:t,type:s.Image})}),(s=>o(a.url(),s))):o(a.url(),new Error("Unknown asset type"))}progress(){return new a(this._total,this._loaded,this._failed)}}class n extends l{fetch(){this._urls.forEach((s=>{this.loadAsset(s,this.onLoadAssetSuccessCallback.bind(this),this.onLoadAssetErrorCallback.bind(this))}))}onLoadAssetSuccessCallback(s){this._loaded++;const t=this.progress();this._onLoad(s,t),this.checkAndCompleteBatch(t)}onLoadAssetErrorCallback(s,t){this._failed++;const e=this.progress();this._onError(s,t),this.checkAndCompleteBatch(e)}checkAndCompleteBatch(s){s.isCompleted()&&this._onComplete(s)}}class h{_store={[s.Image]:{}};image(t){return this._store[s.Image][t]?.asset}fetch(s){new n(s.assets(),((t,e)=>{this._store[t.type][t.name]=t,s.loaded(t.name,e)}),(t=>{s.completed(t)}),((t,e)=>{s.error(t,e)})).fetch()}}class d{options;constructor(s){this.options=s,s.assets=this.uniqueAssets(s.assets)}assets(){return this.options.assets}loaded(s,t){this.options.onLoad&&this.options.onLoad(s,t)}completed(s){this.options.onComplete&&this.options.onComplete(s)}error(s,t){this.options.onError&&this.options.onError(s,t)}uniqueAssets(s){return s.filter(((s,t,e)=>t===e.indexOf(s)))}}var c=e.sP,p=e.Gq,u=e.ke;export{c as Assets,p as AssetsBatch,u as Progress}; |
| !function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define("Assets",[],e):"object"==typeof exports?exports.Assets=e():t.Assets=e()}(self,(()=>(()=>{"use strict";var t,e={d:(t,s)=>{for(var o in s)e.o(s,o)&&!e.o(t,o)&&Object.defineProperty(t,o,{enumerable:!0,get:s[o]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e),r:t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})}},s={};e.r(s),e.d(s,{Assets:()=>d,AssetsBatch:()=>h,Progress:()=>a});class o{}class r extends o{load(t,e,s){const o=new Image;o.crossOrigin="Anonymous",o.onload=()=>{e(o)},o.onerror=t=>{"string"==typeof t&&s(new Error(t))},o.src=t}}class a{_total;_loaded;_failed;constructor(t,e,s){this._total=t,this._loaded=e,this._failed=s}total(){return this._total}loaded(){return this._loaded}failed(){return this._failed}isCompleted(){return this.loaded()+this.failed()>=this.total()}percents(){return Math.floor(100/this.total()*(this.loaded()+this.failed()))}}!function(t){t.Image="image"}(t||(t={}));class i{_url;constructor(t){this._url=t}url(){return this._url}assetName(){return this.fileName().split(".")[0]}type(){let e;switch(this.fileName().split(".").pop()){case"jpg":case"jpeg":case"png":case"gif":case"svg":e=t.Image;break;default:e=void 0}return e}fileName(){return this._url.split("?")[0].split("/").filter((t=>""!==t)).pop()||""}}class l{_total=0;_loaded=0;_failed=0;_urls=[];_onLoad;_onComplete;_onError;constructor(t,e,s,o){this._urls=t,this._total=this._urls.length,this._onLoad=e,this._onComplete=s,this._onError=o}loadAsset(e,s,o){const a=new i(e);a.type()===t.Image?(new r).load(a.url(),(e=>{s({name:a.assetName(),url:a.url(),asset:e,type:t.Image})}),(t=>o(a.url(),t))):o(a.url(),new Error("Unknown asset type"))}progress(){return new a(this._total,this._loaded,this._failed)}}class n extends l{fetch(){this._urls.forEach((t=>{this.loadAsset(t,this.onLoadAssetSuccessCallback.bind(this),this.onLoadAssetErrorCallback.bind(this))}))}onLoadAssetSuccessCallback(t){this._loaded++;const e=this.progress();this._onLoad(t,e),this.checkAndCompleteBatch(e)}onLoadAssetErrorCallback(t,e){this._failed++;const s=this.progress();this._onError(t,e),this.checkAndCompleteBatch(s)}checkAndCompleteBatch(t){t.isCompleted()&&this._onComplete(t)}}class d{_store={[t.Image]:{}};image(e){return this._store[t.Image][e]?.asset}fetch(t){new n(t.assets(),((e,s)=>{this._store[e.type][e.name]=e,t.loaded(e.name,s)}),(e=>{t.completed(e)}),((e,s)=>{t.error(e,s)})).fetch()}}class h{options;constructor(t){this.options=t,t.assets=this.uniqueAssets(t.assets)}assets(){return this.options.assets}loaded(t,e){this.options.onLoad&&this.options.onLoad(t,e)}completed(t){this.options.onComplete&&this.options.onComplete(t)}error(t,e){this.options.onError&&this.options.onError(t,e)}uniqueAssets(t){return t.filter(((t,e,s)=>e===s.indexOf(t)))}}return s})())); |
+13
-8
| { | ||
| "name": "@armniko/assets", | ||
| "version": "1.0.0", | ||
| "version": "1.1.0", | ||
| "description": "Javascript/typescript library for fetching, storing and retrieving assets!", | ||
@@ -18,4 +18,6 @@ "author": "Armīns Nikolajevs <armins.nikolajevs@gmail.com>", | ||
| ], | ||
| "main": "dist/index.js", | ||
| "types": "dist/index.d.ts", | ||
| "main": "./dist/index.umd.js", | ||
| "module": "./dist/index.esm.js", | ||
| "typings": "./dist/index.d.ts", | ||
| "type": "module", | ||
| "files": [ | ||
@@ -25,7 +27,7 @@ "dist" | ||
| "scripts": { | ||
| "build": "tsc --project tsconfig.build.json", | ||
| "dev": "node test/server/main.js & tsc --watch", | ||
| "lint": "eslint src --max-warnings=0", | ||
| "build": "rimraf dist && webpack", | ||
| "dev": "node test/server/main.js & webpack --watch", | ||
| "lint": "tsc --noEmit && eslint src --max-warnings=0", | ||
| "lint:fix": "npm run lint -- --fix", | ||
| "test": "jest", | ||
| "test": "npm run build && jest", | ||
| "test:coverage": "npm run test -- --collect-coverage" | ||
@@ -46,4 +48,7 @@ }, | ||
| "ts-jest": "^29.1.2", | ||
| "typescript": "^5.4.3" | ||
| "ts-loader": "^9.5.1", | ||
| "typescript": "^5.4.3", | ||
| "webpack": "^5.91.0", | ||
| "webpack-cli": "^5.1.4" | ||
| } | ||
| } |
+17
-0
@@ -43,1 +43,18 @@ <h1 align="center">Assets</h1> | ||
| ``` | ||
| ## Changelog | ||
| <table> | ||
| <tr> | ||
| <td>v1.1.0</td> | ||
| <td> | ||
| Precompile UMD and ESM<br> | ||
| </td> | ||
| </tr> | ||
| <tr> | ||
| <td>v1.0.0</td> | ||
| <td> | ||
| Initial version | ||
| </td> | ||
| </tr> | ||
| </table> |
| export var AssetType; | ||
| (function (AssetType) { | ||
| AssetType["Image"] = "image"; | ||
| })(AssetType || (AssetType = {})); |
| /* eslint-disable @typescript-eslint/no-unused-vars */ | ||
| export * from './libraries/assets'; | ||
| export * from './libraries/assets-batch'; | ||
| export * from './libraries/progress'; |
| export {}; |
| import { AssetType } from '../enums/asset-type'; | ||
| export class AssetUrl { | ||
| _url; | ||
| constructor(url) { | ||
| this._url = url; | ||
| } | ||
| url() { | ||
| return this._url; | ||
| } | ||
| assetName() { | ||
| return this.fileName().split('.')[0]; | ||
| } | ||
| type() { | ||
| const extension = this.fileName().split('.').pop(); | ||
| let result; | ||
| switch (extension) { | ||
| case 'jpg': | ||
| case 'jpeg': | ||
| case 'png': | ||
| case 'gif': | ||
| case 'svg': | ||
| result = AssetType.Image; | ||
| break; | ||
| default: | ||
| result = undefined; | ||
| break; | ||
| } | ||
| return result; | ||
| } | ||
| fileName() { | ||
| return (this._url | ||
| .split('?')[0] | ||
| .split('/') | ||
| .filter((segment) => segment !== '') | ||
| .pop() || ''); | ||
| } | ||
| } |
| export class AssetsBatch { | ||
| options; | ||
| constructor(options) { | ||
| this.options = options; | ||
| options.assets = this.uniqueAssets(options.assets); | ||
| } | ||
| assets() { | ||
| return this.options.assets; | ||
| } | ||
| loaded(name, progress) { | ||
| if (this.options.onLoad) { | ||
| this.options.onLoad(name, progress); | ||
| } | ||
| } | ||
| completed(progress) { | ||
| if (this.options.onComplete) { | ||
| this.options.onComplete(progress); | ||
| } | ||
| } | ||
| error(url, error) { | ||
| if (this.options.onError) { | ||
| this.options.onError(url, error); | ||
| } | ||
| } | ||
| uniqueAssets(array) { | ||
| return array.filter((item, index, self) => index === self.indexOf(item)); | ||
| } | ||
| } |
| import { ConcurrentFetcher } from './fetchers/concurrent-fetcher'; | ||
| import { AssetType } from '../enums/asset-type'; | ||
| export class Assets { | ||
| _store = { | ||
| [AssetType.Image]: {}, | ||
| }; | ||
| image(name) { | ||
| return this._store[AssetType.Image][name]?.asset; | ||
| } | ||
| fetch(batch) { | ||
| new ConcurrentFetcher(batch.assets(), (assetRecord, progress) => { | ||
| this._store[assetRecord.type][assetRecord.name] = assetRecord; | ||
| batch.loaded(assetRecord.name, progress); | ||
| }, (progress) => { | ||
| batch.completed(progress); | ||
| }, (url, error) => { | ||
| batch.error(url, error); | ||
| }).fetch(); | ||
| } | ||
| } |
| import { Fetcher } from './fetcher'; | ||
| export class ConcurrentFetcher extends Fetcher { | ||
| fetch() { | ||
| this._urls.forEach((url) => { | ||
| this.loadAsset(url, this.onLoadAssetSuccessCallback.bind(this), this.onLoadAssetErrorCallback.bind(this)); | ||
| }); | ||
| } | ||
| onLoadAssetSuccessCallback(assetRecord) { | ||
| this._loaded++; | ||
| const progress = this.progress(); | ||
| this._onLoad(assetRecord, progress); | ||
| this.checkAndCompleteBatch(progress); | ||
| } | ||
| onLoadAssetErrorCallback(url, error) { | ||
| this._failed++; | ||
| const progress = this.progress(); | ||
| this._onError(url, error); | ||
| this.checkAndCompleteBatch(progress); | ||
| } | ||
| checkAndCompleteBatch(progress) { | ||
| if (progress.isCompleted()) { | ||
| this._onComplete(progress); | ||
| } | ||
| } | ||
| } |
| import { ImageLoader } from '../loaders/image-loader'; | ||
| import { Progress } from '../progress'; | ||
| import { AssetUrl } from '../asset-url'; | ||
| import { AssetType } from '../../enums/asset-type'; | ||
| export class Fetcher { | ||
| _total = 0; | ||
| _loaded = 0; | ||
| _failed = 0; | ||
| _urls = []; | ||
| _onLoad; | ||
| _onComplete; | ||
| _onError; | ||
| constructor(urls, onLoad, onComplete, onError) { | ||
| this._urls = urls; | ||
| this._total = this._urls.length; | ||
| this._onLoad = onLoad; | ||
| this._onComplete = onComplete; | ||
| this._onError = onError; | ||
| } | ||
| loadAsset(url, onSuccess, onError) { | ||
| const assetUrl = new AssetUrl(url); | ||
| switch (assetUrl.type()) { | ||
| case AssetType.Image: | ||
| new ImageLoader().load(assetUrl.url(), (image) => { | ||
| onSuccess({ | ||
| name: assetUrl.assetName(), | ||
| url: assetUrl.url(), | ||
| asset: image, | ||
| type: AssetType.Image, | ||
| }); | ||
| }, (error) => onError(assetUrl.url(), error)); | ||
| break; | ||
| default: | ||
| onError(assetUrl.url(), new Error('Unknown asset type')); | ||
| break; | ||
| } | ||
| } | ||
| progress() { | ||
| return new Progress(this._total, this._loaded, this._failed); | ||
| } | ||
| } |
| import { Loader } from './loader'; | ||
| export class ImageLoader extends Loader { | ||
| load(url, onSuccess, onError) { | ||
| const image = new Image(); | ||
| image.crossOrigin = 'Anonymous'; | ||
| image.onload = () => { | ||
| onSuccess(image); | ||
| }; | ||
| image.onerror = (error) => { | ||
| if (typeof error === 'string') { | ||
| onError(new Error(error)); | ||
| } | ||
| }; | ||
| image.src = url; | ||
| } | ||
| } |
| export class Loader { | ||
| } |
| export class Progress { | ||
| _total; | ||
| _loaded; | ||
| _failed; | ||
| constructor(total, loaded, failed) { | ||
| this._total = total; | ||
| this._loaded = loaded; | ||
| this._failed = failed; | ||
| } | ||
| total() { | ||
| return this._total; | ||
| } | ||
| loaded() { | ||
| return this._loaded; | ||
| } | ||
| failed() { | ||
| return this._failed; | ||
| } | ||
| isCompleted() { | ||
| return this.loaded() + this.failed() >= this.total(); | ||
| } | ||
| percents() { | ||
| return Math.floor((100 / this.total()) * (this.loaded() + this.failed())); | ||
| } | ||
| } |
| export {}; |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
12972
2.28%60
39.53%Yes
NaN16
23.08%18
-37.93%89
-69.73%3
200%10
66.67%