@avro/common-types
Advanced tools
+37
-4
@@ -0,8 +1,41 @@ | ||
| /// <reference types="node" /> | ||
| import { LogicalType, LongType, Schema, Type } from '@avro/types'; | ||
| import { Moment } from 'moment'; | ||
| export declare const bigIntLongType: LongType<bigint, "abstract:long">; | ||
| export declare class MomentType<U = number> extends LogicalType<Moment, U> { | ||
| /** Decimal logical type implementation. */ | ||
| export declare class DecimalType extends LogicalType<Decimal, Buffer> { | ||
| readonly precision: number; | ||
| readonly scale: number; | ||
| protected constructor(schema: Schema, opts: Type.ForSchemaOpts); | ||
| protected _fromValue(val: U): Moment; | ||
| protected _toValue(val: any): U; | ||
| private newDecimal; | ||
| protected _fromValue(buf: Buffer): Decimal; | ||
| protected _toValue(dec: unknown): Buffer; | ||
| protected _resolve(writer: Type): ((v: any) => Decimal) | undefined; | ||
| protected _export(schema: Schema): void; | ||
| } | ||
| export interface DecimalConfig { | ||
| readonly precision: number; | ||
| readonly scale: number; | ||
| readonly unscaled: number; | ||
| } | ||
| export declare class Decimal { | ||
| readonly precision: number; | ||
| readonly scale: number; | ||
| readonly unscaled: number; | ||
| private constructor(); | ||
| toNumber(): number; | ||
| static forConfig(cfg: DecimalConfig): Decimal; | ||
| } | ||
| /** | ||
| * A logical type representing millisecond timestamps as JavaScript `Date`s. | ||
| * This type is most commonly used with the `'timestamp-millis'` key. | ||
| */ | ||
| export declare class DateType<U = number> extends LogicalType<Date, U> { | ||
| protected constructor(schema: Schema, opts: Type.ForSchemaOpts); | ||
| protected _fromValue(val: U): Date; | ||
| protected _toValue(val: unknown): U | undefined; | ||
| protected _resolve(writer: Type): ((v: any) => Date) | undefined; | ||
| } | ||
| export declare const defaultLogicalTypes: Readonly<{ | ||
| decimal: typeof DecimalType; | ||
| 'timestamp-millis': typeof DateType; | ||
| }>; |
+101
-10
| "use strict"; | ||
| var __importDefault = (this && this.__importDefault) || function (mod) { | ||
| return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
| }; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| exports.MomentType = exports.bigIntLongType = void 0; | ||
| exports.defaultLogicalTypes = exports.DateType = exports.Decimal = exports.DecimalType = exports.bigIntLongType = void 0; | ||
| const types_1 = require("@avro/types"); | ||
| const moment_1 = __importDefault(require("moment")); | ||
| const verror_1 = require("verror"); | ||
@@ -28,4 +24,90 @@ exports.bigIntLongType = types_1.LongType.__with({ | ||
| } | ||
| class MomentType extends types_1.LogicalType { | ||
| /** Decimal logical type implementation. */ | ||
| class DecimalType extends types_1.LogicalType { | ||
| constructor(schema, opts) { | ||
| checkTypeName(schema, ['bytes', 'fixed']); | ||
| super(schema, opts); | ||
| const { precision, scale } = schema; | ||
| if (precision !== (precision | 0) || precision <= 0) { | ||
| throw new verror_1.VError('invalid precision: %s', precision); | ||
| } | ||
| if (scale !== (scale | 0) || scale < 0 || scale > precision) { | ||
| throw new verror_1.VError('invalid scale: %s', scale); | ||
| } | ||
| this.precision = precision; | ||
| this.scale = scale; | ||
| const type = this.underlyingType; | ||
| if (types_1.Type.isType(type, 'fixed')) { | ||
| const size = type.size; | ||
| if (size > 6) { | ||
| // TODO: Support larger sizes. | ||
| throw new verror_1.VError('size is too large: %s', size); | ||
| } | ||
| const maxVal = Math.pow(2, 8 * size - 1) - 1; | ||
| const maxPrecision = Math.log(maxVal) / Math.log(10); | ||
| if (precision > (maxPrecision | 0)) { | ||
| throw new verror_1.VError('fixed size too small to hold required precision'); | ||
| } | ||
| } | ||
| } | ||
| newDecimal(unscaled) { | ||
| const { precision, scale } = this; | ||
| return Decimal.forConfig({ precision, scale, unscaled }); | ||
| } | ||
| _fromValue(buf) { | ||
| return this.newDecimal(buf.readIntBE(0, buf.length)); | ||
| } | ||
| _toValue(dec) { | ||
| if (!(dec instanceof Decimal)) { | ||
| throw new verror_1.VError('invalid decimal: %j', dec); | ||
| } | ||
| const { precision, scale } = dec; | ||
| if (precision !== this.precision || scale !== this.scale) { | ||
| throw new verror_1.VError('inconsistent decimal: %j', dec); | ||
| } | ||
| const type = this.underlyingType; | ||
| let buf; | ||
| if (types_1.Type.isType(type, 'fixed')) { | ||
| buf = Buffer.alloc(type.size); | ||
| } | ||
| else { | ||
| const val = +dec; | ||
| const size = Math.log(val > 0 ? val : -2 * val) / (Math.log(2) * 8) | 0; | ||
| buf = Buffer.alloc(size + 1); | ||
| } | ||
| buf.writeIntBE(dec.unscaled, 0, buf.length); | ||
| return buf; | ||
| } | ||
| _resolve(writer) { | ||
| if (writer instanceof DecimalType && | ||
| writer.precision === this.precision && | ||
| writer.scale === this.scale) { | ||
| return (dec) => dec; | ||
| } | ||
| } | ||
| _export(schema) { | ||
| Object.assign(schema, { precision: this.precision, scale: this.scale }); | ||
| } | ||
| } | ||
| exports.DecimalType = DecimalType; | ||
| class Decimal { | ||
| constructor(precision, scale, unscaled) { | ||
| this.precision = precision; | ||
| this.scale = scale; | ||
| this.unscaled = unscaled; | ||
| } | ||
| toNumber() { | ||
| return this.unscaled * Math.pow(10, -this.scale); | ||
| } | ||
| static forConfig(cfg) { | ||
| return new Decimal(cfg.precision, cfg.scale, cfg.unscaled); | ||
| } | ||
| } | ||
| exports.Decimal = Decimal; | ||
| /** | ||
| * A logical type representing millisecond timestamps as JavaScript `Date`s. | ||
| * This type is most commonly used with the `'timestamp-millis'` key. | ||
| */ | ||
| class DateType extends types_1.LogicalType { | ||
| constructor(schema, opts) { | ||
| checkTypeName(schema, ['long']); | ||
@@ -35,11 +117,20 @@ super(schema, opts); | ||
| _fromValue(val) { | ||
| return moment_1.default(this.underlyingType.jsonEncode(val)); | ||
| return new Date(this.underlyingType.jsonEncode(val)); | ||
| } | ||
| _toValue(val) { | ||
| if (!moment_1.default.isMoment(val)) { | ||
| throw new verror_1.VError('invalid moment: %j', val); | ||
| if (!(val instanceof Date)) { | ||
| return undefined; | ||
| } | ||
| return this.underlyingType.jsonDecode(+val); | ||
| } | ||
| _resolve(writer) { | ||
| if (types_1.Type.isType(writer, 'long', 'logical:timestamp-millis')) { | ||
| return this._fromValue; | ||
| } | ||
| } | ||
| } | ||
| exports.MomentType = MomentType; | ||
| exports.DateType = DateType; | ||
| exports.defaultLogicalTypes = Object.freeze({ | ||
| 'decimal': DecimalType, | ||
| 'timestamp-millis': DateType, | ||
| }); |
+4
-4
| { | ||
| "name": "@avro/common-types", | ||
| "version": "0.1.1", | ||
| "version": "0.2.0", | ||
| "description": "Common Avro types", | ||
@@ -11,3 +11,4 @@ "homepage": "https://github.com/mtth/avsc", | ||
| "files": [ | ||
| "lib" | ||
| "lib/**/*.js", | ||
| "lib/**/*.d.ts" | ||
| ], | ||
@@ -38,4 +39,3 @@ "main": "./lib", | ||
| "dependencies": { | ||
| "@avro/types": "^1.0.16", | ||
| "moment": "^2.29.0", | ||
| "@avro/types": "^1.0.18", | ||
| "verror": "^1.10.0" | ||
@@ -42,0 +42,0 @@ }, |
Sorry, the diff of this file is not supported yet
2
-33.33%175
243.14%11401
-83.24%5
-16.67%- Removed
- Removed
Updated