simplentity 0.1.1 → 1.0.0-alpha.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/bun/entity.d.ts +44 -22
- package/dist/bun/field.d.ts +1 -1
- package/dist/bun/index.js +1 -1
- package/dist/cjs/entity.cjs +31 -46
- package/dist/cjs/entity.d.ts +44 -22
- package/dist/cjs/field.d.ts +1 -1
- package/dist/cjs/index.cjs +31 -46
- package/dist/entity.d.ts +44 -22
- package/dist/entity.js +31 -46
- package/dist/field.d.ts +1 -1
- package/dist/index.js +31 -46
- package/package.json +2 -1
package/dist/bun/entity.d.ts
CHANGED
@@ -14,29 +14,51 @@ type EntityPropInputResolver<T extends EntityConfig> = {
|
|
14
14
|
} & {
|
15
15
|
[K in keyof Omit<T, RequiredFieldKeys<T>>]?: FieldTypeResolver<T[K]>;
|
16
16
|
};
|
17
|
+
type MethodDefinition = {
|
18
|
+
[key: string]: (...args: any[]) => any;
|
19
|
+
};
|
20
|
+
interface EntityInterface<C extends EntityConfig> {
|
21
|
+
get: <K extends keyof C>(key: K) => EntityConfigTypeResolver<C>[K];
|
22
|
+
toJSON: () => EntityConfigTypeResolver<C>;
|
23
|
+
}
|
17
24
|
/**
|
18
|
-
*
|
19
|
-
*
|
25
|
+
* Creates an entity factory function that allows defining fields and optional methods for an entity.
|
26
|
+
* The returned factory provides a `create` method to instantiate entities with the specified fields
|
27
|
+
* and methods, while also supporting default values and runtime property manipulation.
|
28
|
+
*
|
29
|
+
* @template C - The configuration type for the entity fields.
|
30
|
+
* @template D - The type of the methods defined for the entity.
|
31
|
+
*
|
32
|
+
* @param fields - An object defining the fields of the entity. Each field should include its configuration
|
33
|
+
* and a method to retrieve its default value.
|
34
|
+
* @param methodDefinitionFunction - An optional function that defines additional methods for the entity.
|
35
|
+
* It receives an object with `set` and `get` functions to manipulate
|
36
|
+
* the entity's properties.
|
37
|
+
*
|
38
|
+
* @returns An object with a `create` method. The `create` method accepts an input object to initialize
|
39
|
+
* the entity's properties and returns an entity instance with the defined fields, methods,
|
40
|
+
* and utility functions (`get`, `set`, `toJSON`).
|
41
|
+
*
|
42
|
+
* @example
|
43
|
+
* ```typescript
|
44
|
+
* const userFactory = entity({
|
45
|
+
* name: string(),
|
46
|
+
* age: number().default(18),
|
47
|
+
* isActive: boolean().default(true),
|
48
|
+
* }, ({ set, get }) => ({
|
49
|
+
* incrementAge: () => set('age', get('age') + 1),
|
50
|
+
* }));
|
51
|
+
*
|
52
|
+
* const user = userFactory.create({ name: 'John' });
|
53
|
+
* console.log(user.props); // { name: 'John', age: 18, isActive: true }
|
54
|
+
* user.incrementAge();
|
55
|
+
* console.log(user.props.age); // 19
|
56
|
+
* ```
|
20
57
|
*/
|
21
|
-
export declare
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
* Get the value of the field by key
|
27
|
-
* @param key
|
28
|
-
*/
|
29
|
-
get<K extends keyof Config>(key: K): EntityConfigTypeResolver<Config>[K];
|
30
|
-
/**
|
31
|
-
* Set the value of the field by key
|
32
|
-
*
|
33
|
-
* WARNING: This method should be called only from the methods of the entity.
|
34
|
-
* Its accessor should be protected but TypeScript declaration does not allow protected methods in exported classes.
|
35
|
-
* @param key
|
36
|
-
* @param value
|
37
|
-
*/
|
38
|
-
set<K extends keyof Config>(key: K, value: EntityConfigTypeResolver<Config>[K]): void;
|
39
|
-
toJSON(): EntityConfigTypeResolver<Config>;
|
40
|
-
};
|
58
|
+
export declare function entity<C extends EntityConfig, D extends MethodDefinition>(fields: C, methodDefinitionFunction?: (params: {
|
59
|
+
set: <K extends keyof C>(key: K, value: EntityConfigTypeResolver<C>[K]) => void;
|
60
|
+
get: <K extends keyof C>(key: K) => EntityConfigTypeResolver<C>[K];
|
61
|
+
}) => D): {
|
62
|
+
create(props: EntityPropInputResolver<C>): EntityInterface<C> & D;
|
41
63
|
};
|
42
64
|
export {};
|
package/dist/bun/field.d.ts
CHANGED
@@ -24,7 +24,7 @@ type HasDefault<T extends ConfigurableFieldBase<unknown>> = T & {
|
|
24
24
|
};
|
25
25
|
export declare abstract class Field<T> implements ConfigurableFieldBase<T> {
|
26
26
|
_: FieldConfig<T>;
|
27
|
-
|
27
|
+
config: FieldRuntimeConfig<T>;
|
28
28
|
constructor();
|
29
29
|
notRequired(): NotRequired<this>;
|
30
30
|
default(value: T): HasDefault<this>;
|
package/dist/bun/index.js
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
// @bun
|
2
|
-
|
2
|
+
function d(n,q){return{create(D){let f=Object.entries(n).reduce((u,[o,r])=>{let e=D[o]??r.getDefaultValue();if(r.getConfig().hasDefault&&e===void 0)u[o]=r.getDefaultValue();else u[o]=e;return u},{}),b=(u,o)=>{f[u]=o},a=(u)=>{return f[u]},p=()=>{return f},C=q?.({set:b,get:a})??{};return{get:a,toJSON:p,...C}}}}class t{config;constructor(){this.config={notRequired:!1,hasDefault:!1,default:void 0}}notRequired(){return this.config.notRequired=!0,this}default(n){return this.config.default=n,this.config.hasDefault=!0,this}defaultFn(n){return this.config.defaultFn=n,this.config.hasDefault=!0,this}getConfig(){return this.config}getDefaultValue(){return this.config.default??this.config.defaultFn?.()}}class i extends t{}var s=()=>{return new i};class h extends t{}var T=()=>{return new h};class g extends t{}var R=()=>{return new g};class l extends t{}var m=()=>{return new l};export{m as string,R as number,d as entity,T as date,s as boolean};
|
package/dist/cjs/entity.cjs
CHANGED
@@ -23,54 +23,39 @@ __export(entity_exports, {
|
|
23
23
|
entity: () => entity
|
24
24
|
});
|
25
25
|
module.exports = __toCommonJS(entity_exports);
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
{
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
* @param key
|
57
|
-
* @param value
|
58
|
-
*/
|
59
|
-
set(key, value) {
|
60
|
-
this.#props[key] = value;
|
61
|
-
}
|
62
|
-
// biome-ignore lint/style/useNamingConvention: toJSON is a name to be used in JSON.stringify
|
63
|
-
toJSON() {
|
64
|
-
return this.#props;
|
65
|
-
}
|
66
|
-
};
|
67
|
-
var entity = (fields) => {
|
68
|
-
return class extends Entity {
|
69
|
-
constructor(props) {
|
70
|
-
super(props, fields);
|
26
|
+
function entity(fields, methodDefinitionFunction) {
|
27
|
+
return {
|
28
|
+
create(props) {
|
29
|
+
const assignedProps = Object.entries(fields).reduce(
|
30
|
+
(acc, [key, field]) => {
|
31
|
+
const value = props[key] ?? field.getDefaultValue();
|
32
|
+
if (field.getConfig().hasDefault && value === void 0) {
|
33
|
+
acc[key] = field.getDefaultValue();
|
34
|
+
} else {
|
35
|
+
acc[key] = value;
|
36
|
+
}
|
37
|
+
return acc;
|
38
|
+
},
|
39
|
+
{}
|
40
|
+
);
|
41
|
+
const set = (key, value) => {
|
42
|
+
assignedProps[key] = value;
|
43
|
+
};
|
44
|
+
const get = (key) => {
|
45
|
+
return assignedProps[key];
|
46
|
+
};
|
47
|
+
const toJSON = () => {
|
48
|
+
return assignedProps;
|
49
|
+
};
|
50
|
+
const methods = methodDefinitionFunction?.({ set, get }) ?? {};
|
51
|
+
return {
|
52
|
+
get,
|
53
|
+
toJSON,
|
54
|
+
...methods
|
55
|
+
};
|
71
56
|
}
|
72
57
|
};
|
73
|
-
}
|
58
|
+
}
|
74
59
|
// Annotate the CommonJS export names for ESM import in node:
|
75
60
|
0 && (module.exports = {
|
76
61
|
entity
|
package/dist/cjs/entity.d.ts
CHANGED
@@ -14,29 +14,51 @@ type EntityPropInputResolver<T extends EntityConfig> = {
|
|
14
14
|
} & {
|
15
15
|
[K in keyof Omit<T, RequiredFieldKeys<T>>]?: FieldTypeResolver<T[K]>;
|
16
16
|
};
|
17
|
+
type MethodDefinition = {
|
18
|
+
[key: string]: (...args: any[]) => any;
|
19
|
+
};
|
20
|
+
interface EntityInterface<C extends EntityConfig> {
|
21
|
+
get: <K extends keyof C>(key: K) => EntityConfigTypeResolver<C>[K];
|
22
|
+
toJSON: () => EntityConfigTypeResolver<C>;
|
23
|
+
}
|
17
24
|
/**
|
18
|
-
*
|
19
|
-
*
|
25
|
+
* Creates an entity factory function that allows defining fields and optional methods for an entity.
|
26
|
+
* The returned factory provides a `create` method to instantiate entities with the specified fields
|
27
|
+
* and methods, while also supporting default values and runtime property manipulation.
|
28
|
+
*
|
29
|
+
* @template C - The configuration type for the entity fields.
|
30
|
+
* @template D - The type of the methods defined for the entity.
|
31
|
+
*
|
32
|
+
* @param fields - An object defining the fields of the entity. Each field should include its configuration
|
33
|
+
* and a method to retrieve its default value.
|
34
|
+
* @param methodDefinitionFunction - An optional function that defines additional methods for the entity.
|
35
|
+
* It receives an object with `set` and `get` functions to manipulate
|
36
|
+
* the entity's properties.
|
37
|
+
*
|
38
|
+
* @returns An object with a `create` method. The `create` method accepts an input object to initialize
|
39
|
+
* the entity's properties and returns an entity instance with the defined fields, methods,
|
40
|
+
* and utility functions (`get`, `set`, `toJSON`).
|
41
|
+
*
|
42
|
+
* @example
|
43
|
+
* ```typescript
|
44
|
+
* const userFactory = entity({
|
45
|
+
* name: string(),
|
46
|
+
* age: number().default(18),
|
47
|
+
* isActive: boolean().default(true),
|
48
|
+
* }, ({ set, get }) => ({
|
49
|
+
* incrementAge: () => set('age', get('age') + 1),
|
50
|
+
* }));
|
51
|
+
*
|
52
|
+
* const user = userFactory.create({ name: 'John' });
|
53
|
+
* console.log(user.props); // { name: 'John', age: 18, isActive: true }
|
54
|
+
* user.incrementAge();
|
55
|
+
* console.log(user.props.age); // 19
|
56
|
+
* ```
|
20
57
|
*/
|
21
|
-
export declare
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
* Get the value of the field by key
|
27
|
-
* @param key
|
28
|
-
*/
|
29
|
-
get<K extends keyof Config>(key: K): EntityConfigTypeResolver<Config>[K];
|
30
|
-
/**
|
31
|
-
* Set the value of the field by key
|
32
|
-
*
|
33
|
-
* WARNING: This method should be called only from the methods of the entity.
|
34
|
-
* Its accessor should be protected but TypeScript declaration does not allow protected methods in exported classes.
|
35
|
-
* @param key
|
36
|
-
* @param value
|
37
|
-
*/
|
38
|
-
set<K extends keyof Config>(key: K, value: EntityConfigTypeResolver<Config>[K]): void;
|
39
|
-
toJSON(): EntityConfigTypeResolver<Config>;
|
40
|
-
};
|
58
|
+
export declare function entity<C extends EntityConfig, D extends MethodDefinition>(fields: C, methodDefinitionFunction?: (params: {
|
59
|
+
set: <K extends keyof C>(key: K, value: EntityConfigTypeResolver<C>[K]) => void;
|
60
|
+
get: <K extends keyof C>(key: K) => EntityConfigTypeResolver<C>[K];
|
61
|
+
}) => D): {
|
62
|
+
create(props: EntityPropInputResolver<C>): EntityInterface<C> & D;
|
41
63
|
};
|
42
64
|
export {};
|
package/dist/cjs/field.d.ts
CHANGED
@@ -24,7 +24,7 @@ type HasDefault<T extends ConfigurableFieldBase<unknown>> = T & {
|
|
24
24
|
};
|
25
25
|
export declare abstract class Field<T> implements ConfigurableFieldBase<T> {
|
26
26
|
_: FieldConfig<T>;
|
27
|
-
|
27
|
+
config: FieldRuntimeConfig<T>;
|
28
28
|
constructor();
|
29
29
|
notRequired(): NotRequired<this>;
|
30
30
|
default(value: T): HasDefault<this>;
|
package/dist/cjs/index.cjs
CHANGED
@@ -29,54 +29,39 @@ __export(index_exports, {
|
|
29
29
|
module.exports = __toCommonJS(index_exports);
|
30
30
|
|
31
31
|
// src/entity.ts
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
{
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
* @param key
|
63
|
-
* @param value
|
64
|
-
*/
|
65
|
-
set(key, value) {
|
66
|
-
this.#props[key] = value;
|
67
|
-
}
|
68
|
-
// biome-ignore lint/style/useNamingConvention: toJSON is a name to be used in JSON.stringify
|
69
|
-
toJSON() {
|
70
|
-
return this.#props;
|
71
|
-
}
|
72
|
-
};
|
73
|
-
var entity = (fields) => {
|
74
|
-
return class extends Entity {
|
75
|
-
constructor(props) {
|
76
|
-
super(props, fields);
|
32
|
+
function entity(fields, methodDefinitionFunction) {
|
33
|
+
return {
|
34
|
+
create(props) {
|
35
|
+
const assignedProps = Object.entries(fields).reduce(
|
36
|
+
(acc, [key, field]) => {
|
37
|
+
const value = props[key] ?? field.getDefaultValue();
|
38
|
+
if (field.getConfig().hasDefault && value === void 0) {
|
39
|
+
acc[key] = field.getDefaultValue();
|
40
|
+
} else {
|
41
|
+
acc[key] = value;
|
42
|
+
}
|
43
|
+
return acc;
|
44
|
+
},
|
45
|
+
{}
|
46
|
+
);
|
47
|
+
const set = (key, value) => {
|
48
|
+
assignedProps[key] = value;
|
49
|
+
};
|
50
|
+
const get = (key) => {
|
51
|
+
return assignedProps[key];
|
52
|
+
};
|
53
|
+
const toJSON = () => {
|
54
|
+
return assignedProps;
|
55
|
+
};
|
56
|
+
const methods = methodDefinitionFunction?.({ set, get }) ?? {};
|
57
|
+
return {
|
58
|
+
get,
|
59
|
+
toJSON,
|
60
|
+
...methods
|
61
|
+
};
|
77
62
|
}
|
78
63
|
};
|
79
|
-
}
|
64
|
+
}
|
80
65
|
|
81
66
|
// src/field.ts
|
82
67
|
var Field = class {
|
package/dist/entity.d.ts
CHANGED
@@ -14,29 +14,51 @@ type EntityPropInputResolver<T extends EntityConfig> = {
|
|
14
14
|
} & {
|
15
15
|
[K in keyof Omit<T, RequiredFieldKeys<T>>]?: FieldTypeResolver<T[K]>;
|
16
16
|
};
|
17
|
+
type MethodDefinition = {
|
18
|
+
[key: string]: (...args: any[]) => any;
|
19
|
+
};
|
20
|
+
interface EntityInterface<C extends EntityConfig> {
|
21
|
+
get: <K extends keyof C>(key: K) => EntityConfigTypeResolver<C>[K];
|
22
|
+
toJSON: () => EntityConfigTypeResolver<C>;
|
23
|
+
}
|
17
24
|
/**
|
18
|
-
*
|
19
|
-
*
|
25
|
+
* Creates an entity factory function that allows defining fields and optional methods for an entity.
|
26
|
+
* The returned factory provides a `create` method to instantiate entities with the specified fields
|
27
|
+
* and methods, while also supporting default values and runtime property manipulation.
|
28
|
+
*
|
29
|
+
* @template C - The configuration type for the entity fields.
|
30
|
+
* @template D - The type of the methods defined for the entity.
|
31
|
+
*
|
32
|
+
* @param fields - An object defining the fields of the entity. Each field should include its configuration
|
33
|
+
* and a method to retrieve its default value.
|
34
|
+
* @param methodDefinitionFunction - An optional function that defines additional methods for the entity.
|
35
|
+
* It receives an object with `set` and `get` functions to manipulate
|
36
|
+
* the entity's properties.
|
37
|
+
*
|
38
|
+
* @returns An object with a `create` method. The `create` method accepts an input object to initialize
|
39
|
+
* the entity's properties and returns an entity instance with the defined fields, methods,
|
40
|
+
* and utility functions (`get`, `set`, `toJSON`).
|
41
|
+
*
|
42
|
+
* @example
|
43
|
+
* ```typescript
|
44
|
+
* const userFactory = entity({
|
45
|
+
* name: string(),
|
46
|
+
* age: number().default(18),
|
47
|
+
* isActive: boolean().default(true),
|
48
|
+
* }, ({ set, get }) => ({
|
49
|
+
* incrementAge: () => set('age', get('age') + 1),
|
50
|
+
* }));
|
51
|
+
*
|
52
|
+
* const user = userFactory.create({ name: 'John' });
|
53
|
+
* console.log(user.props); // { name: 'John', age: 18, isActive: true }
|
54
|
+
* user.incrementAge();
|
55
|
+
* console.log(user.props.age); // 19
|
56
|
+
* ```
|
20
57
|
*/
|
21
|
-
export declare
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
* Get the value of the field by key
|
27
|
-
* @param key
|
28
|
-
*/
|
29
|
-
get<K extends keyof Config>(key: K): EntityConfigTypeResolver<Config>[K];
|
30
|
-
/**
|
31
|
-
* Set the value of the field by key
|
32
|
-
*
|
33
|
-
* WARNING: This method should be called only from the methods of the entity.
|
34
|
-
* Its accessor should be protected but TypeScript declaration does not allow protected methods in exported classes.
|
35
|
-
* @param key
|
36
|
-
* @param value
|
37
|
-
*/
|
38
|
-
set<K extends keyof Config>(key: K, value: EntityConfigTypeResolver<Config>[K]): void;
|
39
|
-
toJSON(): EntityConfigTypeResolver<Config>;
|
40
|
-
};
|
58
|
+
export declare function entity<C extends EntityConfig, D extends MethodDefinition>(fields: C, methodDefinitionFunction?: (params: {
|
59
|
+
set: <K extends keyof C>(key: K, value: EntityConfigTypeResolver<C>[K]) => void;
|
60
|
+
get: <K extends keyof C>(key: K) => EntityConfigTypeResolver<C>[K];
|
61
|
+
}) => D): {
|
62
|
+
create(props: EntityPropInputResolver<C>): EntityInterface<C> & D;
|
41
63
|
};
|
42
64
|
export {};
|
package/dist/entity.js
CHANGED
@@ -1,52 +1,37 @@
|
|
1
1
|
// src/entity.ts
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
{
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
* @param key
|
33
|
-
* @param value
|
34
|
-
*/
|
35
|
-
set(key, value) {
|
36
|
-
this.#props[key] = value;
|
37
|
-
}
|
38
|
-
// biome-ignore lint/style/useNamingConvention: toJSON is a name to be used in JSON.stringify
|
39
|
-
toJSON() {
|
40
|
-
return this.#props;
|
41
|
-
}
|
42
|
-
};
|
43
|
-
var entity = (fields) => {
|
44
|
-
return class extends Entity {
|
45
|
-
constructor(props) {
|
46
|
-
super(props, fields);
|
2
|
+
function entity(fields, methodDefinitionFunction) {
|
3
|
+
return {
|
4
|
+
create(props) {
|
5
|
+
const assignedProps = Object.entries(fields).reduce(
|
6
|
+
(acc, [key, field]) => {
|
7
|
+
const value = props[key] ?? field.getDefaultValue();
|
8
|
+
if (field.getConfig().hasDefault && value === void 0) {
|
9
|
+
acc[key] = field.getDefaultValue();
|
10
|
+
} else {
|
11
|
+
acc[key] = value;
|
12
|
+
}
|
13
|
+
return acc;
|
14
|
+
},
|
15
|
+
{}
|
16
|
+
);
|
17
|
+
const set = (key, value) => {
|
18
|
+
assignedProps[key] = value;
|
19
|
+
};
|
20
|
+
const get = (key) => {
|
21
|
+
return assignedProps[key];
|
22
|
+
};
|
23
|
+
const toJSON = () => {
|
24
|
+
return assignedProps;
|
25
|
+
};
|
26
|
+
const methods = methodDefinitionFunction?.({ set, get }) ?? {};
|
27
|
+
return {
|
28
|
+
get,
|
29
|
+
toJSON,
|
30
|
+
...methods
|
31
|
+
};
|
47
32
|
}
|
48
33
|
};
|
49
|
-
}
|
34
|
+
}
|
50
35
|
export {
|
51
36
|
entity
|
52
37
|
};
|
package/dist/field.d.ts
CHANGED
@@ -24,7 +24,7 @@ type HasDefault<T extends ConfigurableFieldBase<unknown>> = T & {
|
|
24
24
|
};
|
25
25
|
export declare abstract class Field<T> implements ConfigurableFieldBase<T> {
|
26
26
|
_: FieldConfig<T>;
|
27
|
-
|
27
|
+
config: FieldRuntimeConfig<T>;
|
28
28
|
constructor();
|
29
29
|
notRequired(): NotRequired<this>;
|
30
30
|
default(value: T): HasDefault<this>;
|
package/dist/index.js
CHANGED
@@ -1,52 +1,37 @@
|
|
1
1
|
// src/entity.ts
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
{
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
* @param key
|
33
|
-
* @param value
|
34
|
-
*/
|
35
|
-
set(key, value) {
|
36
|
-
this.#props[key] = value;
|
37
|
-
}
|
38
|
-
// biome-ignore lint/style/useNamingConvention: toJSON is a name to be used in JSON.stringify
|
39
|
-
toJSON() {
|
40
|
-
return this.#props;
|
41
|
-
}
|
42
|
-
};
|
43
|
-
var entity = (fields) => {
|
44
|
-
return class extends Entity {
|
45
|
-
constructor(props) {
|
46
|
-
super(props, fields);
|
2
|
+
function entity(fields, methodDefinitionFunction) {
|
3
|
+
return {
|
4
|
+
create(props) {
|
5
|
+
const assignedProps = Object.entries(fields).reduce(
|
6
|
+
(acc, [key, field]) => {
|
7
|
+
const value = props[key] ?? field.getDefaultValue();
|
8
|
+
if (field.getConfig().hasDefault && value === void 0) {
|
9
|
+
acc[key] = field.getDefaultValue();
|
10
|
+
} else {
|
11
|
+
acc[key] = value;
|
12
|
+
}
|
13
|
+
return acc;
|
14
|
+
},
|
15
|
+
{}
|
16
|
+
);
|
17
|
+
const set = (key, value) => {
|
18
|
+
assignedProps[key] = value;
|
19
|
+
};
|
20
|
+
const get = (key) => {
|
21
|
+
return assignedProps[key];
|
22
|
+
};
|
23
|
+
const toJSON = () => {
|
24
|
+
return assignedProps;
|
25
|
+
};
|
26
|
+
const methods = methodDefinitionFunction?.({ set, get }) ?? {};
|
27
|
+
return {
|
28
|
+
get,
|
29
|
+
toJSON,
|
30
|
+
...methods
|
31
|
+
};
|
47
32
|
}
|
48
33
|
};
|
49
|
-
}
|
34
|
+
}
|
50
35
|
|
51
36
|
// src/field.ts
|
52
37
|
var Field = class {
|