@cratis/scene.engine 4.24.0 → 4.26.0
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.
- package/dist/cjs/index.js +52 -0
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/sampleData/SampleRandom.js +77 -0
- package/dist/cjs/sampleData/SampleRandom.js.map +1 -0
- package/dist/cjs/sampleData/describeSampleDataCatalog.js +61 -0
- package/dist/cjs/sampleData/describeSampleDataCatalog.js.map +1 -0
- package/dist/cjs/sampleData/findSampleDataSet.js +18 -0
- package/dist/cjs/sampleData/findSampleDataSet.js.map +1 -0
- package/dist/cjs/sampleData/findSampleRowLiterals.js +73 -0
- package/dist/cjs/sampleData/findSampleRowLiterals.js.map +1 -0
- package/dist/cjs/sampleData/generateSample.js +188 -0
- package/dist/cjs/sampleData/generateSample.js.map +1 -0
- package/dist/cjs/sampleData/inferSampleFieldKind.js +176 -0
- package/dist/cjs/sampleData/inferSampleFieldKind.js.map +1 -0
- package/dist/cjs/sampleData/resolveSampleData.js +107 -0
- package/dist/cjs/sampleData/resolveSampleData.js.map +1 -0
- package/dist/cjs/sampleData/sampleDataRules.js +26 -0
- package/dist/cjs/sampleData/sampleDataRules.js.map +1 -0
- package/dist/cjs/sampleData/sampleLocales.js +359 -0
- package/dist/cjs/sampleData/sampleLocales.js.map +1 -0
- package/dist/cjs/sampleData/validateSampleDataPack.js +44 -0
- package/dist/cjs/sampleData/validateSampleDataPack.js.map +1 -0
- package/dist/cjs/sampleData/validateSampleDataPackages.js +71 -0
- package/dist/cjs/sampleData/validateSampleDataPackages.js.map +1 -0
- package/dist/cjs/sampleData/validateSampleDataSelection.js +52 -0
- package/dist/cjs/sampleData/validateSampleDataSelection.js.map +1 -0
- package/dist/cjs/templateApplicability.js.map +1 -1
- package/dist/cjs/templates/TemplateReferenceResolution.js +15 -0
- package/dist/cjs/templates/TemplateReferenceResolution.js.map +1 -0
- package/dist/cjs/templates/applicableTemplates.js +199 -0
- package/dist/cjs/templates/applicableTemplates.js.map +1 -0
- package/dist/cjs/templates/describeTemplateCatalog.js +11 -6
- package/dist/cjs/templates/describeTemplateCatalog.js.map +1 -1
- package/dist/cjs/templates/resolveTemplateReference.js +89 -0
- package/dist/cjs/templates/resolveTemplateReference.js.map +1 -0
- package/dist/cjs/templates/validateTemplateMetadata.js +9 -0
- package/dist/cjs/templates/validateTemplateMetadata.js.map +1 -1
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +16 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/sampleData/SampleDataPackSource.d.ts +6 -0
- package/dist/esm/sampleData/SampleDataPackSource.d.ts.map +1 -0
- package/dist/esm/sampleData/SampleDataPackSource.js +2 -0
- package/dist/esm/sampleData/SampleDataPackSource.js.map +1 -0
- package/dist/esm/sampleData/SampleDataResolution.d.ts +29 -0
- package/dist/esm/sampleData/SampleDataResolution.d.ts.map +1 -0
- package/dist/esm/sampleData/SampleDataResolution.js +2 -0
- package/dist/esm/sampleData/SampleDataResolution.js.map +1 -0
- package/dist/esm/sampleData/SampleRandom.d.ts +14 -0
- package/dist/esm/sampleData/SampleRandom.d.ts.map +1 -0
- package/dist/esm/sampleData/SampleRandom.js +72 -0
- package/dist/esm/sampleData/SampleRandom.js.map +1 -0
- package/dist/esm/sampleData/describeSampleDataCatalog.d.ts +24 -0
- package/dist/esm/sampleData/describeSampleDataCatalog.d.ts.map +1 -0
- package/dist/esm/sampleData/describeSampleDataCatalog.js +57 -0
- package/dist/esm/sampleData/describeSampleDataCatalog.js.map +1 -0
- package/dist/esm/sampleData/findSampleDataSet.d.ts +3 -0
- package/dist/esm/sampleData/findSampleDataSet.d.ts.map +1 -0
- package/dist/esm/sampleData/findSampleDataSet.js +16 -0
- package/dist/esm/sampleData/findSampleDataSet.js.map +1 -0
- package/dist/esm/sampleData/findSampleRowLiterals.d.ts +11 -0
- package/dist/esm/sampleData/findSampleRowLiterals.d.ts.map +1 -0
- package/dist/esm/sampleData/findSampleRowLiterals.js +69 -0
- package/dist/esm/sampleData/findSampleRowLiterals.js.map +1 -0
- package/dist/esm/sampleData/generateSample.d.ts +6 -0
- package/dist/esm/sampleData/generateSample.d.ts.map +1 -0
- package/dist/esm/sampleData/generateSample.js +183 -0
- package/dist/esm/sampleData/generateSample.js.map +1 -0
- package/dist/esm/sampleData/index.d.ts +15 -0
- package/dist/esm/sampleData/index.d.ts.map +1 -0
- package/dist/esm/sampleData/index.js +15 -0
- package/dist/esm/sampleData/index.js.map +1 -0
- package/dist/esm/sampleData/inferSampleFieldKind.d.ts +3 -0
- package/dist/esm/sampleData/inferSampleFieldKind.d.ts.map +1 -0
- package/dist/esm/sampleData/inferSampleFieldKind.js +174 -0
- package/dist/esm/sampleData/inferSampleFieldKind.js.map +1 -0
- package/dist/esm/sampleData/resolveSampleData.d.ts +3 -0
- package/dist/esm/sampleData/resolveSampleData.d.ts.map +1 -0
- package/dist/esm/sampleData/resolveSampleData.js +105 -0
- package/dist/esm/sampleData/resolveSampleData.js.map +1 -0
- package/dist/esm/sampleData/sampleDataRules.d.ts +7 -0
- package/dist/esm/sampleData/sampleDataRules.d.ts.map +1 -0
- package/dist/esm/sampleData/sampleDataRules.js +19 -0
- package/dist/esm/sampleData/sampleDataRules.js.map +1 -0
- package/dist/esm/sampleData/sampleLocales.d.ts +13 -0
- package/dist/esm/sampleData/sampleLocales.d.ts.map +1 -0
- package/dist/esm/sampleData/sampleLocales.js +355 -0
- package/dist/esm/sampleData/sampleLocales.js.map +1 -0
- package/dist/esm/sampleData/validateSampleDataPack.d.ts +3 -0
- package/dist/esm/sampleData/validateSampleDataPack.d.ts.map +1 -0
- package/dist/esm/sampleData/validateSampleDataPack.js +42 -0
- package/dist/esm/sampleData/validateSampleDataPack.js.map +1 -0
- package/dist/esm/sampleData/validateSampleDataPackages.d.ts +3 -0
- package/dist/esm/sampleData/validateSampleDataPackages.d.ts.map +1 -0
- package/dist/esm/sampleData/validateSampleDataPackages.js +69 -0
- package/dist/esm/sampleData/validateSampleDataPackages.js.map +1 -0
- package/dist/esm/sampleData/validateSampleDataSelection.d.ts +3 -0
- package/dist/esm/sampleData/validateSampleDataSelection.d.ts.map +1 -0
- package/dist/esm/sampleData/validateSampleDataSelection.js +50 -0
- package/dist/esm/sampleData/validateSampleDataSelection.js.map +1 -0
- package/dist/esm/templateApplicability.d.ts +1 -2
- package/dist/esm/templateApplicability.d.ts.map +1 -1
- package/dist/esm/templateApplicability.js.map +1 -1
- package/dist/esm/templates/ApplicableTemplateSet.d.ts +27 -0
- package/dist/esm/templates/ApplicableTemplateSet.d.ts.map +1 -0
- package/dist/esm/templates/ApplicableTemplateSet.js +2 -0
- package/dist/esm/templates/ApplicableTemplateSet.js.map +1 -0
- package/dist/esm/templates/TemplateCatalogEntry.d.ts +3 -1
- package/dist/esm/templates/TemplateCatalogEntry.d.ts.map +1 -1
- package/dist/esm/templates/TemplateLibrary.d.ts +6 -0
- package/dist/esm/templates/TemplateLibrary.d.ts.map +1 -0
- package/dist/esm/templates/TemplateLibrary.js +2 -0
- package/dist/esm/templates/TemplateLibrary.js.map +1 -0
- package/dist/esm/templates/TemplateReferenceResolution.d.ts +16 -0
- package/dist/esm/templates/TemplateReferenceResolution.d.ts.map +1 -0
- package/dist/esm/templates/TemplateReferenceResolution.js +13 -0
- package/dist/esm/templates/TemplateReferenceResolution.js.map +1 -0
- package/dist/esm/templates/TemplateTarget.d.ts +9 -0
- package/dist/esm/templates/TemplateTarget.d.ts.map +1 -0
- package/dist/esm/templates/TemplateTarget.js +2 -0
- package/dist/esm/templates/TemplateTarget.js.map +1 -0
- package/dist/esm/templates/applicableTemplates.d.ts +9 -0
- package/dist/esm/templates/applicableTemplates.d.ts.map +1 -0
- package/dist/esm/templates/applicableTemplates.js +194 -0
- package/dist/esm/templates/applicableTemplates.js.map +1 -0
- package/dist/esm/templates/describeTemplateCatalog.d.ts.map +1 -1
- package/dist/esm/templates/describeTemplateCatalog.js +12 -7
- package/dist/esm/templates/describeTemplateCatalog.js.map +1 -1
- package/dist/esm/templates/index.d.ts +6 -0
- package/dist/esm/templates/index.d.ts.map +1 -1
- package/dist/esm/templates/index.js +6 -0
- package/dist/esm/templates/index.js.map +1 -1
- package/dist/esm/templates/resolveTemplateReference.d.ts +5 -0
- package/dist/esm/templates/resolveTemplateReference.d.ts.map +1 -0
- package/dist/esm/templates/resolveTemplateReference.js +87 -0
- package/dist/esm/templates/resolveTemplateReference.js.map +1 -0
- package/dist/esm/templates/validateTemplateMetadata.d.ts +2 -0
- package/dist/esm/templates/validateTemplateMetadata.d.ts.map +1 -1
- package/dist/esm/templates/validateTemplateMetadata.js +8 -1
- package/dist/esm/templates/validateTemplateMetadata.js.map +1 -1
- package/dist/esm/tsconfig.tsbuildinfo +1 -1
- package/index.ts +1 -0
- package/package.json +3 -3
- package/sampleData/SampleDataPackSource.ts +13 -0
- package/sampleData/SampleDataResolution.ts +93 -0
- package/sampleData/SampleRandom.ts +85 -0
- package/sampleData/describeSampleDataCatalog.ts +108 -0
- package/sampleData/findSampleDataSet.ts +18 -0
- package/sampleData/findSampleRowLiterals.ts +86 -0
- package/sampleData/generateSample.ts +156 -0
- package/sampleData/index.ts +17 -0
- package/sampleData/inferSampleFieldKind.ts +64 -0
- package/sampleData/resolveSampleData.ts +96 -0
- package/sampleData/sampleDataRules.ts +30 -0
- package/sampleData/sampleLocales.ts +91 -0
- package/sampleData/validateSampleDataPack.ts +48 -0
- package/sampleData/validateSampleDataPackages.ts +79 -0
- package/sampleData/validateSampleDataSelection.ts +55 -0
- package/templateApplicability.ts +2 -1
- package/templates/ApplicableTemplateSet.ts +54 -0
- package/templates/TemplateCatalogEntry.ts +5 -1
- package/templates/TemplateLibrary.ts +16 -0
- package/templates/TemplateReferenceResolution.ts +35 -0
- package/templates/TemplateTarget.ts +23 -0
- package/templates/applicableTemplates.ts +148 -0
- package/templates/describeTemplateCatalog.ts +11 -7
- package/templates/index.ts +6 -0
- package/templates/resolveTemplateReference.ts +79 -0
- package/templates/validateTemplateMetadata.ts +11 -0
package/index.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cratis/scene.engine",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.26.0",
|
|
4
4
|
"description": "The Scene runtime: walks a Scene element tree and drives whichever renderer is plugged in. Does not know React, or any other rendering platform, exists.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cratis",
|
|
@@ -48,9 +48,9 @@
|
|
|
48
48
|
"up": "yarn g:up"
|
|
49
49
|
},
|
|
50
50
|
"dependencies": {
|
|
51
|
-
"@cratis/scene.model": "4.
|
|
51
|
+
"@cratis/scene.model": "4.26.0"
|
|
52
52
|
},
|
|
53
53
|
"devDependencies": {
|
|
54
|
-
"@cratis/scene.model": "4.
|
|
54
|
+
"@cratis/scene.model": "4.26.0"
|
|
55
55
|
}
|
|
56
56
|
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { SampleDataPack, ScenePackage } from '@cratis/scene.model';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A package and the sample-data packs it ships, loaded. A React `ScenePackageBundle` already has the manifest;
|
|
8
|
+
* hosts add the loaded packs the manifest's `sampleData` references point to.
|
|
9
|
+
*/
|
|
10
|
+
export interface SampleDataPackSource {
|
|
11
|
+
manifest: ScenePackage;
|
|
12
|
+
packs?: SampleDataPack[];
|
|
13
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { SampleDataPack, SampleShape, UiProfile } from '@cratis/scene.model';
|
|
5
|
+
|
|
6
|
+
/** Which rung of the resolution order supplied the rows. */
|
|
7
|
+
export type SampleDataResolutionSource =
|
|
8
|
+
/** A pack explicitly bound to the read model or shape. */
|
|
9
|
+
| 'binding'
|
|
10
|
+
/** The application's active pack: the profile's `sampleData.activePack`, or the session override. */
|
|
11
|
+
| 'activePack'
|
|
12
|
+
/** The default pack of the blueprint the template came from. */
|
|
13
|
+
| 'blueprintDefault'
|
|
14
|
+
/** Generated, typed by field kind. */
|
|
15
|
+
| 'generated'
|
|
16
|
+
/** Not resolved, because the host is not in design or preview mode (SD6). */
|
|
17
|
+
| 'disabled'
|
|
18
|
+
/** No pack had rows and no shape was given to generate from. */
|
|
19
|
+
| 'none';
|
|
20
|
+
|
|
21
|
+
/** What is being asked for: rows for a read model or shape. */
|
|
22
|
+
export interface SampleDataRequest {
|
|
23
|
+
/** The read model or shape name; defaults to the `shape`'s name when only a shape is given. */
|
|
24
|
+
target?: string;
|
|
25
|
+
|
|
26
|
+
/** The shape of one row; needed for the generated rung to produce anything. */
|
|
27
|
+
shape?: SampleShape;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** What the host knows when it asks for sample data. */
|
|
31
|
+
export interface SampleDataContext {
|
|
32
|
+
/** Every pack available: blueprint packs and the author's own. */
|
|
33
|
+
packs: SampleDataPack[];
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The application's UI profile, whose `sampleData` is the one place the active pack is chosen (plus the
|
|
37
|
+
* rare mappings and bindings). Absent, or a profile without `sampleData`, behaves as no active pack, no
|
|
38
|
+
* mappings and no bindings.
|
|
39
|
+
*/
|
|
40
|
+
profile?: Pick<UiProfile, 'sampleData'>;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* A session-only override of the profile's active pack, as the preview strip's selector sets it: a pack
|
|
44
|
+
* id replaces `profile.sampleData.activePack`, the empty string (the generated entry's id) means no
|
|
45
|
+
* active pack, and `undefined` (absent) leaves the profile's choice. It is never a second source of truth: it is not stored, and bindings and
|
|
46
|
+
* mappings still come from the profile.
|
|
47
|
+
*/
|
|
48
|
+
sessionActivePack?: string;
|
|
49
|
+
|
|
50
|
+
/** The default pack of the blueprint the template came from. */
|
|
51
|
+
blueprintDefaultPack?: string;
|
|
52
|
+
|
|
53
|
+
/** The locale of generated data when no pack supplies one; defaults to `en-GB`. */
|
|
54
|
+
locale?: string;
|
|
55
|
+
|
|
56
|
+
/** The seed for generated data; defaults to 1 so previews are stable. */
|
|
57
|
+
seed?: number;
|
|
58
|
+
|
|
59
|
+
/** The number of generated rows; defaults to 5. */
|
|
60
|
+
rowCount?: number;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Whether the host is design time or preview. Sample data resolves only when this is true; a Stage
|
|
64
|
+
* production build leaves it false (the default) and gets nothing (SD6).
|
|
65
|
+
*/
|
|
66
|
+
isDesignOrPreview?: boolean;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** A problem found while resolving, which never stops the resolution falling through to the next rung. */
|
|
70
|
+
export interface SampleDataDiagnostic {
|
|
71
|
+
/** `packNotFound` or `shapeNotInPack`. */
|
|
72
|
+
code: string;
|
|
73
|
+
message: string;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** The outcome of {@link resolveSampleData}. */
|
|
77
|
+
export interface SampleDataResolution {
|
|
78
|
+
/** Which rung supplied the rows. */
|
|
79
|
+
source: SampleDataResolutionSource;
|
|
80
|
+
|
|
81
|
+
/** The identity of the pack that supplied the rows; absent for generated, disabled and none. */
|
|
82
|
+
pack?: string;
|
|
83
|
+
|
|
84
|
+
/** The locale of the data: the pack's, or the one generated data followed. */
|
|
85
|
+
locale: string;
|
|
86
|
+
|
|
87
|
+
/** The shape name the rows belong to. */
|
|
88
|
+
shape?: string;
|
|
89
|
+
|
|
90
|
+
rows: Record<string, unknown>[];
|
|
91
|
+
|
|
92
|
+
diagnostics: SampleDataDiagnostic[];
|
|
93
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The pseudo-random generator behind generated sample data. The C# engine implements the identical
|
|
6
|
+
* algorithm, and both are asserted against golden rows in `sample-data-resolution-fixtures.json`.
|
|
7
|
+
*
|
|
8
|
+
* Algorithm (all arithmetic on unsigned 32-bit integers, wrapping):
|
|
9
|
+
*
|
|
10
|
+
* - `hash32(x)`: `x = (x ^ (x >>> 16)) * 0x85EBCA6B; x = (x ^ (x >>> 13)) * 0xC2B2AE35; return x ^ (x >>> 16)`.
|
|
11
|
+
* - `splitmix32(state)`: `state += 0x9E3779B9`, then `hash32(state)`.
|
|
12
|
+
* - `fnv1a32(text)`: FNV-1a over the UTF-8 bytes of the text (offset basis `0x811C9DC5`, prime `0x01000193`).
|
|
13
|
+
* - The generator is xoshiro128** seeded with four consecutive `splitmix32` outputs of the seed.
|
|
14
|
+
* - `nextInt(bound)` is `next() % bound`.
|
|
15
|
+
* - Row `i` of a shape named `S` generated with seed `n` uses its own generator seeded with
|
|
16
|
+
* `hash32((n ^ fnv1a32(S)) + (i + 1) * 0x9E3779B9)`, so a row never depends on how many rows are asked for.
|
|
17
|
+
*/
|
|
18
|
+
export function hash32(value: number): number {
|
|
19
|
+
let x = value >>> 0;
|
|
20
|
+
x = Math.imul(x ^ (x >>> 16), 0x85EBCA6B) >>> 0;
|
|
21
|
+
x = Math.imul(x ^ (x >>> 13), 0xC2B2AE35) >>> 0;
|
|
22
|
+
return (x ^ (x >>> 16)) >>> 0;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** FNV-1a (32-bit) over the UTF-8 bytes of a string. */
|
|
26
|
+
export function fnv1a32(text: string): number {
|
|
27
|
+
let hash = 0x811C9DC5;
|
|
28
|
+
for (const byte of new TextEncoder().encode(text)) {
|
|
29
|
+
hash = Math.imul(hash ^ byte, 0x01000193) >>> 0;
|
|
30
|
+
}
|
|
31
|
+
return hash;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** The seed of the generator for one row of one shape. */
|
|
35
|
+
export function rowSeed(seed: number, shapeName: string, rowIndex: number): number {
|
|
36
|
+
return hash32((((seed ^ fnv1a32(shapeName)) >>> 0) + Math.imul(rowIndex + 1, 0x9E3779B9)) >>> 0);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** xoshiro128** with splitmix32 seeding. */
|
|
40
|
+
export class SampleRandom {
|
|
41
|
+
private s0: number;
|
|
42
|
+
private s1: number;
|
|
43
|
+
private s2: number;
|
|
44
|
+
private s3: number;
|
|
45
|
+
|
|
46
|
+
constructor(seed: number) {
|
|
47
|
+
let state = seed >>> 0;
|
|
48
|
+
const step = () => {
|
|
49
|
+
state = (state + 0x9E3779B9) >>> 0;
|
|
50
|
+
return hash32(state);
|
|
51
|
+
};
|
|
52
|
+
this.s0 = step();
|
|
53
|
+
this.s1 = step();
|
|
54
|
+
this.s2 = step();
|
|
55
|
+
this.s3 = step();
|
|
56
|
+
if ((this.s0 | this.s1 | this.s2 | this.s3) === 0) this.s0 = 1;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** The next unsigned 32-bit value. */
|
|
60
|
+
next(): number {
|
|
61
|
+
const result = Math.imul(rotateLeft(Math.imul(this.s1, 5) >>> 0, 7), 9) >>> 0;
|
|
62
|
+
const t = (this.s1 << 9) >>> 0;
|
|
63
|
+
this.s2 = (this.s2 ^ this.s0) >>> 0;
|
|
64
|
+
this.s3 = (this.s3 ^ this.s1) >>> 0;
|
|
65
|
+
this.s1 = (this.s1 ^ this.s2) >>> 0;
|
|
66
|
+
this.s0 = (this.s0 ^ this.s3) >>> 0;
|
|
67
|
+
this.s2 = (this.s2 ^ t) >>> 0;
|
|
68
|
+
this.s3 = rotateLeft(this.s3, 11);
|
|
69
|
+
return result;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** A value in `[0, bound)`; zero when the bound is zero. */
|
|
73
|
+
nextInt(bound: number): number {
|
|
74
|
+
return bound <= 0 ? 0 : this.next() % bound;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** One element of a non-empty list. */
|
|
78
|
+
pick<T>(items: readonly T[]): T {
|
|
79
|
+
return items[this.nextInt(items.length)];
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function rotateLeft(value: number, bits: number): number {
|
|
84
|
+
return ((value << bits) | (value >>> (32 - bits))) >>> 0;
|
|
85
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { SampleDataPackReference, ScenePackage } from '@cratis/scene.model';
|
|
5
|
+
import { SampleDataPackSource } from './SampleDataPackSource';
|
|
6
|
+
import { validateSampleDataPackages } from './validateSampleDataPackages';
|
|
7
|
+
|
|
8
|
+
/** The separator between blueprint and pack in a label (`CRM · Nordic SMB`). */
|
|
9
|
+
export const sampleDataLabelSeparator = ' \u00b7 ';
|
|
10
|
+
|
|
11
|
+
/** One entry of the sample data selector. */
|
|
12
|
+
export interface SampleDataCatalogEntry {
|
|
13
|
+
/** The pack identity a selection stores as `activePack`; empty for the generated entry. */
|
|
14
|
+
id: string;
|
|
15
|
+
|
|
16
|
+
/** Whether this is the generated entry, the choice that stores no active pack. */
|
|
17
|
+
isGenerated: boolean;
|
|
18
|
+
|
|
19
|
+
/** The pack's name within its group (`Nordic SMB`). */
|
|
20
|
+
name: string;
|
|
21
|
+
|
|
22
|
+
/** The full label (`CRM · Nordic SMB`, or just the name for generated and own packs). */
|
|
23
|
+
label: string;
|
|
24
|
+
|
|
25
|
+
/** The locale tag, absent for the generated entry. */
|
|
26
|
+
locale?: string;
|
|
27
|
+
|
|
28
|
+
/** Whether it is the active pack (the generated entry is selected when no pack is active). */
|
|
29
|
+
selected: boolean;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** A blueprint's packs, one group of the Blueprints submenu. */
|
|
33
|
+
export interface SampleDataCatalogGroup {
|
|
34
|
+
/** The blueprint's display name (`CRM`). */
|
|
35
|
+
blueprint: string;
|
|
36
|
+
|
|
37
|
+
packs: SampleDataCatalogEntry[];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** The selector's content: generated, the author's own sets, then blueprint groups. */
|
|
41
|
+
export interface SampleDataCatalog {
|
|
42
|
+
/** `Generic (typed by field kind)`. */
|
|
43
|
+
generated: SampleDataCatalogEntry;
|
|
44
|
+
|
|
45
|
+
/** The author's own data sets, in the order given. */
|
|
46
|
+
own: SampleDataCatalogEntry[];
|
|
47
|
+
|
|
48
|
+
/** Blueprints that ship at least one pack, in package order; blueprints with none are not listed. */
|
|
49
|
+
blueprints: SampleDataCatalogGroup[];
|
|
50
|
+
|
|
51
|
+
/** Problems found validating what the packages ship. */
|
|
52
|
+
problems: string[];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** The name of the generated entry. */
|
|
56
|
+
export const generatedSampleDataName = 'Generic (typed by field kind)';
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Describes the sample data selector's content: the generated entry first, then the author's own packs, then
|
|
60
|
+
* each blueprint's packs grouped by the blueprint's display name (falling back to its package name). A pack
|
|
61
|
+
* whose title already starts with `<Blueprint> · ` shows the rest as its name, so `CRM · Nordic SMB` under
|
|
62
|
+
* `CRM` is `Nordic SMB`. Same own-first, then Blueprints menu pattern as the template picker (spec §11).
|
|
63
|
+
*
|
|
64
|
+
* @param sources The packages and the packs they ship (only their `sampleData` references are listed).
|
|
65
|
+
* @param ownPacks The author's own packs (id, title, locale).
|
|
66
|
+
* @param activePack The selection's active pack id, if any.
|
|
67
|
+
*/
|
|
68
|
+
export function describeSampleDataCatalog(
|
|
69
|
+
sources: readonly SampleDataPackSource[],
|
|
70
|
+
ownPacks: readonly Pick<SampleDataPackReference, 'id' | 'title' | 'locale'>[] = [],
|
|
71
|
+
activePack?: string,
|
|
72
|
+
): SampleDataCatalog {
|
|
73
|
+
const entry = (id: string, name: string, label: string, locale: string): SampleDataCatalogEntry => ({
|
|
74
|
+
id, isGenerated: false, name, label, locale, selected: id === activePack,
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
const blueprints: SampleDataCatalogGroup[] = [];
|
|
78
|
+
for (const source of sources) {
|
|
79
|
+
const references = source.manifest.sampleData ?? [];
|
|
80
|
+
if (references.length === 0) continue;
|
|
81
|
+
const group = groupName(source.manifest);
|
|
82
|
+
const prefix = `${group}${sampleDataLabelSeparator}`;
|
|
83
|
+
blueprints.push({
|
|
84
|
+
blueprint: group,
|
|
85
|
+
packs: references.map(reference => {
|
|
86
|
+
const name = reference.title.startsWith(prefix) ? reference.title.slice(prefix.length) : reference.title;
|
|
87
|
+
return entry(reference.id, name, `${group}${sampleDataLabelSeparator}${name}`, reference.locale);
|
|
88
|
+
}),
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return {
|
|
93
|
+
generated: {
|
|
94
|
+
id: '',
|
|
95
|
+
isGenerated: true,
|
|
96
|
+
name: generatedSampleDataName,
|
|
97
|
+
label: generatedSampleDataName,
|
|
98
|
+
selected: activePack === undefined || activePack === null || activePack === '',
|
|
99
|
+
},
|
|
100
|
+
own: ownPacks.map(pack => entry(pack.id, pack.title, pack.title, pack.locale)),
|
|
101
|
+
blueprints,
|
|
102
|
+
problems: validateSampleDataPackages(sources),
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function groupName(manifest: ScenePackage): string {
|
|
107
|
+
return manifest.displayName !== undefined && manifest.displayName.trim() !== '' ? manifest.displayName : manifest.name;
|
|
108
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { SampleDataMapping, SampleDataPack, SampleDataSet } from '@cratis/scene.model';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Finds the set of a pack that supplies rows for a read model or shape. An explicit mapping whose read model is
|
|
8
|
+
* the target wins; otherwise a set whose shape has the target's name (compared exactly, case-sensitively).
|
|
9
|
+
*/
|
|
10
|
+
export function findSampleDataSet(pack: SampleDataPack, target: string, mappings: readonly SampleDataMapping[] = []): SampleDataSet | undefined {
|
|
11
|
+
for (const mapping of mappings) {
|
|
12
|
+
if (mapping.readModel !== target) continue;
|
|
13
|
+
const mapped = (pack.sets ?? []).find(set => set.shape === mapping.shape);
|
|
14
|
+
if (mapped !== undefined) return mapped;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return (pack.sets ?? []).find(set => set.shape === target);
|
|
18
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { ComponentDescriptor } from '@cratis/scene.model';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Finds sample-row literals inside a value: an array holding plain objects of two or more keys, which is what
|
|
8
|
+
* a hand-written list of demo rows looks like. Returns the path of each, e.g. `rows` or `items[2].children`.
|
|
9
|
+
* A single-key object is a choice or a token, not a row, and is not reported.
|
|
10
|
+
*
|
|
11
|
+
* Spec §11 SD1: components keep declaring shapes only, so a component descriptor's defaults and a template's
|
|
12
|
+
* properties must not carry row literals; demo rows belong to sample-data packs.
|
|
13
|
+
*
|
|
14
|
+
* @param value The value to inspect.
|
|
15
|
+
* @param path The path prefix of the value, empty for the root.
|
|
16
|
+
*/
|
|
17
|
+
export function findSampleRowLiterals(value: unknown, path = ''): string[] {
|
|
18
|
+
const found: string[] = [];
|
|
19
|
+
if (Array.isArray(value)) {
|
|
20
|
+
if (value.some(isRow)) found.push(path === '' ? '(root)' : path);
|
|
21
|
+
value.forEach((item, index) => found.push(...findSampleRowLiterals(item, `${path}[${index}]`)));
|
|
22
|
+
} else if (typeof value === 'object' && value !== null) {
|
|
23
|
+
for (const [key, child] of Object.entries(value)) {
|
|
24
|
+
found.push(...findSampleRowLiterals(child, path === '' ? key : `${path}.${key}`));
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return found;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Finds the sample-row literals in the defaults of component descriptors, as `<component>: <property path>`
|
|
33
|
+
* entries (the property path is followed by the location inside the default).
|
|
34
|
+
*
|
|
35
|
+
* @param descriptors The descriptors to inspect.
|
|
36
|
+
*/
|
|
37
|
+
export function findDescriptorSampleRowLiterals(descriptors: readonly ComponentDescriptor[]): string[] {
|
|
38
|
+
const found: string[] = [];
|
|
39
|
+
for (const descriptor of descriptors) {
|
|
40
|
+
for (const property of descriptor.properties ?? []) {
|
|
41
|
+
for (const location of findSampleRowLiterals(property.default)) {
|
|
42
|
+
const suffix = location === '(root)' ? '' : (location.startsWith('[') ? location : `.${location}`);
|
|
43
|
+
found.push(`${descriptor.component}: ${property.path}${suffix}`);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return found;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function isRow(item: unknown): boolean {
|
|
52
|
+
return typeof item === 'object' && item !== null && !Array.isArray(item) && Object.keys(item).length >= 2;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** The part of a scene element the scan reads: its properties and the child elements in its slots. */
|
|
56
|
+
interface ScannedElement {
|
|
57
|
+
componentName?: string;
|
|
58
|
+
properties?: Record<string, unknown>;
|
|
59
|
+
slots?: Record<string, ScannedElement[]>;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Finds the sample-row literals in the element properties of a template's content, as
|
|
64
|
+
* `<componentName>.<property>` entries (with the location inside the value when it is nested). Only property
|
|
65
|
+
* values are scanned: the element tree itself is made of objects and is not data.
|
|
66
|
+
*
|
|
67
|
+
* @param content A template's content, keyed by slot name.
|
|
68
|
+
*/
|
|
69
|
+
export function findTemplateSampleRowLiterals(content: Record<string, ScannedElement[]> | undefined): string[] {
|
|
70
|
+
const found: string[] = [];
|
|
71
|
+
const visit = (elements: ScannedElement[] | undefined) => {
|
|
72
|
+
for (const element of elements ?? []) {
|
|
73
|
+
for (const [name, value] of Object.entries(element.properties ?? {})) {
|
|
74
|
+
for (const location of findSampleRowLiterals(value)) {
|
|
75
|
+
const suffix = location === '(root)' ? '' : (location.startsWith('[') ? location : `.${location}`);
|
|
76
|
+
found.push(`${element.componentName ?? '(unnamed)'}.${name}${suffix}`);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
Object.values(element.slots ?? {}).forEach(visit);
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
Object.values(content ?? {}).forEach(visit);
|
|
85
|
+
return found;
|
|
86
|
+
}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { SampleFieldKind, SampleShape, SampleShapeField } from '@cratis/scene.model';
|
|
5
|
+
import { inferSampleFieldKind } from './inferSampleFieldKind';
|
|
6
|
+
import { SampleLocale, sampleLocaleFor } from './sampleLocales';
|
|
7
|
+
import { SampleRandom, rowSeed } from './SampleRandom';
|
|
8
|
+
|
|
9
|
+
/** The seed used when none is given, so previews are stable without any configuration. */
|
|
10
|
+
export const defaultSampleSeed = 1;
|
|
11
|
+
|
|
12
|
+
/** How many rows are generated when no count is given. */
|
|
13
|
+
export const defaultSampleRowCount = 5;
|
|
14
|
+
|
|
15
|
+
/** The most rows one call generates. */
|
|
16
|
+
export const maximumSampleRowCount = 1000;
|
|
17
|
+
|
|
18
|
+
/** Days since 1970-01-01 of 2024-01-01, the anchor generated dates count forward from. */
|
|
19
|
+
const anchorDay = 19723;
|
|
20
|
+
|
|
21
|
+
/** Generated dates fall within this many days after the anchor. */
|
|
22
|
+
const dateSpanDays = 731;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Generates `rowCount` rows of a shape, deterministically for a seed: the same shape, seed, row count and
|
|
26
|
+
* locale yield the same rows (byte-identical once serialized) in the C# and TypeScript engines.
|
|
27
|
+
*
|
|
28
|
+
* Each row has its own generator (see {@link rowSeed}), so row `i` is the same however many rows are asked
|
|
29
|
+
* for. Within a row the fields are generated in declaration order. A nullable field first draws whether it is
|
|
30
|
+
* null (one in five is) and then always draws its value, so the sequence never depends on nullability
|
|
31
|
+
* outcomes. Values follow the field's kind and locale; enum fields only ever produce one of their values,
|
|
32
|
+
* dates stay within 2024-2025 and amounts have two decimals.
|
|
33
|
+
*
|
|
34
|
+
* @param shape The shape to generate rows for.
|
|
35
|
+
* @param seed The seed; defaults to {@link defaultSampleSeed}.
|
|
36
|
+
* @param rowCount The number of rows; defaults to {@link defaultSampleRowCount}, clamped to `[0, 1000]`.
|
|
37
|
+
* @param locale The BCP 47 locale tag selecting names, cities and formats; unknown tags use the default pool.
|
|
38
|
+
*/
|
|
39
|
+
export function generateSample(shape: SampleShape, seed?: number, rowCount?: number, locale?: string): Record<string, unknown>[] {
|
|
40
|
+
const count = Math.min(Math.max(Math.trunc(rowCount ?? defaultSampleRowCount), 0), maximumSampleRowCount);
|
|
41
|
+
const effectiveSeed = (seed ?? defaultSampleSeed) >>> 0;
|
|
42
|
+
const pool = sampleLocaleFor(locale);
|
|
43
|
+
const rows: Record<string, unknown>[] = [];
|
|
44
|
+
|
|
45
|
+
for (let index = 0; index < count; index++) {
|
|
46
|
+
const random = new SampleRandom(rowSeed(effectiveSeed, shape.name, index));
|
|
47
|
+
const row: Record<string, unknown> = {};
|
|
48
|
+
for (const field of shape.fields) {
|
|
49
|
+
const isNull = field.nullable === true && random.nextInt(5) === 0;
|
|
50
|
+
const value = generateValue(field, inferSampleFieldKind(field), random, pool);
|
|
51
|
+
row[field.name] = isNull ? null : value;
|
|
52
|
+
}
|
|
53
|
+
rows.push(row);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return rows;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function generateValue(field: SampleShapeField, kind: SampleFieldKind, random: SampleRandom, pool: SampleLocale): unknown {
|
|
60
|
+
switch (kind) {
|
|
61
|
+
case SampleFieldKind.Id:
|
|
62
|
+
return generateId(random);
|
|
63
|
+
case SampleFieldKind.Name:
|
|
64
|
+
return `${random.pick(pool.firstNames)} ${random.pick(pool.lastNames)}`;
|
|
65
|
+
case SampleFieldKind.Company:
|
|
66
|
+
return random.pick(pool.companies);
|
|
67
|
+
case SampleFieldKind.Email: {
|
|
68
|
+
const first = random.pick(pool.firstNames);
|
|
69
|
+
const last = random.pick(pool.lastNames);
|
|
70
|
+
return `${slug(first)}.${slug(last)}@${random.pick(pool.domains)}`;
|
|
71
|
+
}
|
|
72
|
+
case SampleFieldKind.Phone:
|
|
73
|
+
return [...pool.phoneTemplate].map(character => (character === '#' ? String(random.nextInt(10)) : character)).join('');
|
|
74
|
+
case SampleFieldKind.City:
|
|
75
|
+
return random.pick(pool.cities);
|
|
76
|
+
case SampleFieldKind.Text: {
|
|
77
|
+
const words = Array.from({ length: 3 + random.nextInt(4) }, () => random.pick(pool.words));
|
|
78
|
+
return capitalize(words.join(' '));
|
|
79
|
+
}
|
|
80
|
+
case SampleFieldKind.WholeNumber: {
|
|
81
|
+
const [low, high] = range(field.min, field.max, 0, 1000, Math.trunc);
|
|
82
|
+
return low + random.nextInt(high - low + 1);
|
|
83
|
+
}
|
|
84
|
+
case SampleFieldKind.Amount: {
|
|
85
|
+
const [low, high] = range(field.min, field.max, 10, 5000, value => Math.floor(value * 100 + 0.5));
|
|
86
|
+
return (low + random.nextInt(high - low + 1)) / 100;
|
|
87
|
+
}
|
|
88
|
+
case SampleFieldKind.Boolean:
|
|
89
|
+
return random.nextInt(2) === 1;
|
|
90
|
+
case SampleFieldKind.Date:
|
|
91
|
+
return formatDay(anchorDay + random.nextInt(dateSpanDays));
|
|
92
|
+
case SampleFieldKind.DateTime: {
|
|
93
|
+
const day = anchorDay + random.nextInt(dateSpanDays);
|
|
94
|
+
const seconds = random.nextInt(86400);
|
|
95
|
+
return `${formatDay(day)}T${pad(Math.floor(seconds / 3600))}:${pad(Math.floor(seconds / 60) % 60)}:${pad(seconds % 60)}Z`;
|
|
96
|
+
}
|
|
97
|
+
case SampleFieldKind.Enum:
|
|
98
|
+
return field.enumValues !== undefined && field.enumValues !== null && field.enumValues.length > 0 ? random.pick(field.enumValues) : null;
|
|
99
|
+
default:
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function range(min: number | undefined, max: number | undefined, defaultMin: number, defaultMax: number, convert: (value: number) => number): [number, number] {
|
|
105
|
+
const low = convert(min ?? defaultMin);
|
|
106
|
+
const high = convert(max ?? defaultMax);
|
|
107
|
+
return high < low ? [high, low] : [low, high];
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function generateId(random: SampleRandom): string {
|
|
111
|
+
const a = random.next();
|
|
112
|
+
const b = ((random.next() & 0xFFFF0FFF) | 0x00004000) >>> 0;
|
|
113
|
+
const c = ((random.next() & 0x3FFFFFFF) | 0x80000000) >>> 0;
|
|
114
|
+
const d = random.next();
|
|
115
|
+
return `${hex(a, 8)}-${hex(b >>> 16, 4)}-${hex(b & 0xFFFF, 4)}-${hex(c >>> 16, 4)}-${hex(c & 0xFFFF, 4)}${hex(d, 8)}`;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const transliterations = new Map<string, string>([['æ', 'ae'], ['ø', 'o'], ['å', 'a'], ['ä', 'a'], ['ö', 'o'], ['ü', 'u'], ['ß', 'ss'], ['é', 'e']]);
|
|
119
|
+
|
|
120
|
+
function slug(text: string): string {
|
|
121
|
+
let result = '';
|
|
122
|
+
for (const character of text.toLowerCase()) {
|
|
123
|
+
const mapped = transliterations.get(character) ?? character;
|
|
124
|
+
for (const part of mapped) {
|
|
125
|
+
if ((part >= 'a' && part <= 'z') || (part >= '0' && part <= '9')) result += part;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return result;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function capitalize(text: string): string {
|
|
132
|
+
const first = text.charAt(0);
|
|
133
|
+
return first >= 'a' && first <= 'z' ? first.toUpperCase() + text.slice(1) : text;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function hex(value: number, digits: number): string {
|
|
137
|
+
return (value >>> 0).toString(16).padStart(digits, '0');
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function pad(value: number): string {
|
|
141
|
+
return value < 10 ? `0${value}` : String(value);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** Formats days since 1970-01-01 as `yyyy-MM-dd` (Howard Hinnant's civil-from-days). */
|
|
145
|
+
function formatDay(days: number): string {
|
|
146
|
+
const shifted = days + 719468;
|
|
147
|
+
const era = Math.floor(shifted / 146097);
|
|
148
|
+
const dayOfEra = shifted - era * 146097;
|
|
149
|
+
const yearOfEra = Math.floor((dayOfEra - Math.floor(dayOfEra / 1460) + Math.floor(dayOfEra / 36524) - Math.floor(dayOfEra / 146096)) / 365);
|
|
150
|
+
const dayOfYear = dayOfEra - (365 * yearOfEra + Math.floor(yearOfEra / 4) - Math.floor(yearOfEra / 100));
|
|
151
|
+
const monthPrime = Math.floor((5 * dayOfYear + 2) / 153);
|
|
152
|
+
const day = dayOfYear - Math.floor((153 * monthPrime + 2) / 5) + 1;
|
|
153
|
+
const month = monthPrime < 10 ? monthPrime + 3 : monthPrime - 9;
|
|
154
|
+
const year = yearOfEra + era * 400 + (month <= 2 ? 1 : 0);
|
|
155
|
+
return `${String(year).padStart(4, '0')}-${pad(month)}-${pad(day)}`;
|
|
156
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
export * from './SampleRandom';
|
|
5
|
+
export * from './sampleLocales';
|
|
6
|
+
export * from './inferSampleFieldKind';
|
|
7
|
+
export * from './generateSample';
|
|
8
|
+
export * from './SampleDataResolution';
|
|
9
|
+
export * from './findSampleDataSet';
|
|
10
|
+
export * from './resolveSampleData';
|
|
11
|
+
export * from './sampleDataRules';
|
|
12
|
+
export * from './validateSampleDataPack';
|
|
13
|
+
export * from './validateSampleDataSelection';
|
|
14
|
+
export * from './SampleDataPackSource';
|
|
15
|
+
export * from './validateSampleDataPackages';
|
|
16
|
+
export * from './describeSampleDataCatalog';
|
|
17
|
+
export * from './findSampleRowLiterals';
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { SampleFieldKind, SampleShapeField } from '@cratis/scene.model';
|
|
5
|
+
|
|
6
|
+
const typeKinds = new Map<string, SampleFieldKind>([
|
|
7
|
+
['guid', SampleFieldKind.Id],
|
|
8
|
+
['uuid', SampleFieldKind.Id],
|
|
9
|
+
['int', SampleFieldKind.WholeNumber],
|
|
10
|
+
['int16', SampleFieldKind.WholeNumber],
|
|
11
|
+
['int32', SampleFieldKind.WholeNumber],
|
|
12
|
+
['int64', SampleFieldKind.WholeNumber],
|
|
13
|
+
['short', SampleFieldKind.WholeNumber],
|
|
14
|
+
['long', SampleFieldKind.WholeNumber],
|
|
15
|
+
['integer', SampleFieldKind.WholeNumber],
|
|
16
|
+
['number', SampleFieldKind.WholeNumber],
|
|
17
|
+
['decimal', SampleFieldKind.Amount],
|
|
18
|
+
['double', SampleFieldKind.Amount],
|
|
19
|
+
['float', SampleFieldKind.Amount],
|
|
20
|
+
['single', SampleFieldKind.Amount],
|
|
21
|
+
['money', SampleFieldKind.Amount],
|
|
22
|
+
['currency', SampleFieldKind.Amount],
|
|
23
|
+
['bool', SampleFieldKind.Boolean],
|
|
24
|
+
['boolean', SampleFieldKind.Boolean],
|
|
25
|
+
['date', SampleFieldKind.Date],
|
|
26
|
+
['dateonly', SampleFieldKind.Date],
|
|
27
|
+
['datetime', SampleFieldKind.DateTime],
|
|
28
|
+
['datetimeoffset', SampleFieldKind.DateTime],
|
|
29
|
+
['timestamp', SampleFieldKind.DateTime],
|
|
30
|
+
]);
|
|
31
|
+
|
|
32
|
+
const nameKinds: [string[], SampleFieldKind][] = [
|
|
33
|
+
[['email'], SampleFieldKind.Email],
|
|
34
|
+
[['phone', 'mobile'], SampleFieldKind.Phone],
|
|
35
|
+
[['company', 'organization', 'organisation'], SampleFieldKind.Company],
|
|
36
|
+
[['city', 'town'], SampleFieldKind.City],
|
|
37
|
+
[['product', 'item', 'title', 'subject', 'description'], SampleFieldKind.Text],
|
|
38
|
+
[['name', 'customer', 'assignee', 'owner', 'author', 'contact', 'manager', 'employee'], SampleFieldKind.Name],
|
|
39
|
+
];
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The kind of value to generate for a field. An explicit `kind` wins; otherwise a field with `enumValues` is
|
|
43
|
+
* an enum, a recognised `type` decides, and failing that the field's name does (an `Id` suffix, `email`,
|
|
44
|
+
* `phone`, `company`, `city`, person-like names), with free text as the fallback. A trailing `?` on the type
|
|
45
|
+
* is ignored and type names compare case-insensitively.
|
|
46
|
+
*/
|
|
47
|
+
export function inferSampleFieldKind(field: SampleShapeField): SampleFieldKind {
|
|
48
|
+
if (field.kind !== undefined && field.kind !== null) return field.kind as SampleFieldKind;
|
|
49
|
+
if (field.enumValues !== undefined && field.enumValues !== null && field.enumValues.length > 0) return SampleFieldKind.Enum;
|
|
50
|
+
|
|
51
|
+
let type = (field.type ?? '').trim().toLowerCase();
|
|
52
|
+
if (type.endsWith('?')) type = type.slice(0, -1);
|
|
53
|
+
const byType = typeKinds.get(type);
|
|
54
|
+
if (byType !== undefined) return byType;
|
|
55
|
+
|
|
56
|
+
const name = field.name ?? '';
|
|
57
|
+
const lower = name.toLowerCase();
|
|
58
|
+
if (lower === 'id' || (name.length > 2 && name.endsWith('Id'))) return SampleFieldKind.Id;
|
|
59
|
+
for (const [fragments, kind] of nameKinds) {
|
|
60
|
+
if (fragments.some(fragment => lower.includes(fragment))) return kind;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return SampleFieldKind.Text;
|
|
64
|
+
}
|