@dxos/echo-generator 0.3.3-main.72cb938

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/LICENSE ADDED
@@ -0,0 +1,8 @@
1
+ MIT License
2
+ Copyright (c) 2022 DXOS
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5
+
6
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7
+
8
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,21 @@
1
+ # @dxos/echo-generator
2
+
3
+ Data generator.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ pnpm i @dxos/echo-generator
9
+ ```
10
+
11
+ ## DXOS Resources
12
+
13
+ - [Website](https://dxos.org)
14
+ - [Developer Documentation](https://docs.dxos.org)
15
+ - Talk to us on [Discord](https://discord.gg/eXVfryv3sW)
16
+
17
+ ## Contributions
18
+
19
+ Your ideas, issues, and code are most welcome. Please take a look at our [community code of conduct](https://github.com/dxos/dxos/blob/main/CODE_OF_CONDUCT.md), the [issue guide](https://github.com/dxos/dxos/blob/main/CONTRIBUTING.md#submitting-issues), and the [PR contribution guide](https://github.com/dxos/dxos/blob/main/CONTRIBUTING.md#submitting-prs).
20
+
21
+ License: [MIT](./LICENSE) Copyright 2022 © DXOS
@@ -0,0 +1,109 @@
1
+ import "@dxos/node-std/globals";
2
+
3
+ // packages/core/echo/echo-generator/src/generator.ts
4
+ import { faker } from "@faker-js/faker";
5
+ import { Expando, Schema as SchemaType } from "@dxos/client/echo";
6
+ var range = (fn, length) => Array.from({
7
+ length
8
+ }).map((_, i) => fn(i)).filter(Boolean);
9
+ var createFactory = ({ createSchema, createData }) => {
10
+ const schema = createSchema?.();
11
+ return {
12
+ schema,
13
+ createObject: () => new Expando(createData(), {
14
+ schema
15
+ })
16
+ };
17
+ };
18
+ var Status = [
19
+ "pending",
20
+ "active",
21
+ "done"
22
+ ];
23
+ var Priority = [
24
+ 1,
25
+ 2,
26
+ 3,
27
+ 4,
28
+ 5
29
+ ];
30
+ var defaultGenerators = {
31
+ document: {
32
+ createData: () => ({
33
+ title: faker.lorem.sentence(3),
34
+ body: faker.lorem.sentences({
35
+ min: 1,
36
+ max: faker.number.int({
37
+ min: 1,
38
+ max: 3
39
+ })
40
+ })
41
+ })
42
+ },
43
+ // TODO(burdon): Configure images.
44
+ image: {
45
+ createData: () => ({
46
+ title: faker.lorem.sentence(3),
47
+ body: faker.datatype.boolean() ? faker.lorem.sentences() : void 0
48
+ })
49
+ },
50
+ project: {
51
+ createSchema: () => new SchemaType({
52
+ props: [
53
+ {
54
+ id: "title",
55
+ type: SchemaType.PropType.STRING
56
+ },
57
+ {
58
+ id: "repo",
59
+ type: SchemaType.PropType.STRING
60
+ },
61
+ {
62
+ id: "status",
63
+ type: SchemaType.PropType.STRING
64
+ },
65
+ {
66
+ id: "priority",
67
+ type: SchemaType.PropType.NUMBER
68
+ }
69
+ ]
70
+ }),
71
+ createData: () => ({
72
+ title: faker.commerce.productName(),
73
+ repo: faker.datatype.boolean({
74
+ probability: 0.3
75
+ }) ? faker.internet.url() : void 0,
76
+ status: faker.helpers.arrayElement(Status),
77
+ priority: faker.helpers.arrayElement(Priority)
78
+ })
79
+ }
80
+ };
81
+ var TestObjectGenerator = class {
82
+ constructor({ types, factories } = {}) {
83
+ this.factories = factories ?? (types ?? Object.keys(defaultGenerators)).reduce((acc, type) => {
84
+ acc[type] = createFactory(defaultGenerators[type]);
85
+ return acc;
86
+ }, {});
87
+ }
88
+ get schema() {
89
+ return Object.values(this.factories).map((f) => f.schema);
90
+ }
91
+ createObject({ types } = {}) {
92
+ const type = faker.helpers.arrayElement(types ?? Object.keys(this.factories));
93
+ const factory = this.factories[type];
94
+ return factory?.createObject();
95
+ }
96
+ createObjects({ types, length }) {
97
+ return range(() => this.createObject({
98
+ types
99
+ }), length);
100
+ }
101
+ };
102
+ export {
103
+ Priority,
104
+ Status,
105
+ TestObjectGenerator,
106
+ defaultGenerators,
107
+ range
108
+ };
109
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../src/generator.ts"],
4
+ "sourcesContent": ["//\n// Copyright 2023 DXOS.org\n//\n\n// TODO(burdon): Reconcile with @dxos/plugin-debug, @dxos/react-ui/testing.\n\n// TODO(burdon): Bug when adding stale objects to space (e.g., static objects already added in previous story invocation).\n\nimport { faker } from '@faker-js/faker';\n\nimport { Expando, type TypedObject, Schema as SchemaType } from '@dxos/client/echo';\n\n// TODO(burdon): Util.\nexport const range = <T>(fn: (i: number) => T | undefined, length: number): T[] =>\n Array.from({ length })\n .map((_, i) => fn(i))\n .filter(Boolean) as T[];\n\n// TODO(burdon): Commit to using ECHO to generate all test data? Or convert from raw data?\nexport type TestItem = { id: string; type: string } & Record<string, any>;\n\ntype ObjectDataGenerator = {\n createSchema?: () => SchemaType;\n createData: () => any;\n};\n\ntype ObjectFactory<T extends TypedObject> = {\n schema?: SchemaType; // TODO(burdon): Support both typed and expando schema.\n createObject: () => T;\n};\n\ntype ObjectFactoryMap = { [type: string]: ObjectFactory<any> };\n\nconst createFactory = ({ createSchema, createData }: ObjectDataGenerator) => {\n const schema = createSchema?.();\n return {\n schema,\n createObject: () => new Expando(createData(), { schema }),\n };\n};\n\n// TODO(burdon): Handle restricted values.\nexport const Status = ['pending', 'active', 'done'];\nexport const Priority = [1, 2, 3, 4, 5];\n\nexport const defaultGenerators: { [type: string]: ObjectDataGenerator } = {\n document: {\n createData: () => ({\n title: faker.lorem.sentence(3),\n body: faker.lorem.sentences({ min: 1, max: faker.number.int({ min: 1, max: 3 }) }),\n }),\n },\n\n // TODO(burdon): Configure images.\n image: {\n createData: () => ({\n title: faker.lorem.sentence(3),\n body: faker.datatype.boolean() ? faker.lorem.sentences() : undefined,\n }),\n },\n\n project: {\n createSchema: () =>\n new SchemaType({\n props: [\n {\n id: 'title',\n type: SchemaType.PropType.STRING,\n },\n {\n id: 'repo',\n type: SchemaType.PropType.STRING,\n },\n {\n id: 'status',\n type: SchemaType.PropType.STRING,\n },\n {\n id: 'priority',\n type: SchemaType.PropType.NUMBER,\n },\n ],\n }),\n createData: () => ({\n title: faker.commerce.productName(),\n repo: faker.datatype.boolean({ probability: 0.3 }) ? faker.internet.url() : undefined,\n status: faker.helpers.arrayElement(Status),\n priority: faker.helpers.arrayElement(Priority),\n }),\n },\n};\n\n/**\n * Typed object generator.\n */\nexport class TestObjectGenerator {\n public readonly factories: ObjectFactoryMap;\n\n constructor({ types, factories }: { types?: string[]; factories?: ObjectFactoryMap } = {}) {\n this.factories =\n factories ??\n (types ?? Object.keys(defaultGenerators)).reduce<ObjectFactoryMap>((acc, type) => {\n acc[type] = createFactory(defaultGenerators[type]);\n return acc;\n }, {});\n }\n\n get schema(): SchemaType[] {\n return Object.values(this.factories).map((f) => f.schema!);\n }\n\n createObject({ types }: { types?: string[] } = {}) {\n const type = faker.helpers.arrayElement(types ?? Object.keys(this.factories));\n const factory = this.factories[type];\n return factory?.createObject();\n }\n\n createObjects({ types, length }: { types?: string[]; length: number }) {\n return range(() => this.createObject({ types }), length);\n }\n}\n"],
5
+ "mappings": ";;;AAQA,SAASA,aAAa;AAEtB,SA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I,CAAA,IAAKxB,MAAMyB,SAASC,IAAG,IAAKd;MAC5Ee,QAAQ3B,MAAM4B,QAAQC,aAAalC,MAAAA;MACnCmC,UAAU9B,MAAM4B,QAAQC,aAAajC,QAAAA;IACvC;EACF;AACF;AAKO,IAAMmC,sBAAN,MAAMA;EAGXC,YAAY,EAAEC,OAAOC,UAAS,IAAyD,CAAC,GAAG;AACzF,SAAKA,YACHA,cACCD,SAASE,OAAOC,KAAKvC,iBAAAA,GAAoBwC,OAAyB,CAACC,KAAKrB,SAAAA;AACvEqB,UAAIrB,IAAAA,IAAQ5B,cAAcQ,kBAAkBoB,IAAAA,CAAK;AACjD,aAAOqB;IACT,GAAG,CAAC,CAAA;EACR;EAEA,IAAI9C,SAAuB;AACzB,WAAO2C,OAAOI,OAAO,KAAKL,SAAS,EAAElD,IAAI,CAACwD,MAAMA,EAAEhD,MAAM;EAC1D;EAEAC,aAAa,EAAEwC,MAAK,IAA2B,CAAC,GAAG;AACjD,UAAMhB,OAAOjB,MAAM4B,QAAQC,aAAaI,SAASE,OAAOC,KAAK,KAAKF,SAAS,CAAA;AAC3E,UAAMO,UAAU,KAAKP,UAAUjB,IAAAA;AAC/B,WAAOwB,SAAShD,aAAAA;EAClB;EAEAiD,cAAc,EAAET,OAAOpD,OAAM,GAA0C;AACrE,WAAOF,MAAM,MAAM,KAAKc,aAAa;MAAEwC;IAAM,CAAA,GAAIpD,MAAAA;EACnD;AACF;",
6
+ "names": ["faker", "Expando", "Schema", "SchemaType", "range", "fn", "length", "Array", "from", "map", "_", "i", "filter", "Boolean", "createFactory", "createSchema", "createData", "schema", "createObject", "Expando", "Status", "Priority", "defaultGenerators", "document", "title", "faker", "lorem", "sentence", "body", "sentences", "min", "max", "number", "int", "image", "datatype", "boolean", "undefined", "project", "SchemaType", "props", "id", "type", "PropType", "STRING", "NUMBER", "commerce", "productName", "repo", "probability", "internet", "url", "status", "helpers", "arrayElement", "priority", "TestObjectGenerator", "constructor", "types", "factories", "Object", "keys", "reduce", "acc", "values", "f", "factory", "createObjects"]
7
+ }
@@ -0,0 +1 @@
1
+ {"inputs":{"packages/core/echo/echo-generator/src/generator.ts":{"bytes":12300,"imports":[{"path":"@faker-js/faker","kind":"import-statement","external":true},{"path":"@dxos/client/echo","kind":"import-statement","external":true}],"format":"esm"},"packages/core/echo/echo-generator/src/index.ts":{"bytes":478,"imports":[{"path":"packages/core/echo/echo-generator/src/generator.ts","kind":"import-statement","original":"./generator"}],"format":"esm"}},"outputs":{"packages/core/echo/echo-generator/dist/lib/browser/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":6334},"packages/core/echo/echo-generator/dist/lib/browser/index.mjs":{"imports":[{"path":"@faker-js/faker","kind":"import-statement","external":true},{"path":"@dxos/client/echo","kind":"import-statement","external":true}],"exports":["Priority","Status","TestObjectGenerator","defaultGenerators","range"],"entryPoint":"packages/core/echo/echo-generator/src/index.ts","inputs":{"packages/core/echo/echo-generator/src/generator.ts":{"bytesInOutput":2329},"packages/core/echo/echo-generator/src/index.ts":{"bytesInOutput":0}},"bytes":2538}}}
@@ -0,0 +1,138 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // packages/core/echo/echo-generator/src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ Priority: () => Priority,
24
+ Status: () => Status,
25
+ TestObjectGenerator: () => TestObjectGenerator,
26
+ defaultGenerators: () => defaultGenerators,
27
+ range: () => range
28
+ });
29
+ module.exports = __toCommonJS(src_exports);
30
+
31
+ // packages/core/echo/echo-generator/src/generator.ts
32
+ var import_faker = require("@faker-js/faker");
33
+ var import_echo = require("@dxos/client/echo");
34
+ var range = (fn, length) => Array.from({
35
+ length
36
+ }).map((_, i) => fn(i)).filter(Boolean);
37
+ var createFactory = ({ createSchema, createData }) => {
38
+ const schema = createSchema?.();
39
+ return {
40
+ schema,
41
+ createObject: () => new import_echo.Expando(createData(), {
42
+ schema
43
+ })
44
+ };
45
+ };
46
+ var Status = [
47
+ "pending",
48
+ "active",
49
+ "done"
50
+ ];
51
+ var Priority = [
52
+ 1,
53
+ 2,
54
+ 3,
55
+ 4,
56
+ 5
57
+ ];
58
+ var defaultGenerators = {
59
+ document: {
60
+ createData: () => ({
61
+ title: import_faker.faker.lorem.sentence(3),
62
+ body: import_faker.faker.lorem.sentences({
63
+ min: 1,
64
+ max: import_faker.faker.number.int({
65
+ min: 1,
66
+ max: 3
67
+ })
68
+ })
69
+ })
70
+ },
71
+ // TODO(burdon): Configure images.
72
+ image: {
73
+ createData: () => ({
74
+ title: import_faker.faker.lorem.sentence(3),
75
+ body: import_faker.faker.datatype.boolean() ? import_faker.faker.lorem.sentences() : void 0
76
+ })
77
+ },
78
+ project: {
79
+ createSchema: () => new import_echo.Schema({
80
+ props: [
81
+ {
82
+ id: "title",
83
+ type: import_echo.Schema.PropType.STRING
84
+ },
85
+ {
86
+ id: "repo",
87
+ type: import_echo.Schema.PropType.STRING
88
+ },
89
+ {
90
+ id: "status",
91
+ type: import_echo.Schema.PropType.STRING
92
+ },
93
+ {
94
+ id: "priority",
95
+ type: import_echo.Schema.PropType.NUMBER
96
+ }
97
+ ]
98
+ }),
99
+ createData: () => ({
100
+ title: import_faker.faker.commerce.productName(),
101
+ repo: import_faker.faker.datatype.boolean({
102
+ probability: 0.3
103
+ }) ? import_faker.faker.internet.url() : void 0,
104
+ status: import_faker.faker.helpers.arrayElement(Status),
105
+ priority: import_faker.faker.helpers.arrayElement(Priority)
106
+ })
107
+ }
108
+ };
109
+ var TestObjectGenerator = class {
110
+ constructor({ types, factories } = {}) {
111
+ this.factories = factories ?? (types ?? Object.keys(defaultGenerators)).reduce((acc, type) => {
112
+ acc[type] = createFactory(defaultGenerators[type]);
113
+ return acc;
114
+ }, {});
115
+ }
116
+ get schema() {
117
+ return Object.values(this.factories).map((f) => f.schema);
118
+ }
119
+ createObject({ types } = {}) {
120
+ const type = import_faker.faker.helpers.arrayElement(types ?? Object.keys(this.factories));
121
+ const factory = this.factories[type];
122
+ return factory?.createObject();
123
+ }
124
+ createObjects({ types, length }) {
125
+ return range(() => this.createObject({
126
+ types
127
+ }), length);
128
+ }
129
+ };
130
+ // Annotate the CommonJS export names for ESM import in node:
131
+ 0 && (module.exports = {
132
+ Priority,
133
+ Status,
134
+ TestObjectGenerator,
135
+ defaultGenerators,
136
+ range
137
+ });
138
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../src/index.ts", "../../../src/generator.ts"],
4
+ "sourcesContent": ["//\n// Copyright 2023 DXOS.org\n//\n\nexport * from './generator';\n", "//\n// Copyright 2023 DXOS.org\n//\n\n// TODO(burdon): Reconcile with @dxos/plugin-debug, @dxos/react-ui/testing.\n\n// TODO(burdon): Bug when adding stale objects to space (e.g., static objects already added in previous story invocation).\n\nimport { faker } from '@faker-js/faker';\n\nimport { Expando, type TypedObject, Schema as SchemaType } from '@dxos/client/echo';\n\n// TODO(burdon): Util.\nexport const range = <T>(fn: (i: number) => T | undefined, length: number): T[] =>\n Array.from({ length })\n .map((_, i) => fn(i))\n .filter(Boolean) as T[];\n\n// TODO(burdon): Commit to using ECHO to generate all test data? Or convert from raw data?\nexport type TestItem = { id: string; type: string } & Record<string, any>;\n\ntype ObjectDataGenerator = {\n createSchema?: () => SchemaType;\n createData: () => any;\n};\n\ntype ObjectFactory<T extends TypedObject> = {\n schema?: SchemaType; // TODO(burdon): Support both typed and expando schema.\n createObject: () => T;\n};\n\ntype ObjectFactoryMap = { [type: string]: ObjectFactory<any> };\n\nconst createFactory = ({ createSchema, createData }: ObjectDataGenerator) => {\n const schema = createSchema?.();\n return {\n schema,\n createObject: () => new Expando(createData(), { schema }),\n };\n};\n\n// TODO(burdon): Handle restricted values.\nexport const Status = ['pending', 'active', 'done'];\nexport const Priority = [1, 2, 3, 4, 5];\n\nexport const defaultGenerators: { [type: string]: ObjectDataGenerator } = {\n document: {\n createData: () => ({\n title: faker.lorem.sentence(3),\n body: faker.lorem.sentences({ min: 1, max: faker.number.int({ min: 1, max: 3 }) }),\n }),\n },\n\n // TODO(burdon): Configure images.\n image: {\n createData: () => ({\n title: faker.lorem.sentence(3),\n body: faker.datatype.boolean() ? faker.lorem.sentences() : undefined,\n }),\n },\n\n project: {\n createSchema: () =>\n new SchemaType({\n props: [\n {\n id: 'title',\n type: SchemaType.PropType.STRING,\n },\n {\n id: 'repo',\n type: SchemaType.PropType.STRING,\n },\n {\n id: 'status',\n type: SchemaType.PropType.STRING,\n },\n {\n id: 'priority',\n type: SchemaType.PropType.NUMBER,\n },\n ],\n }),\n createData: () => ({\n title: faker.commerce.productName(),\n repo: faker.datatype.boolean({ probability: 0.3 }) ? faker.internet.url() : undefined,\n status: faker.helpers.arrayElement(Status),\n priority: faker.helpers.arrayElement(Priority),\n }),\n },\n};\n\n/**\n * Typed object generator.\n */\nexport class TestObjectGenerator {\n public readonly factories: ObjectFactoryMap;\n\n constructor({ types, factories }: { types?: string[]; factories?: ObjectFactoryMap } = {}) {\n this.factories =\n factories ??\n (types ?? Object.keys(defaultGenerators)).reduce<ObjectFactoryMap>((acc, type) => {\n acc[type] = createFactory(defaultGenerators[type]);\n return acc;\n }, {});\n }\n\n get schema(): SchemaType[] {\n return Object.values(this.factories).map((f) => f.schema!);\n }\n\n createObject({ types }: { types?: string[] } = {}) {\n const type = faker.helpers.arrayElement(types ?? Object.keys(this.factories));\n const factory = this.factories[type];\n return factory?.createObject();\n }\n\n createObjects({ types, length }: { types?: string[]; length: number }) {\n return range(() => this.createObject({ types }), length);\n }\n}\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;;ACQA,mBAAsB;AAEtB,kBAAgE;AAGzD,IAAMA,QAAQ,CAAIC,IAAkCC,WACzDC,MAAMC,KAAK;EAAEF;AAAO,CAAA,EACjBG,IAAI,CAACC,GAAGC,MAAMN,GAAGM,CAAAA,CAAAA,EACjBC,OAAOC,OAAAA;AAiBZ,IAAMC,gBAAgB,CAAC,EAAEC,cAAcC,WAAU,MAAuB;AACtE,QAAMC,SAASF,eAAAA;AACf,SAAO;IACLE;IACAC,cAAc,MAAM,IAAIC,oBAAQH,WAAAA,GAAc;MAAEC;IAAO,CAAA;EACzD;AACF;AAGO,IAAMG,SAAS;EAAC;EAAW;EAAU;;AACrC,IAAMC,WAAW;EAAC;EAAG;EAAG;EAAG;EAAG;;AAE9B,IAAMC,oBAA6D;EACxEC,UAAU;IACRP,YAAY,OAAO;MACjBQ,OAAOC,mBAAMC,MAAMC,SAAS,CAAA;MAC5BC,MAAMH,mBAAMC,MAAMG,UAAU;QAAEC,KAAK;QAAGC,KAAKN,mBAAMO,OAAOC,IAAI;UAAEH,KAAK;UAAGC,KAAK;QAAE,CAAA;MAAG,CAAA;IAClF;EACF;;EAGAG,OAAO;IACLlB,YAAY,OAAO;MACjBQ,OAAOC,mBAAMC,MAAMC,SAAS,CAAA;MAC5BC,MAAMH,mBAAMU,SAASC,QAAO,IAAKX,mBAAMC,MAAMG,UAAS,IAAKQ;IAC7D;EACF;EAEAC,SAAS;IACPvB,cAAc,MACZ,IAAIwB,YAAAA,OAAW;MACbC,OAAO;QACL;UACEC,IAAI;UACJC,MAAMH,YAAAA,OAAWI,SAASC;QAC5B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_", "i", "filter", "Boolean", "createFactory", "createSchema", "createData", "schema", "createObject", "Expando", "Status", "Priority", "defaultGenerators", "document", "title", "faker", "lorem", "sentence", "body", "sentences", "min", "max", "number", "int", "image", "datatype", "boolean", "undefined", "project", "SchemaType", "props", "id", "type", "PropType", "STRING", "NUMBER", "commerce", "productName", "repo", "probability", "internet", "url", "status", "helpers", "arrayElement", "priority", "TestObjectGenerator", "constructor", "types", "factories", "Object", "keys", "reduce", "acc", "values", "f", "factory", "createObjects"]
7
+ }
@@ -0,0 +1 @@
1
+ {"inputs":{"packages/core/echo/echo-generator/src/generator.ts":{"bytes":12300,"imports":[{"path":"@faker-js/faker","kind":"import-statement","external":true},{"path":"@dxos/client/echo","kind":"import-statement","external":true}],"format":"esm"},"packages/core/echo/echo-generator/src/index.ts":{"bytes":478,"imports":[{"path":"packages/core/echo/echo-generator/src/generator.ts","kind":"import-statement","original":"./generator"}],"format":"esm"}},"outputs":{"packages/core/echo/echo-generator/dist/lib/node/index.cjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":6439},"packages/core/echo/echo-generator/dist/lib/node/index.cjs":{"imports":[{"path":"@faker-js/faker","kind":"require-call","external":true},{"path":"@dxos/client/echo","kind":"require-call","external":true}],"exports":[],"entryPoint":"packages/core/echo/echo-generator/src/index.ts","inputs":{"packages/core/echo/echo-generator/src/index.ts":{"bytesInOutput":263},"packages/core/echo/echo-generator/src/generator.ts":{"bytesInOutput":2524}},"bytes":3892}}}
@@ -0,0 +1,42 @@
1
+ import { type TypedObject, Schema as SchemaType } from '@dxos/client/echo';
2
+ export declare const range: <T>(fn: (i: number) => T | undefined, length: number) => T[];
3
+ export type TestItem = {
4
+ id: string;
5
+ type: string;
6
+ } & Record<string, any>;
7
+ type ObjectDataGenerator = {
8
+ createSchema?: () => SchemaType;
9
+ createData: () => any;
10
+ };
11
+ type ObjectFactory<T extends TypedObject> = {
12
+ schema?: SchemaType;
13
+ createObject: () => T;
14
+ };
15
+ type ObjectFactoryMap = {
16
+ [type: string]: ObjectFactory<any>;
17
+ };
18
+ export declare const Status: string[];
19
+ export declare const Priority: number[];
20
+ export declare const defaultGenerators: {
21
+ [type: string]: ObjectDataGenerator;
22
+ };
23
+ /**
24
+ * Typed object generator.
25
+ */
26
+ export declare class TestObjectGenerator {
27
+ readonly factories: ObjectFactoryMap;
28
+ constructor({ types, factories }?: {
29
+ types?: string[];
30
+ factories?: ObjectFactoryMap;
31
+ });
32
+ get schema(): SchemaType[];
33
+ createObject({ types }?: {
34
+ types?: string[];
35
+ }): any;
36
+ createObjects({ types, length }: {
37
+ types?: string[];
38
+ length: number;
39
+ }): any[];
40
+ }
41
+ export {};
42
+ //# sourceMappingURL=generator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"generator.d.ts","sourceRoot":"","sources":["../../../src/generator.ts"],"names":[],"mappings":"AAUA,OAAO,EAAW,KAAK,WAAW,EAAE,MAAM,IAAI,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAGpF,eAAO,MAAM,KAAK,cAAe,MAAM,4BAA4B,MAAM,QAG9C,CAAC;AAG5B,MAAM,MAAM,QAAQ,GAAG;IAAE,EAAE,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;AAE1E,KAAK,mBAAmB,GAAG;IACzB,YAAY,CAAC,EAAE,MAAM,UAAU,CAAC;IAChC,UAAU,EAAE,MAAM,GAAG,CAAC;CACvB,CAAC;AAEF,KAAK,aAAa,CAAC,CAAC,SAAS,WAAW,IAAI;IAC1C,MAAM,CAAC,EAAE,UAAU,CAAC;IACpB,YAAY,EAAE,MAAM,CAAC,CAAC;CACvB,CAAC;AAEF,KAAK,gBAAgB,GAAG;IAAE,CAAC,IAAI,EAAE,MAAM,GAAG,aAAa,CAAC,GAAG,CAAC,CAAA;CAAE,CAAC;AAW/D,eAAO,MAAM,MAAM,UAAgC,CAAC;AACpD,eAAO,MAAM,QAAQ,UAAkB,CAAC;AAExC,eAAO,MAAM,iBAAiB,EAAE;IAAE,CAAC,IAAI,EAAE,MAAM,GAAG,mBAAmB,CAAA;CA6CpE,CAAC;AAEF;;GAEG;AACH,qBAAa,mBAAmB;IAC9B,SAAgB,SAAS,EAAE,gBAAgB,CAAC;gBAEhC,EAAE,KAAK,EAAE,SAAS,EAAE,GAAE;QAAE,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC;QAAC,SAAS,CAAC,EAAE,gBAAgB,CAAA;KAAO;IASzF,IAAI,MAAM,IAAI,UAAU,EAAE,CAEzB;IAED,YAAY,CAAC,EAAE,KAAK,EAAE,GAAE;QAAE,KAAK,CAAC,EAAE,MAAM,EAAE,CAAA;KAAO;IAMjD,aAAa,CAAC,EAAE,KAAK,EAAE,MAAM,EAAE,EAAE;QAAE,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE;CAGtE"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=generator.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"generator.test.d.ts","sourceRoot":"","sources":["../../../src/generator.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ export * from './generator';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAIA,cAAc,aAAa,CAAC"}
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "@dxos/echo-generator",
3
+ "version": "0.3.3-main.72cb938",
4
+ "description": "ECHO data generator for testing.",
5
+ "homepage": "https://dxos.org",
6
+ "bugs": "https://github.com/dxos/dxos/issues",
7
+ "license": "MIT",
8
+ "author": "info@dxos.org",
9
+ "main": "dist/lib/node/index.cjs",
10
+ "browser": {
11
+ "./dist/lib/node/index.cjs": "./dist/lib/browser/index.mjs"
12
+ },
13
+ "types": "dist/types/src/index.d.ts",
14
+ "files": [
15
+ "dist",
16
+ "src"
17
+ ],
18
+ "dependencies": {
19
+ "@faker-js/faker": "^8.0.2",
20
+ "@dxos/client": "0.3.3-main.72cb938",
21
+ "@dxos/node-std": "0.3.3-main.72cb938"
22
+ },
23
+ "devDependencies": {},
24
+ "publishConfig": {
25
+ "access": "public"
26
+ }
27
+ }
@@ -0,0 +1,17 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ import { expect } from 'chai';
6
+
7
+ import { describe, test } from '@dxos/test';
8
+
9
+ import { TestObjectGenerator } from './generator';
10
+
11
+ describe('sanity', () => {
12
+ test('basic', () => {
13
+ const generator = new TestObjectGenerator();
14
+ const object = generator.createObject();
15
+ expect(object).to.exist;
16
+ });
17
+ });
@@ -0,0 +1,121 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ // TODO(burdon): Reconcile with @dxos/plugin-debug, @dxos/react-ui/testing.
6
+
7
+ // TODO(burdon): Bug when adding stale objects to space (e.g., static objects already added in previous story invocation).
8
+
9
+ import { faker } from '@faker-js/faker';
10
+
11
+ import { Expando, type TypedObject, Schema as SchemaType } from '@dxos/client/echo';
12
+
13
+ // TODO(burdon): Util.
14
+ export const range = <T>(fn: (i: number) => T | undefined, length: number): T[] =>
15
+ Array.from({ length })
16
+ .map((_, i) => fn(i))
17
+ .filter(Boolean) as T[];
18
+
19
+ // TODO(burdon): Commit to using ECHO to generate all test data? Or convert from raw data?
20
+ export type TestItem = { id: string; type: string } & Record<string, any>;
21
+
22
+ type ObjectDataGenerator = {
23
+ createSchema?: () => SchemaType;
24
+ createData: () => any;
25
+ };
26
+
27
+ type ObjectFactory<T extends TypedObject> = {
28
+ schema?: SchemaType; // TODO(burdon): Support both typed and expando schema.
29
+ createObject: () => T;
30
+ };
31
+
32
+ type ObjectFactoryMap = { [type: string]: ObjectFactory<any> };
33
+
34
+ const createFactory = ({ createSchema, createData }: ObjectDataGenerator) => {
35
+ const schema = createSchema?.();
36
+ return {
37
+ schema,
38
+ createObject: () => new Expando(createData(), { schema }),
39
+ };
40
+ };
41
+
42
+ // TODO(burdon): Handle restricted values.
43
+ export const Status = ['pending', 'active', 'done'];
44
+ export const Priority = [1, 2, 3, 4, 5];
45
+
46
+ export const defaultGenerators: { [type: string]: ObjectDataGenerator } = {
47
+ document: {
48
+ createData: () => ({
49
+ title: faker.lorem.sentence(3),
50
+ body: faker.lorem.sentences({ min: 1, max: faker.number.int({ min: 1, max: 3 }) }),
51
+ }),
52
+ },
53
+
54
+ // TODO(burdon): Configure images.
55
+ image: {
56
+ createData: () => ({
57
+ title: faker.lorem.sentence(3),
58
+ body: faker.datatype.boolean() ? faker.lorem.sentences() : undefined,
59
+ }),
60
+ },
61
+
62
+ project: {
63
+ createSchema: () =>
64
+ new SchemaType({
65
+ props: [
66
+ {
67
+ id: 'title',
68
+ type: SchemaType.PropType.STRING,
69
+ },
70
+ {
71
+ id: 'repo',
72
+ type: SchemaType.PropType.STRING,
73
+ },
74
+ {
75
+ id: 'status',
76
+ type: SchemaType.PropType.STRING,
77
+ },
78
+ {
79
+ id: 'priority',
80
+ type: SchemaType.PropType.NUMBER,
81
+ },
82
+ ],
83
+ }),
84
+ createData: () => ({
85
+ title: faker.commerce.productName(),
86
+ repo: faker.datatype.boolean({ probability: 0.3 }) ? faker.internet.url() : undefined,
87
+ status: faker.helpers.arrayElement(Status),
88
+ priority: faker.helpers.arrayElement(Priority),
89
+ }),
90
+ },
91
+ };
92
+
93
+ /**
94
+ * Typed object generator.
95
+ */
96
+ export class TestObjectGenerator {
97
+ public readonly factories: ObjectFactoryMap;
98
+
99
+ constructor({ types, factories }: { types?: string[]; factories?: ObjectFactoryMap } = {}) {
100
+ this.factories =
101
+ factories ??
102
+ (types ?? Object.keys(defaultGenerators)).reduce<ObjectFactoryMap>((acc, type) => {
103
+ acc[type] = createFactory(defaultGenerators[type]);
104
+ return acc;
105
+ }, {});
106
+ }
107
+
108
+ get schema(): SchemaType[] {
109
+ return Object.values(this.factories).map((f) => f.schema!);
110
+ }
111
+
112
+ createObject({ types }: { types?: string[] } = {}) {
113
+ const type = faker.helpers.arrayElement(types ?? Object.keys(this.factories));
114
+ const factory = this.factories[type];
115
+ return factory?.createObject();
116
+ }
117
+
118
+ createObjects({ types, length }: { types?: string[]; length: number }) {
119
+ return range(() => this.createObject({ types }), length);
120
+ }
121
+ }
package/src/index.ts ADDED
@@ -0,0 +1,5 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ export * from './generator';