@decaf-ts/decorator-validation 1.4.21 → 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 (173) hide show
  1. package/LICENSE.md +0 -0
  2. package/README.md +0 -0
  3. package/package.json +15 -9
  4. package/dist/decorator-validation.bundle.min.js +0 -2
  5. package/dist/decorator-validation.bundle.min.js.LICENSE.txt +0 -14
  6. package/dist/esm/decorator-validation.bundle.min.esm.js +0 -2
  7. package/dist/esm/decorator-validation.bundle.min.esm.js.LICENSE.txt +0 -14
  8. package/lib/esm/index.d.ts +0 -42
  9. package/lib/esm/index.js +0 -42
  10. package/lib/esm/model/Model.d.ts +0 -139
  11. package/lib/esm/model/Model.js +0 -297
  12. package/lib/esm/model/ModelErrorDefinition.d.ts +0 -22
  13. package/lib/esm/model/ModelErrorDefinition.js +0 -52
  14. package/lib/esm/model/Registry.d.ts +0 -59
  15. package/lib/esm/model/Registry.js +0 -73
  16. package/lib/esm/model/constants.d.ts +0 -56
  17. package/lib/esm/model/constants.js +0 -69
  18. package/lib/esm/model/construction.d.ts +0 -29
  19. package/lib/esm/model/construction.js +0 -63
  20. package/lib/esm/model/decorators.d.ts +0 -25
  21. package/lib/esm/model/decorators.js +0 -62
  22. package/lib/esm/model/index.d.ts +0 -9
  23. package/lib/esm/model/index.js +0 -9
  24. package/lib/esm/model/types.d.ts +0 -79
  25. package/lib/esm/model/types.js +0 -1
  26. package/lib/esm/model/utils.d.ts +0 -11
  27. package/lib/esm/model/utils.js +0 -25
  28. package/lib/esm/model/validation.d.ts +0 -14
  29. package/lib/esm/model/validation.js +0 -134
  30. package/lib/esm/utils/constants.d.ts +0 -26
  31. package/lib/esm/utils/constants.js +0 -27
  32. package/lib/esm/utils/dates.d.ts +0 -76
  33. package/lib/esm/utils/dates.js +0 -243
  34. package/lib/esm/utils/decorators.d.ts +0 -2
  35. package/lib/esm/utils/decorators.js +0 -18
  36. package/lib/esm/utils/hashing.d.ts +0 -52
  37. package/lib/esm/utils/hashing.js +0 -99
  38. package/lib/esm/utils/index.d.ts +0 -7
  39. package/lib/esm/utils/index.js +0 -7
  40. package/lib/esm/utils/registry.d.ts +0 -68
  41. package/lib/esm/utils/registry.js +0 -1
  42. package/lib/esm/utils/serialization.d.ts +0 -79
  43. package/lib/esm/utils/serialization.js +0 -88
  44. package/lib/esm/utils/strings.d.ts +0 -25
  45. package/lib/esm/utils/strings.js +0 -31
  46. package/lib/esm/validation/Validation.d.ts +0 -51
  47. package/lib/esm/validation/Validation.js +0 -71
  48. package/lib/esm/validation/Validators/DateValidator.d.ts +0 -28
  49. package/lib/esm/validation/Validators/DateValidator.js +0 -53
  50. package/lib/esm/validation/Validators/EmailValidator.d.ts +0 -28
  51. package/lib/esm/validation/Validators/EmailValidator.js +0 -48
  52. package/lib/esm/validation/Validators/ListValidator.d.ts +0 -28
  53. package/lib/esm/validation/Validators/ListValidator.js +0 -66
  54. package/lib/esm/validation/Validators/MaxLengthValidator.d.ts +0 -29
  55. package/lib/esm/validation/Validators/MaxLengthValidator.js +0 -53
  56. package/lib/esm/validation/Validators/MaxValidator.d.ts +0 -28
  57. package/lib/esm/validation/Validators/MaxValidator.js +0 -57
  58. package/lib/esm/validation/Validators/MinLengthValidator.d.ts +0 -29
  59. package/lib/esm/validation/Validators/MinLengthValidator.js +0 -53
  60. package/lib/esm/validation/Validators/MinValidator.d.ts +0 -28
  61. package/lib/esm/validation/Validators/MinValidator.js +0 -57
  62. package/lib/esm/validation/Validators/PasswordValidator.d.ts +0 -28
  63. package/lib/esm/validation/Validators/PasswordValidator.js +0 -48
  64. package/lib/esm/validation/Validators/PatternValidator.d.ts +0 -37
  65. package/lib/esm/validation/Validators/PatternValidator.js +0 -70
  66. package/lib/esm/validation/Validators/RequiredValidator.d.ts +0 -27
  67. package/lib/esm/validation/Validators/RequiredValidator.js +0 -55
  68. package/lib/esm/validation/Validators/StepValidator.d.ts +0 -28
  69. package/lib/esm/validation/Validators/StepValidator.js +0 -52
  70. package/lib/esm/validation/Validators/TypeValidator.d.ts +0 -27
  71. package/lib/esm/validation/Validators/TypeValidator.js +0 -60
  72. package/lib/esm/validation/Validators/URLValidator.d.ts +0 -26
  73. package/lib/esm/validation/Validators/URLValidator.js +0 -46
  74. package/lib/esm/validation/Validators/Validator.d.ts +0 -40
  75. package/lib/esm/validation/Validators/Validator.js +0 -47
  76. package/lib/esm/validation/Validators/ValidatorRegistry.d.ts +0 -47
  77. package/lib/esm/validation/Validators/ValidatorRegistry.js +0 -80
  78. package/lib/esm/validation/Validators/constants.d.ts +0 -96
  79. package/lib/esm/validation/Validators/constants.js +0 -134
  80. package/lib/esm/validation/Validators/decorators.d.ts +0 -12
  81. package/lib/esm/validation/Validators/decorators.js +0 -25
  82. package/lib/esm/validation/Validators/index.d.ts +0 -52
  83. package/lib/esm/validation/Validators/index.js +0 -52
  84. package/lib/esm/validation/Validators/types.d.ts +0 -51
  85. package/lib/esm/validation/Validators/types.js +0 -1
  86. package/lib/esm/validation/decorators.d.ts +0 -178
  87. package/lib/esm/validation/decorators.js +0 -288
  88. package/lib/esm/validation/index.d.ts +0 -4
  89. package/lib/esm/validation/index.js +0 -4
  90. package/lib/esm/validation/types.d.ts +0 -46
  91. package/lib/esm/validation/types.js +0 -1
  92. package/lib/index.cjs +0 -59
  93. package/lib/index.d.ts +0 -42
  94. package/lib/model/Model.cjs +0 -301
  95. package/lib/model/Model.d.ts +0 -139
  96. package/lib/model/ModelErrorDefinition.cjs +0 -56
  97. package/lib/model/ModelErrorDefinition.d.ts +0 -22
  98. package/lib/model/Registry.cjs +0 -78
  99. package/lib/model/Registry.d.ts +0 -59
  100. package/lib/model/constants.cjs +0 -72
  101. package/lib/model/constants.d.ts +0 -56
  102. package/lib/model/construction.cjs +0 -68
  103. package/lib/model/construction.d.ts +0 -29
  104. package/lib/model/decorators.cjs +0 -67
  105. package/lib/model/decorators.d.ts +0 -25
  106. package/lib/model/index.cjs +0 -25
  107. package/lib/model/index.d.ts +0 -9
  108. package/lib/model/types.cjs +0 -2
  109. package/lib/model/types.d.ts +0 -79
  110. package/lib/model/utils.cjs +0 -29
  111. package/lib/model/utils.d.ts +0 -11
  112. package/lib/model/validation.cjs +0 -137
  113. package/lib/model/validation.d.ts +0 -14
  114. package/lib/utils/constants.cjs +0 -30
  115. package/lib/utils/constants.d.ts +0 -26
  116. package/lib/utils/dates.cjs +0 -251
  117. package/lib/utils/dates.d.ts +0 -76
  118. package/lib/utils/decorators.cjs +0 -22
  119. package/lib/utils/decorators.d.ts +0 -2
  120. package/lib/utils/hashing.cjs +0 -106
  121. package/lib/utils/hashing.d.ts +0 -52
  122. package/lib/utils/index.cjs +0 -23
  123. package/lib/utils/index.d.ts +0 -7
  124. package/lib/utils/registry.cjs +0 -2
  125. package/lib/utils/registry.d.ts +0 -68
  126. package/lib/utils/serialization.cjs +0 -93
  127. package/lib/utils/serialization.d.ts +0 -79
  128. package/lib/utils/strings.cjs +0 -35
  129. package/lib/utils/strings.d.ts +0 -25
  130. package/lib/validation/Validation.cjs +0 -75
  131. package/lib/validation/Validation.d.ts +0 -51
  132. package/lib/validation/Validators/DateValidator.cjs +0 -56
  133. package/lib/validation/Validators/DateValidator.d.ts +0 -28
  134. package/lib/validation/Validators/EmailValidator.cjs +0 -51
  135. package/lib/validation/Validators/EmailValidator.d.ts +0 -28
  136. package/lib/validation/Validators/ListValidator.cjs +0 -69
  137. package/lib/validation/Validators/ListValidator.d.ts +0 -28
  138. package/lib/validation/Validators/MaxLengthValidator.cjs +0 -56
  139. package/lib/validation/Validators/MaxLengthValidator.d.ts +0 -29
  140. package/lib/validation/Validators/MaxValidator.cjs +0 -60
  141. package/lib/validation/Validators/MaxValidator.d.ts +0 -28
  142. package/lib/validation/Validators/MinLengthValidator.cjs +0 -56
  143. package/lib/validation/Validators/MinLengthValidator.d.ts +0 -29
  144. package/lib/validation/Validators/MinValidator.cjs +0 -60
  145. package/lib/validation/Validators/MinValidator.d.ts +0 -28
  146. package/lib/validation/Validators/PasswordValidator.cjs +0 -51
  147. package/lib/validation/Validators/PasswordValidator.d.ts +0 -28
  148. package/lib/validation/Validators/PatternValidator.cjs +0 -73
  149. package/lib/validation/Validators/PatternValidator.d.ts +0 -37
  150. package/lib/validation/Validators/RequiredValidator.cjs +0 -58
  151. package/lib/validation/Validators/RequiredValidator.d.ts +0 -27
  152. package/lib/validation/Validators/StepValidator.cjs +0 -55
  153. package/lib/validation/Validators/StepValidator.d.ts +0 -28
  154. package/lib/validation/Validators/TypeValidator.cjs +0 -63
  155. package/lib/validation/Validators/TypeValidator.d.ts +0 -27
  156. package/lib/validation/Validators/URLValidator.cjs +0 -49
  157. package/lib/validation/Validators/URLValidator.d.ts +0 -26
  158. package/lib/validation/Validators/Validator.cjs +0 -51
  159. package/lib/validation/Validators/Validator.d.ts +0 -40
  160. package/lib/validation/Validators/ValidatorRegistry.cjs +0 -85
  161. package/lib/validation/Validators/ValidatorRegistry.d.ts +0 -47
  162. package/lib/validation/Validators/constants.cjs +0 -137
  163. package/lib/validation/Validators/constants.d.ts +0 -96
  164. package/lib/validation/Validators/decorators.cjs +0 -28
  165. package/lib/validation/Validators/decorators.d.ts +0 -12
  166. package/lib/validation/Validators/index.cjs +0 -69
  167. package/lib/validation/Validators/types.cjs +0 -2
  168. package/lib/validation/decorators.cjs +0 -304
  169. package/lib/validation/decorators.d.ts +0 -178
  170. package/lib/validation/index.cjs +0 -20
  171. package/lib/validation/index.d.ts +0 -4
  172. package/lib/validation/types.cjs +0 -2
  173. package/lib/validation/types.d.ts +0 -46
@@ -1,76 +0,0 @@
1
- import "reflect-metadata";
2
- /**
3
- * @summary Reverses the process from {@link formatDate}
4
- *
5
- * @param {string} date the date string to be converted back into date
6
- * @param {string} format the date format
7
- * @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?)
8
- *
9
- * @function dateFromFormat
10
- * @memberOf module:decorator-validation.Utils.Dates
11
- * @category Format
12
- */
13
- export declare function dateFromFormat(date: string, format: string): Date;
14
- /**
15
- * @summary Binds a date format to a string
16
- * @param {Date} [date]
17
- * @param {string} [format]
18
- * @memberOf module:decorator-validation.Utils.Format
19
- * @category Utilities
20
- */
21
- export declare function bindDateToString(date: Date | undefined, format: string): Date | undefined;
22
- /**
23
- * @summary Helper function to be used instead of instanceOf Date
24
- * @param date
25
- * @memberOf module:decorator-validation.Utils.Dates
26
- * @category Validation
27
- */
28
- export declare function isValidDate(date: any): boolean;
29
- /**
30
- * @summary Util function to pad numbers
31
- * @param {number} num
32
- *
33
- * @return {string}
34
- *
35
- * @function twoDigitPad
36
- * @memberOf module:decorator-validation.Utils.Format
37
- * @category Format
38
- */
39
- export declare function twoDigitPad(num: number): string;
40
- /**
41
- * @summary Date Format Handling
42
- * @description Code from {@link https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date}
43
- *
44
- * <pre>
45
- * Using similar formatting as Moment.js, Class DateTimeFormatter (Java), and Class SimpleDateFormat (Java),
46
- * I implemented a comprehensive solution formatDate(date, patternStr) where the code is easy to read and modify.
47
- * You can display date, time, AM/PM, etc.
48
- *
49
- * Date and Time Patterns
50
- * yy = 2-digit year; yyyy = full year
51
- * M = digit month; MM = 2-digit month; MMM = short month name; MMMM = full month name
52
- * EEEE = full weekday name; EEE = short weekday name
53
- * d = digit day; dd = 2-digit day
54
- * h = hours am/pm; hh = 2-digit hours am/pm; H = hours; HH = 2-digit hours
55
- * m = minutes; mm = 2-digit minutes; aaa = AM/PM
56
- * s = seconds; ss = 2-digit seconds
57
- * S = miliseconds
58
- * </pre>
59
- *
60
- * @param {Date} date
61
- * @param {string} [patternStr] defaults to 'yyyy/MM/dd'
62
- * @return {string} the formatted date
63
- *
64
- * @function formatDate
65
- * @memberOf module:decorator-validation.Utils.Dates
66
- * @category Format
67
- */
68
- export declare function formatDate(date: Date, patternStr?: string): string;
69
- /**
70
- * @summary Parses a date from a specified format
71
- * @param {string} format
72
- * @param {string | Date | number} [v]
73
- * @memberOf module:decorator-validation.Utils.Dates
74
- * @category Format
75
- */
76
- export declare function parseDate(format: string, v?: string | Date | number): Date | undefined;
@@ -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
- }