@duplojs/utils 1.5.4 → 1.5.6

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.
@@ -77,3 +77,4 @@ export * from "./asserts";
77
77
  export * from "./path";
78
78
  export * from "./transformer";
79
79
  export * from "./toRegExp";
80
+ export * from "./justExec";
@@ -0,0 +1,10 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * {@include common/justExec/index.md}
5
+ */
6
+ function justExec(theFunction) {
7
+ return theFunction();
8
+ }
9
+
10
+ exports.justExec = justExec;
@@ -0,0 +1,28 @@
1
+ /**
2
+ * The justExec() function executes a callback immediately and returns the callback result.
3
+ *
4
+ * **Supported call styles:**
5
+ * - Classic: `justExec(theFunction)` -> executes `theFunction` and returns its output
6
+ * - Curried: not available for this function
7
+ *
8
+ * The callback is called once per invocation. The function itself does not transform the callback output.
9
+ *
10
+ * ```ts
11
+ * const numberResult = justExec(
12
+ * () => 42 as const,
13
+ * );
14
+ * // type: 42
15
+ *
16
+ * const objectResult = justExec(
17
+ * () => ({
18
+ * id: 1,
19
+ * name: "ZeRiix",
20
+ * }),
21
+ * );
22
+ * // type: { id: number; name: string; }
23
+ * ```
24
+ *
25
+ * @see https://utils.duplojs.dev/en/v1/api/common/justExec
26
+ *
27
+ */
28
+ export declare function justExec<GenericOutput extends unknown>(theFunction: () => GenericOutput): GenericOutput;
@@ -0,0 +1,8 @@
1
+ /**
2
+ * {@include common/justExec/index.md}
3
+ */
4
+ function justExec(theFunction) {
5
+ return theFunction();
6
+ }
7
+
8
+ export { justExec };
@@ -7,7 +7,7 @@ function memo(theFunction) {
7
7
  const payload = {
8
8
  get value() {
9
9
  const value = theFunction();
10
- Object.defineProperty(payload, "value", {
10
+ Object.defineProperty(this, "value", {
11
11
  value,
12
12
  });
13
13
  return value;
@@ -5,7 +5,7 @@ function memo(theFunction) {
5
5
  const payload = {
6
6
  get value() {
7
7
  const value = theFunction();
8
- Object.defineProperty(payload, "value", {
8
+ Object.defineProperty(this, "value", {
9
9
  value,
10
10
  });
11
11
  return value;
@@ -20,7 +20,7 @@ function memoPromise(theFunction) {
20
20
  ? promise
21
21
  : Promise.resolve(promise))
22
22
  .then((result) => {
23
- Object.defineProperty(payload, "value", {
23
+ Object.defineProperty(this, "value", {
24
24
  value: result,
25
25
  });
26
26
  resolve(result);
@@ -18,7 +18,7 @@ function memoPromise(theFunction) {
18
18
  ? promise
19
19
  : Promise.resolve(promise))
20
20
  .then((result) => {
21
- Object.defineProperty(payload, "value", {
21
+ Object.defineProperty(this, "value", {
22
22
  value: result,
23
23
  });
24
24
  resolve(result);
@@ -12,7 +12,7 @@ class InvalidBytesInStringError extends kind.kindHeritage("invalid-bytes-in-stri
12
12
  this.input = input;
13
13
  }
14
14
  }
15
- const parseRegExp = /(?<rawValue>[0-9.]+)(?<unit>b|kb|mb|gb|tb|pd)/;
15
+ const parseRegExp = /(?<rawValue>[0-9.]+)(?<unit>b|kb|mb|gb|tb|pb)/;
16
16
  const unitMapper = {
17
17
  b: 1,
18
18
  kb: 1 << 10,
@@ -10,7 +10,7 @@ class InvalidBytesInStringError extends kindHeritage("invalid-bytes-in-string-er
10
10
  this.input = input;
11
11
  }
12
12
  }
13
- const parseRegExp = /(?<rawValue>[0-9.]+)(?<unit>b|kb|mb|gb|tb|pd)/;
13
+ const parseRegExp = /(?<rawValue>[0-9.]+)(?<unit>b|kb|mb|gb|tb|pb)/;
14
14
  const unitMapper = {
15
15
  b: 1,
16
16
  kb: 1 << 10,
@@ -134,10 +134,10 @@ function dataParserInit(kind, definition, exec, specificOverrideHandler) {
134
134
  [KWV]: result,
135
135
  };
136
136
  },
137
- addChecker: (...checkers) => dataParserInit(kind, simpleClone.simpleClone({
137
+ addChecker: (...checkers) => dataParserInit(kind, {
138
138
  ...definition,
139
139
  checkers: [...definition.checkers, ...checkers],
140
- }), exec, specificOverrideHandler),
140
+ }, exec, specificOverrideHandler),
141
141
  clone: () => dataParserInit(kind, simpleClone.simpleClone(definition), exec, specificOverrideHandler),
142
142
  contract: () => self,
143
143
  parseOrThrow(data) {
@@ -132,10 +132,10 @@ function dataParserInit(kind, definition, exec, specificOverrideHandler) {
132
132
  [KWV]: result,
133
133
  };
134
134
  },
135
- addChecker: (...checkers) => dataParserInit(kind, simpleClone({
135
+ addChecker: (...checkers) => dataParserInit(kind, {
136
136
  ...definition,
137
137
  checkers: [...definition.checkers, ...checkers],
138
- }), exec, specificOverrideHandler),
138
+ }, exec, specificOverrideHandler),
139
139
  clone: () => dataParserInit(kind, simpleClone(definition), exec, specificOverrideHandler),
140
140
  contract: () => self,
141
141
  parseOrThrow(data) {
@@ -62,6 +62,9 @@ function dataParserExtendedInit(dataParser, rest, specificOverrideHandler) {
62
62
  contract() {
63
63
  return self;
64
64
  },
65
+ contractExtended() {
66
+ return self;
67
+ },
65
68
  }, extendedKind.setTo, dataParserExtendedInit.overrideHandler.apply, specificOverrideHandler.apply);
66
69
  return self;
67
70
  }
@@ -1,4 +1,4 @@
1
- import { type Kind, type NeverCoalescing, type AnyFunction, type SimplifyTopLevel, type AnyValue, type OverrideHandler, type GetKind, type RemoveKind } from "../common";
1
+ import { type Kind, type NeverCoalescing, type AnyFunction, type SimplifyTopLevel, type AnyValue, type OverrideHandler, type GetKind, type RemoveKind, type IsEqual } from "../common";
2
2
  import { type MergeDefinition } from "./types";
3
3
  import { type Output, type DataParser, type DataParserDefinition } from "./base";
4
4
  import type * as DEither from "../either";
@@ -7,6 +7,7 @@ import * as dataParsersExtended from "./extended";
7
7
  import { type DataParserError } from "./error";
8
8
  export declare const extendedKind: import("../common").KindHandler<import("../common").KindDefinition<"@DuplojsUtilsDataParser/extended", unknown>>;
9
9
  type _DataParserExtended<GenericDefinition extends DataParserDefinition, GenericOutput extends unknown, GenericInput extends unknown> = (DataParser<GenericDefinition, GenericOutput, GenericInput> & Kind<typeof extendedKind.definition>);
10
+ declare const SymbolContractExtendedError: unique symbol;
10
11
  export interface DataParserExtended<GenericDefinition extends DataParserDefinition = DataParserDefinition, GenericOutput extends unknown = unknown, GenericInput extends unknown = GenericOutput> extends _DataParserExtended<GenericDefinition, GenericOutput, GenericInput> {
11
12
  /**
12
13
  * The parse() method runs an extended data parser synchronously and returns an Either with the parsed value or a DataParserError.
@@ -149,6 +150,9 @@ export interface DataParserExtended<GenericDefinition extends DataParserDefiniti
149
150
  *
150
151
  */
151
152
  clone(): this;
153
+ contractExtended<GenericValue extends unknown>(...args: IsEqual<Output<this>, GenericValue> extends true ? [] : [] & {
154
+ [SymbolContractExtendedError]: "ContractExtended error.";
155
+ }): ContractExtended<GenericValue>;
152
156
  /**
153
157
  * Creates an array parser from the current parser.
154
158
  *
@@ -60,6 +60,9 @@ function dataParserExtendedInit(dataParser, rest, specificOverrideHandler) {
60
60
  contract() {
61
61
  return self;
62
62
  },
63
+ contractExtended() {
64
+ return self;
65
+ },
63
66
  }, extendedKind.setTo, dataParserExtendedInit.overrideHandler.apply, specificOverrideHandler.apply);
64
67
  return self;
65
68
  }
package/dist/index.cjs CHANGED
@@ -65,6 +65,7 @@ var asserts = require('./common/asserts.cjs');
65
65
  var path = require('./common/path.cjs');
66
66
  var transformer = require('./common/transformer.cjs');
67
67
  var toRegExp = require('./common/toRegExp.cjs');
68
+ var justExec = require('./common/justExec.cjs');
68
69
 
69
70
 
70
71
 
@@ -165,3 +166,4 @@ exports.toJSON = transformer.toJSON;
165
166
  exports.toNative = transformer.toNative;
166
167
  exports.transformer = transformer.transformer;
167
168
  exports.toRegExp = toRegExp.toRegExp;
169
+ exports.justExec = justExec.justExec;
package/dist/index.mjs CHANGED
@@ -87,3 +87,4 @@ export { AssertsError, asserts } from './common/asserts.mjs';
87
87
  export { Path } from './common/path.mjs';
88
88
  export { createTransformer, toJSON, toNative, transformer } from './common/transformer.mjs';
89
89
  export { toRegExp } from './common/toRegExp.mjs';
90
+ export { justExec } from './common/justExec.mjs';
@@ -1415,6 +1415,15 @@
1415
1415
  {
1416
1416
  "name": "isType.mjs"
1417
1417
  },
1418
+ {
1419
+ "name": "justExec.cjs"
1420
+ },
1421
+ {
1422
+ "name": "justExec.d.ts"
1423
+ },
1424
+ {
1425
+ "name": "justExec.mjs"
1426
+ },
1418
1427
  {
1419
1428
  "name": "justReturn.cjs"
1420
1429
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@duplojs/utils",
3
- "version": "1.5.4",
3
+ "version": "1.5.6",
4
4
  "author": {
5
5
  "name": "mathcovax",
6
6
  "url": "https://github.com/mathcovax"