@duplojs/utils 1.5.5 → 1.5.7

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 (42) hide show
  1. package/dist/clean/entity/index.cjs +94 -0
  2. package/dist/clean/entity/index.d.ts +217 -0
  3. package/dist/clean/entity/index.mjs +85 -0
  4. package/dist/clean/entity/property.cjs +66 -0
  5. package/dist/clean/entity/property.d.ts +43 -0
  6. package/dist/clean/entity/property.mjs +59 -0
  7. package/dist/clean/entity/unwrap.cjs +58 -0
  8. package/dist/clean/{unwrapEntity.d.ts → entity/unwrap.d.ts} +13 -10
  9. package/dist/clean/entity/unwrap.mjs +55 -0
  10. package/dist/clean/index.cjs +14 -6
  11. package/dist/clean/index.d.ts +0 -1
  12. package/dist/clean/index.mjs +3 -2
  13. package/dist/clean/maybe.cjs +3 -3
  14. package/dist/clean/maybe.mjs +1 -1
  15. package/dist/common/memo.cjs +1 -1
  16. package/dist/common/memo.mjs +1 -1
  17. package/dist/common/memoPromise.cjs +1 -1
  18. package/dist/common/memoPromise.mjs +1 -1
  19. package/dist/dataParser/base.cjs +2 -2
  20. package/dist/dataParser/base.mjs +2 -2
  21. package/dist/dataParser/extended/tuple.d.ts +6 -2
  22. package/dist/date/computeTime.cjs +23 -0
  23. package/dist/date/computeTime.d.ts +41 -0
  24. package/dist/date/computeTime.mjs +21 -0
  25. package/dist/date/getDifference.cjs +17 -0
  26. package/dist/date/getDifference.d.ts +44 -0
  27. package/dist/date/getDifference.mjs +15 -0
  28. package/dist/date/index.cjs +4 -0
  29. package/dist/date/index.d.ts +2 -0
  30. package/dist/date/index.mjs +2 -0
  31. package/dist/date/theTime.d.ts +3 -2
  32. package/dist/date/toNative.cjs +11 -5
  33. package/dist/date/toNative.d.ts +21 -7
  34. package/dist/date/toNative.mjs +11 -5
  35. package/dist/date/types/serializedTheTime.d.ts +2 -1
  36. package/dist/metadata.json +50 -18
  37. package/package.json +2 -2
  38. package/dist/clean/entity.cjs +0 -145
  39. package/dist/clean/entity.d.ts +0 -234
  40. package/dist/clean/entity.mjs +0 -141
  41. package/dist/clean/unwrapEntity.cjs +0 -36
  42. package/dist/clean/unwrapEntity.mjs +0 -34
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Represents an immutable time value object (`TheTime`) stored in milliseconds.
2
+ * Represents an immutable signed time value object (`TheTime`) stored in milliseconds.
3
3
  *
4
4
  * Signature: `TheTime` → immutable class-like value object
5
5
  *
@@ -28,6 +28,7 @@
28
28
  *
29
29
  * @remarks
30
30
  * - `TheTime` is not a `number`, but it behaves as an immutable numeric time value.
31
+ * - `TheTime` supports negative values and is not an absolute value by default.
31
32
  * - Date unit operators (`addMinutes`, `subtractDays`, etc.) target `TheDate`; `TheTime` is used as a normalized duration value.
32
33
  * - Use `D.serialize(theTime)` to convert to `SerializedTheTime`.
33
34
  * - Use `D.createTime(...)` / `D.createTimeOrThrow(...)` to construct instances.
@@ -647,6 +647,38 @@
647
647
  }
648
648
  ]
649
649
  },
650
+ {
651
+ "name": "entity",
652
+ "files": [
653
+ {
654
+ "name": "index.cjs"
655
+ },
656
+ {
657
+ "name": "index.d.ts"
658
+ },
659
+ {
660
+ "name": "index.mjs"
661
+ },
662
+ {
663
+ "name": "property.cjs"
664
+ },
665
+ {
666
+ "name": "property.d.ts"
667
+ },
668
+ {
669
+ "name": "property.mjs"
670
+ },
671
+ {
672
+ "name": "unwrap.cjs"
673
+ },
674
+ {
675
+ "name": "unwrap.d.ts"
676
+ },
677
+ {
678
+ "name": "unwrap.mjs"
679
+ }
680
+ ]
681
+ },
650
682
  {
651
683
  "name": "primitive",
652
684
  "files": [
@@ -940,15 +972,6 @@
940
972
  }
941
973
  ]
942
974
  },
943
- {
944
- "name": "entity.cjs"
945
- },
946
- {
947
- "name": "entity.d.ts"
948
- },
949
- {
950
- "name": "entity.mjs"
951
- },
952
975
  {
953
976
  "name": "flag.cjs"
954
977
  },
@@ -1003,15 +1026,6 @@
1003
1026
  {
1004
1027
  "name": "repository.mjs"
1005
1028
  },
1006
- {
1007
- "name": "unwrapEntity.cjs"
1008
- },
1009
- {
1010
- "name": "unwrapEntity.d.ts"
1011
- },
1012
- {
1013
- "name": "unwrapEntity.mjs"
1014
- },
1015
1029
  {
1016
1030
  "name": "useCase.cjs"
1017
1031
  },
@@ -3087,6 +3101,15 @@
3087
3101
  {
3088
3102
  "name": "closestTo.mjs"
3089
3103
  },
3104
+ {
3105
+ "name": "computeTime.cjs"
3106
+ },
3107
+ {
3108
+ "name": "computeTime.d.ts"
3109
+ },
3110
+ {
3111
+ "name": "computeTime.mjs"
3112
+ },
3090
3113
  {
3091
3114
  "name": "constants.cjs"
3092
3115
  },
@@ -3159,6 +3182,15 @@
3159
3182
  {
3160
3183
  "name": "formatTime.mjs"
3161
3184
  },
3185
+ {
3186
+ "name": "getDifference.cjs"
3187
+ },
3188
+ {
3189
+ "name": "getDifference.d.ts"
3190
+ },
3191
+ {
3192
+ "name": "getDifference.mjs"
3193
+ },
3162
3194
  {
3163
3195
  "name": "getTimezoneOffset.cjs"
3164
3196
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@duplojs/utils",
3
- "version": "1.5.5",
3
+ "version": "1.5.7",
4
4
  "author": {
5
5
  "name": "mathcovax",
6
6
  "url": "https://github.com/mathcovax"
@@ -114,7 +114,7 @@
114
114
  "@commitlint/config-conventional": "19.8.1",
115
115
  "@duplojs/dev-tools": "0.2.0",
116
116
  "@duplojs/eslint": "0.5.0",
117
- "@rollup/plugin-typescript": "12.1.4",
117
+ "@rollup/plugin-typescript": "12.3.0",
118
118
  "@types/node": "24.3.0",
119
119
  "@vitest/coverage-istanbul": "3.2.4",
120
120
  "eslint": "9.34.0",
@@ -1,145 +0,0 @@
1
- 'use strict';
2
-
3
- var kind = require('./kind.cjs');
4
- var newType = require('./newType.cjs');
5
- var asserts = require('../common/asserts.cjs');
6
- var kind$1 = require('../common/kind.cjs');
7
- var pipe = require('../common/pipe.cjs');
8
- var map = require('../array/map.cjs');
9
- var toTuple = require('../array/toTuple.cjs');
10
- var first = require('../array/at/first.cjs');
11
- var innerPipe = require('../common/innerPipe.cjs');
12
- var when = require('../pattern/when.cjs');
13
- var isType = require('../common/isType.cjs');
14
- var exhaustive = require('../pattern/exhaustive.cjs');
15
- var when$1 = require('../common/when.cjs');
16
- var index = require('../dataParser/parsers/array/index.cjs');
17
- var min = require('../dataParser/parsers/array/checkers/min.cjs');
18
- var max = require('../dataParser/parsers/array/checkers/max.cjs');
19
- var nullable = require('../dataParser/parsers/nullable.cjs');
20
- var base = require('../dataParser/base.cjs');
21
- var last = require('../array/at/last.cjs');
22
- var entries = require('../object/entries.cjs');
23
- var forward = require('../common/forward.cjs');
24
- var errorKindNamespace = require('../common/errorKindNamespace.cjs');
25
- var fromEntries = require('../object/fromEntries.cjs');
26
- var entry = require('../object/entry.cjs');
27
- var transform = require('../dataParser/parsers/transform.cjs');
28
- var base$1 = require('./constraint/base.cjs');
29
- var wrapValue = require('../common/wrapValue.cjs');
30
- var minElements = require('../array/minElements.cjs');
31
- var union = require('../dataParser/parsers/union.cjs');
32
- var index$1 = require('../dataParser/parsers/object/index.cjs');
33
- var override = require('../common/override.cjs');
34
- var is = require('../either/left/is.cjs');
35
- var unwrap = require('../common/unwrap.cjs');
36
- var create = require('../either/left/create.cjs');
37
- var create$1 = require('../either/right/create.cjs');
38
-
39
- const entityKind = kind.createCleanKind("entity");
40
- const entityHandlerKind = kind.createCleanKind("entity-handler");
41
- class CreateEntityError extends kind$1.kindHeritage("create-entity-error", errorKindNamespace.createErrorKind("create-entity-error"), Error) {
42
- rawProperties;
43
- dataParserError;
44
- constructor(rawProperties, dataParserError) {
45
- super({}, ["Error when create entity."]);
46
- this.rawProperties = rawProperties;
47
- this.dataParserError = dataParserError;
48
- }
49
- }
50
- /**
51
- * {@include clean/createEntity/index.md}
52
- */
53
- function createEntity(name, getPropertiesDefinition) {
54
- function theNew(properties) {
55
- return entityKind.addTo(properties, name);
56
- }
57
- function simplePropertyDefinitionToDataParser(simplePropertyDefinition) {
58
- const constraintKindValue = pipe.pipe(simplePropertyDefinition.constraints, map.map(({ name }) => entry.entry(name, null)), fromEntries.fromEntries);
59
- return transform.transform(simplePropertyDefinition.dataParser, (value) => base$1.constrainedTypeKind.setTo(newType.newTypeKind.setTo(wrapValue.wrapValue(value), simplePropertyDefinition.name), constraintKindValue));
60
- }
61
- function unionPropertyDefinitionToDataParser(unionPropertyDefinition) {
62
- return pipe.pipe(unionPropertyDefinition, map.map(simplePropertyDefinitionToDataParser), (options) => {
63
- asserts.asserts(options, minElements.minElements(1));
64
- return union.union(options);
65
- });
66
- }
67
- const params = {
68
- union: (...type) => ({ type }),
69
- array: (definition, params) => (newType.newTypeHandlerKind.has(definition) || definition instanceof Array
70
- ? {
71
- type: definition,
72
- inArray: params ?? true,
73
- }
74
- : {
75
- ...definition,
76
- inArray: params ?? true,
77
- }),
78
- nullable: (definition) => (newType.newTypeHandlerKind.has(definition) || definition instanceof Array
79
- ? {
80
- type: definition,
81
- nullable: true,
82
- }
83
- : {
84
- ...definition,
85
- nullable: true,
86
- }),
87
- };
88
- const propertiesDefinition = getPropertiesDefinition(params);
89
- const mapDataParser = pipe.pipe(forward.forward(propertiesDefinition), entries.entries, map.map(toTuple.toTuple([
90
- first.first,
91
- innerPipe.innerPipe(last.last, when.when(newType.newTypeHandlerKind.has, simplePropertyDefinitionToDataParser), when.when(isType.isType("array"), unionPropertyDefinitionToDataParser), when.when(isType.isType("object"), (definition) => pipe.pipe(definition.type, when.when(newType.newTypeHandlerKind.has, simplePropertyDefinitionToDataParser), when.when(isType.isType("array"), unionPropertyDefinitionToDataParser), exhaustive.exhaustive, when$1.when(base.dataParserKind.has, innerPipe.innerPipe((dataParser) => {
92
- if (definition.inArray) {
93
- return pipe.pipe(index.array(dataParser), (dataParser) => typeof definition.inArray === "object"
94
- && definition.inArray.min !== undefined
95
- ? dataParser.addChecker(min.checkerArrayMin(definition.inArray.min))
96
- : dataParser, (dataParser) => typeof definition.inArray === "object"
97
- && definition.inArray.max !== undefined
98
- ? dataParser.addChecker(max.checkerArrayMax(definition.inArray.max))
99
- : dataParser);
100
- }
101
- return dataParser;
102
- }, when$1.when(() => definition.nullable === true, nullable.nullable))))), exhaustive.exhaustive),
103
- ])), fromEntries.fromEntries, index$1.object, (dataParser) => transform.transform(dataParser, (value) => entityKind.setTo(value, name)));
104
- function map$1(rawProperties) {
105
- const result = mapDataParser.parse(rawProperties);
106
- if (is.isLeft(result)) {
107
- return create.left("createEntityError", unwrap.unwrap(result));
108
- }
109
- return create$1.right("createEntity", unwrap.unwrap(result));
110
- }
111
- function mapOrThrow(rawProperties) {
112
- const result = mapDataParser.parse(rawProperties);
113
- if (is.isLeft(result)) {
114
- throw new CreateEntityError(rawProperties, unwrap.unwrap(result));
115
- }
116
- return unwrap.unwrap(result);
117
- }
118
- function is$1(input) {
119
- return entityKind.has(input) && entityKind.getValue(input) === name;
120
- }
121
- function update(entity, newProperties) {
122
- const updatedEntity = {};
123
- for (const key in propertiesDefinition) {
124
- updatedEntity[key] = newProperties[key] !== undefined
125
- ? newProperties[key]
126
- : entity[key];
127
- }
128
- return entityKind.setTo(updatedEntity, name);
129
- }
130
- return pipe.pipe({
131
- name,
132
- propertiesDefinition,
133
- mapDataParser,
134
- new: theNew,
135
- map: map$1,
136
- mapOrThrow,
137
- is: is$1,
138
- update,
139
- }, entityHandlerKind.setTo, createEntity.overrideHandler.apply);
140
- }
141
- createEntity.overrideHandler = override.createOverride("@duplojs/utils/clean/entity");
142
-
143
- exports.CreateEntityError = CreateEntityError;
144
- exports.createEntity = createEntity;
145
- exports.entityKind = entityKind;
@@ -1,234 +0,0 @@
1
- import { type SimplifyTopLevel, type Kind, type Unwrap, type IsEqual, type IsExtends, type Or, type NeverCoalescing, type RemoveKind } from "../common";
2
- import { type GetNewType, type NewTypeHandler } from "./newType";
3
- import * as DEither from "../either";
4
- import * as DDataParser from "../dataParser";
5
- import * as DObject from "../object";
6
- import * as DArray from "../array";
7
- export type EntitySimplePropertyDefinition = NewTypeHandler<string, unknown, readonly any[]>;
8
- export type EntityUnionPropertyDefinition = readonly [
9
- EntitySimplePropertyDefinition,
10
- ...EntitySimplePropertyDefinition[]
11
- ];
12
- export interface EntityAdvancedArrayPropertyDefinition {
13
- min?: number;
14
- max?: number;
15
- }
16
- export interface EntityAdvancedPropertyDefinition {
17
- type: (EntitySimplePropertyDefinition | EntityUnionPropertyDefinition);
18
- nullable?: true;
19
- inArray?: true | EntityAdvancedArrayPropertyDefinition;
20
- }
21
- export type EntityPropertyDefinition = (EntitySimplePropertyDefinition | EntityUnionPropertyDefinition | EntityAdvancedPropertyDefinition);
22
- export type EntityPropertiesDefinition = Readonly<Record<string, EntityPropertyDefinition>>;
23
- export type EntityProperties<GenericPropertiesDefinition extends EntityPropertiesDefinition = EntityPropertiesDefinition> = SimplifyTopLevel<{
24
- readonly [Prop in keyof GenericPropertiesDefinition]: (GenericPropertiesDefinition[Prop] extends EntitySimplePropertyDefinition ? GetNewType<GenericPropertiesDefinition[Prop]> : GenericPropertiesDefinition[Prop] extends EntityUnionPropertyDefinition ? GetNewType<GenericPropertiesDefinition[Prop][number]> : GenericPropertiesDefinition[Prop] extends EntityAdvancedPropertyDefinition ? GetNewType<GenericPropertiesDefinition[Prop]["type"] extends EntityUnionPropertyDefinition ? GenericPropertiesDefinition[Prop]["type"][number] : GenericPropertiesDefinition[Prop]["type"] extends EntitySimplePropertyDefinition ? GenericPropertiesDefinition[Prop]["type"] : never> extends infer InferredValue ? (IsEqual<GenericPropertiesDefinition[Prop]["inArray"], true> extends true ? readonly InferredValue[] : GenericPropertiesDefinition[Prop]["inArray"] extends object ? GenericPropertiesDefinition[Prop]["inArray"]["min"] extends number ? readonly [
25
- ...DArray.CreateTuple<InferredValue, GenericPropertiesDefinition[Prop]["inArray"]["min"]>,
26
- ...InferredValue[]
27
- ] : readonly InferredValue[] : InferredValue) extends infer InferredValueWithArray ? IsEqual<GenericPropertiesDefinition[Prop]["nullable"], true> extends true ? InferredValueWithArray | null : InferredValueWithArray : never : never : unknown);
28
- }>;
29
- export type EntityRawProperties<GenericPropertiesDefinition extends EntityPropertiesDefinition = EntityPropertiesDefinition> = SimplifyTopLevel<{
30
- readonly [Prop in keyof GenericPropertiesDefinition]: (GenericPropertiesDefinition[Prop] extends EntitySimplePropertyDefinition ? Unwrap<GetNewType<GenericPropertiesDefinition[Prop]>> : GenericPropertiesDefinition[Prop] extends EntityUnionPropertyDefinition ? Unwrap<GetNewType<GenericPropertiesDefinition[Prop][number]>> : GenericPropertiesDefinition[Prop] extends EntityAdvancedPropertyDefinition ? Unwrap<GetNewType<GenericPropertiesDefinition[Prop]["type"] extends EntityUnionPropertyDefinition ? GenericPropertiesDefinition[Prop]["type"][number] : GenericPropertiesDefinition[Prop]["type"] extends EntitySimplePropertyDefinition ? GenericPropertiesDefinition[Prop]["type"] : never>> extends infer InferredValue ? (IsEqual<GenericPropertiesDefinition[Prop]["inArray"], true> extends true ? readonly InferredValue[] : GenericPropertiesDefinition[Prop]["inArray"] extends object ? GenericPropertiesDefinition[Prop]["inArray"]["min"] extends number ? readonly [
31
- ...DArray.CreateTuple<InferredValue, GenericPropertiesDefinition[Prop]["inArray"]["min"]>,
32
- ...InferredValue[]
33
- ] : readonly InferredValue[] : InferredValue) extends infer InferredValueWithArray ? IsEqual<GenericPropertiesDefinition[Prop]["nullable"], true> extends true ? InferredValueWithArray | null : InferredValueWithArray : never : never : unknown);
34
- }>;
35
- type GetInputFromNewTypeHandler<GenericNewTypeHandler extends NewTypeHandler> = GenericNewTypeHandler extends NewTypeHandler<any, infer InferredValue, any, infer InferredInput> ? IsEqual<InferredInput, never> extends true ? InferredValue : InferredInput : never;
36
- export type PropertiesToMapOfEntity<GenericPropertiesDefinition extends EntityPropertiesDefinition = EntityPropertiesDefinition> = SimplifyTopLevel<{
37
- readonly [Prop in keyof GenericPropertiesDefinition]: (GenericPropertiesDefinition[Prop] extends EntitySimplePropertyDefinition ? GetInputFromNewTypeHandler<GenericPropertiesDefinition[Prop]> : GenericPropertiesDefinition[Prop] extends EntityUnionPropertyDefinition ? GetInputFromNewTypeHandler<GenericPropertiesDefinition[Prop][number]> : GenericPropertiesDefinition[Prop] extends EntityAdvancedPropertyDefinition ? GetInputFromNewTypeHandler<GenericPropertiesDefinition[Prop]["type"] extends EntityUnionPropertyDefinition ? GenericPropertiesDefinition[Prop]["type"][number] : GenericPropertiesDefinition[Prop]["type"] extends EntitySimplePropertyDefinition ? GenericPropertiesDefinition[Prop]["type"] : never> extends infer InferredValue ? (Or<[
38
- IsEqual<GenericPropertiesDefinition[Prop]["inArray"], true>,
39
- IsExtends<GenericPropertiesDefinition[Prop]["inArray"], object>
40
- ]> extends true ? readonly InferredValue[] : InferredValue) extends infer InferredValueWithArray ? IsEqual<GenericPropertiesDefinition[Prop]["nullable"], true> extends true ? InferredValueWithArray | null : InferredValueWithArray : never : never : never);
41
- }>;
42
- export declare const entityKind: import("../common").KindHandler<import("../common").KindDefinition<"@DuplojsUtilsClean/entity", string>>;
43
- export interface Entity<GenericName extends string = string> extends Kind<typeof entityKind.definition, GenericName> {
44
- }
45
- declare const entityHandlerKind: import("../common").KindHandler<import("../common").KindDefinition<"@DuplojsUtilsClean/entity-handler", unknown>>;
46
- export interface EntityHandler<GenericName extends string = string, GenericPropertiesDefinition extends EntityPropertiesDefinition = EntityPropertiesDefinition> extends Kind<typeof entityHandlerKind.definition> {
47
- /**
48
- * The entity name used as a runtime identifier (for example "User").
49
- *
50
- */
51
- readonly name: GenericName;
52
- /**
53
- * The properties schema returned by the definition callback.
54
- *
55
- */
56
- readonly propertiesDefinition: GenericPropertiesDefinition;
57
- readonly mapDataParser: DDataParser.Contract<EntityProperties<GenericPropertiesDefinition>, unknown>;
58
- /**
59
- * Builds an entity from already typed properties.
60
- *
61
- * ```ts
62
- * export function create(params: {
63
- * id: Id;
64
- * name: Name;
65
- * }) {
66
- * return Entity.new({
67
- * ...params,
68
- * nick: null,
69
- * roles: [defaultRole],
70
- * });
71
- * ```
72
- *
73
- */
74
- "new"<const GenericProperties extends EntityProperties<GenericPropertiesDefinition>>(properties: GenericProperties): Entity<GenericName> & GenericProperties;
75
- /**
76
- * Validates permissive raw properties and returns an Either with the typed entity.
77
- *
78
- * ```ts
79
- * if (User.Entity.is(result)) {
80
- * // result: C.Entity<"User">
81
- * }
82
- *
83
- * const mappedResult = User.Entity.map({
84
- * id: 3,
85
- * name: "Eve",
86
- * roles: ["manager"],
87
- * nick: null,
88
- * ```
89
- *
90
- */
91
- map(rawProperties: PropertiesToMapOfEntity<GenericPropertiesDefinition>): (DEither.Right<"createEntity", Entity<GenericName> & EntityProperties<GenericPropertiesDefinition>> | DEither.Left<"createEntityError", DDataParser.DataParserError>);
92
- /**
93
- * Validates permissive raw properties and throws on error.
94
- *
95
- * ```ts
96
- * const mapped = User.Entity.mapOrThrow({
97
- * id: 2,
98
- * name: "Bob",
99
- * roles: ["client"],
100
- * nick: "Bobby",
101
- * });
102
- * ```
103
- *
104
- */
105
- mapOrThrow(rawProperties: PropertiesToMapOfEntity<GenericPropertiesDefinition>): Entity<GenericName> & EntityProperties<GenericPropertiesDefinition>;
106
- /**
107
- * Checks if a value is an entity of this handler (type guard).
108
- *
109
- * ```ts
110
- * const result = true ? mapped : null;
111
- *
112
- * if (User.Entity.is(result)) {
113
- * // result: C.Entity<"User">
114
- * ```
115
- *
116
- */
117
- is<GenericInput extends unknown>(input: GenericInput): input is Extract<GenericInput, Entity<GenericName>>;
118
- /**
119
- * Updates an entity by merging typed properties into an existing entity.
120
- *
121
- * ```ts
122
- * const updated = User.Entity.update(mapped, {
123
- * name: User.Name.createOrThrow("Bobby"),
124
- * nick: null,
125
- * });
126
- * ```
127
- *
128
- */
129
- update<const GenericEntity extends Entity<GenericName>, const GenericProperties extends Partial<EntityProperties<GenericPropertiesDefinition>>>(entity: GenericEntity, properties: GenericProperties): Entity<GenericName> & DObject.AssignObjects<RemoveKind<GenericEntity>, GenericProperties>;
130
- }
131
- declare const CreateEntityError_base: new (params: {
132
- "@DuplojsUtilsError/create-entity-error"?: unknown;
133
- }, parentParams: readonly [message?: string | undefined, options?: ErrorOptions | undefined]) => Error & Kind<import("../common").KindDefinition<"create-entity-error", unknown>, unknown> & Kind<import("../common").KindDefinition<"@DuplojsUtilsError/create-entity-error", unknown>, unknown>;
134
- export declare class CreateEntityError extends CreateEntityError_base {
135
- rawProperties: PropertiesToMapOfEntity;
136
- dataParserError: DDataParser.DataParserError;
137
- constructor(rawProperties: PropertiesToMapOfEntity, dataParserError: DDataParser.DataParserError);
138
- }
139
- export interface PropertiesDefinitionParams {
140
- union<const GenericUnionPropertyDefinition extends EntityUnionPropertyDefinition>(...type: GenericUnionPropertyDefinition): {
141
- type: GenericUnionPropertyDefinition;
142
- };
143
- nullable<const GenericPropertyDefinition extends EntityPropertyDefinition>(definition: GenericPropertyDefinition): GenericPropertyDefinition extends EntityAdvancedPropertyDefinition ? DObject.AssignObjects<GenericPropertyDefinition, {
144
- nullable: true;
145
- }> : {
146
- type: GenericPropertyDefinition;
147
- nullable: true;
148
- };
149
- array<const GenericPropertyDefinition extends EntityPropertyDefinition, const GenericAdvancedArrayPropertyDefinition extends EntityAdvancedArrayPropertyDefinition = never>(definition: GenericPropertyDefinition, params?: GenericAdvancedArrayPropertyDefinition): GenericPropertyDefinition extends EntityAdvancedPropertyDefinition ? DObject.AssignObjects<GenericPropertyDefinition, {
150
- inArray: NeverCoalescing<GenericAdvancedArrayPropertyDefinition, true>;
151
- }> : {
152
- type: GenericPropertyDefinition;
153
- inArray: NeverCoalescing<GenericAdvancedArrayPropertyDefinition, true>;
154
- };
155
- }
156
- /**
157
- * Creates an entity handler from a property definition.
158
- *
159
- * **Supported call styles:**
160
- * - Classic: `createEntity(name, getPropertiesDefinition)` -> returns a handler
161
- *
162
- * Entities model business structures by composing NewTypes. The handler builds entities from typed properties or from raw inputs with validation.
163
- *
164
- * ```ts
165
- * namespace User {
166
- * export const Id = C.createNewType("user-id", DP.number(), C.Positive);
167
- * export type Id = C.GetNewType<typeof Id>;
168
- * export const Name = C.createNewType("user-name", DP.string());
169
- * export type Name = C.GetNewType<typeof Name>;
170
- * export const Role = C.createNewType("UserRole", DP.literal(["admin", "client", "manager"]));
171
- *
172
- * export const Entity = C.createEntity("User", ({ array, nullable }) => ({
173
- * id: Id,
174
- * name: Name,
175
- * roles: array(Role, { min: 1 }),
176
- * nick: nullable(Name),
177
- * }));
178
- * export type Entity = C.GetEntity<typeof Entity>;
179
- *
180
- * const defaultRole = Role.createOrThrow("client");
181
- *
182
- * export function create(params: {
183
- * id: Id;
184
- * name: Name;
185
- * }) {
186
- * return Entity.new({
187
- * ...params,
188
- * nick: null,
189
- * roles: [defaultRole],
190
- * });
191
- * }
192
- * }
193
- *
194
- * const mapped = User.Entity.mapOrThrow({
195
- * id: 2,
196
- * name: "Bob",
197
- * roles: ["client"],
198
- * nick: "Bobby",
199
- * });
200
- *
201
- * const result = true ? mapped : null;
202
- *
203
- * if (User.Entity.is(result)) {
204
- * // result: C.Entity<"User">
205
- * }
206
- *
207
- * const mappedResult = User.Entity.map({
208
- * id: 3,
209
- * name: "Eve",
210
- * roles: ["manager"],
211
- * nick: null,
212
- * });
213
- *
214
- * if (E.isRight(mappedResult)) {
215
- * // mappedResult: E.Right<"createEntity", C.Entity<"User">>
216
- * }
217
- *
218
- * ```
219
- *
220
- * @remarks
221
- * - The definition callback can use helpers like `array`, `nullable`, and `union` to enrich properties.
222
- * - Use `map`/`mapOrThrow` to build from raw inputs (with runtime constraint checks); `new` expects already typed values.
223
- *
224
- * @see https://utils.duplojs.dev/en/v1/api/clean/entity
225
- *
226
- * @namespace C
227
- *
228
- */
229
- export declare function createEntity<GenericName extends string, const GenericPropertiesDefinition extends EntityPropertiesDefinition>(name: GenericName, getPropertiesDefinition: (params: PropertiesDefinitionParams) => GenericPropertiesDefinition & DObject.ForbiddenKey<GenericPropertiesDefinition, "_entityName" | "_flags">): EntityHandler<GenericName, GenericPropertiesDefinition>;
230
- export declare namespace createEntity {
231
- var overrideHandler: import("../common").OverrideHandler<EntityHandler<string, Readonly<Record<string, EntityPropertyDefinition>>>>;
232
- }
233
- export type GetEntity<GenericEntityHandler extends EntityHandler<string, any>> = Extract<ReturnType<GenericEntityHandler["new"]>, any>;
234
- export {};
@@ -1,141 +0,0 @@
1
- import { createCleanKind } from './kind.mjs';
2
- import { newTypeHandlerKind, newTypeKind } from './newType.mjs';
3
- import { asserts } from '../common/asserts.mjs';
4
- import { kindHeritage } from '../common/kind.mjs';
5
- import { pipe } from '../common/pipe.mjs';
6
- import { map } from '../array/map.mjs';
7
- import { toTuple } from '../array/toTuple.mjs';
8
- import { first } from '../array/at/first.mjs';
9
- import { innerPipe } from '../common/innerPipe.mjs';
10
- import { when } from '../pattern/when.mjs';
11
- import { isType } from '../common/isType.mjs';
12
- import { exhaustive } from '../pattern/exhaustive.mjs';
13
- import { when as when$1 } from '../common/when.mjs';
14
- import { array } from '../dataParser/parsers/array/index.mjs';
15
- import { checkerArrayMin } from '../dataParser/parsers/array/checkers/min.mjs';
16
- import { checkerArrayMax } from '../dataParser/parsers/array/checkers/max.mjs';
17
- import { nullable } from '../dataParser/parsers/nullable.mjs';
18
- import { dataParserKind } from '../dataParser/base.mjs';
19
- import { last } from '../array/at/last.mjs';
20
- import { entries } from '../object/entries.mjs';
21
- import { forward } from '../common/forward.mjs';
22
- import { createErrorKind } from '../common/errorKindNamespace.mjs';
23
- import { fromEntries } from '../object/fromEntries.mjs';
24
- import { entry } from '../object/entry.mjs';
25
- import { transform } from '../dataParser/parsers/transform.mjs';
26
- import { constrainedTypeKind } from './constraint/base.mjs';
27
- import { wrapValue } from '../common/wrapValue.mjs';
28
- import { minElements } from '../array/minElements.mjs';
29
- import { union } from '../dataParser/parsers/union.mjs';
30
- import { object } from '../dataParser/parsers/object/index.mjs';
31
- import { createOverride } from '../common/override.mjs';
32
- import { isLeft } from '../either/left/is.mjs';
33
- import { unwrap } from '../common/unwrap.mjs';
34
- import { left } from '../either/left/create.mjs';
35
- import { right } from '../either/right/create.mjs';
36
-
37
- const entityKind = createCleanKind("entity");
38
- const entityHandlerKind = createCleanKind("entity-handler");
39
- class CreateEntityError extends kindHeritage("create-entity-error", createErrorKind("create-entity-error"), Error) {
40
- rawProperties;
41
- dataParserError;
42
- constructor(rawProperties, dataParserError) {
43
- super({}, ["Error when create entity."]);
44
- this.rawProperties = rawProperties;
45
- this.dataParserError = dataParserError;
46
- }
47
- }
48
- /**
49
- * {@include clean/createEntity/index.md}
50
- */
51
- function createEntity(name, getPropertiesDefinition) {
52
- function theNew(properties) {
53
- return entityKind.addTo(properties, name);
54
- }
55
- function simplePropertyDefinitionToDataParser(simplePropertyDefinition) {
56
- const constraintKindValue = pipe(simplePropertyDefinition.constraints, map(({ name }) => entry(name, null)), fromEntries);
57
- return transform(simplePropertyDefinition.dataParser, (value) => constrainedTypeKind.setTo(newTypeKind.setTo(wrapValue(value), simplePropertyDefinition.name), constraintKindValue));
58
- }
59
- function unionPropertyDefinitionToDataParser(unionPropertyDefinition) {
60
- return pipe(unionPropertyDefinition, map(simplePropertyDefinitionToDataParser), (options) => {
61
- asserts(options, minElements(1));
62
- return union(options);
63
- });
64
- }
65
- const params = {
66
- union: (...type) => ({ type }),
67
- array: (definition, params) => (newTypeHandlerKind.has(definition) || definition instanceof Array
68
- ? {
69
- type: definition,
70
- inArray: params ?? true,
71
- }
72
- : {
73
- ...definition,
74
- inArray: params ?? true,
75
- }),
76
- nullable: (definition) => (newTypeHandlerKind.has(definition) || definition instanceof Array
77
- ? {
78
- type: definition,
79
- nullable: true,
80
- }
81
- : {
82
- ...definition,
83
- nullable: true,
84
- }),
85
- };
86
- const propertiesDefinition = getPropertiesDefinition(params);
87
- const mapDataParser = pipe(forward(propertiesDefinition), entries, map(toTuple([
88
- first,
89
- innerPipe(last, when(newTypeHandlerKind.has, simplePropertyDefinitionToDataParser), when(isType("array"), unionPropertyDefinitionToDataParser), when(isType("object"), (definition) => pipe(definition.type, when(newTypeHandlerKind.has, simplePropertyDefinitionToDataParser), when(isType("array"), unionPropertyDefinitionToDataParser), exhaustive, when$1(dataParserKind.has, innerPipe((dataParser) => {
90
- if (definition.inArray) {
91
- return pipe(array(dataParser), (dataParser) => typeof definition.inArray === "object"
92
- && definition.inArray.min !== undefined
93
- ? dataParser.addChecker(checkerArrayMin(definition.inArray.min))
94
- : dataParser, (dataParser) => typeof definition.inArray === "object"
95
- && definition.inArray.max !== undefined
96
- ? dataParser.addChecker(checkerArrayMax(definition.inArray.max))
97
- : dataParser);
98
- }
99
- return dataParser;
100
- }, when$1(() => definition.nullable === true, nullable))))), exhaustive),
101
- ])), fromEntries, object, (dataParser) => transform(dataParser, (value) => entityKind.setTo(value, name)));
102
- function map$1(rawProperties) {
103
- const result = mapDataParser.parse(rawProperties);
104
- if (isLeft(result)) {
105
- return left("createEntityError", unwrap(result));
106
- }
107
- return right("createEntity", unwrap(result));
108
- }
109
- function mapOrThrow(rawProperties) {
110
- const result = mapDataParser.parse(rawProperties);
111
- if (isLeft(result)) {
112
- throw new CreateEntityError(rawProperties, unwrap(result));
113
- }
114
- return unwrap(result);
115
- }
116
- function is(input) {
117
- return entityKind.has(input) && entityKind.getValue(input) === name;
118
- }
119
- function update(entity, newProperties) {
120
- const updatedEntity = {};
121
- for (const key in propertiesDefinition) {
122
- updatedEntity[key] = newProperties[key] !== undefined
123
- ? newProperties[key]
124
- : entity[key];
125
- }
126
- return entityKind.setTo(updatedEntity, name);
127
- }
128
- return pipe({
129
- name,
130
- propertiesDefinition,
131
- mapDataParser,
132
- new: theNew,
133
- map: map$1,
134
- mapOrThrow,
135
- is,
136
- update,
137
- }, entityHandlerKind.setTo, createEntity.overrideHandler.apply);
138
- }
139
- createEntity.overrideHandler = createOverride("@duplojs/utils/clean/entity");
140
-
141
- export { CreateEntityError, createEntity, entityKind };