on ee2f1d7 help sign in
astrolabe/ui/src/lib/utils.ts
37 lines 1.1 kB history raw
1function isStringNaN(value: string): boolean {
2 return window.isNaN(value as any) || Number.isNaN(parseInt(value)) || !(/^-?\d+$/.test(value));
3}
5function 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("', '") + "'";
12function filterObject(obj, predicate) {
13 return Object.fromEntries(Object.entries(obj).filter(([key, value]) => predicate(key, value)));
16function 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('&') : '';
32export {
33 isStringNaN,
34 listStrings,
35 filterObject,
36 buildQuerystring,
37};