Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
},
"dependencies": {
"@ai-sdk/svelte": "^1.1.24",
"@appwrite.io/console": "https://pkg.vc/-/@appwrite/@appwrite.io/console@e621876",
"@appwrite.io/console": "https://pkg.vc/-/@appwrite/@appwrite.io/console@95675c4",
"@appwrite.io/pink-icons": "0.25.0",
"@appwrite.io/pink-icons-svelte": "https://pkg.vc/-/@appwrite/@appwrite.io/pink-icons-svelte@bbad65f",
"@appwrite.io/pink-icons-svelte": "https://pkg.vc/-/@appwrite/@appwrite.io/pink-icons-svelte@b2b1cfe",
"@appwrite.io/pink-legacy": "^1.0.3",
"@appwrite.io/pink-svelte": "https://pkg.vc/-/@appwrite/@appwrite.io/pink-svelte@bbad65f",
"@appwrite.io/pink-svelte": "https://pkg.vc/-/@appwrite/@appwrite.io/pink-svelte@b2b1cfe",
"@faker-js/faker": "^9.9.0",
"@plausible-analytics/tracker": "^0.4.4",
"@popperjs/core": "^2.11.8",
Expand Down
57 changes: 36 additions & 21 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 1 addition & 5 deletions src/lib/components/sidebar.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,7 @@
style:--overlay-on-neutral={$app.themeInUse === 'dark'
? 'var(--neutral-750)'
: 'var(--neutral-100)'}>
<Sidebar.Base
{...$$props}
bind:state
on:resize={(event) => updateSidebarState(event.detail)}
resizable>
<Sidebar.Base bind:state resizable on:resize={(event) => updateSidebarState(event.detail)}>
<div slot="top">
<div class="only-mobile-tablet top">
<div class="icons search-icon">
Expand Down
6 changes: 3 additions & 3 deletions src/lib/elements/forms/inputNumber.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
export let label: string = null;
export let id: string;
export let name = id;
export let value: number = null;
export let value: number | bigint = null;
export let placeholder = '';
export let required = false;
export let nullable = false;
export let disabled = false;
export let readonly = false;
export let autofocus = false;
export let min: number = null;
export let max: number = null;
export let min: number | bigint = null;
export let max: number | bigint = null;
export let step: number | 'any' = 1;
export let helper: string = undefined;
export let leadingIcon: ComponentType | undefined = undefined;
Expand Down
11 changes: 7 additions & 4 deletions src/lib/helpers/faker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { sdk } from '$lib/stores/sdk';
import { faker } from '@faker-js/faker';
import type { NestedNumberArray } from './types';
import { ID, type Models } from '@appwrite.io/console';
import { isWithinSafeRange } from '$lib/helpers/numbers';
import type { DatabaseType, Field } from '$database/(entity)';
import { coerceToNumber, isWithinSafeRange } from '$lib/helpers/numbers';

export async function generateFields(
project: Models.Project,
Expand Down Expand Up @@ -213,10 +213,13 @@ function generateSingleValue(field: Field): string | number | boolean | NestedNu

case 'integer': {
const intAttr = field as Models.ColumnInteger;
const min = isWithinSafeRange(intAttr.min) ? intAttr.min : 0;
const minCompat = coerceToNumber(intAttr.min);
const maxCompat = coerceToNumber(intAttr.max);

const min = isWithinSafeRange(minCompat) ? minCompat : 0;
const fallbackMax = Math.max(min + 100, 100);
const max = isWithinSafeRange(intAttr.max)
? intAttr.max
const max = isWithinSafeRange(maxCompat)
? maxCompat
: Math.min(fallbackMax, Number.MAX_SAFE_INTEGER);
return faker.number.int({ min, max });
}
Expand Down
39 changes: 38 additions & 1 deletion src/lib/helpers/numbers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import {
abbreviateNumber,
formatCurrency,
formatNumberWithCommas,
toDecimals
toDecimals,
toExponential
} from '$lib/helpers/numbers';

/*
Expand Down Expand Up @@ -83,3 +84,39 @@ test('format number according to specified locale and currency', () => {
test('return the input as a string if it is not a number', () => {
expect(formatCurrency(NaN)).toEqual('NaN');
});

test('format regular numbers correctly', () => {
expect(toExponential(1234567)).toEqual('1.23e+6');
expect(toExponential(900719925474103)).toEqual('9.01e+14');
});

test('format bigint correctly without precision loss', () => {
expect(toExponential(900719925474103n)).toEqual('9.00e+14');
expect(toExponential(123456789012345678901234567890n)).toEqual('1.23e+29');
});

test('handle negative numbers correctly', () => {
expect(toExponential(-1234567)).toEqual('-1.23e+6');
expect(toExponential(-900719925474103n)).toEqual('-9.00e+14');
});

test('handle small numbers correctly', () => {
expect(toExponential(123)).toEqual('1.23e+2');
});

test('handle zero correctly', () => {
expect(toExponential(0)).toEqual('0.00e+0');
expect(toExponential(0n)).toEqual('0.00e+0');
expect(toExponential(0n, 0)).toEqual('0e+0');
});

test('validate fractionDigits range', () => {
expect(() => toExponential(123, -1)).toThrow(RangeError);
expect(() => toExponential(123, 101)).toThrow(RangeError);
expect(() => toExponential(123n, -1)).toThrow(RangeError);
});

test('truncate non-integer fractionDigits', () => {
expect(toExponential(123456, 2.9)).toEqual('1.23e+5');
expect(toExponential(123456n, 2.9)).toEqual('1.23e+5');
});
42 changes: 42 additions & 0 deletions src/lib/helpers/numbers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export const LARGE_NUMBER_THRESHOLD = 1_000_000n;
export const LARGE_NUMBER_THRESHOLD_NUM = 1_000_000;

export function abbreviateNumber(num: number, decimals: number = 1): string {
if (isNaN(num)) return String(num);
if (num >= 1_000_000_000) {
Expand Down Expand Up @@ -42,6 +45,45 @@ export function isWithinSafeRange(val: number) {
return Math.abs(val) < Number.MAX_SAFE_INTEGER;
}

export function coerceToNumber(val: number | bigint) {
return Number(val);
}

/**
* Converts a bigint to exponential notation without precision loss.
*/
export function toExponential(value: number | bigint, fractionDigits: number = 2): string {
fractionDigits = Math.trunc(fractionDigits);
if (fractionDigits < 0 || fractionDigits > 100) {
throw new RangeError('toExponential() argument must be between 0 and 100');
}

if (typeof value === 'number') {
return value.toExponential(fractionDigits);
}

if (value === 0n) {
return fractionDigits === 0 ? '0e+0' : `0.${'0'.repeat(fractionDigits)}e+0`;
}

const bigIntString = value.toString();

// split to get sign and value as string!
const [sign, digits] = !bigIntString.startsWith('-')
? ['', bigIntString]
: ['-', bigIntString.slice(1)];

// when no decimal
if (fractionDigits === 0) {
return `${sign}${digits[0]}e+${digits.length - 1}`;
}

const fraction = digits.slice(1, 1 + fractionDigits).padEnd(fractionDigits, '0');

// example: 900719925474103n => "9.00e+14"
return `${sign}${digits[0]}.${fraction}e+${digits.length - 1}`;
}

/**
* Clamps a number to a minimum value
*
Expand Down
4 changes: 2 additions & 2 deletions src/lib/stores/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
Tokens,
TablesDB,
Domains,
DocumentsDB,
/*DocumentsDB,*/
Realtime,
Organizations
} from '@appwrite.io/console';
Expand Down Expand Up @@ -138,7 +138,7 @@ const sdkForProject = {
migrations: new Migrations(clientProject),
sites: new Sites(clientProject),
tablesDB: new TablesDB(clientProject),
documentsDB: new DocumentsDB(clientProject),
/*documentsDB: new DocumentsDB(clientProject),*/
console: new Console(clientProject) // for suggestions API
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,8 @@ export function useDatabaseSdk(
const { total, tables } = await baseSdk.tablesDB.listTables(params);
return { total, entities: tables.map(toSupportiveEntity) };
}
case 'documentsdb': {
const { total, collections } =
await baseSdk.documentsDB.listCollections(params);
return { total, entities: collections.map(toSupportiveEntity) };
}
case 'vectordb':
case 'documentsdb':
throw new Error(`Database type not supported yet`);
default:
throw new Error(`Unknown database type`);
Expand All @@ -88,13 +84,7 @@ export function useDatabaseSdk(
});
return toSupportiveEntity(table);
}
case 'documentsdb': {
const table = await baseSdk.documentsDB.getCollection({
databaseId: params.databaseId,
collectionId: params.entityId
});
return toSupportiveEntity(table);
}
case 'documentsdb':
case 'vectordb':
throw new Error(`Database type not supported yet`);
default:
Expand All @@ -108,7 +98,6 @@ export function useDatabaseSdk(
case 'tablesdb':
return await baseSdk.tablesDB.delete(params);
case 'documentsdb':
return await baseSdk.documentsDB.delete(params);
case 'vectordb':
throw new Error(`Database type not supported yet`);
default:
Expand Down
Loading