url-safe-bitpacking 0.3.1 → 0.3.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.js CHANGED
@@ -118,12 +118,12 @@ var rawStringifier2 = (value, intData2) => {
118
118
  };
119
119
 
120
120
  // src/parsers/enumParser.ts
121
- var getBitsCount3 = (versionData2) => versionData2.bits;
122
- var rawParser3 = (rawString, versionData2) => rawValueParser(rawString, versionData2.bits);
123
- var rawStringifier3 = (value, versionData2) => {
124
- if (value > versionData2.max)
121
+ var getBitsCount3 = (enumData2) => enumData2.bits;
122
+ var rawParser3 = (rawString, enumData2) => rawValueParser(rawString, enumData2.bits);
123
+ var rawStringifier3 = (value, enumData2) => {
124
+ if (value > enumData2.max)
125
125
  throw new Error("Version exceeds max");
126
- return rawIntStringifier(value, versionData2.bits);
126
+ return rawIntStringifier(value, enumData2.bits);
127
127
  };
128
128
 
129
129
  // src/parsers/versionParser.ts
@@ -691,8 +691,11 @@ class StateNode {
691
691
  }
692
692
  updateUpstream = () => {
693
693
  const newBitstring = this.getBitString();
694
- if (newBitstring !== this.bitstring)
695
- this.bitstring = newBitstring, this.parent && this.parent.updateUpstream();
694
+ if (newBitstring !== this.bitstring) {
695
+ this.bitstring = newBitstring;
696
+ if (this.parent)
697
+ this.parent.updateUpstream();
698
+ }
696
699
  };
697
700
  getStateBits = () => {
698
701
  throw new Error("not implemented for " + this.type);
@@ -752,10 +755,8 @@ class SimpleStateNodes extends StateNode {
752
755
  }
753
756
  getChildren = () => [];
754
757
  getDescription = () => `${this.name}: ${this.value}`;
755
- updateValue = (value) => {
756
- this.value = constrainValue7(this.descriptor, value);
757
- this.updateUpstream();
758
- };
758
+ updateValue = (value) => (this.value = constrainValue7(this.descriptor, value), this.updateUpstream());
759
+ updateDescriptor = (entry) => (this.descriptor = entry, this.updateValue(this.value));
759
760
  toDataEntry = () => ({
760
761
  ...this.descriptor,
761
762
  name: this.name,
@@ -783,121 +784,118 @@ class EnumArrayNode extends SimpleStateNodes {
783
784
  getDescription = () => `${this.name}: [${this.value.map((v) => this.descriptor.mapping[v]).join(", ")}]`;
784
785
  }
785
786
 
786
- class OptionalNode extends StateNode {
787
+ class ComplexStateNodes extends StateNode {
787
788
  state;
788
- stateBits;
789
789
  descriptor;
790
- child = null;
791
790
  constructor(entry, parent) {
792
791
  super(entry, parent);
793
792
  this.state = entry.state;
794
- this.stateBits = entry.stateBits;
795
- this.descriptor = entry["descriptor"];
793
+ this.descriptor = entry;
794
+ }
795
+ }
796
+
797
+ class OptionalNode extends ComplexStateNodes {
798
+ child = null;
799
+ constructor(entry, parent) {
800
+ super(entry, parent);
801
+ this.child = this.initializedChild();
796
802
  this.bitstring = this.getBitString();
797
803
  }
798
804
  getChildren = () => [this.child];
799
805
  getStateBits = () => rawStateStringifier3(this.state);
800
806
  getValueBits = () => this.child ? this.child.bitstring : "";
807
+ initializedChild = () => this.descriptor.descriptor[this.state ? 1 : 0] ? NodeFactory(this.descriptor.descriptor[this.state ? 1 : 0], this) : null;
801
808
  updateState = (newState) => {
802
- this.state = constrainState(newState);
803
- if (this.state === this.state)
809
+ const constrainedNewState = constrainState(newState);
810
+ if (this.state === constrainedNewState)
804
811
  return;
805
- this.child = this.descriptor[this.state ? 1 : 0] ? NodeFactory(this.descriptor[this.state ? 1 : 0], this) : null;
812
+ this.state = constrainedNewState;
813
+ this.child = this.descriptor.descriptor[this.state ? 1 : 0] ? NodeFactory(this.descriptor.descriptor[this.state ? 1 : 0], this) : null;
814
+ this.updateUpstream();
815
+ };
816
+ updateDescriptor = (entry) => {
817
+ this.descriptor = entry;
818
+ this.child = this.initializedChild();
806
819
  this.updateUpstream();
807
820
  };
808
821
  toDataEntry = () => ({
809
- type: "OPTIONAL",
822
+ ...this.descriptor,
810
823
  name: this.name,
811
824
  state: this.state,
812
- stateBits: this.stateBits,
813
- descriptor: this.descriptor,
814
825
  value: this.child ? this.child.toDataEntry() : null
815
826
  });
816
827
  getDescription = () => `${this.name}: ${this.state}`;
817
828
  }
818
829
 
819
- class EnumOptionsNode extends StateNode {
820
- state;
821
- stateBits;
822
- descriptor;
823
- mapping;
830
+ class EnumOptionsNode extends ComplexStateNodes {
824
831
  child = null;
825
832
  constructor(entry, parent) {
826
833
  super(entry, parent);
827
- this.state = entry.state;
828
- this.stateBits = entry.stateBits;
829
- this.descriptor = entry["descriptor"];
830
- this.mapping = entry["mapping"];
831
- this.child = entry["value"] ? NodeFactory(entry["value"], this) : null;
834
+ this.child = this.initializedChild();
832
835
  this.bitstring = this.getBitString();
833
836
  }
834
837
  getChildren = () => [this.child];
835
- getStateBits = () => rawStateStringifier4(this.state, this.stateBits);
838
+ getStateBits = () => rawStateStringifier4(this.state, this.descriptor.stateBits);
836
839
  getValueBits = () => this.child ? this.child.bitstring : "";
840
+ initializedChild = () => this.descriptor.descriptor[this.state] ? NodeFactory(this.descriptor.descriptor[this.state], this) : null;
837
841
  updateState = (newState) => {
838
- const constrainedNewState = constrainState2(this.descriptor.length, newState);
842
+ const constrainedNewState = constrainState2(this.descriptor.descriptor.length, newState);
839
843
  if (constrainedNewState === this.state)
840
844
  return;
841
- const validationResult = validateDataEntry(this.descriptor[constrainedNewState], this.child ? this.child.toDataEntry() : null);
845
+ const validationResult = validateDataEntry(this.descriptor.descriptor[constrainedNewState], this.child ? this.child.toDataEntry() : null);
842
846
  this.child = validationResult ? NodeFactory(validationResult, this) : null;
843
847
  this.state = constrainedNewState;
844
848
  this.updateUpstream();
845
849
  };
850
+ updateDescriptor = (entry) => {
851
+ this.descriptor = entry;
852
+ this.child = this.initializedChild();
853
+ this.updateUpstream();
854
+ };
846
855
  toDataEntry = () => ({
847
- type: "ENUM_OPTIONS",
856
+ ...this.descriptor,
848
857
  name: this.name,
849
858
  state: this.state,
850
- stateBits: this.stateBits,
851
- descriptor: this.descriptor,
852
- mapping: this.mapping,
853
859
  value: this.child ? this.child.toDataEntry() : null
854
860
  });
855
- getDescription = () => `${this.name}: ${this.state} of ${this.mapping.length} options`;
861
+ getDescription = () => `${this.name}: ${this.state} of ${this.descriptor.mapping.length} options`;
856
862
  }
857
863
 
858
- class ArrayNode extends StateNode {
859
- descriptor;
864
+ class ArrayNode extends ComplexStateNodes {
860
865
  children;
861
- minCount;
862
- maxCount;
863
- stateBits;
864
- state;
865
866
  constructor(entry, parent) {
866
867
  super(entry, parent);
867
- this.descriptor = entry["descriptor"];
868
- this.children = entry["value"].map((child) => NodeFactory(child, this));
869
- this.minCount = entry["minCount"];
870
- this.maxCount = entry["maxCount"];
871
- this.stateBits = entry["stateBits"];
872
- this.state = entry["state"];
868
+ this.children = this.initializedChildren();
873
869
  this.bitstring = this.getBitString();
874
870
  }
871
+ initializedChildren = () => this.descriptor.value.map((child) => NodeFactory(child, this));
875
872
  getChildren = () => this.children;
876
- getStateBits = () => rawStateStringifier2(this.state, this.minCount, this.stateBits);
873
+ getStateBits = () => rawStateStringifier2(this.state, this.descriptor.minCount, this.descriptor.stateBits);
877
874
  getValueBits = () => this.children.map((child) => child.bitstring).join("");
875
+ updateDescriptor = (entry) => {
876
+ this.descriptor = entry;
877
+ this.children = this.initializedChildren();
878
+ this.updateUpstream();
879
+ };
878
880
  updateState = (newState) => {
879
- const constrainedNewState = constrainState3(newState, this.minCount, this.maxCount);
881
+ const constrainedNewState = constrainState3(newState, this.descriptor.minCount, this.descriptor.maxCount);
880
882
  if (constrainedNewState === this.state)
881
883
  return;
882
884
  if (constrainedNewState < this.state)
883
885
  this.children = this.children.slice(0, constrainedNewState);
884
886
  else
885
887
  for (let i = this.state;i < constrainedNewState; i++)
886
- this.children.push(NodeFactory(this.descriptor, this));
888
+ this.children.push(NodeFactory(this.descriptor.descriptor, this));
887
889
  this.state = constrainedNewState;
888
890
  this.updateUpstream();
889
891
  };
890
892
  toDataEntry = () => ({
891
- type: "ARRAY",
893
+ ...this.descriptor,
892
894
  name: this.name,
893
895
  value: this.children.map((child) => child.toDataEntry()),
894
- minCount: this.minCount,
895
- maxCount: this.maxCount,
896
- stateBits: this.stateBits,
897
- state: this.state,
898
- descriptor: this.descriptor
896
+ state: this.state
899
897
  });
900
- getDescription = () => `${this.name}: ${this.state} of (${this.minCount}, ${this.maxCount})`;
898
+ getDescription = () => `${this.name}: ${this.state} of (${this.descriptor.minCount}, ${this.descriptor.maxCount})`;
901
899
  }
902
900
 
903
901
  class ObjectNode extends StateNode {
@@ -905,18 +903,23 @@ class ObjectNode extends StateNode {
905
903
  children;
906
904
  constructor(entry, parent) {
907
905
  super(entry, parent);
908
- this.descriptor = entry["descriptor"];
909
- this.children = entry["value"].map((child) => NodeFactory(child, this));
906
+ this.descriptor = entry;
907
+ this.children = this.initializedChild();
910
908
  this.bitstring = this.getBitString();
911
909
  }
910
+ initializedChild = () => this.descriptor.value.map((child) => NodeFactory(child, this));
911
+ updateDescriptor = (entry) => {
912
+ this.descriptor = entry;
913
+ this.children = this.initializedChild();
914
+ this.updateUpstream();
915
+ };
912
916
  getChildren = () => this.children;
913
917
  getStateBits = () => "";
914
918
  getValueBits = () => this.children.map((child) => child.bitstring).join("");
915
919
  toDataEntry = () => ({
916
- type: "OBJECT",
920
+ ...this.descriptor,
917
921
  name: this.name,
918
922
  value: this.children.map((child) => child.toDataEntry()),
919
- descriptor: this.descriptor,
920
923
  stateBits: 0
921
924
  });
922
925
  }
@@ -1,4 +1,4 @@
1
1
  import { EnumData } from '../types/enumData';
2
- export declare const getBitsCount: (versionData: EnumData) => number;
3
- export declare const rawParser: (rawString: string, versionData: EnumData) => number;
4
- export declare const rawStringifier: (value: number, versionData: EnumData) => string;
2
+ export declare const getBitsCount: (enumData: EnumData) => number;
3
+ export declare const rawParser: (rawString: string, enumData: EnumData) => number;
4
+ export declare const rawStringifier: (value: number, enumData: EnumData) => string;
@@ -1,5 +1,5 @@
1
1
  import { DataType } from '../enums';
2
- import { ArrayDataEntry, BooleanDataEntry, DataEntry, EnumArrayDataEntry, EnumDataEntry, EnumOptionsDataEntry, FloatDataEntry, IntDataEntry, ObjectDataEntry, OptionalDataEntry, VersionDataEntry, UpdateWithValidationTypes } from '../types';
2
+ import { ArrayDataEntry, BooleanDataEntry, DataEntry, EnumArrayDataEntry, EnumDataEntry, EnumOptionsDataEntry, FloatDataEntry, IntDataEntry, ObjectDataEntry, OptionalDataEntry, VersionDataEntry, UpdateWithValidationTypes, UpdateWithStateEntries } from '../types';
3
3
  export declare class StateNode {
4
4
  protected parent: StateNode | null;
5
5
  protected root: StateNode;
@@ -35,6 +35,7 @@ declare class SimpleStateNodes<T extends UpdateWithValidationTypes> extends Stat
35
35
  getChildren: () => SpecificTypeNode[];
36
36
  getDescription: () => string;
37
37
  updateValue: (value: T["value"]) => void;
38
+ updateDescriptor: (entry: T) => void;
38
39
  toDataEntry: () => T;
39
40
  }
40
41
  export declare class VersionNode extends SimpleStateNodes<VersionDataEntry> {
@@ -51,44 +52,43 @@ export declare class EnumArrayNode extends SimpleStateNodes<EnumArrayDataEntry>
51
52
  getStateBits: () => string;
52
53
  getDescription: () => string;
53
54
  }
54
- export declare class OptionalNode extends StateNode {
55
- private state;
56
- private stateBits;
57
- private descriptor;
55
+ declare class ComplexStateNodes<T extends UpdateWithStateEntries> extends StateNode {
56
+ state: T['state'];
57
+ descriptor: T;
58
+ constructor(entry: T, parent: SpecificTypeNode | null);
59
+ }
60
+ export declare class OptionalNode extends ComplexStateNodes<OptionalDataEntry> {
58
61
  private child;
59
62
  constructor(entry: OptionalDataEntry, parent: SpecificTypeNode | null);
60
63
  getChildren: () => (SpecificTypeNode | null)[];
61
64
  getStateBits: () => string;
62
65
  getValueBits: () => string;
66
+ private initializedChild;
63
67
  updateState: (newState: OptionalDataEntry["state"]) => void;
68
+ updateDescriptor: (entry: OptionalDataEntry) => void;
64
69
  toDataEntry: () => OptionalDataEntry;
65
70
  getDescription: () => string;
66
71
  }
67
- export declare class EnumOptionsNode extends StateNode {
68
- private state;
69
- private stateBits;
70
- private descriptor;
71
- private mapping;
72
+ export declare class EnumOptionsNode extends ComplexStateNodes<EnumOptionsDataEntry> {
72
73
  private child;
73
74
  constructor(entry: EnumOptionsDataEntry, parent: SpecificTypeNode | null);
74
75
  getChildren: () => (SpecificTypeNode | null)[];
75
76
  getStateBits: () => string;
76
77
  getValueBits: () => string;
78
+ private initializedChild;
77
79
  updateState: (newState: EnumOptionsDataEntry["state"]) => void;
80
+ updateDescriptor: (entry: EnumOptionsDataEntry) => void;
78
81
  toDataEntry: () => EnumOptionsDataEntry;
79
82
  getDescription: () => string;
80
83
  }
81
- export declare class ArrayNode extends StateNode {
82
- private descriptor;
84
+ export declare class ArrayNode extends ComplexStateNodes<ArrayDataEntry> {
83
85
  private children;
84
- private minCount;
85
- private maxCount;
86
- private stateBits;
87
- private state;
88
86
  constructor(entry: ArrayDataEntry, parent: SpecificTypeNode | null);
87
+ private initializedChildren;
89
88
  getChildren: () => SpecificTypeNode[];
90
89
  getStateBits: () => string;
91
90
  getValueBits: () => string;
91
+ updateDescriptor: (entry: ArrayDataEntry) => void;
92
92
  updateState: (newState: ArrayDataEntry["state"]) => void;
93
93
  toDataEntry: () => ArrayDataEntry;
94
94
  getDescription: () => string;
@@ -97,6 +97,8 @@ export declare class ObjectNode extends StateNode {
97
97
  private descriptor;
98
98
  private children;
99
99
  constructor(entry: ObjectDataEntry, parent: SpecificTypeNode | null);
100
+ private initializedChild;
101
+ updateDescriptor: (entry: ObjectDataEntry) => void;
100
102
  getChildren: () => SpecificTypeNode[];
101
103
  getStateBits: () => string;
102
104
  getValueBits: () => string;
package/package.json CHANGED
@@ -6,7 +6,7 @@
6
6
  "dist/*"
7
7
  ],
8
8
  "type": "module",
9
- "version": "0.3.1",
9
+ "version": "0.3.2",
10
10
  "author": "Jonas Ward",
11
11
  "description": "Library for creating web safe base64 objects with custom bith widths and dynamic values.",
12
12
  "scripts": {