@leyyo/common 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/aliases.d.ts CHANGED
@@ -91,16 +91,44 @@ type OneOfByKey<T> = {
91
91
  type XorIn<T> = {
92
92
  [K in keyof T]: T[K];
93
93
  } & unknown;
94
+ /**
95
+ * An interface which contains secure mode members and provides to shift to main mode
96
+ * */
94
97
  export interface ShiftSecure<S extends ShiftMain<any>> {
98
+ /**
99
+ * Shifts to secure mode
100
+ * */
95
101
  get $secure(): S;
96
102
  }
97
- export interface ShiftFlat<D> {
98
- get $flat(): D;
99
- }
103
+ /**
104
+ * An interface which contains main mode members and provides to shift to secure mode
105
+ *
106
+ * IT's so useful to hide some public members
107
+ * - to see clean auto-completed members in IDE
108
+ * - to indicated that secure mode members should be used in special cases
109
+ * */
100
110
  export interface ShiftMain<M extends ShiftSecure<any>> {
111
+ /**
112
+ * Shifts to main mode
113
+ * */
101
114
  get $back(): M;
102
115
  }
116
+ /**
117
+ * An interface which provides to flat generic interfaces/classes to prevent verbose casting commands
118
+ * */
119
+ export interface ShiftFlat<D> {
120
+ /**
121
+ * Flats current classes, or eliminate generic parameters
122
+ * */
123
+ get $flat(): D;
124
+ }
125
+ /**
126
+ * Useful interface which provides initialization state for instances
127
+ * */
103
128
  export interface InitLike {
129
+ /**
130
+ * Initializes the instance
131
+ * */
104
132
  $init(...args: Arr): void;
105
133
  }
106
134
  /**
@@ -1,34 +1,66 @@
1
1
  import { AssertionCallback, AssertionOpt, CommonAssertion, CommonAssertionSecure } from "./index-types";
2
2
  import { Leyyo } from "../leyyo";
3
- import { TypeOpt } from "../to";
3
+ import { ToTypeOpt } from "../to";
4
4
  import { Arr, Obj } from "../aliases";
5
+ /** @inheritDoc */
5
6
  export declare class CommonAssertionImpl implements CommonAssertion, CommonAssertionSecure {
6
7
  private is;
7
8
  private callback;
8
9
  constructor();
9
- private buildMessage;
10
+ /**
11
+ * Build error parameters as message and params
12
+ * */
13
+ private buildErrorParameters;
14
+ /**
15
+ * Inner secure json function, it will be used after creating weak set
16
+ * */
10
17
  _secureJson(value: unknown, level: number, set: WeakSet<Obj>): unknown;
18
+ /** @inheritDoc */
11
19
  raise(opt: string | AssertionOpt | AssertionCallback, value?: any, indicator?: string, def?: string): void;
20
+ /** @inheritDoc */
12
21
  emptyFn(...params: Arr): void;
22
+ /** @inheritDoc */
13
23
  secureJson<E = unknown>(value: unknown): E;
14
- realNumber(value: number, opt?: TypeOpt): number;
24
+ /** @inheritDoc */
15
25
  array(value: unknown, opt?: string | AssertionOpt | AssertionCallback): void;
26
+ /** @inheritDoc */
16
27
  boolean(value: unknown, opt?: string | AssertionOpt | AssertionCallback): void;
28
+ /** @inheritDoc */
17
29
  clazz(value: unknown, opt?: string | AssertionOpt | AssertionCallback): void;
30
+ /** @inheritDoc */
18
31
  date(value: unknown, opt?: string | AssertionOpt | AssertionCallback): void;
32
+ /** @inheritDoc */
19
33
  func(value: unknown, opt?: string | AssertionOpt | AssertionCallback): void;
34
+ /** @inheritDoc */
20
35
  integer(value: unknown, opt?: string | AssertionOpt | AssertionCallback): void;
36
+ /** @inheritDoc */
21
37
  key(value: unknown, opt?: string | AssertionOpt | AssertionCallback): void;
38
+ /** @inheritDoc */
22
39
  notEmpty(value: unknown, opt?: string | AssertionOpt | AssertionCallback): void;
40
+ /** @inheritDoc */
23
41
  number(value: unknown, opt?: string | AssertionOpt | AssertionCallback): void;
42
+ /** @inheritDoc */
43
+ realNumber(value: number, opt?: ToTypeOpt): number;
44
+ /** @inheritDoc */
24
45
  object(value: unknown, opt?: string | AssertionOpt | AssertionCallback): void;
46
+ /** @inheritDoc */
25
47
  positiveInteger(value: unknown, opt?: string | AssertionOpt | AssertionCallback): void;
48
+ /** @inheritDoc */
49
+ safeInteger(value: any, opt?: string | AssertionCallback | AssertionOpt): void;
50
+ /** @inheritDoc */
26
51
  positiveNumber(value: unknown, opt?: string | AssertionOpt | AssertionCallback): void;
52
+ /** @inheritDoc */
27
53
  primitive(value: unknown, opt?: string | AssertionOpt | AssertionCallback): void;
54
+ /** @inheritDoc */
28
55
  string(value: unknown, opt?: string | AssertionOpt | AssertionCallback): void;
56
+ /** @inheritDoc */
29
57
  text(value: unknown, opt?: string | AssertionOpt | AssertionCallback): string;
30
- value(value: unknown, opt?: string | AssertionOpt | AssertionCallback): void;
58
+ /** @inheritDoc */
59
+ realValue(value: unknown, opt?: string | AssertionOpt | AssertionCallback): void;
60
+ /** @inheritDoc */
31
61
  get $back(): CommonAssertion;
62
+ /** @inheritDoc */
32
63
  $init(leyyo: Leyyo): void;
64
+ /** @inheritDoc */
33
65
  get $secure(): CommonAssertionSecure;
34
66
  }
@@ -2,12 +2,16 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.CommonAssertionImpl = void 0;
4
4
  const error_1 = require("../error");
5
+ /** @inheritDoc */
5
6
  class CommonAssertionImpl {
6
7
  // endregion properties
7
8
  constructor() {
8
9
  }
9
10
  // region internal
10
- buildMessage(v2, indicator) {
11
+ /**
12
+ * Build error parameters as message and params
13
+ * */
14
+ buildErrorParameters(v2, indicator) {
11
15
  if (typeof v2 === 'string') {
12
16
  return { message: v2, params: { indicator } };
13
17
  }
@@ -21,7 +25,7 @@ class CommonAssertionImpl {
21
25
  }
22
26
  if (typeof v2 === 'function') {
23
27
  try {
24
- return this.buildMessage(v2(), indicator);
28
+ return this.buildErrorParameters(v2(), indicator);
25
29
  }
26
30
  catch (e) {
27
31
  return { params: { indicator, callbackError: e.message } };
@@ -36,6 +40,9 @@ class CommonAssertionImpl {
36
40
  }
37
41
  return { params: { json, indicator } };
38
42
  }
43
+ /**
44
+ * Inner secure json function, it will be used after creating weak set
45
+ * */
39
46
  _secureJson(value, level, set) {
40
47
  if ([null, undefined].includes(value)) {
41
48
  return null;
@@ -76,8 +83,9 @@ class CommonAssertionImpl {
76
83
  }
77
84
  // endregion internal
78
85
  // region raise
86
+ /** @inheritDoc */
79
87
  raise(opt, value, indicator, def) {
80
- const { message, params } = this.buildMessage(opt, indicator);
88
+ const { message, params } = this.buildErrorParameters(opt, indicator);
81
89
  const type = typeof value;
82
90
  switch (type) {
83
91
  case "object":
@@ -93,116 +101,145 @@ class CommonAssertionImpl {
93
101
  def = def !== null && def !== void 0 ? def : 'Assertion error';
94
102
  throw new error_1.AssertionException(message !== null && message !== void 0 ? message : def, params);
95
103
  }
104
+ /** @inheritDoc */
96
105
  // noinspection JSUnusedLocalSymbols
97
- emptyFn(...params) { }
106
+ emptyFn(...params) {
107
+ }
108
+ /** @inheritDoc */
98
109
  secureJson(value) {
99
110
  return this._secureJson(value, 0, new WeakSet());
100
111
  }
101
- realNumber(value, opt) {
102
- if (isNaN(value) || !isFinite(value)) {
103
- if (!opt) {
104
- opt = {};
105
- }
106
- if (!opt.silent) {
107
- delete opt.silent;
108
- this.raise(opt, value, 'not.real.number', 'Not real number');
109
- }
110
- return null;
111
- }
112
- return value;
113
- }
114
112
  // endregion raise
115
113
  // region types
114
+ /** @inheritDoc */
116
115
  array(value, opt) {
117
116
  if (!this.is.array(value)) {
118
117
  this.raise(opt, value, 'invalid.array.value', 'Invalid array value');
119
118
  }
120
119
  }
120
+ /** @inheritDoc */
121
121
  boolean(value, opt) {
122
122
  if (!this.is.boolean(value)) {
123
123
  this.raise(opt, value, 'invalid.boolean.value', 'Invalid boolean value');
124
124
  }
125
125
  }
126
+ /** @inheritDoc */
126
127
  clazz(value, opt) {
127
128
  if (!this.is.clazz(value)) {
128
129
  this.raise(opt, value, 'invalid.class.value', 'Invalid class value');
129
130
  }
130
131
  }
132
+ /** @inheritDoc */
131
133
  date(value, opt) {
132
134
  if (!(value instanceof Date)) {
133
135
  this.raise(opt, value, 'invalid.date.value', 'Invalid date value');
134
136
  }
135
137
  }
138
+ /** @inheritDoc */
136
139
  func(value, opt) {
137
140
  if (!this.is.func(value)) {
138
141
  this.raise(opt, value, 'invalid.function.value', 'Invalid function value');
139
142
  }
140
143
  }
144
+ /** @inheritDoc */
141
145
  integer(value, opt) {
142
146
  if (!this.is.integer(value)) {
143
147
  this.raise(opt, value, 'invalid.integer.value', 'Invalid integer value');
144
148
  }
145
149
  }
150
+ /** @inheritDoc */
146
151
  key(value, opt) {
147
152
  if (!this.is.key(value)) {
148
153
  this.raise(opt, value, 'invalid.key.value', 'Invalid key value');
149
154
  }
150
155
  }
156
+ /** @inheritDoc */
151
157
  notEmpty(value, opt) {
152
158
  if (this.is.empty(value)) {
153
159
  this.raise(opt, value, 'empty.value', 'Empty value');
154
160
  }
155
161
  }
162
+ /** @inheritDoc */
156
163
  number(value, opt) {
157
164
  if (!this.is.number(value)) {
158
165
  this.raise(opt, value, 'invalid.number.value', 'Invalid number value');
159
166
  }
160
167
  }
168
+ /** @inheritDoc */
169
+ realNumber(value, opt) {
170
+ if (isNaN(value) || !isFinite(value)) {
171
+ if (!opt) {
172
+ opt = {};
173
+ }
174
+ if (!opt.silent) {
175
+ delete opt.silent;
176
+ this.raise(opt, value, 'not.real.number', 'Not real number');
177
+ }
178
+ return null;
179
+ }
180
+ return value;
181
+ }
182
+ /** @inheritDoc */
161
183
  object(value, opt) {
162
184
  if (!this.is.object(value)) {
163
185
  this.raise(opt, value, 'invalid.object.value', 'Invalid object value');
164
186
  }
165
187
  }
188
+ /** @inheritDoc */
166
189
  positiveInteger(value, opt) {
167
190
  if (!this.is.integer(value) || (value <= 0)) {
168
191
  this.raise(opt, value, 'invalid.positive.integer.value', 'Invalid positive integer value');
169
192
  }
170
193
  }
194
+ /** @inheritDoc */
195
+ safeInteger(value, opt) {
196
+ if (!this.is.safeInteger(value) || (value <= 0)) {
197
+ this.raise(opt, value, 'invalid.safe.integer.value', 'Invalid safe integer value');
198
+ }
199
+ }
200
+ /** @inheritDoc */
171
201
  positiveNumber(value, opt) {
172
202
  if (!this.is.number(value) || (value <= 0)) {
173
203
  this.raise(opt, value, 'invalid.positive.number.value', 'Invalid positive number value');
174
204
  }
175
205
  }
206
+ /** @inheritDoc */
176
207
  primitive(value, opt) {
177
208
  if (!this.is.primitive(value)) {
178
209
  this.raise(opt, value, 'invalid.primitive.value', 'Invalid primitive value');
179
210
  }
180
211
  }
212
+ /** @inheritDoc */
181
213
  string(value, opt) {
182
214
  if (!this.is.string(value)) {
183
215
  this.raise(opt, value, 'invalid.string.value', 'Invalid string value');
184
216
  }
185
217
  }
218
+ /** @inheritDoc */
186
219
  text(value, opt) {
187
220
  if (!this.is.text(value)) {
188
221
  this.raise(opt, value, 'invalid.text.value', 'Invalid text value');
189
222
  }
190
223
  return value.trim();
191
224
  }
192
- value(value, opt) {
225
+ /** @inheritDoc */
226
+ realValue(value, opt) {
193
227
  if (!this.is.text(value)) {
194
228
  this.raise(opt, value, 'invalid.value.value', 'Invalid regular value');
195
229
  }
196
230
  }
197
231
  // endregion types
198
232
  // region secure
233
+ /** @inheritDoc */
199
234
  get $back() {
200
235
  return this;
201
236
  }
237
+ /** @inheritDoc */
202
238
  $init(leyyo) {
203
239
  this.is = leyyo.is;
204
240
  this.callback = leyyo.callback;
205
241
  }
242
+ /** @inheritDoc */
206
243
  get $secure() {
207
244
  return this;
208
245
  }