@duplojs/utils 1.4.43 → 1.4.44

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.
@@ -116,6 +116,15 @@ function createEntity(name, getPropertiesDefinition) {
116
116
  function is$1(input) {
117
117
  return entityKind.has(input) && entityKind.getValue(input) === name;
118
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
+ }
119
128
  return entityHandlerKind.setTo({
120
129
  name,
121
130
  propertiesDefinition,
@@ -124,6 +133,7 @@ function createEntity(name, getPropertiesDefinition) {
124
133
  map: map$1,
125
134
  mapOrThrow,
126
135
  is: is$1,
136
+ update,
127
137
  });
128
138
  }
129
139
 
@@ -1,4 +1,4 @@
1
- import { type SimplifyTopLevel, type Kind, type Unwrap, type IsEqual, type IsExtends, type Or, type NeverCoalescing } from "../common";
1
+ import { type SimplifyTopLevel, type Kind, type Unwrap, type IsEqual, type IsExtends, type Or, type NeverCoalescing, type RemoveKind } from "../common";
2
2
  import { type GetNewType, type NewTypeHandler } from "./newType";
3
3
  import * as DEither from "../either";
4
4
  import * as DDataParser from "../dataParser";
@@ -112,6 +112,18 @@ export interface EntityHandler<GenericName extends string = string, GenericPrope
112
112
  *
113
113
  */
114
114
  is<GenericInput extends unknown>(input: GenericInput): input is Extract<GenericInput, Entity<GenericName>>;
115
+ /**
116
+ * Updates an entity by merging typed properties into an existing entity.
117
+ *
118
+ * ```ts
119
+ * const updated = User.Entity.update(mapped, {
120
+ * name: User.Name.createOrThrow("Bobby"),
121
+ * nick: null,
122
+ * });
123
+ * ```
124
+ *
125
+ */
126
+ update<const GenericEntity extends Entity<GenericName>, const GenericProperties extends Partial<EntityProperties<GenericPropertiesDefinition>>>(entity: GenericEntity, properties: GenericProperties): Entity<GenericName> & DObject.AssignObjects<RemoveKind<GenericEntity>, GenericProperties>;
115
127
  }
116
128
  declare const CreateEntityError_base: new (params: {
117
129
  "@DuplojsUtilsError/create-entity-error"?: unknown;
@@ -114,6 +114,15 @@ function createEntity(name, getPropertiesDefinition) {
114
114
  function is(input) {
115
115
  return entityKind.has(input) && entityKind.getValue(input) === name;
116
116
  }
117
+ function update(entity, newProperties) {
118
+ const updatedEntity = {};
119
+ for (const key in propertiesDefinition) {
120
+ updatedEntity[key] = newProperties[key] !== undefined
121
+ ? newProperties[key]
122
+ : entity[key];
123
+ }
124
+ return entityKind.setTo(updatedEntity, name);
125
+ }
117
126
  return entityHandlerKind.setTo({
118
127
  name,
119
128
  propertiesDefinition,
@@ -122,6 +131,7 @@ function createEntity(name, getPropertiesDefinition) {
122
131
  map: map$1,
123
132
  mapOrThrow,
124
133
  is,
134
+ update,
125
135
  });
126
136
  }
127
137
 
@@ -1 +1 @@
1
- export type MaybeArray<GenericValue extends unknown> = GenericValue | GenericValue[];
1
+ export type MaybeArray<GenericValue extends unknown> = GenericValue | readonly GenericValue[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@duplojs/utils",
3
- "version": "1.4.43",
3
+ "version": "1.4.44",
4
4
  "author": {
5
5
  "name": "mathcovax",
6
6
  "url": "https://github.com/mathcovax"