@axi-engine/fields 0.3.6 → 0.3.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.
package/dist/index.d.mts CHANGED
@@ -1,5 +1,5 @@
1
1
  import * as _axi_engine_utils from '@axi-engine/utils';
2
- import { Subscribable, ConstructorRegistry, Emitter, Constructor, PathType } from '@axi-engine/utils';
2
+ import { Subscribable, ConstructorRegistry, Emitter, Constructor, PathType, DataStorage } from '@axi-engine/utils';
3
3
 
4
4
  interface Policy<T> {
5
5
  readonly id: string;
@@ -789,7 +789,7 @@ interface StoreCreateFieldOptions {
789
789
  * It acts as a facade, simplifying access to the underlying FieldTree and providing
790
790
  * both type-safe and dynamic methods for manipulating data.
791
791
  */
792
- interface Store {
792
+ interface Store extends DataStorage {
793
793
  /**
794
794
  * Retrieves the raw value of a Field at a specific path.
795
795
  * @template T The expected type of the value.
@@ -955,8 +955,55 @@ declare class DataStore implements Store {
955
955
  remove(path: PathType): void;
956
956
  private isPathToRootFields;
957
957
  private getDestinationFields;
958
+ has(path: PathType): boolean;
959
+ /** implementation of the DataStore from utils */
960
+ get(path: PathType): unknown;
961
+ set(path: PathType, value: unknown): void;
962
+ create(path: PathType, value: unknown): void;
963
+ upset(path: PathType, value: unknown): void;
964
+ delete(path: PathType): void;
958
965
  }
959
966
 
967
+ /**
968
+ * Type guard that checks if a value is an instance of the `Fields` class.
969
+ * It verifies this by checking the static `typeName` property on the instance.
970
+ *
971
+ * @param value The value to check.
972
+ * @returns {boolean} `true` if the value is a `Fields` instance, otherwise `false`.
973
+ */
974
+ declare function isFields(value: unknown): value is Fields;
975
+ /**
976
+ * Type guard that checks if a value is an instance of the `FieldTree` class.
977
+ * It verifies this by checking the static `typeName` property on the instance.
978
+ *
979
+ * @param value The value to check.
980
+ * @returns {boolean} `true` if the value is a `FieldTree` instance, otherwise `false`.
981
+ */
982
+ declare function isFieldTree(value: unknown): value is FieldTree<any>;
983
+ /**
984
+ * Type guard that checks if an unknown value conforms to the `Store` interface.
985
+ *
986
+ * It performs a structural check (duck typing) by verifying the presence of methods
987
+ * that are unique to the `Store` interface and are not part of the simpler `DataSource`
988
+ * or `DataStorage` contracts, such as `createFields` and `createTree`.
989
+ *
990
+ * @param value The `unknown` value to check.
991
+ * @returns {boolean} `true` if the value is a `Store`-like object, `false` otherwise.
992
+ *
993
+ * @example
994
+ * function processData(source: DataSource) {
995
+ * if (isStore(source)) {
996
+ * // Inside this block, TypeScript now knows `source` is a full `Store`.
997
+ * // We can safely call Store-specific methods like `createFields`.
998
+ * source.createFields('new.data.group');
999
+ * } else {
1000
+ * // Fallback logic for simpler data sources that are not a `Store`.
1001
+ * console.warn('Cannot create new groups with a simple data source.');
1002
+ * }
1003
+ * }
1004
+ */
1005
+ declare function isStore(value: unknown): value is Store;
1006
+
960
1007
  /**
961
1008
  * Creates and configures a FieldRegistry with all the core field types.
962
1009
  * @returns {FieldRegistry} A pre-configured FieldRegistry instance.
@@ -994,4 +1041,4 @@ declare function createCoreFieldSystem(config?: CoreFieldSystemConfig): {
994
1041
  serializer: FieldTreeSerializer<CoreFields>;
995
1042
  };
996
1043
 
997
- export { type BooleanField, ClampMaxPolicy, ClampMaxPolicySerializerHandler, ClampMinPolicy, ClampMinPolicySerializerHandler, ClampPolicy, ClampPolicySerializerHandler, CoreBooleanField, type CoreBooleanFieldOptions, CoreField, type CoreFieldSystemConfig, CoreFieldTree, CoreFields, CoreFieldsFactory, CoreNumericField, type CoreNumericFieldOptions, CoreStringField, type CoreStringFieldOptions, CoreTreeNodeFactory, DataStore, type Field, type FieldOptions, FieldRegistry, FieldSerializer, type FieldSnapshot, FieldTree, type FieldTreeFactory, FieldTreeSerializer, type FieldTreeSnapshot, Fields, type FieldsFactory, FieldsSerializer, type FieldsSnapshot, type NumericField, Policies, type Policy, PolicySerializer, type PolicySerializerHandler, type Store, type StoreCreateFieldOptions, type StringField, type TreeNode, clampMaxPolicy, clampMinPolicy, clampPolicy, createCoreFieldRegistry, createCoreFieldSystem, createCorePolicySerializer, createCoreTreeNodeFactory, createCoreTreeSerializer, createTypedMethodsMixin };
1044
+ export { type BooleanField, ClampMaxPolicy, ClampMaxPolicySerializerHandler, ClampMinPolicy, ClampMinPolicySerializerHandler, ClampPolicy, ClampPolicySerializerHandler, CoreBooleanField, type CoreBooleanFieldOptions, CoreField, type CoreFieldSystemConfig, CoreFieldTree, CoreFields, CoreFieldsFactory, CoreNumericField, type CoreNumericFieldOptions, CoreStringField, type CoreStringFieldOptions, CoreTreeNodeFactory, DataStore, type Field, type FieldOptions, FieldRegistry, FieldSerializer, type FieldSnapshot, FieldTree, type FieldTreeFactory, FieldTreeSerializer, type FieldTreeSnapshot, Fields, type FieldsFactory, FieldsSerializer, type FieldsSnapshot, type NumericField, Policies, type Policy, PolicySerializer, type PolicySerializerHandler, type Store, type StoreCreateFieldOptions, type StringField, type TreeNode, clampMaxPolicy, clampMinPolicy, clampPolicy, createCoreFieldRegistry, createCoreFieldSystem, createCorePolicySerializer, createCoreTreeNodeFactory, createCoreTreeSerializer, createTypedMethodsMixin, isFieldTree, isFields, isStore };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import * as _axi_engine_utils from '@axi-engine/utils';
2
- import { Subscribable, ConstructorRegistry, Emitter, Constructor, PathType } from '@axi-engine/utils';
2
+ import { Subscribable, ConstructorRegistry, Emitter, Constructor, PathType, DataStorage } from '@axi-engine/utils';
3
3
 
4
4
  interface Policy<T> {
5
5
  readonly id: string;
@@ -789,7 +789,7 @@ interface StoreCreateFieldOptions {
789
789
  * It acts as a facade, simplifying access to the underlying FieldTree and providing
790
790
  * both type-safe and dynamic methods for manipulating data.
791
791
  */
792
- interface Store {
792
+ interface Store extends DataStorage {
793
793
  /**
794
794
  * Retrieves the raw value of a Field at a specific path.
795
795
  * @template T The expected type of the value.
@@ -955,8 +955,55 @@ declare class DataStore implements Store {
955
955
  remove(path: PathType): void;
956
956
  private isPathToRootFields;
957
957
  private getDestinationFields;
958
+ has(path: PathType): boolean;
959
+ /** implementation of the DataStore from utils */
960
+ get(path: PathType): unknown;
961
+ set(path: PathType, value: unknown): void;
962
+ create(path: PathType, value: unknown): void;
963
+ upset(path: PathType, value: unknown): void;
964
+ delete(path: PathType): void;
958
965
  }
959
966
 
967
+ /**
968
+ * Type guard that checks if a value is an instance of the `Fields` class.
969
+ * It verifies this by checking the static `typeName` property on the instance.
970
+ *
971
+ * @param value The value to check.
972
+ * @returns {boolean} `true` if the value is a `Fields` instance, otherwise `false`.
973
+ */
974
+ declare function isFields(value: unknown): value is Fields;
975
+ /**
976
+ * Type guard that checks if a value is an instance of the `FieldTree` class.
977
+ * It verifies this by checking the static `typeName` property on the instance.
978
+ *
979
+ * @param value The value to check.
980
+ * @returns {boolean} `true` if the value is a `FieldTree` instance, otherwise `false`.
981
+ */
982
+ declare function isFieldTree(value: unknown): value is FieldTree<any>;
983
+ /**
984
+ * Type guard that checks if an unknown value conforms to the `Store` interface.
985
+ *
986
+ * It performs a structural check (duck typing) by verifying the presence of methods
987
+ * that are unique to the `Store` interface and are not part of the simpler `DataSource`
988
+ * or `DataStorage` contracts, such as `createFields` and `createTree`.
989
+ *
990
+ * @param value The `unknown` value to check.
991
+ * @returns {boolean} `true` if the value is a `Store`-like object, `false` otherwise.
992
+ *
993
+ * @example
994
+ * function processData(source: DataSource) {
995
+ * if (isStore(source)) {
996
+ * // Inside this block, TypeScript now knows `source` is a full `Store`.
997
+ * // We can safely call Store-specific methods like `createFields`.
998
+ * source.createFields('new.data.group');
999
+ * } else {
1000
+ * // Fallback logic for simpler data sources that are not a `Store`.
1001
+ * console.warn('Cannot create new groups with a simple data source.');
1002
+ * }
1003
+ * }
1004
+ */
1005
+ declare function isStore(value: unknown): value is Store;
1006
+
960
1007
  /**
961
1008
  * Creates and configures a FieldRegistry with all the core field types.
962
1009
  * @returns {FieldRegistry} A pre-configured FieldRegistry instance.
@@ -994,4 +1041,4 @@ declare function createCoreFieldSystem(config?: CoreFieldSystemConfig): {
994
1041
  serializer: FieldTreeSerializer<CoreFields>;
995
1042
  };
996
1043
 
997
- export { type BooleanField, ClampMaxPolicy, ClampMaxPolicySerializerHandler, ClampMinPolicy, ClampMinPolicySerializerHandler, ClampPolicy, ClampPolicySerializerHandler, CoreBooleanField, type CoreBooleanFieldOptions, CoreField, type CoreFieldSystemConfig, CoreFieldTree, CoreFields, CoreFieldsFactory, CoreNumericField, type CoreNumericFieldOptions, CoreStringField, type CoreStringFieldOptions, CoreTreeNodeFactory, DataStore, type Field, type FieldOptions, FieldRegistry, FieldSerializer, type FieldSnapshot, FieldTree, type FieldTreeFactory, FieldTreeSerializer, type FieldTreeSnapshot, Fields, type FieldsFactory, FieldsSerializer, type FieldsSnapshot, type NumericField, Policies, type Policy, PolicySerializer, type PolicySerializerHandler, type Store, type StoreCreateFieldOptions, type StringField, type TreeNode, clampMaxPolicy, clampMinPolicy, clampPolicy, createCoreFieldRegistry, createCoreFieldSystem, createCorePolicySerializer, createCoreTreeNodeFactory, createCoreTreeSerializer, createTypedMethodsMixin };
1044
+ export { type BooleanField, ClampMaxPolicy, ClampMaxPolicySerializerHandler, ClampMinPolicy, ClampMinPolicySerializerHandler, ClampPolicy, ClampPolicySerializerHandler, CoreBooleanField, type CoreBooleanFieldOptions, CoreField, type CoreFieldSystemConfig, CoreFieldTree, CoreFields, CoreFieldsFactory, CoreNumericField, type CoreNumericFieldOptions, CoreStringField, type CoreStringFieldOptions, CoreTreeNodeFactory, DataStore, type Field, type FieldOptions, FieldRegistry, FieldSerializer, type FieldSnapshot, FieldTree, type FieldTreeFactory, FieldTreeSerializer, type FieldTreeSnapshot, Fields, type FieldsFactory, FieldsSerializer, type FieldsSnapshot, type NumericField, Policies, type Policy, PolicySerializer, type PolicySerializerHandler, type Store, type StoreCreateFieldOptions, type StringField, type TreeNode, clampMaxPolicy, clampMinPolicy, clampPolicy, createCoreFieldRegistry, createCoreFieldSystem, createCorePolicySerializer, createCoreTreeNodeFactory, createCoreTreeSerializer, createTypedMethodsMixin, isFieldTree, isFields, isStore };
package/dist/index.js CHANGED
@@ -51,7 +51,10 @@ __export(index_exports, {
51
51
  createCorePolicySerializer: () => createCorePolicySerializer,
52
52
  createCoreTreeNodeFactory: () => createCoreTreeNodeFactory,
53
53
  createCoreTreeSerializer: () => createCoreTreeSerializer,
54
- createTypedMethodsMixin: () => createTypedMethodsMixin
54
+ createTypedMethodsMixin: () => createTypedMethodsMixin,
55
+ isFieldTree: () => isFieldTree,
56
+ isFields: () => isFields,
57
+ isStore: () => isStore
55
58
  });
56
59
  module.exports = __toCommonJS(index_exports);
57
60
 
@@ -1002,6 +1005,9 @@ var FieldTreeSerializer = class {
1002
1005
  }
1003
1006
  };
1004
1007
 
1008
+ // src/data-store.ts
1009
+ var import_utils10 = require("@axi-engine/utils");
1010
+
1005
1011
  // src/data-store-field-resolver.ts
1006
1012
  var import_utils9 = require("@axi-engine/utils");
1007
1013
  var NumericFieldResolver = class {
@@ -1024,7 +1030,6 @@ var StringFieldResolver = class {
1024
1030
  };
1025
1031
 
1026
1032
  // src/data-store.ts
1027
- var import_utils10 = require("@axi-engine/utils");
1028
1033
  var DataStore = class {
1029
1034
  constructor(tree) {
1030
1035
  this.tree = tree;
@@ -1155,8 +1160,43 @@ var DataStore = class {
1155
1160
  const leafName = pathArr.pop();
1156
1161
  return { fields: this.tree.getOrCreateFields(path), leafName };
1157
1162
  }
1163
+ has(path) {
1164
+ const pathArr = (0, import_utils10.ensurePathArray)(path);
1165
+ if (this.isPathToRootFields(pathArr)) {
1166
+ return this.rootFields.has(pathArr[0]);
1167
+ }
1168
+ return this.tree.hasPath(pathArr);
1169
+ }
1170
+ /** implementation of the DataStore from utils */
1171
+ get(path) {
1172
+ return this.getField(path).value;
1173
+ }
1174
+ set(path, value) {
1175
+ this.setValue(path, value);
1176
+ }
1177
+ create(path, value) {
1178
+ this.createValue(path, value);
1179
+ }
1180
+ upset(path, value) {
1181
+ this.upsetValue(path, value);
1182
+ }
1183
+ delete(path) {
1184
+ this.remove(path);
1185
+ }
1158
1186
  };
1159
1187
 
1188
+ // src/guards.ts
1189
+ var import_utils11 = require("@axi-engine/utils");
1190
+ function isFields(value) {
1191
+ return value != null && value.typeName === Fields.typeName;
1192
+ }
1193
+ function isFieldTree(value) {
1194
+ return value != null && value.typeName === FieldTree.typeName;
1195
+ }
1196
+ function isStore(value) {
1197
+ return !(0, import_utils11.isNullOrUndefined)(value) && typeof value.createFields === "function" && typeof value.createTree === "function";
1198
+ }
1199
+
1160
1200
  // src/setup.ts
1161
1201
  function createCoreFieldRegistry() {
1162
1202
  const fieldRegistry = new FieldRegistry();
@@ -1224,5 +1264,8 @@ function createCoreFieldSystem(config) {
1224
1264
  createCorePolicySerializer,
1225
1265
  createCoreTreeNodeFactory,
1226
1266
  createCoreTreeSerializer,
1227
- createTypedMethodsMixin
1267
+ createTypedMethodsMixin,
1268
+ isFieldTree,
1269
+ isFields,
1270
+ isStore
1228
1271
  });
package/dist/index.mjs CHANGED
@@ -945,6 +945,9 @@ var FieldTreeSerializer = class {
945
945
  }
946
946
  };
947
947
 
948
+ // src/data-store.ts
949
+ import { ensurePathArray as ensurePathArray2, ensurePathString as ensurePathString2, throwIfEmpty as throwIfEmpty4 } from "@axi-engine/utils";
950
+
948
951
  // src/data-store-field-resolver.ts
949
952
  import { isBoolean, isNumber, isString as isString2 } from "@axi-engine/utils";
950
953
  var NumericFieldResolver = class {
@@ -967,7 +970,6 @@ var StringFieldResolver = class {
967
970
  };
968
971
 
969
972
  // src/data-store.ts
970
- import { ensurePathArray as ensurePathArray2, ensurePathString as ensurePathString2, throwIfEmpty as throwIfEmpty4 } from "@axi-engine/utils";
971
973
  var DataStore = class {
972
974
  constructor(tree) {
973
975
  this.tree = tree;
@@ -1098,8 +1100,43 @@ var DataStore = class {
1098
1100
  const leafName = pathArr.pop();
1099
1101
  return { fields: this.tree.getOrCreateFields(path), leafName };
1100
1102
  }
1103
+ has(path) {
1104
+ const pathArr = ensurePathArray2(path);
1105
+ if (this.isPathToRootFields(pathArr)) {
1106
+ return this.rootFields.has(pathArr[0]);
1107
+ }
1108
+ return this.tree.hasPath(pathArr);
1109
+ }
1110
+ /** implementation of the DataStore from utils */
1111
+ get(path) {
1112
+ return this.getField(path).value;
1113
+ }
1114
+ set(path, value) {
1115
+ this.setValue(path, value);
1116
+ }
1117
+ create(path, value) {
1118
+ this.createValue(path, value);
1119
+ }
1120
+ upset(path, value) {
1121
+ this.upsetValue(path, value);
1122
+ }
1123
+ delete(path) {
1124
+ this.remove(path);
1125
+ }
1101
1126
  };
1102
1127
 
1128
+ // src/guards.ts
1129
+ import { isNullOrUndefined as isNullOrUndefined3 } from "@axi-engine/utils";
1130
+ function isFields(value) {
1131
+ return value != null && value.typeName === Fields.typeName;
1132
+ }
1133
+ function isFieldTree(value) {
1134
+ return value != null && value.typeName === FieldTree.typeName;
1135
+ }
1136
+ function isStore(value) {
1137
+ return !isNullOrUndefined3(value) && typeof value.createFields === "function" && typeof value.createTree === "function";
1138
+ }
1139
+
1103
1140
  // src/setup.ts
1104
1141
  function createCoreFieldRegistry() {
1105
1142
  const fieldRegistry = new FieldRegistry();
@@ -1166,5 +1203,8 @@ export {
1166
1203
  createCorePolicySerializer,
1167
1204
  createCoreTreeNodeFactory,
1168
1205
  createCoreTreeSerializer,
1169
- createTypedMethodsMixin
1206
+ createTypedMethodsMixin,
1207
+ isFieldTree,
1208
+ isFields,
1209
+ isStore
1170
1210
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@axi-engine/fields",
3
- "version": "0.3.6",
3
+ "version": "0.3.7",
4
4
  "description": "A compact, reactive state management library based on a tree of observable fields.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -36,9 +36,9 @@
36
36
  "dequal": "^2.0.3"
37
37
  },
38
38
  "devDependencies": {
39
- "@axi-engine/utils": "^0.2.0"
39
+ "@axi-engine/utils": "^0.2.4"
40
40
  },
41
41
  "peerDependencies": {
42
- "@axi-engine/utils": "^0.2.0"
42
+ "@axi-engine/utils": "^0.2.4"
43
43
  }
44
44
  }