@decaf-ts/decorator-validation 1.5.1 → 1.5.2

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 (170) hide show
  1. package/package.json +15 -9
  2. package/dist/decorator-validation.bundle.min.js +0 -2
  3. package/dist/decorator-validation.bundle.min.js.LICENSE.txt +0 -14
  4. package/dist/esm/decorator-validation.bundle.min.esm.js +0 -2
  5. package/dist/esm/decorator-validation.bundle.min.esm.js.LICENSE.txt +0 -14
  6. package/lib/esm/index.d.ts +0 -42
  7. package/lib/esm/index.js +0 -42
  8. package/lib/esm/model/Model.d.ts +0 -138
  9. package/lib/esm/model/Model.js +0 -296
  10. package/lib/esm/model/ModelErrorDefinition.d.ts +0 -22
  11. package/lib/esm/model/ModelErrorDefinition.js +0 -52
  12. package/lib/esm/model/Registry.d.ts +0 -59
  13. package/lib/esm/model/Registry.js +0 -73
  14. package/lib/esm/model/constants.d.ts +0 -56
  15. package/lib/esm/model/constants.js +0 -69
  16. package/lib/esm/model/construction.d.ts +0 -29
  17. package/lib/esm/model/construction.js +0 -63
  18. package/lib/esm/model/decorators.d.ts +0 -25
  19. package/lib/esm/model/decorators.js +0 -62
  20. package/lib/esm/model/index.d.ts +0 -9
  21. package/lib/esm/model/index.js +0 -9
  22. package/lib/esm/model/types.d.ts +0 -79
  23. package/lib/esm/model/types.js +0 -1
  24. package/lib/esm/model/utils.d.ts +0 -11
  25. package/lib/esm/model/utils.js +0 -25
  26. package/lib/esm/model/validation.d.ts +0 -14
  27. package/lib/esm/model/validation.js +0 -135
  28. package/lib/esm/utils/constants.d.ts +0 -26
  29. package/lib/esm/utils/constants.js +0 -27
  30. package/lib/esm/utils/dates.d.ts +0 -76
  31. package/lib/esm/utils/dates.js +0 -243
  32. package/lib/esm/utils/decorators.d.ts +0 -2
  33. package/lib/esm/utils/decorators.js +0 -18
  34. package/lib/esm/utils/hashing.d.ts +0 -52
  35. package/lib/esm/utils/hashing.js +0 -99
  36. package/lib/esm/utils/index.d.ts +0 -7
  37. package/lib/esm/utils/index.js +0 -7
  38. package/lib/esm/utils/registry.d.ts +0 -68
  39. package/lib/esm/utils/registry.js +0 -1
  40. package/lib/esm/utils/serialization.d.ts +0 -79
  41. package/lib/esm/utils/serialization.js +0 -88
  42. package/lib/esm/utils/strings.d.ts +0 -25
  43. package/lib/esm/utils/strings.js +0 -31
  44. package/lib/esm/validation/Validation.d.ts +0 -51
  45. package/lib/esm/validation/Validation.js +0 -71
  46. package/lib/esm/validation/Validators/DateValidator.d.ts +0 -30
  47. package/lib/esm/validation/Validators/DateValidator.js +0 -54
  48. package/lib/esm/validation/Validators/EmailValidator.d.ts +0 -27
  49. package/lib/esm/validation/Validators/EmailValidator.js +0 -50
  50. package/lib/esm/validation/Validators/ListValidator.d.ts +0 -30
  51. package/lib/esm/validation/Validators/ListValidator.js +0 -65
  52. package/lib/esm/validation/Validators/MaxLengthValidator.d.ts +0 -31
  53. package/lib/esm/validation/Validators/MaxLengthValidator.js +0 -52
  54. package/lib/esm/validation/Validators/MaxValidator.d.ts +0 -30
  55. package/lib/esm/validation/Validators/MaxValidator.js +0 -57
  56. package/lib/esm/validation/Validators/MinLengthValidator.d.ts +0 -31
  57. package/lib/esm/validation/Validators/MinLengthValidator.js +0 -52
  58. package/lib/esm/validation/Validators/MinValidator.d.ts +0 -30
  59. package/lib/esm/validation/Validators/MinValidator.js +0 -57
  60. package/lib/esm/validation/Validators/PasswordValidator.d.ts +0 -27
  61. package/lib/esm/validation/Validators/PasswordValidator.js +0 -50
  62. package/lib/esm/validation/Validators/PatternValidator.d.ts +0 -39
  63. package/lib/esm/validation/Validators/PatternValidator.js +0 -70
  64. package/lib/esm/validation/Validators/RequiredValidator.d.ts +0 -27
  65. package/lib/esm/validation/Validators/RequiredValidator.js +0 -57
  66. package/lib/esm/validation/Validators/StepValidator.d.ts +0 -31
  67. package/lib/esm/validation/Validators/StepValidator.js +0 -52
  68. package/lib/esm/validation/Validators/TypeValidator.d.ts +0 -29
  69. package/lib/esm/validation/Validators/TypeValidator.js +0 -60
  70. package/lib/esm/validation/Validators/URLValidator.d.ts +0 -32
  71. package/lib/esm/validation/Validators/URLValidator.js +0 -46
  72. package/lib/esm/validation/Validators/Validator.d.ts +0 -43
  73. package/lib/esm/validation/Validators/Validator.js +0 -47
  74. package/lib/esm/validation/Validators/ValidatorRegistry.d.ts +0 -47
  75. package/lib/esm/validation/Validators/ValidatorRegistry.js +0 -80
  76. package/lib/esm/validation/Validators/constants.d.ts +0 -96
  77. package/lib/esm/validation/Validators/constants.js +0 -134
  78. package/lib/esm/validation/Validators/decorators.d.ts +0 -12
  79. package/lib/esm/validation/Validators/decorators.js +0 -25
  80. package/lib/esm/validation/Validators/index.d.ts +0 -52
  81. package/lib/esm/validation/Validators/index.js +0 -52
  82. package/lib/esm/validation/Validators/types.d.ts +0 -51
  83. package/lib/esm/validation/Validators/types.js +0 -1
  84. package/lib/esm/validation/decorators.d.ts +0 -178
  85. package/lib/esm/validation/decorators.js +0 -288
  86. package/lib/esm/validation/index.d.ts +0 -4
  87. package/lib/esm/validation/index.js +0 -4
  88. package/lib/esm/validation/types.d.ts +0 -46
  89. package/lib/esm/validation/types.js +0 -1
  90. package/lib/index.cjs +0 -59
  91. package/lib/index.d.ts +0 -42
  92. package/lib/model/Model.cjs +0 -300
  93. package/lib/model/Model.d.ts +0 -138
  94. package/lib/model/ModelErrorDefinition.cjs +0 -56
  95. package/lib/model/ModelErrorDefinition.d.ts +0 -22
  96. package/lib/model/Registry.cjs +0 -78
  97. package/lib/model/Registry.d.ts +0 -59
  98. package/lib/model/constants.cjs +0 -72
  99. package/lib/model/constants.d.ts +0 -56
  100. package/lib/model/construction.cjs +0 -68
  101. package/lib/model/construction.d.ts +0 -29
  102. package/lib/model/decorators.cjs +0 -67
  103. package/lib/model/decorators.d.ts +0 -25
  104. package/lib/model/index.cjs +0 -25
  105. package/lib/model/index.d.ts +0 -9
  106. package/lib/model/types.cjs +0 -2
  107. package/lib/model/types.d.ts +0 -79
  108. package/lib/model/utils.cjs +0 -29
  109. package/lib/model/utils.d.ts +0 -11
  110. package/lib/model/validation.cjs +0 -138
  111. package/lib/model/validation.d.ts +0 -14
  112. package/lib/utils/constants.cjs +0 -30
  113. package/lib/utils/constants.d.ts +0 -26
  114. package/lib/utils/dates.cjs +0 -251
  115. package/lib/utils/dates.d.ts +0 -76
  116. package/lib/utils/decorators.cjs +0 -22
  117. package/lib/utils/decorators.d.ts +0 -2
  118. package/lib/utils/hashing.cjs +0 -106
  119. package/lib/utils/hashing.d.ts +0 -52
  120. package/lib/utils/index.cjs +0 -23
  121. package/lib/utils/index.d.ts +0 -7
  122. package/lib/utils/registry.cjs +0 -2
  123. package/lib/utils/registry.d.ts +0 -68
  124. package/lib/utils/serialization.cjs +0 -93
  125. package/lib/utils/serialization.d.ts +0 -79
  126. package/lib/utils/strings.cjs +0 -35
  127. package/lib/utils/strings.d.ts +0 -25
  128. package/lib/validation/Validation.cjs +0 -75
  129. package/lib/validation/Validation.d.ts +0 -51
  130. package/lib/validation/Validators/DateValidator.cjs +0 -57
  131. package/lib/validation/Validators/DateValidator.d.ts +0 -30
  132. package/lib/validation/Validators/EmailValidator.cjs +0 -53
  133. package/lib/validation/Validators/EmailValidator.d.ts +0 -27
  134. package/lib/validation/Validators/ListValidator.cjs +0 -68
  135. package/lib/validation/Validators/ListValidator.d.ts +0 -30
  136. package/lib/validation/Validators/MaxLengthValidator.cjs +0 -55
  137. package/lib/validation/Validators/MaxLengthValidator.d.ts +0 -31
  138. package/lib/validation/Validators/MaxValidator.cjs +0 -60
  139. package/lib/validation/Validators/MaxValidator.d.ts +0 -30
  140. package/lib/validation/Validators/MinLengthValidator.cjs +0 -55
  141. package/lib/validation/Validators/MinLengthValidator.d.ts +0 -31
  142. package/lib/validation/Validators/MinValidator.cjs +0 -60
  143. package/lib/validation/Validators/MinValidator.d.ts +0 -30
  144. package/lib/validation/Validators/PasswordValidator.cjs +0 -53
  145. package/lib/validation/Validators/PasswordValidator.d.ts +0 -27
  146. package/lib/validation/Validators/PatternValidator.cjs +0 -73
  147. package/lib/validation/Validators/PatternValidator.d.ts +0 -39
  148. package/lib/validation/Validators/RequiredValidator.cjs +0 -60
  149. package/lib/validation/Validators/RequiredValidator.d.ts +0 -27
  150. package/lib/validation/Validators/StepValidator.cjs +0 -55
  151. package/lib/validation/Validators/StepValidator.d.ts +0 -31
  152. package/lib/validation/Validators/TypeValidator.cjs +0 -63
  153. package/lib/validation/Validators/TypeValidator.d.ts +0 -29
  154. package/lib/validation/Validators/URLValidator.cjs +0 -49
  155. package/lib/validation/Validators/URLValidator.d.ts +0 -32
  156. package/lib/validation/Validators/Validator.cjs +0 -51
  157. package/lib/validation/Validators/Validator.d.ts +0 -43
  158. package/lib/validation/Validators/ValidatorRegistry.cjs +0 -85
  159. package/lib/validation/Validators/ValidatorRegistry.d.ts +0 -47
  160. package/lib/validation/Validators/constants.cjs +0 -137
  161. package/lib/validation/Validators/constants.d.ts +0 -96
  162. package/lib/validation/Validators/decorators.cjs +0 -28
  163. package/lib/validation/Validators/index.cjs +0 -69
  164. package/lib/validation/Validators/types.cjs +0 -2
  165. package/lib/validation/decorators.cjs +0 -304
  166. package/lib/validation/decorators.d.ts +0 -178
  167. package/lib/validation/index.cjs +0 -20
  168. package/lib/validation/index.d.ts +0 -4
  169. package/lib/validation/types.cjs +0 -2
  170. package/lib/validation/types.d.ts +0 -46
@@ -1,243 +0,0 @@
1
- import "reflect-metadata";
2
- import { DAYS_OF_WEEK_NAMES, MONTH_NAMES, } from "../validation/Validators/constants";
3
- import { sf } from "./strings";
4
- /**
5
- * @summary Reverses the process from {@link formatDate}
6
- *
7
- * @param {string} date the date string to be converted back into date
8
- * @param {string} format the date format
9
- * @return {Date} the date from the format or the standard new Date({@prop date}) if the string couldn't be parsed (are you sure the format matches the string?)
10
- *
11
- * @function dateFromFormat
12
- * @memberOf module:decorator-validation.Utils.Dates
13
- * @category Format
14
- */
15
- export function dateFromFormat(date, format) {
16
- let formatRegexp = format;
17
- // Hour
18
- if (formatRegexp.match(/hh/))
19
- formatRegexp = formatRegexp.replace("hh", "(?<hour>\\d{2})");
20
- else if (formatRegexp.match(/h/))
21
- formatRegexp = formatRegexp.replace("h", "(?<hour>\\d{1,2})");
22
- else if (formatRegexp.match(/HH/))
23
- formatRegexp = formatRegexp.replace("HH", "(?<hour>\\d{2})");
24
- else if (formatRegexp.match(/H/))
25
- formatRegexp = formatRegexp.replace("H", "(?<hour>\\d{1,2})");
26
- // Minutes
27
- if (formatRegexp.match(/mm/))
28
- formatRegexp = formatRegexp.replace("mm", "(?<minutes>\\d{2})");
29
- else if (formatRegexp.match(/m/))
30
- formatRegexp = formatRegexp.replace("m", "(?<minutes>\\d{1,2})");
31
- // Seconds
32
- if (formatRegexp.match(/ss/))
33
- formatRegexp = formatRegexp.replace("ss", "(?<seconds>\\d{2})");
34
- else if (formatRegexp.match(/s/))
35
- formatRegexp = formatRegexp.replace("s", "(?<seconds>\\d{1,2})");
36
- // Day
37
- if (formatRegexp.match(/dd/))
38
- formatRegexp = formatRegexp.replace("dd", "(?<day>\\d{2})");
39
- else if (formatRegexp.match(/d/))
40
- formatRegexp = formatRegexp.replace("d", "(?<day>\\d{1,2})");
41
- // Day Of Week
42
- if (formatRegexp.match(/EEEE/))
43
- formatRegexp = formatRegexp.replace("EEEE", "(?<dayofweek>\\w+)");
44
- // eslint-disable-next-line no-dupe-else-if
45
- else if (formatRegexp.match(/EEEE/))
46
- formatRegexp = formatRegexp.replace("EEE", "(?<dayofweek>\\w+)");
47
- // Year
48
- if (formatRegexp.match(/yyyy/))
49
- formatRegexp = formatRegexp.replace("yyyy", "(?<year>\\d{4})");
50
- else if (formatRegexp.match(/yy/))
51
- formatRegexp = formatRegexp.replace("yy", "(?<year>\\d{2})");
52
- // Month
53
- if (formatRegexp.match(/MMMM/))
54
- formatRegexp = formatRegexp.replace("MMMM", "(?<monthname>\\w+)");
55
- else if (formatRegexp.match(/MMM/))
56
- formatRegexp = formatRegexp.replace("MMM", "(?<monthnamesmall>\\w+)");
57
- if (formatRegexp.match(/MM/))
58
- formatRegexp = formatRegexp.replace("MM", "(?<month>\\d{2})");
59
- else if (formatRegexp.match(/M/))
60
- formatRegexp = formatRegexp.replace("M", "(?<month>\\d{1,2})");
61
- // Milis and Am Pm
62
- formatRegexp = formatRegexp
63
- .replace("S", "(?<milis>\\d{1,3})")
64
- .replace("aaa", "(?<ampm>\\w{2})");
65
- const regexp = new RegExp(formatRegexp, "g");
66
- const match = regexp.exec(date);
67
- if (!match || !match.groups)
68
- return new Date(date);
69
- const safeParseInt = function (n) {
70
- if (!n)
71
- return 0;
72
- const result = parseInt(n);
73
- return isNaN(result) ? 0 : result;
74
- };
75
- const year = safeParseInt(match.groups.year);
76
- const day = safeParseInt(match.groups.day);
77
- const amPm = match.groups.ampm;
78
- let hour = safeParseInt(match.groups.hour);
79
- if (amPm)
80
- hour = amPm === "PM" ? hour + 12 : hour;
81
- const minutes = safeParseInt(match.groups.minutes);
82
- const seconds = safeParseInt(match.groups.seconds);
83
- const ms = safeParseInt(match.groups.milis);
84
- const monthName = match.groups.monthname;
85
- const monthNameSmall = match.groups.monthnamesmall;
86
- let month = match.groups.month;
87
- if (monthName)
88
- month = MONTH_NAMES.indexOf(monthName);
89
- else if (monthNameSmall) {
90
- const m = MONTH_NAMES.find((m) => m.toLowerCase().startsWith(monthNameSmall.toLowerCase()));
91
- if (!m)
92
- return new Date(date);
93
- month = MONTH_NAMES.indexOf(m);
94
- }
95
- else
96
- month = safeParseInt(`${month}`);
97
- return new Date(year, month - 1, day, hour, minutes, seconds, ms);
98
- }
99
- /**
100
- * @summary Binds a date format to a string
101
- * @param {Date} [date]
102
- * @param {string} [format]
103
- * @memberOf module:decorator-validation.Utils.Format
104
- * @category Utilities
105
- */
106
- export function bindDateToString(date, format) {
107
- if (!date)
108
- return;
109
- const func = () => formatDate(date, format);
110
- Object.defineProperty(date, "toISOString", {
111
- enumerable: false,
112
- configurable: false,
113
- value: func,
114
- });
115
- Object.defineProperty(date, "toString", {
116
- enumerable: false,
117
- configurable: false,
118
- value: func,
119
- });
120
- // Object.setPrototypeOf(date, Date.prototype);
121
- return date;
122
- }
123
- /**
124
- * @summary Helper function to be used instead of instanceOf Date
125
- * @param date
126
- * @memberOf module:decorator-validation.Utils.Dates
127
- * @category Validation
128
- */
129
- export function isValidDate(date) {
130
- return (date &&
131
- Object.prototype.toString.call(date) === "[object Date]" &&
132
- !Number.isNaN(date));
133
- }
134
- /**
135
- * @summary Util function to pad numbers
136
- * @param {number} num
137
- *
138
- * @return {string}
139
- *
140
- * @function twoDigitPad
141
- * @memberOf module:decorator-validation.Utils.Format
142
- * @category Format
143
- */
144
- export function twoDigitPad(num) {
145
- return num < 10 ? "0" + num : num.toString();
146
- }
147
- /**
148
- * @summary Date Format Handling
149
- * @description Code from {@link https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date}
150
- *
151
- * <pre>
152
- * Using similar formatting as Moment.js, Class DateTimeFormatter (Java), and Class SimpleDateFormat (Java),
153
- * I implemented a comprehensive solution formatDate(date, patternStr) where the code is easy to read and modify.
154
- * You can display date, time, AM/PM, etc.
155
- *
156
- * Date and Time Patterns
157
- * yy = 2-digit year; yyyy = full year
158
- * M = digit month; MM = 2-digit month; MMM = short month name; MMMM = full month name
159
- * EEEE = full weekday name; EEE = short weekday name
160
- * d = digit day; dd = 2-digit day
161
- * h = hours am/pm; hh = 2-digit hours am/pm; H = hours; HH = 2-digit hours
162
- * m = minutes; mm = 2-digit minutes; aaa = AM/PM
163
- * s = seconds; ss = 2-digit seconds
164
- * S = miliseconds
165
- * </pre>
166
- *
167
- * @param {Date} date
168
- * @param {string} [patternStr] defaults to 'yyyy/MM/dd'
169
- * @return {string} the formatted date
170
- *
171
- * @function formatDate
172
- * @memberOf module:decorator-validation.Utils.Dates
173
- * @category Format
174
- */
175
- export function formatDate(date, patternStr = "yyyy/MM/dd") {
176
- const day = date.getDate(), month = date.getMonth(), year = date.getFullYear(), hour = date.getHours(), minute = date.getMinutes(), second = date.getSeconds(), miliseconds = date.getMilliseconds(), h = hour % 12, hh = twoDigitPad(h), HH = twoDigitPad(hour), mm = twoDigitPad(minute), ss = twoDigitPad(second), aaa = hour < 12 ? "AM" : "PM", EEEE = DAYS_OF_WEEK_NAMES[date.getDay()], EEE = EEEE.substr(0, 3), dd = twoDigitPad(day), M = month + 1, MM = twoDigitPad(M), MMMM = MONTH_NAMES[month], MMM = MMMM.substr(0, 3), yyyy = year + "", yy = yyyy.substr(2, 2);
177
- // checks to see if month name will be used
178
- patternStr = patternStr
179
- .replace("hh", hh)
180
- .replace("h", h.toString())
181
- .replace("HH", HH)
182
- .replace("H", hour.toString())
183
- .replace("mm", mm)
184
- .replace("m", minute.toString())
185
- .replace("ss", ss)
186
- .replace("s", second.toString())
187
- .replace("S", miliseconds.toString())
188
- .replace("dd", dd)
189
- .replace("d", day.toString())
190
- .replace("EEEE", EEEE)
191
- .replace("EEE", EEE)
192
- .replace("yyyy", yyyy)
193
- .replace("yy", yy)
194
- .replace("aaa", aaa);
195
- if (patternStr.indexOf("MMM") > -1) {
196
- patternStr = patternStr.replace("MMMM", MMMM).replace("MMM", MMM);
197
- }
198
- else {
199
- patternStr = patternStr.replace("MM", MM).replace("M", M.toString());
200
- }
201
- return patternStr;
202
- }
203
- /**
204
- * @summary Parses a date from a specified format
205
- * @param {string} format
206
- * @param {string | Date | number} [v]
207
- * @memberOf module:decorator-validation.Utils.Dates
208
- * @category Format
209
- */
210
- export function parseDate(format, v) {
211
- let value = undefined;
212
- if (!v)
213
- return undefined;
214
- if (v instanceof Date)
215
- try {
216
- value = dateFromFormat(formatDate(v, format), format);
217
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
218
- }
219
- catch (e) {
220
- throw new Error(sf("Could not convert date {0} to format: {1}", v.toString(), format));
221
- }
222
- else if (typeof v === "string") {
223
- value = dateFromFormat(v, format);
224
- }
225
- else if (typeof v === "number") {
226
- const d = new Date(v);
227
- value = dateFromFormat(formatDate(d, format), format);
228
- }
229
- else if (isValidDate(v)) {
230
- try {
231
- const d = new Date(v);
232
- value = dateFromFormat(formatDate(d, format), format);
233
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
234
- }
235
- catch (e) {
236
- throw new Error(sf("Could not convert date {0} to format: {1}", v, format));
237
- }
238
- }
239
- else {
240
- throw new Error(`Invalid value provided ${v}`);
241
- }
242
- return bindDateToString(value, format);
243
- }
@@ -1,2 +0,0 @@
1
- export declare function prop(key?: string): (model: object, propertyKey?: any) => void;
2
- export declare function propMetadata<V>(key: string, value: V): (target: object, propertyKey?: string | symbol | unknown, descriptor?: PropertyDescriptor) => void;
@@ -1,18 +0,0 @@
1
- import { apply, metadata } from "@decaf-ts/reflection";
2
- import { ModelKeys } from "./constants";
3
- export function prop(key = ModelKeys.ATTRIBUTE) {
4
- return (model, propertyKey) => {
5
- let props;
6
- if (Object.prototype.hasOwnProperty.call(model, key)) {
7
- props = model[key];
8
- }
9
- else {
10
- props = model[key] = [];
11
- }
12
- if (!props.includes(propertyKey))
13
- props.push(propertyKey);
14
- };
15
- }
16
- export function propMetadata(key, value) {
17
- return apply(prop(), metadata(key, value));
18
- }
@@ -1,52 +0,0 @@
1
- /**
2
- * @summary Mimics Java's String's Hash implementation
3
- *
4
- * @param {string | number | symbol | Date} obj
5
- * @return {number} hash value of obj
6
- *
7
- * @function hashCode
8
- * @memberOf module:decorator-validation.Utils.Hashing
9
- * @category Hashing
10
- */
11
- export declare function hashCode(obj: string | number | symbol | Date): string;
12
- /**
13
- * @summary Defines teh type for a Hashing function
14
- * @memberOf module:decorator-validation.Utils.Hashing
15
- * @category Hashing
16
- */
17
- export type HashingFunction = (value: any, ...args: any[]) => string;
18
- /**
19
- * @summary Hashes an object serializing it and then hashing the string
20
- * @description The Serialization algorithm used by default (JSON.stringify)
21
- * is not deterministic and should not be used for hashing
22
- *
23
- * @param {Record<string, any>} obj
24
- * @return {string} the resulting hash
25
- *
26
- * @function hashSerialization
27
- * @memberOf module:decorator-validation.Utils.Hashing
28
- *
29
- * @category Hashing
30
- */
31
- export declare function hashSerialization(obj: Record<string, any> | any[]): string;
32
- /**
33
- * @summary Hashes an object by combining the hash of all its properties
34
- *
35
- * @param {Record<string, any>} obj
36
- * @return {string} the resulting hash
37
- *
38
- * @function hashObj
39
- * @memberOf module:decorator-validation.Utils.Hashing
40
- * @category Hashing
41
- */
42
- export declare function hashObj(obj: Record<string, any> | any[]): string;
43
- export declare const DefaultHashingMethod = "default";
44
- export declare class Hashing {
45
- private static current;
46
- private static cache;
47
- private constructor();
48
- private static get;
49
- static register(key: string, func: HashingFunction, setDefault?: boolean): void;
50
- static hash(obj: any, method?: string, ...args: any[]): any;
51
- static setDefault(method: string): void;
52
- }
@@ -1,99 +0,0 @@
1
- import { Serialization } from "./serialization";
2
- /**
3
- * @summary Mimics Java's String's Hash implementation
4
- *
5
- * @param {string | number | symbol | Date} obj
6
- * @return {number} hash value of obj
7
- *
8
- * @function hashCode
9
- * @memberOf module:decorator-validation.Utils.Hashing
10
- * @category Hashing
11
- */
12
- export function hashCode(obj) {
13
- obj = String(obj);
14
- let hash = 0;
15
- for (let i = 0; i < obj.length; i++) {
16
- const character = obj.charCodeAt(i);
17
- hash = (hash << 5) - hash + character;
18
- hash = hash & hash; // Convert to 32bit integer
19
- }
20
- return hash.toString();
21
- }
22
- /**
23
- * @summary Hashes an object serializing it and then hashing the string
24
- * @description The Serialization algorithm used by default (JSON.stringify)
25
- * is not deterministic and should not be used for hashing
26
- *
27
- * @param {Record<string, any>} obj
28
- * @return {string} the resulting hash
29
- *
30
- * @function hashSerialization
31
- * @memberOf module:decorator-validation.Utils.Hashing
32
- *
33
- * @category Hashing
34
- */
35
- export function hashSerialization(obj) {
36
- return hashCode(Serialization.serialize(obj));
37
- }
38
- /**
39
- * @summary Hashes an object by combining the hash of all its properties
40
- *
41
- * @param {Record<string, any>} obj
42
- * @return {string} the resulting hash
43
- *
44
- * @function hashObj
45
- * @memberOf module:decorator-validation.Utils.Hashing
46
- * @category Hashing
47
- */
48
- export function hashObj(obj) {
49
- const hashReducer = function (h, el) {
50
- const elHash = hashFunction(el);
51
- if (typeof elHash === "string")
52
- return hashFunction((h || "") + hashFunction(el));
53
- h = h || 0;
54
- h = (h << 5) - h + elHash;
55
- return h & h;
56
- };
57
- const func = hashCode;
58
- const hashFunction = function (value) {
59
- if (typeof value === "undefined")
60
- return "";
61
- if (["string", "number", "symbol"].indexOf(typeof value) !== -1)
62
- return func(value.toString());
63
- if (value instanceof Date)
64
- return func(value.getTime());
65
- if (Array.isArray(value))
66
- return value.reduce(hashReducer, undefined);
67
- return Object.values(value).reduce(hashReducer, undefined);
68
- };
69
- const result = Object.values(obj).reduce(hashReducer, 0);
70
- return (typeof result === "number" ? Math.abs(result) : result).toString();
71
- }
72
- export const DefaultHashingMethod = "default";
73
- export class Hashing {
74
- static { this.current = DefaultHashingMethod; }
75
- static { this.cache = {
76
- default: hashObj,
77
- }; }
78
- constructor() { }
79
- static get(key) {
80
- if (key in this.cache)
81
- return this.cache[key];
82
- throw new Error(`No hashing method registered under ${key}`);
83
- }
84
- static register(key, func, setDefault = false) {
85
- if (key in this.cache)
86
- throw new Error(`Hashing method ${key} already registered`);
87
- this.cache[key] = func;
88
- if (setDefault)
89
- this.current = key;
90
- }
91
- static hash(obj, method, ...args) {
92
- if (!method)
93
- return this.get(this.current)(obj, ...args);
94
- return this.get(method)(obj, ...args);
95
- }
96
- static setDefault(method) {
97
- this.current = this.get(method);
98
- }
99
- }
@@ -1,7 +0,0 @@
1
- export * from "./constants";
2
- export * from "./dates";
3
- export * from "./decorators";
4
- export * from "./hashing";
5
- export * from "./registry";
6
- export * from "./serialization";
7
- export * from "./strings";
@@ -1,7 +0,0 @@
1
- export * from "./constants";
2
- export * from "./dates";
3
- export * from "./decorators";
4
- export * from "./hashing";
5
- export * from "./registry";
6
- export * from "./serialization";
7
- export * from "./strings";
@@ -1,68 +0,0 @@
1
- import { Constructor } from "../model/types";
2
- /**
3
- * @summary Basic interface for Registries
4
- *
5
- * @interface IRegistry
6
- *
7
- * @category Utilities
8
- */
9
- export interface IRegistry<T> {
10
- /**
11
- * @summary Registers an Object
12
- *
13
- * @param {T} obj
14
- * @param {any[]} args
15
- *
16
- * @method
17
- */
18
- register(obj: T | any, ...args: any[]): void;
19
- /**
20
- * @summary Retrieves an Object if it can find it
21
- *
22
- * @param {any} key
23
- * @param {any[]} args
24
- * @return {T | undefined}
25
- *
26
- * @method
27
- */
28
- get(key: any, ...args: any[]): T | undefined;
29
- }
30
- /**
31
- * @summary Basic Builder Registry Interface
32
- *
33
- * @typedef T
34
- * @interface BuilderRegistry<T>
35
- *
36
- * @category Construction
37
- */
38
- export interface BuilderRegistry<T> extends IRegistry<Constructor<T>> {
39
- /**
40
- * @summary Retrieves an Builder Object by name if it can
41
- *
42
- * @param {string} name
43
- * @param {any[]} args
44
- *
45
- * @method
46
- */
47
- get(name: string, ...args: any[]): Constructor<T> | undefined;
48
- /**
49
- * @summary Registers a constructor by name
50
- *
51
- * @param {Constructor<T>} [constructor]
52
- * @param {name} name
53
- * @param {any[]} args
54
- *
55
- * @method
56
- */
57
- register(constructor: Constructor<T>, name?: string, ...args: any[]): void;
58
- /**
59
- * @summary Builds an Object by name
60
- *
61
- * @param {{}} obj
62
- * @param {any[]} args
63
- * @return T
64
- *
65
- * @method
66
- */
67
- build(obj: Record<string, any> | T, ...args: any[]): T;
68
- }
@@ -1 +0,0 @@
1
- export {};
@@ -1,79 +0,0 @@
1
- import { Model } from "../model/Model";
2
- import { Constructor } from "../model/types";
3
- /**
4
- * @summary Helper in serialization
5
- *
6
- * @interface Serializer
7
- * @category Serialization
8
- */
9
- export interface Serializer<T extends Model> {
10
- /**
11
- * @summary Serializes a model
12
- * @param {T} model
13
- *
14
- * @param args
15
- * @method
16
- *
17
- * @throws {Error}
18
- */
19
- serialize(model: T, ...args: any[]): string;
20
- /**
21
- * @summary Rebuilds a model from serialization
22
- * @param {string} str
23
- *
24
- * @param args
25
- * @method
26
- *
27
- * @throws {Error}
28
- */
29
- deserialize(str: string, ...args: any[]): T;
30
- }
31
- /**
32
- * @summary Concrete implementation of a {@link Serializer} in JSON format
33
- * @description JS's native JSON.stringify (used here) is not deterministic
34
- * and therefore should not be used for hashing purposes
35
- *
36
- * To keep dependencies low, we will not implement this, but we recommend
37
- * implementing a similar {@link JSONSerializer} using 'deterministic-json' libraries
38
- *
39
- * @class JSONSerializer
40
- * @implements Serializer
41
- *
42
- * @category Serialization
43
- */
44
- export declare class JSONSerializer<T extends Model> implements Serializer<T> {
45
- /**
46
- * @summary prepares the model for serialization
47
- * @description returns a shallow copy of the object, containing an enumerable {@link ModelKeys#ANCHOR} property
48
- * so the object can be recognized upon deserialization
49
- *
50
- * @param {T} model
51
- * @protected
52
- */
53
- protected preSerialize(model: T): Record<string, any>;
54
- /**
55
- * @summary Rebuilds a model from a serialization
56
- * @param {string} str
57
- *
58
- * @throws {Error} If it fails to parse the string, or to build the model
59
- */
60
- deserialize(str: string): T;
61
- /**
62
- * @summary Serializes a model
63
- * @param {T} model
64
- *
65
- * @throws {Error} if fails to serialize
66
- */
67
- serialize(model: T): string;
68
- }
69
- export declare const DefaultSerializationMethod = "json";
70
- export declare class Serialization {
71
- private static current;
72
- private static cache;
73
- private constructor();
74
- private static get;
75
- static register(key: string, func: Constructor<Serializer<any>>, setDefault?: boolean): void;
76
- static serialize(obj: any, method?: string, ...args: any[]): any;
77
- static deserialize(obj: string, method?: string, ...args: any[]): any;
78
- static setDefault(method: string): void;
79
- }
@@ -1,88 +0,0 @@
1
- import { Model } from "../model/Model";
2
- import { ModelKeys } from "./constants";
3
- /**
4
- * @summary Concrete implementation of a {@link Serializer} in JSON format
5
- * @description JS's native JSON.stringify (used here) is not deterministic
6
- * and therefore should not be used for hashing purposes
7
- *
8
- * To keep dependencies low, we will not implement this, but we recommend
9
- * implementing a similar {@link JSONSerializer} using 'deterministic-json' libraries
10
- *
11
- * @class JSONSerializer
12
- * @implements Serializer
13
- *
14
- * @category Serialization
15
- */
16
- export class JSONSerializer {
17
- /**
18
- * @summary prepares the model for serialization
19
- * @description returns a shallow copy of the object, containing an enumerable {@link ModelKeys#ANCHOR} property
20
- * so the object can be recognized upon deserialization
21
- *
22
- * @param {T} model
23
- * @protected
24
- */
25
- preSerialize(model) {
26
- // TODO: nested preserialization (so increase performance when deserializing)
27
- const toSerialize = Object.assign({}, model);
28
- const metadata = Model.getMetadata(model);
29
- toSerialize[ModelKeys.ANCHOR] = metadata || model.constructor.name;
30
- return toSerialize;
31
- }
32
- /**
33
- * @summary Rebuilds a model from a serialization
34
- * @param {string} str
35
- *
36
- * @throws {Error} If it fails to parse the string, or to build the model
37
- */
38
- deserialize(str) {
39
- const deserialization = JSON.parse(str);
40
- const className = deserialization[ModelKeys.ANCHOR];
41
- if (!className)
42
- throw new Error("Could not find class reference in serialized model");
43
- const model = Model.build(deserialization, className);
44
- return model;
45
- }
46
- /**
47
- * @summary Serializes a model
48
- * @param {T} model
49
- *
50
- * @throws {Error} if fails to serialize
51
- */
52
- serialize(model) {
53
- return JSON.stringify(this.preSerialize(model));
54
- }
55
- }
56
- export const DefaultSerializationMethod = "json";
57
- export class Serialization {
58
- static { this.current = DefaultSerializationMethod; }
59
- static { this.cache = {
60
- json: new JSONSerializer(),
61
- }; }
62
- constructor() { }
63
- static get(key) {
64
- if (key in this.cache)
65
- return this.cache[key];
66
- throw new Error(`No serialization method registered under ${key}`);
67
- }
68
- static register(key, func, setDefault = false) {
69
- if (key in this.cache)
70
- throw new Error(`Serialization method ${key} already registered`);
71
- this.cache[key] = new func();
72
- if (setDefault)
73
- this.current = key;
74
- }
75
- static serialize(obj, method, ...args) {
76
- if (!method)
77
- return this.get(this.current).serialize(obj, ...args);
78
- return this.get(method).serialize(obj, ...args);
79
- }
80
- static deserialize(obj, method, ...args) {
81
- if (!method)
82
- return this.get(this.current).deserialize(obj, ...args);
83
- return this.get(method).deserialize(obj, ...args);
84
- }
85
- static setDefault(method) {
86
- this.current = this.get(method);
87
- }
88
- }