summaryrefslogtreecommitdiff
path: root/public/src/util.tsx
diff options
context:
space:
mode:
authoraura <nw@moneybot.cc>2026-02-19 19:57:10 +0100
committeraura <nw@moneybot.cc>2026-02-19 19:57:10 +0100
commitbedf43af45d97a10a6f62b4f1bb21cd66fda1d71 (patch)
tree6501bb95977a574c188bef6a228ff7500b243f3a /public/src/util.tsx
parentb5fca421c8f5f0f8f26d1392ef48635196887fa3 (diff)
empty moneyjsx site
Diffstat (limited to 'public/src/util.tsx')
-rw-r--r--public/src/util.tsx60
1 files changed, 60 insertions, 0 deletions
diff --git a/public/src/util.tsx b/public/src/util.tsx
new file mode 100644
index 0000000..f083d9f
--- /dev/null
+++ b/public/src/util.tsx
@@ -0,0 +1,60 @@
+export function escapeHtml( html: string ) {
+ const entityMap = {
+ '&': '&amp;',
+ '<': '&lt;',
+ '>': '&gt;',
+ '"': '&quot;',
+ "'": '&#39;',
+ '/': '&#x2F;',
+ '`': '&#x60;',
+ '=': '&#x3D;'
+ };
+
+ return String( html ).replace( /[&<>"'`=\/]/g, ( s ) => {
+ return entityMap[s];
+ } );
+}
+
+export function parseJWT( token: string ) : any {
+ const parts = token.split( '.' );
+ let encoded = parts[1];
+ encoded = encoded.replace(/-/g, '+').replace(/_/g, '/');
+ const pad = encoded.length % 4;
+ if( pad === 1 )
+ throw new Error( 'what the fuck' );
+ if( pad > 1 )
+ encoded += new Array( 5 - pad ).join( '=' );
+
+ const payload = JSON.parse( atob( encoded ) );
+ return payload;
+}
+
+export function sizeHumanReadable( size: number, short: boolean = false ) {
+ if( size < 1024 )
+ return size + (short? 'B' : ' B');
+ else if( size < 1024 * 1024 )
+ return ( size / 1024 ).toFixed( short? 1: 2 ) + (short? 'K' : ' KB');
+ else if( size < 1024 * 1024 * 1024 )
+ return ( size / 1024 / 1024 ).toFixed( short? 1 : 2 ) + (short? 'M' : ' MB');
+ else
+ return ( size / 1024 / 1024 / 1024 ).toFixed( short? 1 : 2 ) + (short? 'G':' GB');
+}
+
+export function monthToNumber( month: string ) {
+ const months = [
+ '',
+ 'jan',
+ 'feb',
+ 'mar',
+ 'apr',
+ 'may',
+ 'jun',
+ 'jul',
+ 'aug',
+ 'sep',
+ 'oct',
+ 'nov',
+ 'dec'
+ ];
+ return months.indexOf( month.toLowerCase() );
+};