url-safe-bitpacking 0.2.1 → 0.3.1
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 +20 -60
- package/dist/enums/dataTypes.d.ts +16 -4
- package/dist/factory/arrayFactory.d.ts +3 -2
- package/dist/factory/booleanFactory.d.ts +2 -1
- package/dist/factory/enumArrayFactory.d.ts +3 -2
- package/dist/factory/enumFactory.d.ts +2 -1
- package/dist/factory/enumOptionsFactory.d.ts +3 -2
- package/dist/factory/factory.d.ts +14 -10
- package/dist/factory/floatFactory.d.ts +2 -2
- package/dist/factory/intFactory.d.ts +2 -1
- package/dist/factory/objectFactory.d.ts +3 -0
- package/dist/factory/optionalFactory.d.ts +3 -2
- package/dist/factory/utils.d.ts +1 -1
- package/dist/factory/versionFactory.d.ts +2 -1
- package/dist/index.d.ts +5 -5
- package/dist/index.js +767 -532
- package/dist/parsers/arrayParser.d.ts +4 -3
- package/dist/parsers/enumArrayParser.d.ts +3 -1
- package/dist/parsers/enumOptionsParser.d.ts +5 -0
- package/dist/parsers/index.d.ts +1 -0
- package/dist/parsers/intParser.d.ts +2 -4
- package/dist/parsers/objectParser.d.ts +4 -0
- package/dist/parsers/optionalParser.d.ts +4 -3
- package/dist/parsers/parserNestedDataUtils.d.ts +14 -0
- package/dist/parsers/parserUtils.d.ts +56 -0
- package/dist/parsers/parsers.d.ts +14 -80
- package/dist/stateHandling/index.d.ts +2 -1
- package/dist/stateHandling/stateData.d.ts +2 -3
- package/dist/stateHandling/stateNode.d.ts +123 -0
- package/dist/typeFactory/dataEntryTyping.d.ts +2 -2
- package/dist/types/arrayData.d.ts +3 -3
- package/dist/types/dataEntry.d.ts +45 -15
- package/dist/types/enumArrayData.d.ts +2 -0
- package/dist/types/enumData.d.ts +1 -1
- package/dist/types/enumOptionsData.d.ts +5 -3
- package/dist/types/index.d.ts +3 -1
- package/dist/types/objectData.d.ts +8 -0
- package/dist/types/optionalData.d.ts +4 -3
- package/dist/types/stateData.d.ts +4 -0
- package/dist/types/stateDataEntry.d.ts +10 -6
- package/dist/types/updateType.d.ts +16 -0
- package/dist/update/arrayUpdate.d.ts +4 -2
- package/dist/update/booleanUpdate.d.ts +3 -2
- package/dist/update/enumArrayUpdate.d.ts +3 -2
- package/dist/update/enumOptionsUpdate.d.ts +3 -2
- package/dist/update/enumUpdate.d.ts +2 -1
- package/dist/update/floatUpdate.d.ts +2 -1
- package/dist/update/index.d.ts +1 -0
- package/dist/update/intUpdate.d.ts +2 -1
- package/dist/update/objectUpdate.d.ts +2 -0
- package/dist/update/optionalUpdate.d.ts +3 -2
- package/dist/update/updateUtils.d.ts +27 -0
- package/dist/update/updateValues.d.ts +5 -4
- package/dist/update/validateUtils.d.ts +2 -0
- package/dist/update/versionUpdate.d.ts +3 -2
- package/dist/utils/interpolateData.d.ts +4 -1
- package/dist/utils/relativeValue.d.ts +4 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Package for creating definitions of parametric models that can be stored as compactly as possible in a URL by storing it in a web-safe base-64 string. This pacakge is till very much WIP. Feel free to suggest by making an issue or pull-request [GitHub](https://github.com/JonasWard/url-safe-bitpacking).
|
|
4
4
|
|
|
5
|
-
### goal for
|
|
5
|
+
### goal for 1.0
|
|
6
6
|
|
|
7
7
|
| what | code | tests | docs |
|
|
8
8
|
| ---------------------------------------------------- | -------- | -------- | -------- |
|
|
@@ -19,65 +19,25 @@ Package for creating definitions of parametric models that can be stored as comp
|
|
|
19
19
|
|
|
20
20
|
The goal of this library is to offer a flexible, minimal and, variable object definition that can be stored in the browser URL. The main imagined use-case is parametric models that have nested and variable sub-object definitions. The library heavily relies on the bitpacking of custom bitwidth numeric values. Because of that, the biggest trade-off for this library is legibility. Without the related object definition, it would be impossible to reconstruct the state. The big advantage though is the ability to store rather many variables in a very condensed URL, allowing to store all information in rather short urls which then can be used for qr code generation.
|
|
21
21
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
### int
|
|
43
|
-
|
|
44
|
-
An int type is rather similar to the enum, except that it starts at a specific minimum value. The range that then needs to be able to be stored is: max - min. In case you would need values from -30 to 10, you would have to be able to store 10 - 30 + 1 = 51 states. This would require a bitwidth of 6 (log2(51) ~ 5.67). The max bitwidth is now hardcoded to be 12 (which would be the range [minimum, 4095 - minimum])
|
|
45
|
-
|
|
46
|
-
```typescript
|
|
47
|
-
DescriptorFactory.INT(5, 3, 20, 'circleDivisions');
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
### float
|
|
51
|
-
|
|
52
|
-
Floating points work very much like the integer type, with the main difference that one can also define a precision to define at what order of magnitude (from -3 to +3) the variable should be considered. The significand can be up to 20 bits, which eg. at precision -3 would allow a range of .001 to 1048.576.
|
|
53
|
-
|
|
54
|
-
```typescript
|
|
55
|
-
DescriptorFactory.FLOAT(20, 10, 200, -1, 'shapePreProcessingWarptotal');
|
|
56
|
-
```
|
|
57
|
-
|
|
58
|
-
### enum array
|
|
59
|
-
|
|
60
|
-
Enum arrays are a special type of arrays where integer values are intepreted as being values of a **specific base** to then be transformed to base 2. The base is derived from the delta of the max `and` the `min` value of the enums. Besides that, there is also a `minCount` and `maxCount` value (which can be the same value, but `minCount` is at least 1). This only offers a compression rate of upto 22% vis-a-vis an array of `IntDataEntry` (worst case its 0% percent, it never takes up more space), so sometimes questionable whether it makese sense to use ^^.
|
|
61
|
-
|
|
62
|
-
```typescript
|
|
63
|
-
DescriptorFactory.ENUM_ARRAY([0, 1, 2], 0, 10, 3, 5, 'enumArrayA')
|
|
64
|
-
```
|
|
65
|
-
|
|
66
|
-
### version
|
|
67
|
-
|
|
68
|
-
There is also a Version object which is a special case of the enum data type and has a bitwidth of 4, 6, 8 or, 10 and always occupies the first bits of the bitarray.
|
|
69
|
-
|
|
70
|
-
```typescript
|
|
71
|
-
DescriptorFactory.VERSION(0, 8, 'version');
|
|
72
|
-
```
|
|
73
|
-
|
|
74
|
-
## nested attribute definitions
|
|
75
|
-
|
|
76
|
-
More often than not in parametric models, certain data belongs together. It can also happen that the specific state of some values will have an impact on other ones being present (or which ranges are allowed). To be able to deal with this there is the option to nest data. I tried to come with an as concise yet versatile way of defining the object definitions.
|
|
77
|
-
|
|
78
|
-
### single nes
|
|
79
|
-
|
|
80
|
-
For the Double Nested arrays there are currently two variations: either an Optional type, which only accepts two values of which is an empty array and will be toggle on/off in relation to a certain boolean data entry.
|
|
22
|
+
# Types defined in `url-safe-bitpacking`
|
|
23
|
+
`url-safe-bitpacking` stores the state of an object according to its definition (`descriptor`) into memory (for now using a `bitstring`, a string consisting of `0` and `1`s, parsed into a `base64` string). Attributes of the object are packed according to their specific `bitwidths`, the amount of bits they take up in memory.
|
|
24
|
+
|
|
25
|
+
From a memory footprint perspective there are two types of attributes in an object defintion. Those that have a definition that gives them a `fixed` bitwidth and those that have a `variable` bitwidth. For the **variable** ones the state of its value will have an impact on the memory footprint of the object. For the **fixed** types, their definition defines their memory footprint. Some of the objects with a **variable** footprint have some state to them, which informs how the state should be parsed. The size of this state is defined by the **descriptor** of the object.
|
|
26
|
+
|
|
27
|
+
| | VERSION | BOOLEAN | ENUM | INT | FLOAT | ENUM_ARRAY | OPTIONAL | ENUM_OPTIONS | ARRAY | OBJECT |
|
|
28
|
+
| ------------------------ | ----------- | ----------- | ----------- | ----------- | ----------- | -------------- | ------------ | ---------------- | ----------- | ----------- |
|
|
29
|
+
| Predefined Bitwidth | **✓** | **✓** | **✓** | **✓** | **✓** | ✗ | ✗ | ✗ | ✗ | ✗ |
|
|
30
|
+
| Has State Bit | ✗ | ✗ | ✗ | ✗ | ✗ | **✓** | **✓** | **✓** | **✓** | ✗ |
|
|
31
|
+
| Min Bitwidth | `4` | `1` | `1` | `0` | `0` | `1` | – | – | – | – |
|
|
32
|
+
| Max Bitwidth | `10` | `1` | `10` | `21` | `21` | `10` | – | – | – | – |
|
|
33
|
+
| Min State Bitwidth | – | – | – | – | – | `0` | `1` | `1` | `0` | – |
|
|
34
|
+
| Max State Bitwidth | – | – | – | – | – | `10` | `1` | `10` | `10` | – |
|
|
35
|
+
| Max Available States | `1024` | `2` | `1024` | `2097152` | `2097152` | `1024 x 1024` | `2` | `1024` | `1024` | `0` |
|
|
36
|
+
| Has Mapping | ✗ | ✗ | **✓** | ✗ | ✗ | **✓** | ✗ | **✓** | ✗ | ✗ |
|
|
37
|
+
| Has Nested Objects | ✗ | ✗ | ✗ | ✗ | ✗ | ✗ | **✓** | **✓** | **✓** | **✓** |
|
|
38
|
+
| **UPDATING** | **VERSION** | **BOOLEAN** | **ENUM** | **INT** | **FLOAT** | **ENUM_ARRAY** | **OPTIONAL** | **ENUM_OPTIONS** | **ARRAY** | **OBJECT** |
|
|
39
|
+
| Requires Value on Update | **✓** | **✓** | **✓** | **✓** | **✓** | **✓** | **✗** | **✗** | **✗** | **✗** |
|
|
40
|
+
| Requires State on Update | – | – | – | – | – | **✗** | **✓** | **✓** | **✓** | – |
|
|
81
41
|
|
|
82
42
|
# Install
|
|
83
43
|
|
|
@@ -1,4 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
export declare const
|
|
3
|
-
export
|
|
4
|
-
export
|
|
1
|
+
import { DataEntry } from '../types';
|
|
2
|
+
export declare const ConstantBitWidthDataTypes: readonly ["VERSION", "BOOLEAN", "ENUM", "INT", "FLOAT"];
|
|
3
|
+
export declare const VariableBitWidthDataTypes: readonly ["ENUM_ARRAY", "OPTIONAL", "ENUM_OPTIONS", "ARRAY", "OBJECT"];
|
|
4
|
+
export declare const HasMappingDataTypes: readonly ["ENUM", "ENUM_ARRAY", "ENUM_OPTIONS"];
|
|
5
|
+
export declare const HasStateBitsDataTypes: readonly ["ENUM_ARRAY", "OPTIONAL", "ENUM_OPTIONS", "ARRAY"];
|
|
6
|
+
export declare const HasChildDataTypes: readonly ["OPTIONAL", "ENUM_OPTIONS"];
|
|
7
|
+
export declare const HasChildrenDataTypes: readonly ["ARRAY", "OBJECT"];
|
|
8
|
+
export declare const HasNestedDataTypes: readonly ["OPTIONAL", "ENUM_OPTIONS", "ARRAY", "OBJECT"];
|
|
9
|
+
export declare const ValueUpdateDataTypes: readonly ["VERSION", "BOOLEAN", "ENUM", "INT", "FLOAT", "ENUM_ARRAY"];
|
|
10
|
+
export declare const StateUpdateDataTypes: readonly ["OPTIONAL", "ENUM_OPTIONS", "ARRAY"];
|
|
11
|
+
export type ValueUpdateType = (typeof ValueUpdateDataTypes)[number];
|
|
12
|
+
export type StateUpdateType = (typeof StateUpdateDataTypes)[number];
|
|
13
|
+
export type DataType = (typeof ConstantBitWidthDataTypes)[number] | (typeof VariableBitWidthDataTypes)[number];
|
|
14
|
+
export type SpecifiedDataEntry<T extends DataType> = DataEntry & {
|
|
15
|
+
type: T;
|
|
16
|
+
};
|
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
import { ArrayDataEntry
|
|
2
|
-
export
|
|
1
|
+
import { ArrayDataEntry } from '../types';
|
|
2
|
+
export type ArrayFactory = (descriptor: ArrayDataEntry['descriptor'], defaultState?: number, minCount?: number, maxCount?: number, name?: string) => ArrayDataEntry;
|
|
3
|
+
export declare const create: ArrayFactory;
|
|
@@ -1,2 +1,3 @@
|
|
|
1
1
|
import { BooleanDataEntry } from '../types';
|
|
2
|
-
export
|
|
2
|
+
export type BooleanFactory = (value: boolean, name?: string) => BooleanDataEntry;
|
|
3
|
+
export declare const create: BooleanFactory;
|
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
import { EnumArrayDataEntry, EnumOptionsType } from '
|
|
2
|
-
export
|
|
1
|
+
import { EnumArrayDataEntry, EnumOptionsType } from '../types';
|
|
2
|
+
export type EnumArrayFactory = (value: number[], options: EnumOptionsType, minCount?: number, maxCount?: number, name?: string) => EnumArrayDataEntry;
|
|
3
|
+
export declare const create: EnumArrayFactory;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
import { EnumDataEntry } from '../types';
|
|
2
2
|
import { EnumOptionsType } from '../types/enumData';
|
|
3
|
-
export
|
|
3
|
+
export type EnumFactory = (value: number, options: EnumOptionsType, name?: string) => EnumDataEntry;
|
|
4
|
+
export declare const create: EnumFactory;
|
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
import { EnumOptionsDataEntry,
|
|
2
|
-
export
|
|
1
|
+
import { DataEntry, EnumOptionsDataEntry, EnumOptionsType } from '../types';
|
|
2
|
+
export type EnumOptionsFactory = (descriptor: (DataEntry | null)[], defaultState?: number, name?: string, options?: EnumOptionsType) => EnumOptionsDataEntry;
|
|
3
|
+
export declare const create: EnumOptionsFactory;
|
|
@@ -7,17 +7,21 @@ import { create as createEnumArray } from './enumArrayFactory';
|
|
|
7
7
|
import { create as createOptional } from './optionalFactory';
|
|
8
8
|
import { create as createEnumOptions } from './enumOptionsFactory';
|
|
9
9
|
import { create as createArray } from './arrayFactory';
|
|
10
|
+
import { create as createObject } from './objectFactory';
|
|
11
|
+
import { DataEntry, ObjectDataEntry, VersionDataEntry } from '../types';
|
|
10
12
|
/**
|
|
11
13
|
* Record containing all the factory methods for the different data entry objects
|
|
12
14
|
*/
|
|
13
|
-
export declare const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
15
|
+
export declare const DataEntryFactory: {
|
|
16
|
+
FLOAT: typeof createFloat;
|
|
17
|
+
INT: typeof createInt;
|
|
18
|
+
ENUM: typeof createEnum;
|
|
19
|
+
BOOLEAN: typeof createBoolean;
|
|
20
|
+
VERSION: typeof createVersion;
|
|
21
|
+
ENUM_ARRAY: typeof createEnumArray;
|
|
22
|
+
OPTIONAL: typeof createOptional;
|
|
23
|
+
ENUM_OPTIONS: typeof createEnumOptions;
|
|
24
|
+
ARRAY: typeof createArray;
|
|
25
|
+
OBJECT: typeof createObject;
|
|
23
26
|
};
|
|
27
|
+
export declare const StateDescriptorFactoryMethod: (descriptor: [VersionDataEntry, ...DataEntry[]], name?: string) => ObjectDataEntry;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { FloatDataEntry } from '../types';
|
|
2
2
|
import { PrecisionRangeType } from '../types/floatData';
|
|
3
|
+
export type FloatFactory = (value: number, min?: number, max?: number, precision?: PrecisionRangeType, name?: string) => FloatDataEntry;
|
|
3
4
|
/**
|
|
4
5
|
* Method to create a float data entry
|
|
5
6
|
* @param value - `number` default value, should be between `min` and `max`
|
|
@@ -7,6 +8,5 @@ import { PrecisionRangeType } from '../types/floatData';
|
|
|
7
8
|
* @param max - `number` (default: 1), should be larger than `min`
|
|
8
9
|
* @param precision - `PrecisionRangeType` (default: 2 -> .01),
|
|
9
10
|
* @param name - `string`
|
|
10
|
-
* @param index - `number`
|
|
11
11
|
*/
|
|
12
|
-
export declare const create:
|
|
12
|
+
export declare const create: FloatFactory;
|
|
@@ -1,2 +1,3 @@
|
|
|
1
1
|
import { IntDataEntry } from '../types';
|
|
2
|
-
export
|
|
2
|
+
export type IntFactory = (value: number, min?: number, max?: number, name?: string) => IntDataEntry;
|
|
3
|
+
export declare const create: IntFactory;
|
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export
|
|
1
|
+
import { DataEntry, OptionalDataEntry } from '../types';
|
|
2
|
+
export type OptionalFactory = (descriptor: [DataEntry, null] | [null, DataEntry], defaultState?: boolean, name?: string) => OptionalDataEntry;
|
|
3
|
+
export declare const create: OptionalFactory;
|
package/dist/factory/utils.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { EnumMappingType, EnumOptionsType } from '
|
|
1
|
+
import { EnumMappingType, EnumOptionsType } from '../types';
|
|
2
2
|
/**
|
|
3
3
|
* Method to get the max and mapping from the options
|
|
4
4
|
* @param options - `any[] | string | number` the options to get the max and mapping from
|
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
import { VersionDataEntry } from '../types';
|
|
2
2
|
import { VersionRangeType } from '../types/versionData';
|
|
3
|
-
export
|
|
3
|
+
export type VersionFactory = (value: number, bits?: VersionRangeType, name?: string) => VersionDataEntry;
|
|
4
|
+
export declare const create: VersionFactory;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
export { DataType,
|
|
2
|
-
export {
|
|
3
|
-
export {
|
|
4
|
-
export {
|
|
5
|
-
export { createStateDataObject, getInitialStateFromBase64 } from './stateHandling';
|
|
1
|
+
export { DataType, ConstantBitWidthDataTypes, VariableBitWidthDataTypes } from './enums';
|
|
2
|
+
export { PrecisionRangeType, SignificandMaxBits, FloatDataEntry, IntegerMaxBits, IntDataEntry, EnumDataEntry, EnumArrayDataEntry, VersionRangeType, VersionDataEntry, BooleanDataEntry, DataEntry, ProtectedAttributeNames, EnumOptionsType, EnumMappingType, PROTECTED_ATTRIBUTE_NAMES, StateDataObject, StateDataObjectValue } from './types';
|
|
3
|
+
export { getBitsCount, dataEntryBitstringParser, dataEntryCorrecting, dataEntryBitsStringifier, parseBase64ToBits } from './parsers';
|
|
4
|
+
export { SpecificTypeNode, NodeFactory, GetStateNodeTree, FromState, ArrayNode, BooleanNode, EnumNode, IntNode, FloatNode, EnumArrayNode, OptionalNode, EnumOptionsNode, ObjectNode, StateNode, getStateData } from './stateHandling';
|
|
6
5
|
export { interpolateEntryAt, getRelativeValue } from './utils';
|
|
6
|
+
export { DataEntryFactory } from './factory';
|
|
7
7
|
export { getEnumMaxAndMappingFromOptions, getOptionsFromMaxAndMapping } from './factory/utils';
|