Skip to content

Commit

Permalink
Apply prettier config
Browse files Browse the repository at this point in the history
  • Loading branch information
csillag committed Oct 11, 2024
1 parent a9cdec5 commit 19bbd3b
Showing 1 changed file with 54 additions and 56 deletions.
110 changes: 54 additions & 56 deletions frontend/src/utils/slug.ts
Original file line number Diff line number Diff line change
@@ -1,81 +1,79 @@
import { BytesLike, getBytes, hexlify } from "ethers";
import { BytesLike, getBytes, hexlify } from 'ethers'

// https://en.wikipedia.org/wiki/Base32#z-base-32
const BASE32_ALPHABET = 'ybndrfg8ejkmcpqxot1uwisza345h769';
const BASE32_ALPHABET = 'ybndrfg8ejkmcpqxot1uwisza345h769'

function base32Decode (src: string) {
const srcSize = src.length;
let srcPtr = 0;
function base32Decode(src: string) {
const srcSize = src.length
let srcPtr = 0

const dst = new Uint8Array(srcSize * 5 / 8);
let dstPtr = 0;
const dst = new Uint8Array((srcSize * 5) / 8)
let dstPtr = 0

let value = 0;
for (let i = 0; i < srcSize; i++) {
value = (value << 5) | BASE32_ALPHABET.indexOf(src[i]);
srcPtr += 5;
let value = 0
for (let i = 0; i < srcSize; i++) {
value = (value << 5) | BASE32_ALPHABET.indexOf(src[i])
srcPtr += 5

if (srcPtr >= 8) {
dst[dstPtr++] = (value >>> (srcPtr - 8)) & 0xFF;
srcPtr -= 8;
}
if (srcPtr >= 8) {
dst[dstPtr++] = (value >>> (srcPtr - 8)) & 0xff
srcPtr -= 8
}
}
return dst
}

return dst;
};

function base32Encode (src: Uint8Array) {
const srcSize = src.byteLength;
let srcPtr = 0;
function base32Encode(src: Uint8Array) {
const srcSize = src.byteLength
let srcPtr = 0

let dst = '';
let dst = ''

let value = 0;
for (let i = 0; i < srcSize; i++) {
value = (value << 8) | src[i];
srcPtr += 8;
while (srcPtr >= 5) {
dst += BASE32_ALPHABET[(value >>> (srcPtr - 5)) & 31];
srcPtr -= 5;
}
let value = 0
for (let i = 0; i < srcSize; i++) {
value = (value << 8) | src[i]
srcPtr += 8
while (srcPtr >= 5) {
dst += BASE32_ALPHABET[(value >>> (srcPtr - 5)) & 31]
srcPtr -= 5
}
}

if (srcPtr > 0)
dst += BASE32_ALPHABET[(value << (5 - srcPtr)) & 31];
if (srcPtr > 0) dst += BASE32_ALPHABET[(value << (5 - srcPtr)) & 31]

return dst;
};
return dst
}

// ----------------------------------------------------------------------------

const SLUG_BYTES = 8;
const SLUG_BYTES = 8

function ensureMinLength(array: Uint8Array, minLength: number): Uint8Array {
if (array.length >= minLength) {
return array;
}
const paddedArray = new Uint8Array(minLength);
paddedArray.set(array, 0);
return paddedArray;
if (array.length >= minLength) {
return array
}
const paddedArray = new Uint8Array(minLength)
paddedArray.set(array, 0)
return paddedArray
}

function removeTrailingZeros (arr: Uint8Array): Uint8Array {
let lastNonZeroIndex = arr.length - 1;
while (lastNonZeroIndex >= 0 && arr[lastNonZeroIndex] === 0) {
lastNonZeroIndex--;
}
return arr.slice(0, lastNonZeroIndex + 1);
function removeTrailingZeros(arr: Uint8Array): Uint8Array {
let lastNonZeroIndex = arr.length - 1
while (lastNonZeroIndex >= 0 && arr[lastNonZeroIndex] === 0) {
lastNonZeroIndex--
}
return arr.slice(0, lastNonZeroIndex + 1)
}

export function proposalIdToSlug(proposalId:BytesLike) {
const bytes = removeTrailingZeros(getBytes(proposalId));
return base32Encode(ensureMinLength(bytes, SLUG_BYTES));
export function proposalIdToSlug(proposalId: BytesLike) {
const bytes = removeTrailingZeros(getBytes(proposalId))
return base32Encode(ensureMinLength(bytes, SLUG_BYTES))
}

export function slugToProposalId(slug:string) {
// Special case for demo poll
if( slug === 'demo' ) {
return slug;
}
return hexlify(base32Decode(slug.toLowerCase())).substring(2).padEnd(64, '0');
export function slugToProposalId(slug: string) {
// Special case for demo poll
if (slug === 'demo') {
return slug
}
return hexlify(base32Decode(slug.toLowerCase())).substring(2).padEnd(64, '0')
}

0 comments on commit 19bbd3b

Please sign in to comment.