@atlantjs/arch 13.3.0 → 14.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/@tool-box/utils/datatypes/boolean-utils.d.ts +7 -0
  2. package/@tool-box/utils/datatypes/boolean-utils.js +146 -4
  3. package/@tool-box/utils/datatypes/string-utils.d.ts +20 -4
  4. package/@tool-box/utils/datatypes/string-utils.js +300 -14
  5. package/@tool-box/utils/ducts/common.d.ts +49 -0
  6. package/@tool-box/utils/ducts/common.js +36 -3
  7. package/@tool-box/utils/ducts/optional-type.d.ts +85 -0
  8. package/@tool-box/utils/ducts/optional-type.js +79 -0
  9. package/@tool-box/utils/ducts/return-type.d.ts +17 -17
  10. package/@tool-box/utils/ducts/return-type.js +14 -4
  11. package/@tool-box/utils/http-provider/http-provider.d.ts +6 -0
  12. package/@tool-box/utils/http-provider/http-provider.js +43 -14
  13. package/@tool-box/utils/map/map.abstract.d.ts +39 -0
  14. package/@tool-box/utils/map/map.abstract.js +70 -6
  15. package/@tool-box/utils/random/random.d.ts +168 -0
  16. package/@tool-box/utils/random/random.js +235 -0
  17. package/@tool-box/utils/type-guard/guardian.d.ts +450 -7
  18. package/@tool-box/utils/type-guard/guardian.js +539 -35
  19. package/objects/@common/edges/cron-expression.edge.d.ts +30 -2
  20. package/objects/@common/edges/cron-expression.edge.js +77 -5
  21. package/objects/@common/edges/email.edge.d.ts +39 -1
  22. package/objects/@common/edges/email.edge.js +80 -2
  23. package/objects/@common/edges/ulid.sketch.edge.d.ts +48 -1
  24. package/objects/@common/edges/ulid.sketch.edge.js +75 -4
  25. package/objects/@common/edges/url.edge.d.ts +182 -0
  26. package/objects/@common/edges/url.edge.js +249 -0
  27. package/objects/@common/edges/username.edge.d.ts +9 -0
  28. package/objects/@common/edges/username.edge.js +34 -0
  29. package/objects/@common/edges/uuid.sketch.edge.d.ts +97 -1
  30. package/objects/@common/edges/uuid.sketch.edge.js +127 -6
  31. package/objects/amount/amount-value.edge.d.ts +39 -0
  32. package/objects/amount/amount-value.edge.js +69 -0
  33. package/objects/amount/amount.edge.d.ts +378 -2
  34. package/objects/amount/amount.edge.js +493 -4
  35. package/objects/datetime/edges/datetime.edge.d.ts +422 -4
  36. package/objects/datetime/edges/datetime.edge.js +538 -33
  37. package/objects/password/password.edge.d.ts +90 -0
  38. package/objects/password/password.edge.js +140 -6
  39. package/objects/primitives/boolean.edge.sketch.d.ts +105 -3
  40. package/objects/primitives/boolean.edge.sketch.js +132 -6
  41. package/objects/primitives/number.edge.sketch.d.ts +236 -0
  42. package/objects/primitives/number.edge.sketch.js +310 -24
  43. package/objects/primitives/string.edge.sketch.d.ts +148 -0
  44. package/objects/primitives/string.edge.sketch.js +191 -7
  45. package/objects/schedule/schedule.edge.d.ts +194 -0
  46. package/objects/schedule/schedule.edge.js +269 -2
  47. package/objects/time/time.edge.d.ts +285 -2
  48. package/objects/time/time.edge.js +385 -6
  49. package/package.json +1 -1
  50. package/tsconfig.tsbuildinfo +1 -1
@@ -1,24 +1,109 @@
1
1
  import { FalseyValues, IterType, SuccessReturn, ValueReturn } from "./common";
2
+ /**
3
+ * Representa um Optional com valor presente.
4
+ * @example
5
+ * const value = Optional("ok");
6
+ * if (value.hasSomething()) {
7
+ * value.unpack(); // "ok"
8
+ * }
9
+ */
2
10
  export type Something<T> = OptionalType<T> & {
3
11
  [SuccessReturn]: true;
4
12
  };
13
+ /**
14
+ * Representa um Optional sem valor.
15
+ * @example
16
+ * const value = Optional("");
17
+ * value.hasNothing(); // true
18
+ */
5
19
  export type Nothing = OptionalType<never> & {
6
20
  [SuccessReturn]: false;
7
21
  };
22
+ /**
23
+ * Tipo principal de Optional.
24
+ * @example
25
+ * const maybeName: Optional<string> = Optional("John");
26
+ */
8
27
  export type Optional<T> = OptionalType<T>;
9
28
  type From<T> = Exclude<T, Error | FalseyValues>;
10
29
  declare class OptionalType<T> {
11
30
  readonly [SuccessReturn]: boolean;
12
31
  readonly [ValueReturn]: T;
32
+ /**
33
+ * Cria uma nova instância de OptionalType.
34
+ * @param val Valor interno.
35
+ * @param something Indica presença de valor.
36
+ * @example
37
+ * const some = new (class extends OptionalType<string> {})("x", true);
38
+ */
13
39
  constructor(val: T, something: boolean);
40
+ /**
41
+ * Permite iteração quando houver valor iterável.
42
+ * @example
43
+ * const maybeArr = Optional([1, 2, 3]);
44
+ * for (const n of maybeArr) {
45
+ * // 1, 2, 3
46
+ * }
47
+ */
14
48
  [Symbol.iterator](this: Optional<T>): IterType<T>;
49
+ /**
50
+ * Verifica se existe valor.
51
+ * @returns true se houver valor.
52
+ * @example
53
+ * Optional("abc").hasSomething(); // true
54
+ * Optional("").hasSomething(); // false
55
+ */
15
56
  hasSomething(this: Optional<T>): this is Something<T>;
57
+ /**
58
+ * Verifica se não existe valor.
59
+ * @returns true se não houver valor.
60
+ * @example
61
+ * Optional(null).hasNothing(); // true
62
+ */
16
63
  hasNothing(this: Optional<T>): this is Nothing;
64
+ /**
65
+ * Retorna o valor interno.
66
+ * @throws {Error} Se não houver valor.
67
+ * @example
68
+ * Optional(10).unpack(); // 10
69
+ */
17
70
  unpack(this: Optional<T>): T;
71
+ /**
72
+ * Retorna o valor interno mesmo quando vazio.
73
+ * @returns Valor interno ou undefined.
74
+ * @example
75
+ * Optional("x").unpackAnyway(); // "x"
76
+ * Optional("").unpackAnyway(); // undefined
77
+ */
18
78
  unpackAnyway(this: Optional<T>): T | undefined;
79
+ /**
80
+ * Retorna o valor interno ou valor padrão.
81
+ * @param def Valor padrão.
82
+ * @example
83
+ * Optional("").unpackOr("default"); // "default"
84
+ */
19
85
  unpackOr(this: Optional<T>, def: T): T;
86
+ /**
87
+ * Retorna o valor interno ou calcula valor padrão sob demanda.
88
+ * @param f Função para gerar valor padrão.
89
+ * @example
90
+ * Optional("").unpackOrElse(() => "fallback"); // "fallback"
91
+ */
20
92
  unpackOrElse(this: Optional<T>, f: () => T): T;
21
93
  }
94
+ /**
95
+ * Cria um Optional a partir de um valor.
96
+ * Valores falsey e Error resultam em Nothing.
97
+ * @param val Valor de entrada.
98
+ * @example
99
+ * Optional("abc").hasSomething(); // true
100
+ * Optional(0).hasNothing(); // true
101
+ */
22
102
  export declare function Optional<T>(val: T): Optional<From<T>>;
103
+ /**
104
+ * Singleton representando ausência de valor.
105
+ * @example
106
+ * Nothing.hasNothing(); // true
107
+ */
23
108
  export declare const Nothing: Readonly<OptionalType<never>>;
24
109
  export {};
@@ -4,44 +4,123 @@ exports.Nothing = void 0;
4
4
  exports.Optional = Optional;
5
5
  const common_1 = require("./common");
6
6
  class OptionalType {
7
+ /**
8
+ * Cria uma nova instância de OptionalType.
9
+ * @param val Valor interno.
10
+ * @param something Indica presença de valor.
11
+ * @example
12
+ * const some = new (class extends OptionalType<string> {})("x", true);
13
+ */
7
14
  constructor(val, something) {
8
15
  this[common_1.SuccessReturn] = something;
9
16
  this[common_1.ValueReturn] = val;
10
17
  }
18
+ /**
19
+ * Permite iteração quando houver valor iterável.
20
+ * @example
21
+ * const maybeArr = Optional([1, 2, 3]);
22
+ * for (const n of maybeArr) {
23
+ * // 1, 2, 3
24
+ * }
25
+ */
11
26
  [Symbol.iterator]() {
12
27
  return this[common_1.SuccessReturn]
13
28
  ? this[common_1.ValueReturn][Symbol.iterator]()
14
29
  : common_1.EmptyArray[Symbol.iterator]();
15
30
  }
31
+ /**
32
+ * Verifica se existe valor.
33
+ * @returns true se houver valor.
34
+ * @example
35
+ * Optional("abc").hasSomething(); // true
36
+ * Optional("").hasSomething(); // false
37
+ */
16
38
  hasSomething() {
17
39
  return this[common_1.SuccessReturn];
18
40
  }
41
+ /**
42
+ * Verifica se não existe valor.
43
+ * @returns true se não houver valor.
44
+ * @example
45
+ * Optional(null).hasNothing(); // true
46
+ */
19
47
  hasNothing() {
20
48
  return !this[common_1.SuccessReturn];
21
49
  }
50
+ /**
51
+ * Retorna o valor interno.
52
+ * @throws {Error} Se não houver valor.
53
+ * @example
54
+ * Optional(10).unpack(); // 10
55
+ */
22
56
  unpack() {
23
57
  if (this[common_1.SuccessReturn]) {
24
58
  return this[common_1.ValueReturn];
25
59
  }
26
60
  throw new Error("Failed to unpack optional value. There is nothing in here");
27
61
  }
62
+ /**
63
+ * Retorna o valor interno mesmo quando vazio.
64
+ * @returns Valor interno ou undefined.
65
+ * @example
66
+ * Optional("x").unpackAnyway(); // "x"
67
+ * Optional("").unpackAnyway(); // undefined
68
+ */
28
69
  unpackAnyway() {
29
70
  return this[common_1.ValueReturn];
30
71
  }
72
+ /**
73
+ * Retorna o valor interno ou valor padrão.
74
+ * @param def Valor padrão.
75
+ * @example
76
+ * Optional("").unpackOr("default"); // "default"
77
+ */
31
78
  unpackOr(def) {
32
79
  return this[common_1.SuccessReturn] ? this[common_1.ValueReturn] : def;
33
80
  }
81
+ /**
82
+ * Retorna o valor interno ou calcula valor padrão sob demanda.
83
+ * @param f Função para gerar valor padrão.
84
+ * @example
85
+ * Optional("").unpackOrElse(() => "fallback"); // "fallback"
86
+ */
34
87
  unpackOrElse(f) {
35
88
  return this[common_1.SuccessReturn] ? this[common_1.ValueReturn] : f();
36
89
  }
37
90
  }
91
+ /**
92
+ * Cria um Optional a partir de um valor.
93
+ * Valores falsey e Error resultam em Nothing.
94
+ * @param val Valor de entrada.
95
+ * @example
96
+ * Optional("abc").hasSomething(); // true
97
+ * Optional(0).hasNothing(); // true
98
+ */
38
99
  function Optional(val) {
39
100
  return from(val);
40
101
  }
102
+ /**
103
+ * Cria um Optional no estado Something.
104
+ * @param val Valor presente.
105
+ * @example
106
+ * const some = Something("ok");
107
+ */
41
108
  function Something(val) {
42
109
  return new OptionalType(val, true);
43
110
  }
111
+ /**
112
+ * Singleton representando ausência de valor.
113
+ * @example
114
+ * Nothing.hasNothing(); // true
115
+ */
44
116
  exports.Nothing = Object.freeze(new OptionalType(undefined, false));
117
+ /**
118
+ * Converte valor para Optional conforme regra de truthy.
119
+ * @param val Valor de entrada.
120
+ * @example
121
+ * from("x").hasSomething(); // true
122
+ * from(false).hasNothing(); // true
123
+ */
45
124
  function from(val) {
46
125
  return (0, common_1.isTruthy)(val) && !(val instanceof Error)
47
126
  ? Something(val)
@@ -1,22 +1,22 @@
1
- import { FalseyValues, IterType, SuccessReturn, ValueReturn } from "./common";
1
+ import { FalseyValues, IterType, SuccessReturn as SuccessReturnSymbol, ValueReturn as ValueReturnSymbol } from "./common";
2
2
  import { FailureAbstract } from "../../../objects/failure/edges/failure.abstract";
3
- export type Success<SuccessReturn> = ReturnType<SuccessReturn, never>;
4
- export type Failure<FailureReturn> = ReturnType<never, FailureReturn>;
5
- export type Return<T, E> = ReturnType<T, E>;
3
+ export type Success<TSuccess> = ReturnType<TSuccess, never>;
4
+ export type Failure<TFailure> = ReturnType<never, TFailure>;
5
+ export type Return<TSuccess, TFailure> = ReturnType<TSuccess, TFailure>;
6
6
  type From<T> = Exclude<T, Error | FalseyValues>;
7
- export declare class ReturnType<SuccessReturn, FailureReturn> {
8
- readonly [SuccessReturn]: boolean;
9
- readonly [ValueReturn]: SuccessReturn | FailureReturn | undefined;
10
- constructor(value: SuccessReturn | FailureReturn | undefined, hasSuccess: boolean);
11
- [Symbol.iterator](this: Return<SuccessReturn, FailureReturn>): IterType<SuccessReturn>;
12
- hasSuccess(this: Return<SuccessReturn, FailureReturn>): this is Success<SuccessReturn>;
13
- hasFailure(this: Return<SuccessReturn, FailureReturn>): this is Failure<FailureReturn>;
14
- unwrap(this: Return<SuccessReturn, FailureReturn>): SuccessReturn;
15
- unwrapFail(this: Return<SuccessReturn, FailureReturn>): FailureReturn;
16
- unwrapOr(this: Return<SuccessReturn, FailureReturn>, def: SuccessReturn): SuccessReturn;
17
- unwrapAnyway(this: Return<SuccessReturn, FailureReturn>): SuccessReturn | FailureReturn | undefined;
7
+ export declare class ReturnType<TSuccess, TFailure> {
8
+ readonly [SuccessReturnSymbol]: boolean;
9
+ readonly [ValueReturnSymbol]: TSuccess | TFailure | undefined;
10
+ constructor(value: TSuccess | TFailure | undefined, hasSuccess: boolean);
11
+ [Symbol.iterator](this: Return<TSuccess, TFailure>): IterType<TSuccess>;
12
+ hasSuccess(this: Return<TSuccess, TFailure>): this is Success<TSuccess>;
13
+ hasFailure(this: Return<TSuccess, TFailure>): this is Failure<TFailure>;
14
+ unwrap(this: Return<TSuccess, TFailure>): TSuccess;
15
+ unwrapFail(this: Return<TSuccess, TFailure>): TFailure;
16
+ unwrapOr(this: Return<TSuccess, TFailure>, def: TSuccess): TSuccess;
17
+ unwrapAnyway(this: Return<TSuccess, TFailure>): TSuccess | TFailure | undefined;
18
18
  }
19
19
  export declare function Return<T>(val: T): Return<From<T>, (T extends Error ? T : never) | (Extract<FalseyValues, T> extends never ? never : null)>;
20
- export declare function Success<SuccessReturn>(successValue?: SuccessReturn): Success<SuccessReturn>;
21
- export declare function Failure<FailureReturn extends FailureAbstract | null | undefined>(failureValue?: FailureReturn): Failure<FailureReturn>;
20
+ export declare function Success<TSuccess>(successValue?: TSuccess): Success<TSuccess>;
21
+ export declare function Failure<TFailure extends FailureAbstract | Error | null | undefined>(failureValue?: TFailure): Failure<TFailure>;
22
22
  export {};
@@ -11,9 +11,17 @@ class ReturnType {
11
11
  this[common_1.SuccessReturn] = hasSuccess;
12
12
  }
13
13
  [Symbol.iterator]() {
14
- return this[common_1.SuccessReturn]
15
- ? this[common_1.ValueReturn][Symbol.iterator]()
16
- : common_1.EmptyArray[Symbol.iterator]();
14
+ if (!this[common_1.SuccessReturn]) {
15
+ return common_1.EmptyArray[Symbol.iterator]();
16
+ }
17
+ const value = this[common_1.ValueReturn];
18
+ if (value !== null &&
19
+ value !== undefined &&
20
+ typeof value[Symbol.iterator] ===
21
+ "function") {
22
+ return value[Symbol.iterator]();
23
+ }
24
+ return common_1.EmptyArray[Symbol.iterator]();
17
25
  }
18
26
  hasSuccess() {
19
27
  return this[common_1.SuccessReturn];
@@ -34,7 +42,9 @@ class ReturnType {
34
42
  return this[common_1.ValueReturn];
35
43
  }
36
44
  unwrapOr(def) {
37
- return this[common_1.SuccessReturn] ? this[common_1.ValueReturn] : def;
45
+ return this[common_1.SuccessReturn]
46
+ ? this[common_1.ValueReturn]
47
+ : def;
38
48
  }
39
49
  unwrapAnyway() {
40
50
  return this[common_1.ValueReturn];
@@ -7,5 +7,11 @@ export declare class HttpProvider {
7
7
  readonly baseUrl: string;
8
8
  protected readonly axios: AxiosInstance;
9
9
  constructor(baseUrl: string);
10
+ setDefaultHeader(key: string, value: string): void;
11
+ removeDefaultHeader(key: string): void;
12
+ setBearerToken(token: string): void;
10
13
  request(request: RequestAbstract): Promise<Return<HttpProviderSuccessResponse, HttpProviderFailureResponse>>;
14
+ private isSuccessStatus;
15
+ private buildFailureFromResponse;
16
+ private extractErrorMessage;
11
17
  }
@@ -8,7 +8,6 @@ const axios_1 = __importDefault(require("axios"));
8
8
  const return_type_1 = require("../ducts/return-type");
9
9
  const http_provider_failure_response_1 = require("./http-provider-failure-response");
10
10
  const http_provider_success_response_1 = require("./http-provider-success-response");
11
- const guardian_1 = require("../type-guard/guardian");
12
11
  const http_status_codes_point_1 = require("../../../objects/@common/points/http-status-codes.point");
13
12
  function replaceUrlParams(url, params) {
14
13
  return url.replace(/:([a-zA-Z0-9_]+)/g, (_, key) => {
@@ -26,31 +25,61 @@ class HttpProvider {
26
25
  baseURL: baseUrl,
27
26
  });
28
27
  }
28
+ setDefaultHeader(key, value) {
29
+ this.axios.defaults.headers.common[key] = value;
30
+ }
31
+ removeDefaultHeader(key) {
32
+ delete this.axios.defaults.headers.common[key];
33
+ }
34
+ setBearerToken(token) {
35
+ this.setDefaultHeader("Authorization", `Bearer ${token}`);
36
+ }
29
37
  async request(request) {
38
+ let resolvedUrl = request.url;
30
39
  try {
31
- let url = request.url;
32
- if (guardian_1._.isNotUndefined(request.params)) {
33
- url = replaceUrlParams(url, request.params);
40
+ if (request.params) {
41
+ resolvedUrl = replaceUrlParams(resolvedUrl, request.params);
34
42
  }
35
43
  const response = await this.axios.request({
36
- url,
44
+ url: resolvedUrl,
37
45
  method: request.method,
38
- params: { ...request.queryParams },
46
+ params: request.queryParams,
39
47
  data: request.payload,
40
- headers: { ...request.headers },
48
+ headers: request.headers,
41
49
  responseType: request.responseType,
42
- validateStatus: (status) => {
43
- return true;
44
- },
50
+ validateStatus: () => true,
45
51
  });
46
- if (guardian_1._.or(guardian_1._.isEqual(response.status, http_status_codes_point_1.HttpStatusCodes.UNAUTHORIZED), guardian_1._.isEqual(response.status, http_status_codes_point_1.HttpStatusCodes.FORBIDDEN), guardian_1._.isEqual(response.status, http_status_codes_point_1.HttpStatusCodes.INTERNAL_SERVER_ERROR), guardian_1._.isEqual(response.status, http_status_codes_point_1.HttpStatusCodes.BAD_REQUEST), guardian_1._.isEqual(response.status, http_status_codes_point_1.HttpStatusCodes.NOT_FOUND))) {
47
- return (0, return_type_1.Failure)(new http_provider_failure_response_1.HttpProviderFailureResponse(response.data.message, response.status, request.url));
52
+ if (!this.isSuccessStatus(response.status)) {
53
+ return (0, return_type_1.Failure)(this.buildFailureFromResponse(response, resolvedUrl));
48
54
  }
49
55
  return (0, return_type_1.Success)(new http_provider_success_response_1.HttpProviderSuccessResponse(response.status, response.data));
50
56
  }
51
- catch {
52
- return (0, return_type_1.Failure)(new http_provider_failure_response_1.HttpProviderFailureResponse("The request could not be completed. The server is currently unavailable.", http_status_codes_point_1.HttpStatusCodes.SERVICE_UNAVAILABLE, request.url));
57
+ catch (error) {
58
+ if (axios_1.default.isAxiosError(error) && error.response) {
59
+ return (0, return_type_1.Failure)(this.buildFailureFromResponse(error.response, resolvedUrl));
60
+ }
61
+ return (0, return_type_1.Failure)(new http_provider_failure_response_1.HttpProviderFailureResponse("The request could not be completed. The server is currently unavailable.", http_status_codes_point_1.HttpStatusCodes.SERVICE_UNAVAILABLE, resolvedUrl));
62
+ }
63
+ }
64
+ isSuccessStatus(status) {
65
+ return status >= 200 && status < 300;
66
+ }
67
+ buildFailureFromResponse(response, url) {
68
+ return new http_provider_failure_response_1.HttpProviderFailureResponse(this.extractErrorMessage(response.data, response.statusText), response.status, url);
69
+ }
70
+ extractErrorMessage(data, fallback) {
71
+ if (typeof data === "string" && data.trim().length > 0) {
72
+ return data;
73
+ }
74
+ if (data && typeof data === "object") {
75
+ const maybeMessage = data.message;
76
+ if (typeof maybeMessage === "string" && maybeMessage.trim().length > 0) {
77
+ return maybeMessage;
78
+ }
53
79
  }
80
+ return fallback?.trim()
81
+ ? fallback
82
+ : "The request failed. Please try again later.";
54
83
  }
55
84
  }
56
85
  exports.HttpProvider = HttpProvider;
@@ -1,6 +1,45 @@
1
1
  export declare abstract class MapAbstract<K, V> {
2
2
  protected abstract map: Map<K, V>;
3
+ /**
4
+ * Mantido por compatibilidade.
5
+ * Retorna o valor mapeado para a chave ou lança erro.
6
+ */
3
7
  toMap(key: K): V;
8
+ /**
9
+ * Retorna o valor mapeado para a chave ou lança erro.
10
+ */
11
+ get(key: K): V;
12
+ /**
13
+ * Retorna true se a chave existir no map.
14
+ */
15
+ has(key: K): boolean;
16
+ /**
17
+ * Retorna o valor ou undefined.
18
+ */
19
+ getOrUndefined(key: K): V | undefined;
20
+ /**
21
+ * Retorna a quantidade de itens.
22
+ */
23
+ size(): number;
24
+ /**
25
+ * Retorna true se o map estiver vazio.
26
+ */
27
+ isEmpty(): boolean;
28
+ /**
29
+ * Retorna todas as chaves.
30
+ */
31
+ keys(): K[];
32
+ /**
33
+ * Retorna todos os valores.
34
+ */
35
+ values(): V[];
36
+ /**
37
+ * Retorna todas as entradas.
38
+ */
39
+ entries(): Array<[K, V]>;
4
40
  protected getOrDefault(key: K, defaultValue: V): V;
5
41
  protected getOrThrow(key: K, errorMessage?: string): V;
42
+ protected set(key: K, value: V): void;
43
+ protected delete(key: K): boolean;
44
+ protected clear(): void;
6
45
  }
@@ -1,20 +1,84 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MapAbstract = void 0;
4
- const guardian_1 = require("../type-guard/guardian");
5
4
  class MapAbstract {
5
+ /**
6
+ * Mantido por compatibilidade.
7
+ * Retorna o valor mapeado para a chave ou lança erro.
8
+ */
6
9
  toMap(key) {
7
10
  return this.getOrThrow(key);
8
11
  }
12
+ /**
13
+ * Retorna o valor mapeado para a chave ou lança erro.
14
+ */
15
+ get(key) {
16
+ return this.getOrThrow(key);
17
+ }
18
+ /**
19
+ * Retorna true se a chave existir no map.
20
+ */
21
+ has(key) {
22
+ return this.map.has(key);
23
+ }
24
+ /**
25
+ * Retorna o valor ou undefined.
26
+ */
27
+ getOrUndefined(key) {
28
+ return this.map.get(key);
29
+ }
30
+ /**
31
+ * Retorna a quantidade de itens.
32
+ */
33
+ size() {
34
+ return this.map.size;
35
+ }
36
+ /**
37
+ * Retorna true se o map estiver vazio.
38
+ */
39
+ isEmpty() {
40
+ return this.map.size === 0;
41
+ }
42
+ /**
43
+ * Retorna todas as chaves.
44
+ */
45
+ keys() {
46
+ return Array.from(this.map.keys());
47
+ }
48
+ /**
49
+ * Retorna todos os valores.
50
+ */
51
+ values() {
52
+ return Array.from(this.map.values());
53
+ }
54
+ /**
55
+ * Retorna todas as entradas.
56
+ */
57
+ entries() {
58
+ return Array.from(this.map.entries());
59
+ }
9
60
  getOrDefault(key, defaultValue) {
10
- return this.map.get(key) ?? defaultValue;
61
+ // bug fix: usar has() evita confundir chave existente com valor undefined
62
+ if (!this.map.has(key)) {
63
+ return defaultValue;
64
+ }
65
+ return this.map.get(key);
11
66
  }
12
67
  getOrThrow(key, errorMessage = "Key not mapped") {
13
- const value = this.map.get(key);
14
- if (guardian_1._.isUndefined(value)) {
15
- throw Error(errorMessage);
68
+ // bug fix: validar existência da chave com has(), não pelo valor retornado
69
+ if (!this.map.has(key)) {
70
+ throw new Error(errorMessage);
16
71
  }
17
- return value;
72
+ return this.map.get(key);
73
+ }
74
+ set(key, value) {
75
+ this.map.set(key, value);
76
+ }
77
+ delete(key) {
78
+ return this.map.delete(key);
79
+ }
80
+ clear() {
81
+ this.map.clear();
18
82
  }
19
83
  }
20
84
  exports.MapAbstract = MapAbstract;