Improve ergonomics of Double/Single and floatValue

This commit is contained in:
Tony Garnock-Jones 2021-02-17 15:34:31 +01:00
parent 95c04bd5d5
commit 83b09d9406
3 changed files with 25 additions and 11 deletions

View File

@ -1,6 +1,6 @@
{
"name": "preserves",
"version": "0.5.2",
"version": "0.5.3",
"description": "Experimental data serialization format",
"homepage": "https://gitlab.com/preserves/preserves",
"license": "Apache-2.0",

View File

@ -3,7 +3,7 @@
import {
underlying,
Annotated,
Dictionary, Set, Bytes, Record, Single, Double,
Dictionary, Set, Bytes, Record, SingleFloat, DoubleFloat,
BytesLike,
Value,
} from './values';
@ -156,8 +156,8 @@ export class Decoder<T extends object> {
switch (tag) {
case Tag.False: return this.wrap(false);
case Tag.True: return this.wrap(true);
case Tag.Float: return this.wrap(new Single(this.nextbytes(4).getFloat32(0, false)));
case Tag.Double: return this.wrap(new Double(this.nextbytes(8).getFloat64(0, false)));
case Tag.Float: return this.wrap(new SingleFloat(this.nextbytes(4).getFloat32(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.Annotation: {
const a = this.next();

View File

@ -12,7 +12,7 @@ const textDecoder = new TextDecoder();
export type DefaultPointer = object
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 const IsPreservesRecord = Symbol.for('IsPreservesRecord');
@ -92,15 +92,21 @@ export abstract class Float {
return (x?.[FloatType] === t);
}
static isSingle = (x: any): x is Single => Float.isFloat(x, 'Single');
static isDouble = (x: any): x is Double => Float.isFloat(x, 'Double');
static isSingle = (x: any): x is SingleFloat => Float.isFloat(x, 'Single');
static isDouble = (x: any): x is DoubleFloat => Float.isFloat(x, 'Double');
}
static unwrap(f: number | Float): number {
return typeof f === 'number' ? f : f.value;
export function floatValue(f: any): number {
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> {
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> {
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 class Bytes implements Preservable<never> {