@axi-engine/fields 0.1.1 → 0.1.2

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,6 @@
1
1
  import { ReadonlySignal, Signal } from '@preact/signals-core';
2
- import EventEmitter3 from 'eventemitter3';
2
+ import { AxiEventEmitter } from '@axi-engine/events';
3
+ import { PathType } from '@axi-engine/utils';
3
4
 
4
5
  declare const enum FieldsNodeType {
5
6
  fieldTree = "FieldTree",
@@ -131,15 +132,6 @@ declare class NumberField extends Field<number> {
131
132
  dec(amount?: number): void;
132
133
  }
133
134
 
134
- /**
135
- * This class is a wrapper around EventEmitter3, providing a stable,
136
- *
137
- * A high performance event emitter
138
- * @see {@link https://github.com/primus/eventemitter3}
139
- */
140
- declare class AxiEventEmitter<T extends string | symbol> extends EventEmitter3<T> {
141
- }
142
-
143
135
  /**
144
136
  * An abstract base class for managing a reactive collection of `Field` instances.
145
137
  *
@@ -226,15 +218,6 @@ declare class Fields extends BaseFields<any> {
226
218
  declare class TypedFields<T> extends BaseFields<T> {
227
219
  }
228
220
 
229
- /**
230
- * Represents a path that can be provided as a single string
231
- * or an array of segments.
232
- * @example
233
- * 'player/stats/health'
234
- * ['player', 'stats', 'health']
235
- */
236
- type PathType = string | string[];
237
-
238
221
  /** A type alias for any container that can be a child node in a FieldTree */
239
222
  type TreeOrFieldsContainer = FieldTree | Fields | TypedFields<any>;
240
223
  /** Describes the payload for events emitted when a container is created or removed from a FieldTree. */
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { ReadonlySignal, Signal } from '@preact/signals-core';
2
- import EventEmitter3 from 'eventemitter3';
2
+ import { AxiEventEmitter } from '@axi-engine/events';
3
+ import { PathType } from '@axi-engine/utils';
3
4
 
4
5
  declare const enum FieldsNodeType {
5
6
  fieldTree = "FieldTree",
@@ -131,15 +132,6 @@ declare class NumberField extends Field<number> {
131
132
  dec(amount?: number): void;
132
133
  }
133
134
 
134
- /**
135
- * This class is a wrapper around EventEmitter3, providing a stable,
136
- *
137
- * A high performance event emitter
138
- * @see {@link https://github.com/primus/eventemitter3}
139
- */
140
- declare class AxiEventEmitter<T extends string | symbol> extends EventEmitter3<T> {
141
- }
142
-
143
135
  /**
144
136
  * An abstract base class for managing a reactive collection of `Field` instances.
145
137
  *
@@ -226,15 +218,6 @@ declare class Fields extends BaseFields<any> {
226
218
  declare class TypedFields<T> extends BaseFields<T> {
227
219
  }
228
220
 
229
- /**
230
- * Represents a path that can be provided as a single string
231
- * or an array of segments.
232
- * @example
233
- * 'player/stats/health'
234
- * ['player', 'stats', 'health']
235
- */
236
- type PathType = string | string[];
237
-
238
221
  /** A type alias for any container that can be a child node in a FieldTree */
239
222
  type TreeOrFieldsContainer = FieldTree | Fields | TypedFields<any>;
240
223
  /** Describes the payload for events emitted when a container is created or removed from a FieldTree. */
package/dist/index.js CHANGED
@@ -372,39 +372,8 @@ var Field = class {
372
372
  }
373
373
  };
374
374
 
375
- // ../utils/src/guards.ts
376
- function isNullOrUndefined(val) {
377
- return val === void 0 || val === null;
378
- }
379
-
380
- // ../utils/src/assertion.ts
381
- function throwIf(conditionForThrow, exceptionMessage) {
382
- if (conditionForThrow) {
383
- throw new Error(exceptionMessage);
384
- }
385
- }
386
- function throwIfEmpty(value, exceptionMessage) {
387
- const isArrayAndEmpty = Array.isArray(value) && value.length === 0;
388
- if (isNullOrUndefined(value) || isArrayAndEmpty) {
389
- throw new Error(exceptionMessage);
390
- }
391
- }
392
-
393
- // ../utils/src/config.ts
394
- var defaultConfig = {
395
- pathSeparator: "/"
396
- };
397
- var axiSettings = { ...defaultConfig };
398
-
399
- // ../utils/src/path.ts
400
- function ensurePathArray(path, separator = axiSettings.pathSeparator) {
401
- return Array.isArray(path) ? [...path] : path.split(separator);
402
- }
403
- function ensurePathString(path, separator = axiSettings.pathSeparator) {
404
- return !Array.isArray(path) ? path : path.join(separator);
405
- }
406
-
407
375
  // src/number-field.ts
376
+ var import_utils = require("@axi-engine/utils");
408
377
  var NumberField = class extends Field {
409
378
  get min() {
410
379
  const policy = this.getPolicy(ClampPolicy.id) ?? this.getPolicy(ClampMinPolicy.id);
@@ -416,19 +385,19 @@ var NumberField = class extends Field {
416
385
  }
417
386
  get isMin() {
418
387
  const min = this.min;
419
- return isNullOrUndefined(min) ? false : this.val <= min;
388
+ return (0, import_utils.isNullOrUndefined)(min) ? false : this.val <= min;
420
389
  }
421
390
  get isMax() {
422
391
  const max = this.max;
423
- return isNullOrUndefined(max) ? false : this.val >= max;
392
+ return (0, import_utils.isNullOrUndefined)(max) ? false : this.val >= max;
424
393
  }
425
394
  constructor(name, initialVal, options) {
426
395
  const policies = options?.policies ?? [];
427
- if (!isNullOrUndefined(options?.min) && !isNullOrUndefined(options?.max)) {
396
+ if (!(0, import_utils.isNullOrUndefined)(options?.min) && !(0, import_utils.isNullOrUndefined)(options?.max)) {
428
397
  policies.unshift(clampPolicy(options.min, options.max));
429
- } else if (!isNullOrUndefined(options?.min)) {
398
+ } else if (!(0, import_utils.isNullOrUndefined)(options?.min)) {
430
399
  policies.unshift(clampMinPolicy(options.min));
431
- } else if (!isNullOrUndefined(options?.max)) {
400
+ } else if (!(0, import_utils.isNullOrUndefined)(options?.max)) {
432
401
  policies.unshift(clampMaxPolicy(options.max));
433
402
  }
434
403
  super(name, initialVal, { policies });
@@ -455,6 +424,7 @@ var AxiEventEmitter = class extends eventemitter3_default {
455
424
  };
456
425
 
457
426
  // src/base-fields.ts
427
+ var import_utils2 = require("@axi-engine/utils");
458
428
  var BaseFields = class {
459
429
  constructor() {
460
430
  this._fields = (0, import_signals_core2.signal)(/* @__PURE__ */ new Map());
@@ -492,7 +462,7 @@ var BaseFields = class {
492
462
  * @returns The added `Field` instance.
493
463
  */
494
464
  add(field) {
495
- throwIf(this.has(field.name), `Field with name '${field.name}' already exists`);
465
+ (0, import_utils2.throwIf)(this.has(field.name), `Field with name '${field.name}' already exists`);
496
466
  const fieldsMap = new Map(this._fields.value);
497
467
  fieldsMap.set(field.name, field);
498
468
  this._fields.value = fieldsMap;
@@ -509,7 +479,7 @@ var BaseFields = class {
509
479
  * @returns The `Field` instance.
510
480
  */
511
481
  get(name) {
512
- throwIf(!this._fields.value.has(name), `Field with name '${name}' not exists`);
482
+ (0, import_utils2.throwIf)(!this._fields.value.has(name), `Field with name '${name}' not exists`);
513
483
  return this._fields.value.get(name);
514
484
  }
515
485
  /**
@@ -582,6 +552,7 @@ var BaseFields = class {
582
552
  };
583
553
 
584
554
  // src/fields.ts
555
+ var import_utils3 = require("@axi-engine/utils");
585
556
  var Fields = class extends BaseFields {
586
557
  createNumber(name, initialValue, options) {
587
558
  return this.add(new NumberField(name, initialValue, options));
@@ -596,7 +567,7 @@ var Fields = class extends BaseFields {
596
567
  }
597
568
  getNumber(name) {
598
569
  const field = this.get(name);
599
- throwIf(!(field instanceof NumberField), `wrong field type, field ${name} not a instance of NUmberField`);
570
+ (0, import_utils3.throwIf)(!(field instanceof NumberField), `wrong field type, field ${name} not a instance of NUmberField`);
600
571
  return field;
601
572
  }
602
573
  create(name, initialValue) {
@@ -611,7 +582,7 @@ var Fields = class extends BaseFields {
611
582
  return this.create(name, value);
612
583
  }
613
584
  get(name) {
614
- throwIf(!this._fields.value.has(name), `Field with name '${name}' not exists`);
585
+ (0, import_utils3.throwIf)(!this._fields.value.has(name), `Field with name '${name}' not exists`);
615
586
  return this._fields.value.get(name);
616
587
  }
617
588
  };
@@ -622,6 +593,7 @@ var TypedFields = class extends BaseFields {
622
593
 
623
594
  // src/field-tree.ts
624
595
  var import_signals_core3 = require("@preact/signals-core");
596
+ var import_utils4 = require("@axi-engine/utils");
625
597
  var FieldTree = class _FieldTree {
626
598
  constructor() {
627
599
  this.events = new AxiEventEmitter();
@@ -639,7 +611,7 @@ var FieldTree = class _FieldTree {
639
611
  * @returns true if the entire path exists, false otherwise.
640
612
  */
641
613
  hasPath(path) {
642
- const pathParts = ensurePathArray(path);
614
+ const pathParts = (0, import_utils4.ensurePathArray)(path);
643
615
  let currentNode = this;
644
616
  for (let i = 0; i < pathParts.length; i++) {
645
617
  const part = pathParts[i];
@@ -651,9 +623,9 @@ var FieldTree = class _FieldTree {
651
623
  if (i === pathParts.length - 1) {
652
624
  return true;
653
625
  }
654
- throwIf(
626
+ (0, import_utils4.throwIf)(
655
627
  pathParts.length - i > 2,
656
- `Path validation failed, full path: ${ensurePathString(path)}, has extra nodes after Fields placed at: ${ensurePathString(pathParts.slice(0, i + 1))}`
628
+ `Path validation failed, full path: ${(0, import_utils4.ensurePathString)(path)}, has extra nodes after Fields placed at: ${(0, import_utils4.ensurePathString)(pathParts.slice(0, i + 1))}`
657
629
  );
658
630
  return nextNode.has(pathParts[i + 1]);
659
631
  }
@@ -669,7 +641,7 @@ var FieldTree = class _FieldTree {
669
641
  */
670
642
  getFieldTree(name) {
671
643
  const node = this.getNode(name);
672
- throwIf(!(node instanceof _FieldTree), `Node '${name}' should be instance of FieldTree`);
644
+ (0, import_utils4.throwIf)(!(node instanceof _FieldTree), `Node '${name}' should be instance of FieldTree`);
673
645
  return node;
674
646
  }
675
647
  /**
@@ -680,7 +652,7 @@ var FieldTree = class _FieldTree {
680
652
  */
681
653
  getFields(name) {
682
654
  const node = this.getNode(name);
683
- throwIf(!(node instanceof Fields), `Node '${name}' should be instance of Fields`);
655
+ (0, import_utils4.throwIf)(!(node instanceof Fields), `Node '${name}' should be instance of Fields`);
684
656
  return node;
685
657
  }
686
658
  /**
@@ -691,7 +663,7 @@ var FieldTree = class _FieldTree {
691
663
  */
692
664
  getTypedFields(name) {
693
665
  const node = this.getNode(name);
694
- throwIf(!(node instanceof TypedFields), `Node '${name}' should be instance of TypedFields`);
666
+ (0, import_utils4.throwIf)(!(node instanceof TypedFields), `Node '${name}' should be instance of TypedFields`);
695
667
  return node;
696
668
  }
697
669
  /**
@@ -702,7 +674,7 @@ var FieldTree = class _FieldTree {
702
674
  */
703
675
  getNode(name) {
704
676
  const node = this._items.value.get(name);
705
- throwIfEmpty(node, `Can't find node with name '${name}'`);
677
+ (0, import_utils4.throwIfEmpty)(node, `Can't find node with name '${name}'`);
706
678
  return node;
707
679
  }
708
680
  /**
@@ -736,8 +708,8 @@ var FieldTree = class _FieldTree {
736
708
  * @throws If the path is empty, or any intermediate node is not a `FieldTree`.
737
709
  */
738
710
  getFieldsByPath(path) {
739
- const pathParts = ensurePathArray(path);
740
- throwIf(!pathParts.length, "Empty path");
711
+ const pathParts = (0, import_utils4.ensurePathArray)(path);
712
+ (0, import_utils4.throwIf)(!pathParts.length, "Empty path");
741
713
  let container = this;
742
714
  for (let i = 0; i < pathParts.length - 1; i++) {
743
715
  container = container.getFieldTree(pathParts[i]);
@@ -752,9 +724,9 @@ var FieldTree = class _FieldTree {
752
724
  * @returns The newly created `Field` instance.
753
725
  */
754
726
  create(path, initialValue) {
755
- const fullPath = [...ensurePathArray(path)];
727
+ const fullPath = [...(0, import_utils4.ensurePathArray)(path)];
756
728
  const fieldName = fullPath.pop();
757
- throwIf(!fullPath.length, `Wrong path format of one field creating: '${ensurePathString(path)}', should be at least two sections`);
729
+ (0, import_utils4.throwIf)(!fullPath.length, `Wrong path format of one field creating: '${(0, import_utils4.ensurePathString)(path)}', should be at least two sections`);
758
730
  return this.getFieldsByPath(fullPath).create(fieldName, initialValue);
759
731
  }
760
732
  /**
@@ -764,7 +736,7 @@ var FieldTree = class _FieldTree {
764
736
  * @returns The newly created `NumberField` instance.
765
737
  */
766
738
  createNumber(path, initialValue) {
767
- const fullPath = [...ensurePathArray(path)];
739
+ const fullPath = [...(0, import_utils4.ensurePathArray)(path)];
768
740
  const fieldName = fullPath.pop();
769
741
  return this.getFieldsByPath(fullPath).createNumber(fieldName, initialValue);
770
742
  }
@@ -774,7 +746,7 @@ var FieldTree = class _FieldTree {
774
746
  * @returns The `Field` instance at the specified path.
775
747
  */
776
748
  get(path) {
777
- const fullPath = [...ensurePathArray(path)];
749
+ const fullPath = [...(0, import_utils4.ensurePathArray)(path)];
778
750
  const fieldName = fullPath.pop();
779
751
  return this.getFieldsByPath(fullPath).get(fieldName);
780
752
  }
@@ -784,7 +756,7 @@ var FieldTree = class _FieldTree {
784
756
  * @returns The `NumberField` instance at the specified path.
785
757
  */
786
758
  getNumber(path) {
787
- const fullPath = [...ensurePathArray(path)];
759
+ const fullPath = [...(0, import_utils4.ensurePathArray)(path)];
788
760
  const fieldName = fullPath.pop();
789
761
  return this.getFieldsByPath(fullPath).getNumber(fieldName);
790
762
  }
@@ -833,7 +805,7 @@ var FieldTree = class _FieldTree {
833
805
  */
834
806
  createNode(name, ctor) {
835
807
  const currentItems = this._items.value;
836
- throwIf(currentItems.has(name), `Can't create node with name: '${name}', node already exists`);
808
+ (0, import_utils4.throwIf)(currentItems.has(name), `Can't create node with name: '${name}', node already exists`);
837
809
  const res = new ctor();
838
810
  const newItems = new Map(currentItems);
839
811
  newItems.set(name, res);
package/dist/index.mjs CHANGED
@@ -347,39 +347,8 @@ var Field = class {
347
347
  }
348
348
  };
349
349
 
350
- // ../utils/src/guards.ts
351
- function isNullOrUndefined(val) {
352
- return val === void 0 || val === null;
353
- }
354
-
355
- // ../utils/src/assertion.ts
356
- function throwIf(conditionForThrow, exceptionMessage) {
357
- if (conditionForThrow) {
358
- throw new Error(exceptionMessage);
359
- }
360
- }
361
- function throwIfEmpty(value, exceptionMessage) {
362
- const isArrayAndEmpty = Array.isArray(value) && value.length === 0;
363
- if (isNullOrUndefined(value) || isArrayAndEmpty) {
364
- throw new Error(exceptionMessage);
365
- }
366
- }
367
-
368
- // ../utils/src/config.ts
369
- var defaultConfig = {
370
- pathSeparator: "/"
371
- };
372
- var axiSettings = { ...defaultConfig };
373
-
374
- // ../utils/src/path.ts
375
- function ensurePathArray(path, separator = axiSettings.pathSeparator) {
376
- return Array.isArray(path) ? [...path] : path.split(separator);
377
- }
378
- function ensurePathString(path, separator = axiSettings.pathSeparator) {
379
- return !Array.isArray(path) ? path : path.join(separator);
380
- }
381
-
382
350
  // src/number-field.ts
351
+ import { isNullOrUndefined } from "@axi-engine/utils";
383
352
  var NumberField = class extends Field {
384
353
  get min() {
385
354
  const policy = this.getPolicy(ClampPolicy.id) ?? this.getPolicy(ClampMinPolicy.id);
@@ -430,6 +399,7 @@ var AxiEventEmitter = class extends eventemitter3_default {
430
399
  };
431
400
 
432
401
  // src/base-fields.ts
402
+ import { throwIf } from "@axi-engine/utils";
433
403
  var BaseFields = class {
434
404
  constructor() {
435
405
  this._fields = signal2(/* @__PURE__ */ new Map());
@@ -557,6 +527,7 @@ var BaseFields = class {
557
527
  };
558
528
 
559
529
  // src/fields.ts
530
+ import { throwIf as throwIf2 } from "@axi-engine/utils";
560
531
  var Fields = class extends BaseFields {
561
532
  createNumber(name, initialValue, options) {
562
533
  return this.add(new NumberField(name, initialValue, options));
@@ -571,7 +542,7 @@ var Fields = class extends BaseFields {
571
542
  }
572
543
  getNumber(name) {
573
544
  const field = this.get(name);
574
- throwIf(!(field instanceof NumberField), `wrong field type, field ${name} not a instance of NUmberField`);
545
+ throwIf2(!(field instanceof NumberField), `wrong field type, field ${name} not a instance of NUmberField`);
575
546
  return field;
576
547
  }
577
548
  create(name, initialValue) {
@@ -586,7 +557,7 @@ var Fields = class extends BaseFields {
586
557
  return this.create(name, value);
587
558
  }
588
559
  get(name) {
589
- throwIf(!this._fields.value.has(name), `Field with name '${name}' not exists`);
560
+ throwIf2(!this._fields.value.has(name), `Field with name '${name}' not exists`);
590
561
  return this._fields.value.get(name);
591
562
  }
592
563
  };
@@ -597,6 +568,7 @@ var TypedFields = class extends BaseFields {
597
568
 
598
569
  // src/field-tree.ts
599
570
  import { signal as signal3 } from "@preact/signals-core";
571
+ import { ensurePathArray, ensurePathString, throwIf as throwIf3, throwIfEmpty } from "@axi-engine/utils";
600
572
  var FieldTree = class _FieldTree {
601
573
  constructor() {
602
574
  this.events = new AxiEventEmitter();
@@ -626,7 +598,7 @@ var FieldTree = class _FieldTree {
626
598
  if (i === pathParts.length - 1) {
627
599
  return true;
628
600
  }
629
- throwIf(
601
+ throwIf3(
630
602
  pathParts.length - i > 2,
631
603
  `Path validation failed, full path: ${ensurePathString(path)}, has extra nodes after Fields placed at: ${ensurePathString(pathParts.slice(0, i + 1))}`
632
604
  );
@@ -644,7 +616,7 @@ var FieldTree = class _FieldTree {
644
616
  */
645
617
  getFieldTree(name) {
646
618
  const node = this.getNode(name);
647
- throwIf(!(node instanceof _FieldTree), `Node '${name}' should be instance of FieldTree`);
619
+ throwIf3(!(node instanceof _FieldTree), `Node '${name}' should be instance of FieldTree`);
648
620
  return node;
649
621
  }
650
622
  /**
@@ -655,7 +627,7 @@ var FieldTree = class _FieldTree {
655
627
  */
656
628
  getFields(name) {
657
629
  const node = this.getNode(name);
658
- throwIf(!(node instanceof Fields), `Node '${name}' should be instance of Fields`);
630
+ throwIf3(!(node instanceof Fields), `Node '${name}' should be instance of Fields`);
659
631
  return node;
660
632
  }
661
633
  /**
@@ -666,7 +638,7 @@ var FieldTree = class _FieldTree {
666
638
  */
667
639
  getTypedFields(name) {
668
640
  const node = this.getNode(name);
669
- throwIf(!(node instanceof TypedFields), `Node '${name}' should be instance of TypedFields`);
641
+ throwIf3(!(node instanceof TypedFields), `Node '${name}' should be instance of TypedFields`);
670
642
  return node;
671
643
  }
672
644
  /**
@@ -712,7 +684,7 @@ var FieldTree = class _FieldTree {
712
684
  */
713
685
  getFieldsByPath(path) {
714
686
  const pathParts = ensurePathArray(path);
715
- throwIf(!pathParts.length, "Empty path");
687
+ throwIf3(!pathParts.length, "Empty path");
716
688
  let container = this;
717
689
  for (let i = 0; i < pathParts.length - 1; i++) {
718
690
  container = container.getFieldTree(pathParts[i]);
@@ -729,7 +701,7 @@ var FieldTree = class _FieldTree {
729
701
  create(path, initialValue) {
730
702
  const fullPath = [...ensurePathArray(path)];
731
703
  const fieldName = fullPath.pop();
732
- throwIf(!fullPath.length, `Wrong path format of one field creating: '${ensurePathString(path)}', should be at least two sections`);
704
+ throwIf3(!fullPath.length, `Wrong path format of one field creating: '${ensurePathString(path)}', should be at least two sections`);
733
705
  return this.getFieldsByPath(fullPath).create(fieldName, initialValue);
734
706
  }
735
707
  /**
@@ -808,7 +780,7 @@ var FieldTree = class _FieldTree {
808
780
  */
809
781
  createNode(name, ctor) {
810
782
  const currentItems = this._items.value;
811
- throwIf(currentItems.has(name), `Can't create node with name: '${name}', node already exists`);
783
+ throwIf3(currentItems.has(name), `Can't create node with name: '${name}', node already exists`);
812
784
  const res = new ctor();
813
785
  const newItems = new Map(currentItems);
814
786
  newItems.set(name, res);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@axi-engine/fields",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "",
5
5
  "license": "MIT",
6
6
  "keywords": [
@@ -20,7 +20,7 @@
20
20
  }
21
21
  },
22
22
  "scripts": {
23
- "build": "tsup src/index.ts --format cjs,esm --dts --clean",
23
+ "build": "tsup",
24
24
  "docs": "typedoc src/index.ts --out docs/api --options ../../typedoc.json",
25
25
  "test": "echo 'No tests yet for @axi-engine/fields'"
26
26
  },
@@ -28,10 +28,10 @@
28
28
  "dist"
29
29
  ],
30
30
  "dependencies": {
31
- "@preact/signals-core": "^1.12.1"
31
+ "@axi-engine/utils": "*",
32
+ "@axi-engine/events": "*"
32
33
  },
33
- "devDependencies": {
34
- "@axi-engine/events": "*",
35
- "@axi-engine/utils": "*"
34
+ "peerDependencies": {
35
+ "@preact/signals-core": "^1.12.1"
36
36
  }
37
37
  }