From 7c784f78afef3dc9006109bd6c8c774070e151a1 Mon Sep 17 00:00:00 2001 From: tglide <26071571+TGlide@users.noreply.github.com> Date: Mon, 12 Dec 2022 17:49:26 +0000 Subject: [PATCH 1/3] chore: remove date on array jsdoc --- src/lib/helpers/array.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/lib/helpers/array.ts b/src/lib/helpers/array.ts index 47bddeb771..57468b22dc 100644 --- a/src/lib/helpers/array.ts +++ b/src/lib/helpers/array.ts @@ -50,7 +50,6 @@ export function at(array: readonly T[] | [], index: number): T | undefined { /** * Get last item - * @date 12/12/2022 - 12:08:03 PM * * @export * @template T From f1b4205fe67d690ddee7627ba474029b07abcfd9 Mon Sep 17 00:00:00 2001 From: tglide <26071571+TGlide@users.noreply.github.com> Date: Mon, 12 Dec 2022 18:38:10 +0000 Subject: [PATCH 2/3] feat: add attribute types to input labels --- src/lib/elements/forms/inputCheckbox.svelte | 3 + src/lib/elements/forms/inputDateTime.svelte | 3 + src/lib/elements/forms/inputNumber.svelte | 3 + src/lib/elements/forms/inputSelect.svelte | 3 + src/lib/elements/forms/inputText.svelte | 7 +- src/lib/helpers/object.ts | 75 +++++++++++++++++++ src/lib/helpers/string.ts | 10 +++ .../document-[document]/attribute.svelte | 3 + .../document-[document]/attributeForm.svelte | 55 +++++++++----- .../attributes/boolean.svelte | 4 +- .../attributes/datetime.svelte | 9 ++- .../attributes/enum.svelte | 2 + .../attributes/integer.svelte | 2 + .../attributes/string.svelte | 2 + .../collection-[collection]/store.ts | 7 ++ tests/unit/helpers/object.test.ts | 74 ++++++++++++++++++ 16 files changed, 239 insertions(+), 23 deletions(-) create mode 100644 src/lib/helpers/object.ts create mode 100644 src/lib/helpers/string.ts create mode 100644 tests/unit/helpers/object.test.ts diff --git a/src/lib/elements/forms/inputCheckbox.svelte b/src/lib/elements/forms/inputCheckbox.svelte index cecc2a4419..62de5ec3ee 100644 --- a/src/lib/elements/forms/inputCheckbox.svelte +++ b/src/lib/elements/forms/inputCheckbox.svelte @@ -2,6 +2,7 @@ import { FormItem, Helper } from '.'; export let label: string; + export let optionalText: string | undefined = undefined; export let showLabel = true; export let id: string; export let value = false; @@ -27,6 +28,8 @@ + {optionalText} +
+ {optionalText} +
+ {optionalText} +
+ {optionalText} +
(obj: T) { + return Object.entries(obj) as Array<[keyof T, T[keyof T]]>; +} + +type KeyTypesMap = Record | ((v: unknown) => boolean) | string>; +/** + * Checks if the given value is an object of type T, given a map of keys and + * their types/validators. + * + * @category Object + * + * @template T + * @param {unknown} object + * @param {KeyTypesMap} keyTypesMap + * @returns {value is T} + * + * @example + * ``` + * type ExampleType = { + * a: string; + * b: number; + * c: string | number; + * d: string[] + * e: { + * f: string; + * g: number; + * } + * } + * + * export const isExampleType = (value: unknown): value is ExampleType => { + * return isObjectType(value, { + * a: 'string', + * b: 'number', + * c: ['string', 'number'], + * d: (v) => Array.isArray(v) && v.every((v) => typeof v === 'string'), + * e: (v) => isObjectType(v, { + * f: 'string', + * g: 'number', + * }), + * }); + * }; + * ``` + */ +export const isObjectType = ( + object: unknown, + keyTypesMap: Partial> +): object is T => { + if (typeof object !== 'object' || object === null) { + return false; + } + + for (const [key, check] of objectEntries(keyTypesMap)) { + const value = (object as Record)[key]; + + if (typeof check === 'function') { + if (!check(value)) { + return false; + } + } else if (typeof check === 'string') { + if (typeof value !== check) { + return false; + } + } else if (!check.includes(typeof value)) { + return false; + } + } + + return true; +}; diff --git a/src/lib/helpers/string.ts b/src/lib/helpers/string.ts new file mode 100644 index 0000000000..e67be0e074 --- /dev/null +++ b/src/lib/helpers/string.ts @@ -0,0 +1,10 @@ +/** + * Capitalizes the first letter of a string + * + * @export + * @param {string} str + * @returns {string} + */ +export function capitalize(str: string): string { + return str.charAt(0).toUpperCase() + str.slice(1); +} diff --git a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/document-[document]/attribute.svelte b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/document-[document]/attribute.svelte index 1a390b645c..80c66eb4bc 100644 --- a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/document-[document]/attribute.svelte +++ b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/document-[document]/attribute.svelte @@ -9,6 +9,7 @@ export let id: string; export let label: string; export let value: string | number | boolean; + export let optionalText: string | undefined = undefined; export let attribute: | Models.AttributeBoolean | Models.AttributeEmail @@ -43,6 +44,7 @@ {id} {label} {attribute} + {optionalText} bind:value /> {:else} {/if} {/if} diff --git a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/document-[document]/attributeForm.svelte b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/document-[document]/attributeForm.svelte index 7cb95c5ab3..0cd78ae5d6 100644 --- a/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/document-[document]/attributeForm.svelte +++ b/src/routes/console/project-[project]/databases/database-[database]/collection-[collection]/document-[document]/attributeForm.svelte @@ -3,7 +3,8 @@ import { FormList } from '$lib/elements/forms'; import Button from '$lib/elements/forms/button.svelte'; import Pill from '$lib/elements/pill.svelte'; - import type { Attributes } from '../store'; + import { capitalize } from '$lib/helpers/string'; + import { isAttributeEnum, type Attributes } from '../store'; import Attribute from './attribute.svelte'; export let attributes: Attributes[] = []; @@ -26,6 +27,11 @@ [key]: [...formValues[key], null] }; } + + function getAttributeType(attribute: Attributes) { + if (isAttributeEnum(attribute)) return 'Enum'; + return `${capitalize(attribute.type)}${attribute.array ? '[]' : ''}`; + } {#if attributes.length} @@ -35,7 +41,11 @@ {#if attribute.array} {#if formValues[attribute.key].length === 0}
- {label} +
+ {label} + {getAttributeType(attribute)} +
+
{/if} - {#each [...formValues[attribute.key].keys()] as index} -
  • -
    - -
    -
    - -
    -
  • - {/each} - {#if formValues[attribute.key].length !== 0} +
      + {#each [...formValues[attribute.key].keys()] as index} +
    • +
      + +
      +
      + +
      +
    • + {/each} +