url-safe-bitpacking 0.3.5 → 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.
@@ -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 { EnumOptionsDataEntry, EnumOptionsType, ObjectDataEntry } from '../types';
2
- export type EnumOptionsFactory = (descriptor: (ObjectDataEntry | 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
  };
package/package.json CHANGED
@@ -6,7 +6,7 @@
6
6
  "dist/*"
7
7
  ],
8
8
  "type": "module",
9
- "version": "0.3.5",
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": {