@cratis/fundamentals 7.10.6 → 7.14.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.
Files changed (44) hide show
  1. package/ConceptAs.ts +30 -0
  2. package/JsonSerializer.ts +40 -0
  3. package/dist/ConceptAs.d.ts +7 -0
  4. package/dist/ConceptAs.d.ts.map +1 -0
  5. package/dist/ConceptAs.js +13 -0
  6. package/dist/ConceptAs.js.map +1 -0
  7. package/dist/JsonSerializer.d.ts.map +1 -1
  8. package/dist/JsonSerializer.js +23 -0
  9. package/dist/JsonSerializer.js.map +1 -1
  10. package/dist/cjs/ConceptAs.d.ts +7 -0
  11. package/dist/cjs/ConceptAs.d.ts.map +1 -0
  12. package/dist/cjs/ConceptAs.js +17 -0
  13. package/dist/cjs/ConceptAs.js.map +1 -0
  14. package/dist/cjs/JsonSerializer.d.ts.map +1 -1
  15. package/dist/cjs/JsonSerializer.js +23 -0
  16. package/dist/cjs/JsonSerializer.js.map +1 -1
  17. package/dist/cjs/index.d.ts +1 -0
  18. package/dist/cjs/index.d.ts.map +1 -1
  19. package/dist/cjs/index.js +2 -0
  20. package/dist/cjs/index.js.map +1 -1
  21. package/dist/esm/ConceptAs.d.ts +7 -0
  22. package/dist/esm/ConceptAs.d.ts.map +1 -0
  23. package/dist/esm/ConceptAs.js +15 -0
  24. package/dist/esm/ConceptAs.js.map +1 -0
  25. package/dist/esm/JsonSerializer.d.ts.map +1 -1
  26. package/dist/esm/JsonSerializer.js +23 -0
  27. package/dist/esm/JsonSerializer.js.map +1 -1
  28. package/dist/esm/index.d.ts +1 -0
  29. package/dist/esm/index.d.ts.map +1 -1
  30. package/dist/esm/index.js +1 -0
  31. package/dist/esm/index.js.map +1 -1
  32. package/dist/index.d.ts +1 -0
  33. package/dist/index.d.ts.map +1 -1
  34. package/dist/index.js +1 -0
  35. package/dist/index.js.map +1 -1
  36. package/dist/tsconfig.tsbuildinfo +1 -1
  37. package/for_ConceptAs/when_creating_a_concept_with_number_value.ts +15 -0
  38. package/for_ConceptAs/when_creating_a_concept_with_string_value.ts +15 -0
  39. package/for_JsonSerializer/Types.ts +7 -0
  40. package/for_JsonSerializer/when_deserializing_object_with_concept_properties.ts +28 -0
  41. package/for_JsonSerializer/when_serializing_object_with_concept_properties.ts +31 -0
  42. package/for_JsonSerializer/when_using_union_types_with_concept_properties.ts +85 -0
  43. package/index.ts +1 -0
  44. package/package.json +1 -1
@@ -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
+ import { JsonSerializer } from '../JsonSerializer';
5
+ import { UserId, OrderCount } from './Types';
6
+ import { field } from '../fieldDecorator';
7
+
8
+ class TestObjectWithUnionTypes {
9
+ @field(UserId)
10
+ userId!: UserId | string;
11
+
12
+ @field(OrderCount)
13
+ orderCount!: OrderCount | number;
14
+
15
+ @field(String)
16
+ name!: string;
17
+ }
18
+
19
+ describe('when using union types with concept properties', () => {
20
+ describe('and assigning ConceptAs instances', () => {
21
+ const obj = new TestObjectWithUnionTypes();
22
+ obj.userId = new UserId('user-123');
23
+ obj.orderCount = new OrderCount(42);
24
+ obj.name = 'Test';
25
+
26
+ const json = JsonSerializer.serialize(obj);
27
+ const parsed = JSON.parse(json);
28
+
29
+ it('should serialize userId ConceptAs instance as string value', () => parsed.userId.should.equal('user-123'));
30
+ it('should serialize orderCount ConceptAs instance as number value', () => parsed.orderCount.should.equal(42));
31
+ it('should serialize name as string', () => parsed.name.should.equal('Test'));
32
+
33
+ const deserialized = JsonSerializer.deserialize(TestObjectWithUnionTypes, json);
34
+
35
+ it('should deserialize userId as UserId instance', () => deserialized.userId.should.be.instanceOf(UserId));
36
+ it('should deserialize userId with correct value', () => (deserialized.userId as UserId).value.should.equal('user-123'));
37
+ it('should deserialize orderCount as OrderCount instance', () => deserialized.orderCount.should.be.instanceOf(OrderCount));
38
+ it('should deserialize orderCount with correct value', () => (deserialized.orderCount as OrderCount).value.should.equal(42));
39
+ });
40
+
41
+ describe('and assigning primitive values', () => {
42
+ const obj = new TestObjectWithUnionTypes();
43
+ obj.userId = 'user-456';
44
+ obj.orderCount = 99;
45
+ obj.name = 'Test2';
46
+
47
+ const json = JsonSerializer.serialize(obj);
48
+ const parsed = JSON.parse(json);
49
+
50
+ it('should serialize primitive userId as string value', () => parsed.userId.should.equal('user-456'));
51
+ it('should serialize primitive orderCount as number value', () => parsed.orderCount.should.equal(99));
52
+ it('should serialize name as string', () => parsed.name.should.equal('Test2'));
53
+
54
+ const deserialized = JsonSerializer.deserialize(TestObjectWithUnionTypes, json);
55
+
56
+ it('should deserialize primitive userId as UserId instance', () => deserialized.userId.should.be.instanceOf(UserId));
57
+ it('should deserialize primitive userId with correct value', () => (deserialized.userId as UserId).value.should.equal('user-456'));
58
+ it('should deserialize primitive orderCount as OrderCount instance', () => deserialized.orderCount.should.be.instanceOf(OrderCount));
59
+ it('should deserialize primitive orderCount with correct value', () => (deserialized.orderCount as OrderCount).value.should.equal(99));
60
+ });
61
+
62
+ describe('and mixing ConceptAs and primitive values', () => {
63
+ const obj = new TestObjectWithUnionTypes();
64
+ obj.userId = new UserId('user-789');
65
+ obj.orderCount = 55; // primitive
66
+ obj.name = 'Test3';
67
+
68
+ const json = JsonSerializer.serialize(obj);
69
+ const parsed = JSON.parse(json);
70
+
71
+ it('should serialize both types correctly', () => {
72
+ parsed.userId.should.equal('user-789');
73
+ parsed.orderCount.should.equal(55);
74
+ });
75
+
76
+ const deserialized = JsonSerializer.deserialize(TestObjectWithUnionTypes, json);
77
+
78
+ it('should deserialize mixed types correctly', () => {
79
+ deserialized.userId.should.be.instanceOf(UserId);
80
+ (deserialized.userId as UserId).value.should.equal('user-789');
81
+ deserialized.orderCount.should.be.instanceOf(OrderCount);
82
+ (deserialized.orderCount as OrderCount).value.should.equal(55);
83
+ });
84
+ });
85
+ });
package/index.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  // Copyright (c) Cratis. All rights reserved.
2
2
  // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
3
 
4
+ export * from './ConceptAs';
4
5
  export * from './Constructor';
5
6
  export * from './Field';
6
7
  export * from './Fields';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cratis/fundamentals",
3
- "version": "7.10.6",
3
+ "version": "7.14.0",
4
4
  "description": "",
5
5
  "author": "Cratis",
6
6
  "license": "MIT",