@quatrain/core 1.1.24 → 1.1.28

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (234) hide show
  1. package/README.md +56 -8
  2. package/{lib → dist}/Core.d.ts +3 -17
  3. package/dist/Core.d.ts.map +1 -0
  4. package/dist/Core.js +105 -0
  5. package/dist/Core.js.map +1 -0
  6. package/dist/common/DataGenerator.d.ts +3 -0
  7. package/dist/common/DataGenerator.d.ts.map +1 -0
  8. package/dist/common/DataGenerator.js +86 -0
  9. package/dist/common/DataGenerator.js.map +1 -0
  10. package/{lib → dist}/common/ResourcesErrors.d.ts +1 -0
  11. package/dist/common/ResourcesErrors.d.ts.map +1 -0
  12. package/{lib → dist}/common/ResourcesErrors.js +1 -0
  13. package/dist/common/ResourcesErrors.js.map +1 -0
  14. package/{lib → dist}/common/statuses.d.ts +1 -0
  15. package/dist/common/statuses.d.ts.map +1 -0
  16. package/{lib → dist}/common/statuses.js +1 -0
  17. package/dist/common/statuses.js.map +1 -0
  18. package/{lib → dist}/components/AbstractObject.d.ts +1 -5
  19. package/dist/components/AbstractObject.d.ts.map +1 -0
  20. package/{lib → dist}/components/AbstractObject.js +7 -10
  21. package/dist/components/AbstractObject.js.map +1 -0
  22. package/{lib → dist}/components/BaseObject.d.ts +4 -17
  23. package/dist/components/BaseObject.d.ts.map +1 -0
  24. package/dist/components/BaseObject.js +71 -0
  25. package/dist/components/BaseObject.js.map +1 -0
  26. package/dist/components/BaseObjectProperties.d.ts +2 -0
  27. package/dist/components/BaseObjectProperties.d.ts.map +1 -0
  28. package/{lib → dist}/components/BaseObjectProperties.js +1 -2
  29. package/dist/components/BaseObjectProperties.js.map +1 -0
  30. package/{lib → dist}/components/DataObject.d.ts +2 -51
  31. package/dist/components/DataObject.d.ts.map +1 -0
  32. package/{lib → dist}/components/DataObject.js +38 -111
  33. package/dist/components/DataObject.js.map +1 -0
  34. package/{lib → dist}/components/Entity.d.ts +1 -0
  35. package/dist/components/Entity.d.ts.map +1 -0
  36. package/dist/components/Entity.js +48 -0
  37. package/dist/components/Entity.js.map +1 -0
  38. package/{lib → dist}/components/ObjectUri.d.ts +1 -26
  39. package/dist/components/ObjectUri.d.ts.map +1 -0
  40. package/{lib → dist}/components/ObjectUri.js +13 -41
  41. package/dist/components/ObjectUri.js.map +1 -0
  42. package/{lib → dist}/components/User.d.ts +1 -0
  43. package/dist/components/User.d.ts.map +1 -0
  44. package/{lib → dist}/components/User.js +7 -26
  45. package/dist/components/User.js.map +1 -0
  46. package/dist/components/__test__/fixtures/dao.d.ts +37 -0
  47. package/dist/components/__test__/fixtures/dao.d.ts.map +1 -0
  48. package/dist/components/__test__/fixtures/dao.js +100 -0
  49. package/dist/components/__test__/fixtures/dao.js.map +1 -0
  50. package/{lib → dist}/components/index.d.ts +5 -2
  51. package/dist/components/index.d.ts.map +1 -0
  52. package/{lib → dist}/components/index.js +1 -0
  53. package/dist/components/index.js.map +1 -0
  54. package/{lib → dist}/components/types/AbstractObjectClass.d.ts +1 -0
  55. package/dist/components/types/AbstractObjectClass.d.ts.map +1 -0
  56. package/dist/components/types/AbstractObjectClass.js +3 -0
  57. package/dist/components/types/AbstractObjectClass.js.map +1 -0
  58. package/{lib → dist}/components/types/BaseObjectClass.d.ts +1 -0
  59. package/dist/components/types/BaseObjectClass.d.ts.map +1 -0
  60. package/{lib → dist}/components/types/BaseObjectClass.js +1 -0
  61. package/dist/components/types/BaseObjectClass.js.map +1 -0
  62. package/{lib → dist}/components/types/BaseObjectType.d.ts +2 -1
  63. package/dist/components/types/BaseObjectType.d.ts.map +1 -0
  64. package/{lib → dist}/components/types/BaseObjectType.js +1 -0
  65. package/dist/components/types/BaseObjectType.js.map +1 -0
  66. package/{lib → dist}/components/types/DataObjectClass.d.ts +1 -0
  67. package/dist/components/types/DataObjectClass.d.ts.map +1 -0
  68. package/{lib → dist}/components/types/DataObjectClass.js +1 -0
  69. package/dist/components/types/DataObjectClass.js.map +1 -0
  70. package/dist/components/types/DataObjectParams.d.ts +8 -0
  71. package/dist/components/types/DataObjectParams.d.ts.map +1 -0
  72. package/dist/components/types/DataObjectParams.js +3 -0
  73. package/dist/components/types/DataObjectParams.js.map +1 -0
  74. package/{lib → dist}/components/types/DataObjectType.d.ts +1 -0
  75. package/dist/components/types/DataObjectType.d.ts.map +1 -0
  76. package/{lib → dist}/components/types/DataObjectType.js +1 -0
  77. package/dist/components/types/DataObjectType.js.map +1 -0
  78. package/{lib → dist}/components/types/EntityClass.d.ts +1 -0
  79. package/dist/components/types/EntityClass.d.ts.map +1 -0
  80. package/{lib → dist}/components/types/EntityClass.js +1 -0
  81. package/dist/components/types/EntityClass.js.map +1 -0
  82. package/{lib → dist}/components/types/Payload.d.ts +1 -0
  83. package/dist/components/types/Payload.d.ts.map +1 -0
  84. package/{lib → dist}/components/types/Payload.js +1 -0
  85. package/dist/components/types/Payload.js.map +1 -0
  86. package/{lib → dist}/components/types/Persisted.d.ts +1 -0
  87. package/dist/components/types/Persisted.d.ts.map +1 -0
  88. package/{lib → dist}/components/types/Persisted.js +1 -0
  89. package/dist/components/types/Persisted.js.map +1 -0
  90. package/dist/components/types/Properties.d.ts +2 -0
  91. package/dist/components/types/Properties.d.ts.map +1 -0
  92. package/{lib/components/types/AbstractObjectClass.js → dist/components/types/Properties.js} +1 -0
  93. package/dist/components/types/Properties.js.map +1 -0
  94. package/{lib → dist}/components/types/ProxyConstructor.d.ts +1 -0
  95. package/dist/components/types/ProxyConstructor.d.ts.map +1 -0
  96. package/{lib → dist}/components/types/ProxyConstructor.js +1 -0
  97. package/dist/components/types/ProxyConstructor.js.map +1 -0
  98. package/{lib → dist}/components/types/toJSONParams.d.ts +1 -0
  99. package/dist/components/types/toJSONParams.d.ts.map +1 -0
  100. package/{lib → dist}/components/types/toJSONParams.js +1 -0
  101. package/dist/components/types/toJSONParams.js.map +1 -0
  102. package/{lib → dist}/index.d.ts +5 -1
  103. package/dist/index.d.ts.map +1 -0
  104. package/{lib → dist}/index.js +4 -1
  105. package/dist/index.js.map +1 -0
  106. package/{lib → dist}/properties/ArrayProperty.d.ts +1 -0
  107. package/dist/properties/ArrayProperty.d.ts.map +1 -0
  108. package/{lib → dist}/properties/ArrayProperty.js +5 -4
  109. package/dist/properties/ArrayProperty.js.map +1 -0
  110. package/{lib → dist}/properties/BaseProperty.d.ts +3 -2
  111. package/dist/properties/BaseProperty.d.ts.map +1 -0
  112. package/{lib → dist}/properties/BaseProperty.js +18 -11
  113. package/dist/properties/BaseProperty.js.map +1 -0
  114. package/{lib → dist}/properties/BooleanProperty.d.ts +1 -0
  115. package/dist/properties/BooleanProperty.d.ts.map +1 -0
  116. package/{lib → dist}/properties/BooleanProperty.js +2 -1
  117. package/dist/properties/BooleanProperty.js.map +1 -0
  118. package/{lib → dist}/properties/CollectionProperty.d.ts +1 -0
  119. package/dist/properties/CollectionProperty.d.ts.map +1 -0
  120. package/{lib → dist}/properties/CollectionProperty.js +6 -4
  121. package/dist/properties/CollectionProperty.js.map +1 -0
  122. package/{lib → dist}/properties/DateTimeProperty.d.ts +1 -0
  123. package/dist/properties/DateTimeProperty.d.ts.map +1 -0
  124. package/{lib → dist}/properties/DateTimeProperty.js +12 -5
  125. package/dist/properties/DateTimeProperty.js.map +1 -0
  126. package/{lib → dist}/properties/EnumProperty.d.ts +1 -0
  127. package/dist/properties/EnumProperty.d.ts.map +1 -0
  128. package/{lib → dist}/properties/EnumProperty.js +4 -3
  129. package/dist/properties/EnumProperty.js.map +1 -0
  130. package/{lib → dist}/properties/FileProperty.d.ts +1 -0
  131. package/dist/properties/FileProperty.d.ts.map +1 -0
  132. package/{lib → dist}/properties/FileProperty.js +3 -2
  133. package/dist/properties/FileProperty.js.map +1 -0
  134. package/{lib → dist}/properties/HashProperty.d.ts +2 -7
  135. package/dist/properties/HashProperty.d.ts.map +1 -0
  136. package/{lib → dist}/properties/HashProperty.js +15 -23
  137. package/dist/properties/HashProperty.js.map +1 -0
  138. package/{lib → dist}/properties/MapProperty.d.ts +1 -0
  139. package/dist/properties/MapProperty.d.ts.map +1 -0
  140. package/{lib → dist}/properties/MapProperty.js +2 -1
  141. package/dist/properties/MapProperty.js.map +1 -0
  142. package/{lib → dist}/properties/NumberProperty.d.ts +1 -0
  143. package/dist/properties/NumberProperty.d.ts.map +1 -0
  144. package/{lib → dist}/properties/NumberProperty.js +15 -14
  145. package/dist/properties/NumberProperty.js.map +1 -0
  146. package/{lib → dist}/properties/ObjectProperty.d.ts +1 -0
  147. package/dist/properties/ObjectProperty.d.ts.map +1 -0
  148. package/{lib → dist}/properties/ObjectProperty.js +4 -3
  149. package/dist/properties/ObjectProperty.js.map +1 -0
  150. package/{lib → dist}/properties/Property.d.ts +1 -0
  151. package/dist/properties/Property.d.ts.map +1 -0
  152. package/{lib → dist}/properties/Property.js +12 -11
  153. package/dist/properties/Property.js.map +1 -0
  154. package/{lib → dist}/properties/StringProperty.d.ts +3 -5
  155. package/dist/properties/StringProperty.d.ts.map +1 -0
  156. package/{lib → dist}/properties/StringProperty.js +20 -28
  157. package/dist/properties/StringProperty.js.map +1 -0
  158. package/{lib → dist}/properties/index.d.ts +4 -2
  159. package/dist/properties/index.d.ts.map +1 -0
  160. package/{lib → dist}/properties/index.js +1 -0
  161. package/dist/properties/index.js.map +1 -0
  162. package/{lib → dist}/properties/types/AbstractPropertyType.d.ts +1 -0
  163. package/dist/properties/types/AbstractPropertyType.d.ts.map +1 -0
  164. package/{lib → dist}/properties/types/AbstractPropertyType.js +1 -0
  165. package/dist/properties/types/AbstractPropertyType.js.map +1 -0
  166. package/{lib → dist}/properties/types/PropertyClassType.d.ts +1 -0
  167. package/dist/properties/types/PropertyClassType.d.ts.map +1 -0
  168. package/{lib → dist}/properties/types/PropertyClassType.js +1 -0
  169. package/dist/properties/types/PropertyClassType.js.map +1 -0
  170. package/{lib → dist}/properties/types/PropertyHTMLType.d.ts +1 -0
  171. package/dist/properties/types/PropertyHTMLType.d.ts.map +1 -0
  172. package/{lib → dist}/properties/types/PropertyHTMLType.js +1 -2
  173. package/dist/properties/types/PropertyHTMLType.js.map +1 -0
  174. package/{lib → dist}/properties/types/PropertyTypes.d.ts +1 -0
  175. package/dist/properties/types/PropertyTypes.d.ts.map +1 -0
  176. package/{lib → dist}/properties/types/PropertyTypes.js +1 -0
  177. package/dist/properties/types/PropertyTypes.js.map +1 -0
  178. package/package.json +47 -46
  179. package/src/Core.ts +145 -0
  180. package/src/common/DataGenerator.ts +81 -0
  181. package/src/common/ResourcesErrors.ts +17 -0
  182. package/src/common/statuses.ts +20 -0
  183. package/src/components/AbstractObject.ts +71 -0
  184. package/src/components/BaseObject.ts +123 -0
  185. package/src/components/BaseObjectProperties.ts +65 -0
  186. package/src/components/DataObject.ts +418 -0
  187. package/src/components/Entity.ts +38 -0
  188. package/src/components/ObjectUri.test.ts +74 -0
  189. package/src/components/ObjectUri.ts +174 -0
  190. package/src/components/User.ts +111 -0
  191. package/src/components/index.ts +19 -0
  192. package/src/components/types/AbstractObjectClass.ts +7 -0
  193. package/src/components/types/BaseObjectClass.ts +8 -0
  194. package/src/components/types/BaseObjectType.ts +10 -0
  195. package/src/components/types/DataObjectClass.ts +27 -0
  196. package/src/components/types/DataObjectParams.ts +8 -0
  197. package/src/components/types/DataObjectType.ts +27 -0
  198. package/src/components/types/EntityClass.ts +3 -0
  199. package/src/components/types/Payload.ts +11 -0
  200. package/src/components/types/Persisted.ts +7 -0
  201. package/src/components/types/Properties.ts +1 -0
  202. package/src/components/types/ProxyConstructor.ts +13 -0
  203. package/src/components/types/toJSONParams.ts +12 -0
  204. package/src/index.ts +97 -0
  205. package/src/properties/ArrayProperty.test.ts +46 -0
  206. package/src/properties/ArrayProperty.ts +87 -0
  207. package/src/properties/BaseProperty.test.ts +79 -0
  208. package/src/properties/BaseProperty.ts +140 -0
  209. package/src/properties/BooleanProperty.ts +11 -0
  210. package/src/properties/CollectionProperty.test.ts +22 -0
  211. package/src/properties/CollectionProperty.ts +43 -0
  212. package/src/properties/DateTimeProperty.ts +47 -0
  213. package/src/properties/EnumProperty.ts +36 -0
  214. package/src/properties/FileProperty.ts +14 -0
  215. package/src/properties/HashProperty.test.ts +108 -0
  216. package/src/properties/HashProperty.ts +70 -0
  217. package/src/properties/MapProperty.ts +19 -0
  218. package/src/properties/NumberProperty.test.ts +38 -0
  219. package/src/properties/NumberProperty.ts +110 -0
  220. package/src/properties/ObjectProperty.ts +118 -0
  221. package/src/properties/Property.ts +85 -0
  222. package/src/properties/StringProperty.test.ts +46 -0
  223. package/src/properties/StringProperty.ts +121 -0
  224. package/src/properties/index.ts +62 -0
  225. package/src/properties/types/AbstractPropertyType.ts +8 -0
  226. package/src/properties/types/PropertyClassType.ts +11 -0
  227. package/src/properties/types/PropertyHTMLType.ts +23 -0
  228. package/src/properties/types/PropertyTypes.ts +13 -0
  229. package/lib/Core.js +0 -133
  230. package/lib/components/BaseObject.js +0 -103
  231. package/lib/components/BaseObjectProperties.d.ts +0 -1
  232. package/lib/components/Entity.js +0 -71
  233. package/lib/components/types/Properties.d.ts +0 -3
  234. package/lib/components/types/Properties.js +0 -1
@@ -0,0 +1,38 @@
1
+ import { NumberProperty } from './NumberProperty'
2
+
3
+ describe('Number Property accept all numbers', () => {
4
+ const prop = new NumberProperty({ name: 'toto' })
5
+ test('allow positive number', () => expect(prop.set(10).val()).toBe(10))
6
+ test('allow zero number', () => expect(prop.set(0).val()).toBe(0))
7
+ test('allow negative number', () => expect(prop.set(-10).val()).toBe(-10))
8
+ })
9
+
10
+ describe('Number Property can restrict signed numbers', () => {
11
+ const prop = new NumberProperty({
12
+ name: 'toto',
13
+ sign: NumberProperty.TYPE_UNSIGNED,
14
+ })
15
+ test('only allow positive or zero number', () =>
16
+ expect(() => prop.set(-1)).toThrow())
17
+ })
18
+
19
+ describe('Number Property can display prefix or/and suffix', () => {
20
+ test('display prefix', () =>
21
+ expect(
22
+ new NumberProperty({
23
+ name: 'toto',
24
+ prefix: 'EUR',
25
+ })
26
+ .set(123)
27
+ .val(NumberProperty.TRANSFORM_FORMATTED)
28
+ ).toBe('EUR 123'))
29
+ test('display suffix', () =>
30
+ expect(
31
+ new NumberProperty({
32
+ name: 'toto',
33
+ suffix: '€',
34
+ })
35
+ .set(123)
36
+ .val(NumberProperty.TRANSFORM_FORMATTED)
37
+ ).toBe('123 €'))
38
+ })
@@ -0,0 +1,110 @@
1
+ import { BaseProperty, BasePropertyType } from './BaseProperty'
2
+
3
+ export type NumberSign =
4
+ | typeof NumberProperty.TYPE_SIGNED
5
+ | typeof NumberProperty.TYPE_UNSIGNED
6
+
7
+ export type NumberType =
8
+ | typeof NumberProperty.TYPE_INTEGER
9
+ | typeof NumberProperty.TYPE_FLOAT
10
+
11
+ export interface NumberPropertyType extends BasePropertyType {
12
+ minVal?: number
13
+ maxVal?: number
14
+ sign?: NumberSign
15
+ type?: NumberType
16
+ prefix?: string
17
+ suffix?: string
18
+ precision?: number
19
+ }
20
+
21
+ export class NumberProperty extends BaseProperty {
22
+ static TYPE = 'number'
23
+ static TYPE_SIGNED = 'signed'
24
+ static TYPE_UNSIGNED = 'unsigned'
25
+
26
+ static TYPE_INTEGER = 'integer'
27
+ static TYPE_FLOAT = 'float'
28
+
29
+ static SEPARATOR = ' '
30
+
31
+ static TRANSFORM_FORMATTED = 'formatted'
32
+
33
+ protected _minVal: number | undefined = undefined
34
+ protected _maxVal: number | undefined = undefined
35
+ protected _type: NumberType = NumberProperty.TYPE_INTEGER
36
+ protected _sign: NumberSign = NumberProperty.TYPE_SIGNED
37
+ protected _prefix: string = ''
38
+ protected _suffix: string = ''
39
+ protected _precision: number = 0
40
+
41
+ declare protected _value: number | undefined
42
+
43
+ constructor(config: NumberPropertyType) {
44
+ super(config)
45
+ this.minVal = config.minVal || undefined
46
+ this.maxVal = config.maxVal || undefined
47
+ this._prefix = config.prefix
48
+ ? `${config.prefix}${NumberProperty.SEPARATOR}`
49
+ : ''
50
+ this._suffix = config.suffix
51
+ ? `${NumberProperty.SEPARATOR}${config.suffix}`
52
+ : ''
53
+ if (config.type) {
54
+ this._type = config.type
55
+ }
56
+ if (config.sign) {
57
+ this._sign = config.sign
58
+ }
59
+ }
60
+
61
+ set(value: number, setChanged = true) {
62
+ if (this._sign === NumberProperty.TYPE_UNSIGNED && value < 0) {
63
+ throw new Error(`Value must be unsigned`)
64
+ }
65
+
66
+ if (this._minVal && value < this._minVal) {
67
+ throw new Error(`Value is below ${this._minVal}`)
68
+ }
69
+
70
+ if (this._maxVal && value > this._maxVal) {
71
+ throw new Error(`Value is above ${this._maxVal}`)
72
+ }
73
+
74
+ if (this._type === NumberProperty.TYPE_INTEGER) {
75
+ value = Math.floor(value)
76
+ }
77
+
78
+ return super.set(value, setChanged)
79
+ }
80
+
81
+ val(transform: string | undefined = undefined) {
82
+ switch (transform) {
83
+ case NumberProperty.TRANSFORM_FORMATTED:
84
+ return `${this._prefix}${this._value}${this._suffix}`
85
+
86
+ default:
87
+ return this._value
88
+ }
89
+ }
90
+
91
+ set minVal(min: number | undefined) {
92
+ this._minVal = min
93
+ }
94
+
95
+ get minVal() {
96
+ return this._minVal
97
+ }
98
+
99
+ set maxVal(max: number | undefined) {
100
+ this._maxVal = max
101
+ }
102
+
103
+ get maxVal() {
104
+ return this._maxVal
105
+ }
106
+
107
+ get type() {
108
+ return this._type
109
+ }
110
+ }
@@ -0,0 +1,118 @@
1
+ import { DataObject } from '../components/DataObject'
2
+ import { ObjectUri } from '../components/ObjectUri'
3
+ import { BaseProperty, BasePropertyType } from './BaseProperty'
4
+ import { BaseObjectClass } from '../components/types/BaseObjectClass'
5
+ import { Core } from '../Core'
6
+
7
+ // TODO move in types
8
+ export enum returnAs {
9
+ AS_OBJECTURIS = 'objectUris',
10
+ AS_DATAOBJECTS = 'dataObjects',
11
+ AS_INSTANCES = 'classInstances',
12
+ AS_IS = 'asIs',
13
+ }
14
+
15
+ export interface ObjectPropertyType extends BasePropertyType {
16
+ instanceOf: any //Function | string | Object
17
+ }
18
+
19
+ export class ObjectProperty extends BaseProperty {
20
+ static TYPE = 'object'
21
+ _value: BaseObjectClass | ObjectUri | undefined = undefined
22
+ _instanceOf: any //Function | string | Object
23
+
24
+ constructor(config: ObjectPropertyType) {
25
+ super(config)
26
+ this._instanceOf = config.instanceOf
27
+ }
28
+
29
+ get instanceOf() {
30
+ return this._instanceOf
31
+ }
32
+
33
+ val(transform: string | undefined = undefined) {
34
+ try {
35
+ if (typeof this._instanceOf === 'string') {
36
+ this._instanceOf = Core.getClass(this._instanceOf)
37
+ }
38
+
39
+ if (!this._value) {
40
+ return this._defaultValue
41
+ }
42
+
43
+ switch (transform) {
44
+ case returnAs.AS_DATAOBJECTS:
45
+ if (this._value instanceof DataObject) {
46
+ Core.debug(`Returning already existing dataObject`)
47
+ return this._value
48
+ } else if (this._value instanceof ObjectUri) {
49
+ Core.debug(`Converting objectUri -> dataObject`)
50
+ return DataObject.factory({
51
+ properties: Reflect.get(
52
+ this._instanceOf,
53
+ 'PROPS_DEFINITION'
54
+ ),
55
+ uri: this._value,
56
+ })
57
+ } else {
58
+ Core.debug(`Converting instance -> dataObject`)
59
+ return this._value.dataObject
60
+ }
61
+ case returnAs.AS_INSTANCES:
62
+ if (this._value instanceof DataObject) {
63
+ Core.debug(`Converting dataObject -> instance`)
64
+ return Reflect.construct(this._instanceOf, [this._value])
65
+ } else if (this._value instanceof ObjectUri) {
66
+ Core.debug(`Converting objectUri -> dataObject -> instance`)
67
+ const dao = DataObject.factory({
68
+ properties: Reflect.get(
69
+ this._instanceOf,
70
+ 'PROPS_DEFINITION'
71
+ ),
72
+ uri: this._value,
73
+ })
74
+ return Reflect.construct(this._instanceOf, [dao])
75
+ } else {
76
+ Core.log(`Returning already existing instance`)
77
+ return this._value
78
+ }
79
+ case returnAs.AS_OBJECTURIS:
80
+ default:
81
+ return this._value
82
+ }
83
+ } catch (err) {
84
+ Core.error(
85
+ `Error in ObjectProperty.val() for property ${this._name}: ${err}`
86
+ )
87
+ console.log(err)
88
+ return undefined
89
+ }
90
+ }
91
+
92
+ set(value: object, setChanged = true) {
93
+ if (
94
+ value! instanceof ObjectUri &&
95
+ value! instanceof DataObject &&
96
+ value.constructor.name !== this._instanceOf.constructor.name
97
+ ) {
98
+ throw new Error(
99
+ `value ${JSON.stringify(value)} is not an instance of ${
100
+ this._instanceOf.constructor.name
101
+ }`
102
+ )
103
+ }
104
+
105
+ return super.set(value, setChanged)
106
+ }
107
+
108
+ toJSON() {
109
+ if (this._value instanceof ObjectUri) {
110
+ return this._value.toJSON()
111
+ }
112
+
113
+ return this._value &&
114
+ (this._value.dataObject || this._value instanceof DataObject)
115
+ ? this._value.dataObject.toReference()
116
+ : null
117
+ }
118
+ }
@@ -0,0 +1,85 @@
1
+ import { DataObjectClass } from '../components/types/DataObjectClass'
2
+ import { BaseProperty } from './BaseProperty'
3
+ import { BooleanProperty, BooleanPropertyType } from './BooleanProperty'
4
+ import { DateTimeProperty, DateTimePropertyType } from './DateTimeProperty'
5
+ import { EnumProperty, EnumPropertyType } from './EnumProperty'
6
+ import { HashProperty, HashPropertyType } from './HashProperty'
7
+ import { StringProperty, StringPropertyType } from './StringProperty'
8
+ import { NumberProperty, NumberPropertyType } from './NumberProperty'
9
+ import { ObjectProperty, ObjectPropertyType } from './ObjectProperty'
10
+ import { CollectionProperty } from './CollectionProperty'
11
+ import { ArrayProperty } from './ArrayProperty'
12
+ import { MapProperty } from './MapProperty'
13
+ import { FileProperty, FilePropertyType } from './FileProperty'
14
+
15
+ export class Property {
16
+ static TYPE_ANY = 'any'
17
+ static TYPE_NUMBER = 'number'
18
+ static TYPE_STRING = 'string'
19
+ static TYPE_OBJECT = 'object'
20
+ static TYPE_ENUM = 'enum'
21
+ static TYPE_BOOLEAN = 'boolean'
22
+ static TYPE_HASH = 'hash'
23
+ static TYPE_DATETIME = 'datetime'
24
+ static TYPE_ARRAY = 'array'
25
+ static TYPE_MAP = 'map'
26
+ static TYPE_FILE = 'file'
27
+
28
+ static factory(
29
+ params: ObjectPropertyType &
30
+ StringPropertyType &
31
+ NumberPropertyType &
32
+ EnumPropertyType &
33
+ BooleanPropertyType &
34
+ HashPropertyType &
35
+ DateTimePropertyType &
36
+ FilePropertyType,
37
+ parent: DataObjectClass<any>
38
+ ) {
39
+ params.parent = parent
40
+ switch (params.type) {
41
+ case BaseProperty.TYPE:
42
+ return new BaseProperty(params)
43
+
44
+ case StringProperty.TYPE:
45
+ return new StringProperty(params)
46
+
47
+ case NumberProperty.TYPE:
48
+ return new NumberProperty(params)
49
+
50
+ case ObjectProperty.TYPE:
51
+ return new ObjectProperty(params)
52
+
53
+ case EnumProperty.TYPE:
54
+ return new EnumProperty(params)
55
+
56
+ case BooleanProperty.TYPE:
57
+ return new BooleanProperty(params)
58
+
59
+ case HashProperty.TYPE:
60
+ return new HashProperty(params)
61
+
62
+ case DateTimeProperty.TYPE:
63
+ return new DateTimeProperty(params)
64
+
65
+ case CollectionProperty.TYPE:
66
+ if (!('instanceOf' in params)) {
67
+ throw new Error('Missing property instanceOf!')
68
+ }
69
+
70
+ return new CollectionProperty(params as any)
71
+
72
+ case ArrayProperty.TYPE:
73
+ return new ArrayProperty(params)
74
+
75
+ case MapProperty.TYPE:
76
+ return new MapProperty(params)
77
+
78
+ case FileProperty.TYPE:
79
+ return new FileProperty(params)
80
+
81
+ default:
82
+ throw new Error(`Unknown property type ${params.type}`)
83
+ }
84
+ }
85
+ }
@@ -0,0 +1,46 @@
1
+ import { StringProperty } from './StringProperty'
2
+
3
+ describe('String Property without digits', () => {
4
+ const prop = new StringProperty({ name: 'toto', allowDigits: false })
5
+ test('allow digits is not set', () =>
6
+ expect(prop.allows).not.toContain(StringProperty.ALLOW_DIGITS))
7
+ test('no digits allowed', () => expect(() => prop.set('va1ue')).toThrow())
8
+ })
9
+
10
+ describe('String Property without spaces', () => {
11
+ const prop = new StringProperty({ name: 'toto', allowSpaces: false })
12
+ test('allow spaces is not set', () =>
13
+ expect(prop.allows).not.toContain(StringProperty.ALLOW_SPACES))
14
+ test('no space allowed', () => expect(() => prop.set('va1 ue')).toThrow())
15
+ })
16
+
17
+ describe('String Property without letters', () => {
18
+ const prop = new StringProperty({ name: 'toto', allowLetters: false })
19
+ test('allow letters is not set', () =>
20
+ expect(prop.allows).not.toContain(StringProperty.ALLOW_LETTERS))
21
+ test('no letters allowed', () => expect(() => prop.set('value')).toThrow())
22
+ })
23
+
24
+ describe('String Property with minimal length', () => {
25
+ const prop = new StringProperty({ name: 'toto', minLength: 6 })
26
+ test('minlength is set', () => expect(prop.minLength).toBeGreaterThan(0))
27
+ test('minimum length of 5', () => expect(() => prop.set('value')).toThrow())
28
+ })
29
+
30
+ describe('String Property with maximal length', () => {
31
+ const prop = new StringProperty({ name: 'toto', maxLength: 4 })
32
+ test('maxlength is set', () => expect(prop.maxLength).toBeGreaterThan(0))
33
+ test('maximum length of 5', () => expect(() => prop.set('value')).toThrow())
34
+ })
35
+
36
+ describe('String Property provides transformers', () => {
37
+ const prop = new StringProperty({ name: 'toto' })
38
+ test('transform to upper case', () =>
39
+ expect(prop.set('value').get(StringProperty.TRANSFORM_UCASE)).toBe(
40
+ 'VALUE'
41
+ ))
42
+ test('transform to lower case', () =>
43
+ expect(prop.set('VALUE').get(StringProperty.TRANSFORM_LCASE)).toBe(
44
+ 'value'
45
+ ))
46
+ })
@@ -0,0 +1,121 @@
1
+ //import { Property } from './Property'
2
+ import { BaseProperty, BasePropertyType } from './BaseProperty'
3
+
4
+ export interface StringPropertyType extends BasePropertyType {
5
+ minLength?: number
6
+ maxLength?: number
7
+ allowSpaces?: boolean
8
+ allowDigits?: boolean
9
+ allowLetters?: boolean
10
+ allowPattern?: string
11
+ fullSearch?: boolean
12
+ }
13
+
14
+ export class StringProperty extends BaseProperty {
15
+ static TYPE = 'string'
16
+
17
+ static TRANSFORM_UCASE = 'upper'
18
+ static TRANSFORM_LCASE = 'lower'
19
+
20
+ static ALLOW_SPACES = 'spaces'
21
+ static ALLOW_LETTERS = 'letters'
22
+ static ALLOW_DIGITS = 'digits'
23
+ static ALLOW_STRINGS = 'strings'
24
+ static ALLOW_NUMBERS = 'numbers'
25
+
26
+ protected _minLength: number = 0
27
+ protected _maxLength: number = 0
28
+ protected _fullSearch: boolean = false
29
+
30
+ protected declare _value: string | undefined
31
+
32
+ /**
33
+ * Set to false to bypass some rules
34
+ */
35
+ protected _rawValue = true
36
+
37
+ constructor(config: StringPropertyType) {
38
+ super(config)
39
+ this.minLength = config.minLength || 0
40
+ this.maxLength = config.maxLength || 0
41
+ this.fullSearch = config.fullSearch || false
42
+ if (this._enable(config.allowSpaces)) {
43
+ this._allows.push(StringProperty.ALLOW_SPACES)
44
+ }
45
+ if (this._enable(config.allowDigits)) {
46
+ this._allows.push(StringProperty.ALLOW_DIGITS)
47
+ }
48
+ if (this._enable(config.allowLetters)) {
49
+ this._allows.push(StringProperty.ALLOW_LETTERS)
50
+ }
51
+ }
52
+
53
+ set(value: any, setChanged = true) {
54
+ if (this._rawValue && value !== null && value !== undefined) {
55
+ if (
56
+ !this._allows.includes(StringProperty.ALLOW_DIGITS) &&
57
+ /\d/.test(value)
58
+ ) {
59
+ throw new Error(`Digits are not allowed in value`)
60
+ }
61
+
62
+ if (
63
+ !this._allows.includes(StringProperty.ALLOW_SPACES) &&
64
+ /\s/.test(value)
65
+ ) {
66
+ throw new Error(`Spaces are not allowed in value`)
67
+ }
68
+
69
+ if (
70
+ !this._allows.includes(StringProperty.ALLOW_LETTERS) &&
71
+ /[a-zA-Z]/.test(value)
72
+ ) {
73
+ throw new Error(`Letters are not allowed in value`)
74
+ }
75
+
76
+ if (this._minLength > 0 && value.length < this._minLength) {
77
+ throw new Error(`Value is too short`)
78
+ }
79
+
80
+ if (this._maxLength > 0 && value.length > this._maxLength) {
81
+ throw new Error(`${this._name}: value '${value}' is too long`)
82
+ }
83
+ }
84
+ return super.set(value, setChanged)
85
+ }
86
+
87
+ get(transform: string | undefined = undefined) {
88
+ switch (transform) {
89
+ case StringProperty.TRANSFORM_LCASE:
90
+ return this._value && this._value.toLowerCase()
91
+ case StringProperty.TRANSFORM_UCASE:
92
+ return this._value && this._value.toUpperCase()
93
+ default:
94
+ return this._value
95
+ }
96
+ }
97
+
98
+ set minLength(min: number) {
99
+ this._minLength = min >= 0 ? min : 0
100
+ }
101
+
102
+ get minLength() {
103
+ return this._minLength
104
+ }
105
+
106
+ set maxLength(max: number) {
107
+ this._maxLength = max >= 0 ? max : 0
108
+ }
109
+
110
+ get maxLength() {
111
+ return this._maxLength
112
+ }
113
+
114
+ set fullSearch(mode: boolean) {
115
+ this._fullSearch = mode
116
+ }
117
+
118
+ get fullSearch() {
119
+ return this._fullSearch
120
+ }
121
+ }
@@ -0,0 +1,62 @@
1
+ import { Property } from './Property'
2
+ import { BaseProperty, BasePropertyType } from './BaseProperty'
3
+ import { ArrayProperty, ArrayPropertyType } from './ArrayProperty'
4
+ import { NumberProperty, NumberPropertyType } from './NumberProperty'
5
+ import { StringProperty, StringPropertyType } from './StringProperty'
6
+ import { HashProperty, HashPropertyType } from './HashProperty'
7
+ import { EnumProperty, EnumPropertyType } from './EnumProperty'
8
+ import { BooleanProperty, BooleanPropertyType } from './BooleanProperty'
9
+ import { DateTimeProperty, DateTimePropertyType } from './DateTimeProperty'
10
+ import { ObjectProperty, ObjectPropertyType } from './ObjectProperty'
11
+ import { MapProperty, MapPropertyType } from './MapProperty'
12
+ import { FileProperty, FilePropertyType } from './FileProperty'
13
+ import {
14
+ CollectionProperty,
15
+ CollectionPropertyType,
16
+ } from './CollectionProperty'
17
+ import { PropertyTypes } from './types/PropertyTypes'
18
+
19
+ export {
20
+ Property,
21
+ BaseProperty,
22
+ ArrayProperty,
23
+ NumberProperty,
24
+ BooleanProperty,
25
+ DateTimeProperty,
26
+ EnumProperty,
27
+ ObjectProperty,
28
+ StringProperty,
29
+ HashProperty,
30
+ MapProperty,
31
+ FileProperty,
32
+ CollectionProperty,
33
+ }
34
+ export type {
35
+ BasePropertyType,
36
+ ArrayPropertyType,
37
+ NumberPropertyType,
38
+ BooleanPropertyType,
39
+ DateTimePropertyType,
40
+ EnumPropertyType,
41
+ ObjectPropertyType,
42
+ StringPropertyType,
43
+ HashPropertyType,
44
+ MapPropertyType,
45
+ FilePropertyType,
46
+ CollectionPropertyType,
47
+ }
48
+
49
+ type BaseType = { type: PropertyTypes }
50
+
51
+ export type DataObjectProperties = (
52
+ | (NumberPropertyType & BaseType)
53
+ | (BooleanPropertyType & BaseType)
54
+ | (EnumPropertyType & BaseType)
55
+ | (ObjectPropertyType & BaseType)
56
+ | (StringPropertyType & BaseType)
57
+ | (HashPropertyType & BaseType)
58
+ | (DateTimePropertyType & BaseType)
59
+ | (ArrayPropertyType & BaseType)
60
+ | (MapPropertyType & BaseType)
61
+ | (CollectionPropertyType & BaseType)
62
+ )[]
@@ -0,0 +1,8 @@
1
+ import { PropertyTypes } from './PropertyTypes'
2
+
3
+ export interface AbstractPropertyType {
4
+ name: string
5
+ id?: string
6
+ type?: PropertyTypes
7
+ defaultValue?: any
8
+ }
@@ -0,0 +1,11 @@
1
+ import { AbstractPropertyType } from './AbstractPropertyType'
2
+
3
+ export interface PropertyClassType {
4
+ id?: string
5
+ name: string
6
+ set(value: any, setChanged?: boolean): AbstractPropertyType
7
+ val(transform?: any): any
8
+ toJSON(): any
9
+ clone(): PropertyClassType
10
+ hasChanged?: boolean
11
+ }
@@ -0,0 +1,23 @@
1
+ // @see https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/autocomplete
2
+ // @see https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#autofill
3
+
4
+ export const OFF = 'off'
5
+ export const NAME = 'name'
6
+ export const GIVEN_NAME = 'given-name'
7
+ export const FAMILY_NAME = 'family-name'
8
+ export const EMAIL = 'email'
9
+ export const PASSWORD = 'password'
10
+ export const BIRTHDAY = 'bday'
11
+ export const GENDER = 'sex'
12
+ export const ORG = 'organization'
13
+
14
+ export type PropertyHTMLType =
15
+ | typeof OFF
16
+ | typeof NAME
17
+ | typeof GIVEN_NAME
18
+ | typeof FAMILY_NAME
19
+ | typeof EMAIL
20
+ | typeof PASSWORD
21
+ | typeof BIRTHDAY
22
+ | typeof GENDER
23
+ | typeof ORG
@@ -0,0 +1,13 @@
1
+ import { Property } from '..'
2
+
3
+ export type PropertyTypes =
4
+ | typeof Property.TYPE_STRING
5
+ | typeof Property.TYPE_NUMBER
6
+ | typeof Property.TYPE_ENUM
7
+ | typeof Property.TYPE_BOOLEAN
8
+ | typeof Property.TYPE_OBJECT
9
+ | typeof Property.TYPE_DATETIME
10
+ | typeof Property.TYPE_HASH
11
+ | typeof Property.TYPE_ARRAY
12
+
13
+ export type BaseType = { type: PropertyTypes }