@jskit-ai/crud-core 0.1.26 → 0.1.28

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,54 @@
1
+ import { normalizeText } from "@jskit-ai/kernel/shared/support/normalize";
2
+ import {
3
+ DEFAULT_CRUD_LOOKUP_CONTAINER_KEY,
4
+ normalizeCrudLookupContainerKey
5
+ } from "@jskit-ai/kernel/shared/support/crudLookup";
6
+
7
+ const CRUD_RUNTIME_LOOKUPS_FIELD_KEY = DEFAULT_CRUD_LOOKUP_CONTAINER_KEY;
8
+ const CRUD_LOOKUP_FORM_CONTROL_AUTOCOMPLETE = "autocomplete";
9
+ const CRUD_LOOKUP_FORM_CONTROL_SELECT = "select";
10
+
11
+ function checkCrudLookupFormControl(
12
+ value,
13
+ {
14
+ context = "crud fieldMeta ui.formControl",
15
+ defaultValue = CRUD_LOOKUP_FORM_CONTROL_AUTOCOMPLETE
16
+ } = {}
17
+ ) {
18
+ const resolvedValue = value === undefined || value === null || value === "" ? defaultValue : value;
19
+ if (resolvedValue === "") {
20
+ return "";
21
+ }
22
+
23
+ if (
24
+ resolvedValue === CRUD_LOOKUP_FORM_CONTROL_AUTOCOMPLETE ||
25
+ resolvedValue === CRUD_LOOKUP_FORM_CONTROL_SELECT
26
+ ) {
27
+ return resolvedValue;
28
+ }
29
+
30
+ throw new Error(
31
+ `${context} must be "${CRUD_LOOKUP_FORM_CONTROL_AUTOCOMPLETE}" or "${CRUD_LOOKUP_FORM_CONTROL_SELECT}". ` +
32
+ `Received: ${JSON.stringify(resolvedValue)}.`
33
+ );
34
+ }
35
+
36
+ function isCrudRuntimeOutputOnlyFieldKey(
37
+ value = "",
38
+ {
39
+ lookupContainerKey = CRUD_RUNTIME_LOOKUPS_FIELD_KEY
40
+ } = {}
41
+ ) {
42
+ const resolvedLookupContainerKey = normalizeCrudLookupContainerKey(lookupContainerKey, {
43
+ context: "crud runtime lookup container key"
44
+ });
45
+ return normalizeText(value) === resolvedLookupContainerKey;
46
+ }
47
+
48
+ export {
49
+ CRUD_LOOKUP_FORM_CONTROL_AUTOCOMPLETE,
50
+ CRUD_LOOKUP_FORM_CONTROL_SELECT,
51
+ CRUD_RUNTIME_LOOKUPS_FIELD_KEY,
52
+ checkCrudLookupFormControl,
53
+ isCrudRuntimeOutputOnlyFieldKey
54
+ };
@@ -0,0 +1,31 @@
1
+ import { normalizeText } from "@jskit-ai/kernel/shared/support/normalize";
2
+
3
+ function normalizeCrudNamespace(value = "") {
4
+ return normalizeText(value)
5
+ .toLowerCase()
6
+ .replace(/[^a-z0-9-]+/g, "-")
7
+ .replace(/-+/g, "-")
8
+ .replace(/^-+|-+$/g, "");
9
+ }
10
+
11
+ function requireCrudNamespace(namespace, { context = "requireCrudNamespace" } = {}) {
12
+ const normalizedNamespace = normalizeCrudNamespace(namespace);
13
+ if (!normalizedNamespace) {
14
+ throw new TypeError(`${context} requires a non-empty namespace.`);
15
+ }
16
+
17
+ return normalizedNamespace;
18
+ }
19
+
20
+ function resolveCrudRecordChangedEvent(namespace = "") {
21
+ const normalizedNamespace = requireCrudNamespace(namespace, {
22
+ context: "resolveCrudRecordChangedEvent"
23
+ });
24
+ return `${normalizedNamespace.replace(/-/g, "_")}.record.changed`;
25
+ }
26
+
27
+ export {
28
+ normalizeCrudNamespace,
29
+ requireCrudNamespace,
30
+ resolveCrudRecordChangedEvent
31
+ };