Improve ergonomics of Double/Single and floatValue
This commit is contained in:
parent
95c04bd5d5
commit
83b09d9406
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "preserves",
|
"name": "preserves",
|
||||||
"version": "0.5.2",
|
"version": "0.5.3",
|
||||||
"description": "Experimental data serialization format",
|
"description": "Experimental data serialization format",
|
||||||
"homepage": "https://gitlab.com/preserves/preserves",
|
"homepage": "https://gitlab.com/preserves/preserves",
|
||||||
"license": "Apache-2.0",
|
"license": "Apache-2.0",
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
import {
|
import {
|
||||||
underlying,
|
underlying,
|
||||||
Annotated,
|
Annotated,
|
||||||
Dictionary, Set, Bytes, Record, Single, Double,
|
Dictionary, Set, Bytes, Record, SingleFloat, DoubleFloat,
|
||||||
BytesLike,
|
BytesLike,
|
||||||
Value,
|
Value,
|
||||||
} from './values';
|
} from './values';
|
||||||
|
@ -156,8 +156,8 @@ export class Decoder<T extends object> {
|
||||||
switch (tag) {
|
switch (tag) {
|
||||||
case Tag.False: return this.wrap(false);
|
case Tag.False: return this.wrap(false);
|
||||||
case Tag.True: return this.wrap(true);
|
case Tag.True: return this.wrap(true);
|
||||||
case Tag.Float: return this.wrap(new Single(this.nextbytes(4).getFloat32(0, false)));
|
case Tag.Float: return this.wrap(new SingleFloat(this.nextbytes(4).getFloat32(0, false)));
|
||||||
case Tag.Double: return this.wrap(new Double(this.nextbytes(8).getFloat64(0, false)));
|
case Tag.Double: return this.wrap(new DoubleFloat(this.nextbytes(8).getFloat64(0, false)));
|
||||||
case Tag.End: throw new DecodeError("Unexpected Compound end marker");
|
case Tag.End: throw new DecodeError("Unexpected Compound end marker");
|
||||||
case Tag.Annotation: {
|
case Tag.Annotation: {
|
||||||
const a = this.next();
|
const a = this.next();
|
||||||
|
|
|
@ -12,7 +12,7 @@ const textDecoder = new TextDecoder();
|
||||||
export type DefaultPointer = object
|
export type DefaultPointer = object
|
||||||
|
|
||||||
export type Value<T extends object = DefaultPointer> = Atom | Compound<T> | T | Annotated<T>;
|
export type Value<T extends object = DefaultPointer> = Atom | Compound<T> | T | Annotated<T>;
|
||||||
export type Atom = boolean | Single | Double | number | string | Bytes | symbol;
|
export type Atom = boolean | SingleFloat | DoubleFloat | number | string | Bytes | symbol;
|
||||||
export type Compound<T extends object = DefaultPointer> = Record<T> | Array<Value<T>> | Set<T> | Dictionary<Value<T>, T>;
|
export type Compound<T extends object = DefaultPointer> = Record<T> | Array<Value<T>> | Set<T> | Dictionary<Value<T>, T>;
|
||||||
|
|
||||||
export const IsPreservesRecord = Symbol.for('IsPreservesRecord');
|
export const IsPreservesRecord = Symbol.for('IsPreservesRecord');
|
||||||
|
@ -92,15 +92,21 @@ export abstract class Float {
|
||||||
return (x?.[FloatType] === t);
|
return (x?.[FloatType] === t);
|
||||||
}
|
}
|
||||||
|
|
||||||
static isSingle = (x: any): x is Single => Float.isFloat(x, 'Single');
|
static isSingle = (x: any): x is SingleFloat => Float.isFloat(x, 'Single');
|
||||||
static isDouble = (x: any): x is Double => Float.isFloat(x, 'Double');
|
static isDouble = (x: any): x is DoubleFloat => Float.isFloat(x, 'Double');
|
||||||
|
}
|
||||||
|
|
||||||
static unwrap(f: number | Float): number {
|
export function floatValue(f: any): number {
|
||||||
return typeof f === 'number' ? f : f.value;
|
if (typeof f === 'number') {
|
||||||
|
return f;
|
||||||
|
} else if (f?.[FloatType] !== void 0) {
|
||||||
|
return f.value;
|
||||||
|
} else {
|
||||||
|
return NaN;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Single extends Float implements Preservable<never> {
|
export class SingleFloat extends Float implements Preservable<never> {
|
||||||
[AsPreserve]<T extends object = DefaultPointer>(): Value<T> {
|
[AsPreserve]<T extends object = DefaultPointer>(): Value<T> {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
@ -121,7 +127,11 @@ export class Single extends Float implements Preservable<never> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Double extends Float implements Preservable<never> {
|
export function Single(value: number | Float) {
|
||||||
|
return new SingleFloat(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
export class DoubleFloat extends Float implements Preservable<never> {
|
||||||
[AsPreserve]<T extends object = DefaultPointer>(): Value<T> {
|
[AsPreserve]<T extends object = DefaultPointer>(): Value<T> {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
@ -142,6 +152,10 @@ export class Double extends Float implements Preservable<never> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function Double(value: number | Float) {
|
||||||
|
return new DoubleFloat(value);
|
||||||
|
}
|
||||||
|
|
||||||
export type BytesLike = Bytes | Uint8Array;
|
export type BytesLike = Bytes | Uint8Array;
|
||||||
|
|
||||||
export class Bytes implements Preservable<never> {
|
export class Bytes implements Preservable<never> {
|
||||||
|
|
Loading…
Reference in New Issue