url-safe-bitpacking 0.3.4 → 0.3.6

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/README.md CHANGED
@@ -29,10 +29,10 @@ From a memory footprint perspective there are two types of attributes in an obje
29
29
  | Predefined Bitwidth | **✓** | **✓** | **✓** | **✓** | **✓** | ✗ | ✗ | ✗ | ✗ | ✗ |
30
30
  | Has State Bit | ✗ | ✗ | ✗ | ✗ | ✗ | **✓** | **✓** | **✓** | **✓** | ✗ |
31
31
  | Min Bitwidth | `4` | `1` | `1` | `0` | `0` | `1` | – | – | – | – |
32
- | Max Bitwidth | `10` | `1` | `10` | `21` | `21` | `10` | – | – | – | – |
32
+ | Max Bitwidth | `10` | `1` | `8` | `10` | `21` | `8` | – | – | – | – |
33
33
  | Min State Bitwidth | – | – | – | – | – | `0` | `1` | `1` | `0` | – |
34
34
  | Max State Bitwidth | – | – | – | – | – | `10` | `1` | `10` | `10` | – |
35
- | Max Available States | `1024` | `2` | `1024` | `2097152` | `2097152` | `1024 x 1024` | `2` | `1024` | `1024` | `0` |
35
+ | Max Available States | `1024` | `2` | `256` | `1024` | `2097152` | `1024 x 256` | `2` | `256` | `1024` | - |
36
36
  | Has Mapping | ✗ | ✗ | **✓** | ✗ | ✗ | **✓** | ✗ | **✓** | ✗ | ✗ |
37
37
  | Has Nested Objects | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | **✓** | **✓** | **✓** | **✓** |
38
38
  | **UPDATING** | **VERSION** | **BOOLEAN** | **ENUM** | **INT** | **FLOAT** | **ENUM_ARRAY** | **OPTIONAL** | **ENUM_OPTIONS** | **ARRAY** | **OBJECT** |
@@ -0,0 +1,6 @@
1
+ import { DataEntry } from '../types';
2
+ /**
3
+ * Method that only copies the state and / or value of a data entry (doesn't touches the descriptor)
4
+ * @param d - `DataEntry` to copy
5
+ */
6
+ export declare const getCopy: (d: DataEntry | null) => DataEntry | null;
@@ -1,3 +1,3 @@
1
- import { DataEntry, EnumOptionsDataEntry, EnumOptionsType } from '../types';
2
- export type EnumOptionsFactory = (descriptor: (DataEntry | null)[], defaultState?: number, name?: string, options?: EnumOptionsType) => EnumOptionsDataEntry;
1
+ import { EnumOptionsDataEntry, ObjectDataEntry } from '../types';
2
+ export type EnumOptionsFactory = (descriptor: (ObjectDataEntry | null)[], defaultState?: number, name?: string) => EnumOptionsDataEntry;
3
3
  export declare const create: EnumOptionsFactory;
package/dist/index.js CHANGED
@@ -561,6 +561,30 @@ var create6 = (value, options, minCount = 0, maxCount = 10, name = "enum array")
561
561
  return { type: "ENUM_ARRAY", minCount: r.min, maxCount: r.max, value, max, name, mapping, stateBits: r.bitwidth };
562
562
  };
563
563
 
564
+ // src/factory/copy.ts
565
+ var getCopy = (d) => {
566
+ if (d === null)
567
+ return null;
568
+ switch (d.type) {
569
+ case "VERSION":
570
+ case "BOOLEAN":
571
+ case "ENUM":
572
+ case "INT":
573
+ case "FLOAT":
574
+ return { ...d };
575
+ case "ENUM_ARRAY":
576
+ return { ...d, value: [...d.value] };
577
+ case "OPTIONAL":
578
+ return { ...d, value: getCopy(d.value) };
579
+ case "ENUM_OPTIONS":
580
+ return { ...d, value: getCopy(d.value) };
581
+ case "ARRAY":
582
+ return { ...d, value: d.value.map(getCopy) };
583
+ case "OBJECT":
584
+ return { ...d, value: d.value.map(getCopy) };
585
+ }
586
+ };
587
+
564
588
  // src/factory/optionalFactory.ts
565
589
  var create7 = (descriptor, defaultState = false, name = "an optional") => {
566
590
  if (descriptor[0] === null && descriptor[1] === null)
@@ -571,7 +595,7 @@ var create7 = (descriptor, defaultState = false, name = "an optional") => {
571
595
  type: "OPTIONAL",
572
596
  state: defaultState,
573
597
  descriptor,
574
- value: JSON.parse(JSON.stringify(defaultState ? descriptor[1] : descriptor[0])),
598
+ value: getCopy(defaultState ? descriptor[1] : descriptor[0]),
575
599
  name,
576
600
  stateBits: 1
577
601
  };
@@ -589,21 +613,18 @@ var getMinimumBitsForInteger = (v) => Math.ceil(Math.log2(v + 1));
589
613
  // src/factory/enumOptionsFactory.ts
590
614
  var maxEnumOptions = 64;
591
615
  var maxEnumOptionsBits = getMinimumBitsForInteger(maxEnumOptions);
592
- var create8 = (descriptor, defaultState = 0, name = "enum options", options) => {
593
- if (descriptor.length < 2)
594
- throw new Error("descriptor must have at least two entries");
616
+ var create8 = (descriptor, defaultState = 0, name = "enum options") => {
617
+ if (descriptor.length < 1)
618
+ throw new Error("descriptor must have at least one entry");
595
619
  if (descriptor.length - 1 < defaultState)
596
620
  throw new Error("defaultState must be less than the length of the descriptor");
597
- const mapping = [];
598
- if (options) {
599
- const { max, mapping: mapping2 } = getEnumMaxAndMappingFromOptions(options);
600
- if (max !== descriptor.length - 1)
601
- throw new Error("max must be equal to the length of the descriptor - 1");
602
- mapping2.push(...mapping2);
603
- } else
604
- mapping.push(...descriptor.map((_, i) => i));
621
+ if (descriptor.some((v) => v !== null && v.type !== "OBJECT"))
622
+ throw new Error("descriptors must be either ObjectDataEntry or null");
623
+ const { max, mapping } = getEnumMaxAndMappingFromOptions(descriptor.map((v, i) => v ? v.name : i));
624
+ if (max !== descriptor.length - 1)
625
+ throw new Error("max must be equal to the length of the descriptor - 1");
605
626
  return {
606
- value: JSON.parse(JSON.stringify(descriptor[defaultState])),
627
+ value: getCopy(descriptor[defaultState]),
607
628
  descriptor,
608
629
  name,
609
630
  mapping,
@@ -617,7 +638,7 @@ var create8 = (descriptor, defaultState = 0, name = "enum options", options) =>
617
638
  var create9 = (descriptor, defaultState = 0, minCount = 0, maxCount = 10, name = "an array") => {
618
639
  const r = validateUnsignedInt(minCount, maxCount, defaultState, name, "ARRAY", IntegerMaxBits);
619
640
  return {
620
- value: [...Array(r.value)].map(() => JSON.parse(JSON.stringify(descriptor))),
641
+ value: Array.from({ length: r.value }, () => getCopy(descriptor)),
621
642
  descriptor,
622
643
  type: "ARRAY",
623
644
  minCount: r.min,
@@ -633,7 +654,7 @@ var create10 = (descriptor, name = "an object") => {
633
654
  return {
634
655
  type: "OBJECT",
635
656
  descriptor,
636
- value: descriptor,
657
+ value: descriptor.map(getCopy),
637
658
  name
638
659
  };
639
660
  };
@@ -929,6 +950,7 @@ var getObjectForDataEntries = (entries) => {
929
950
  });
930
951
  return object;
931
952
  };
953
+ var getStringForDataEntries = (values) => Array.isArray(values) && values.every((v) => typeof v === "string" && v.length === 1) ? values.join("") : values;
932
954
  var getStateData = (entry) => {
933
955
  switch (entry.type) {
934
956
  case "BOOLEAN":
@@ -939,7 +961,7 @@ var getStateData = (entry) => {
939
961
  case "ENUM":
940
962
  return entry.mapping[entry.value];
941
963
  case "ENUM_ARRAY":
942
- return entry.value.map((v) => entry.mapping[v]);
964
+ return getStringForDataEntries(entry.value.map((v) => entry.mapping[v]));
943
965
  case "OPTIONAL":
944
966
  return entry.value === null ? null : getStateData(entry.value);
945
967
  case "ENUM_OPTIONS":
@@ -948,7 +970,7 @@ var getStateData = (entry) => {
948
970
  switch (entry.value.type) {
949
971
  case "OBJECT":
950
972
  return {
951
- ...getStateData(entry.value),
973
+ ...getObjectForDataEntries(entry.value.value),
952
974
  state
953
975
  };
954
976
  default:
@@ -959,7 +981,7 @@ var getStateData = (entry) => {
959
981
  case "OBJECT":
960
982
  return getObjectForDataEntries(entry.value);
961
983
  case "ARRAY":
962
- return entry.value.map((v) => getStateData(v));
984
+ return getStringForDataEntries(entry.value.map(getStateData));
963
985
  }
964
986
  };
965
987
  // src/utils/interpolateData.ts
@@ -1,10 +1,10 @@
1
- import { DataEntry } from './dataEntry';
1
+ import { ObjectDataEntry } from './dataEntry';
2
2
  import { EnumMappingType } from './enumData';
3
3
  export type EnumOptionsData = {
4
4
  type: 'ENUM_OPTIONS';
5
5
  stateBits: number;
6
6
  state: number;
7
- descriptor: (DataEntry | null)[];
7
+ descriptor: (ObjectDataEntry | null)[];
8
8
  mapping: EnumMappingType;
9
- value: DataEntry | null;
9
+ value: ObjectDataEntry | null;
10
10
  };
package/package.json CHANGED
@@ -6,7 +6,7 @@
6
6
  "dist/*"
7
7
  ],
8
8
  "type": "module",
9
- "version": "0.3.4",
9
+ "version": "0.3.6",
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": {