on 1e69bbd help sign in
astrolabe/ui/src/lib/utils.ts
61 lines 1.7 kB history raw
1export function isStringNaN(value: string): boolean {
2 return window.isNaN(value as any) || Number.isNaN(parseInt(value)) || !(/^-?\d+$/.test(value));
3}
5export function listStrings(list: string[], includeAnd: boolean = false): string {
6 if (includeAnd && list.length > 1) {
7 return listStrings(list.slice(0, -1)) + ' and ' + listStrings(list.slice(-1));
8 }
9 return "'" + list.join("', '") + "'";
12export function filterObject(obj, predicate) {
13 return Object.fromEntries(Object.entries(obj).filter(([key, value]) => predicate(key, value)));
16export function buildQuerystring(data) {
17 if (typeof (data) === 'string') return data;
19 var query = [];
20 for (let key in data) {
21 if (data.hasOwnProperty(key)) {
22 // Encode each key and value, concatenate them into a string, and push them to the array
23 query.push(encodeURIComponent(key) + '=' + encodeURIComponent(data[key]));
24 }
25 }
27 // Join each item in the array with a `&` and return the resulting string
28 return query.length ? '?' + query.join('&') : '';
31export function setStoreKey(store, key, value) {
32 store.update((store) => ({
33 ...store,
34 [key]: value,
35 }));
38export function toggleStoreKey(store, key) {
39 store.update((store) => ({
40 ...store,
41 [key]: !store[key],
42 }));
45export function capitalize(string: string): string {
46 return string.charAt(0).toUpperCase() + string.slice(1);
49export function uxToHex(ux: string) {
50 let value = ux;
51 if (ux.length > 2 && ux.substring(0, 2) === '0x') {
52 value = ux.substring(2)
53 }
55 value = value.replace('.', '').padStart(6, '0');
56 return '#' + value;
59export function sleep(ms) {
60 return new Promise(resolve => setTimeout(resolve, ms));