diff --git a/implementations/javascript/package.json b/implementations/javascript/package.json index ea0f1c8..db3bc38 100644 --- a/implementations/javascript/package.json +++ b/implementations/javascript/package.json @@ -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", diff --git a/implementations/javascript/src/codec.ts b/implementations/javascript/src/codec.ts index 02d0e8d..ff8f076 100644 --- a/implementations/javascript/src/codec.ts +++ b/implementations/javascript/src/codec.ts @@ -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 { 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(); diff --git a/implementations/javascript/src/values.ts b/implementations/javascript/src/values.ts index 579aebd..84bf60d 100644 --- a/implementations/javascript/src/values.ts +++ b/implementations/javascript/src/values.ts @@ -12,7 +12,7 @@ const textDecoder = new TextDecoder(); export type DefaultPointer = object export type Value = Atom | Compound | T | Annotated; -export type Atom = boolean | Single | Double | number | string | Bytes | symbol; +export type Atom = boolean | SingleFloat | DoubleFloat | number | string | Bytes | symbol; export type Compound = Record | Array> | Set | Dictionary, 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 { +export class SingleFloat extends Float implements Preservable { [AsPreserve](): Value { return this; } @@ -121,7 +127,11 @@ export class Single extends Float implements Preservable { } } -export class Double extends Float implements Preservable { +export function Single(value: number | Float) { + return new SingleFloat(value); +} + +export class DoubleFloat extends Float implements Preservable { [AsPreserve](): Value { return this; } @@ -142,6 +152,10 @@ export class Double extends Float implements Preservable { } } +export function Double(value: number | Float) { + return new DoubleFloat(value); +} + export type BytesLike = Bytes | Uint8Array; export class Bytes implements Preservable {