Comparing version 0.7.0 to 0.7.1
@@ -21,2 +21,5 @@ type BitsToBytes = { | ||
}): type is Valid; | ||
export declare function checkValid(type: { | ||
toString(): string; | ||
}): asserts type is Valid; | ||
export {}; |
@@ -14,1 +14,6 @@ import { capitalize } from '../string.js'; | ||
} | ||
export function checkValid(type) { | ||
if (!isValid(type)) { | ||
throw new TypeError('Not a valid primitive type: ' + type); | ||
} | ||
} |
@@ -60,4 +60,6 @@ import { ClassLike } from '../types.js'; | ||
export declare function isInstance<T extends Metadata = Metadata>(arg: unknown): arg is Instance<T>; | ||
export declare function checkInstance<T extends Metadata = Metadata>(arg: unknown): asserts arg is Instance<T>; | ||
export declare function isStruct<T extends Metadata = Metadata>(arg: unknown): arg is Instance<T> | Static<T>; | ||
export declare function checkStruct<T extends Metadata = Metadata>(arg: unknown): asserts arg is Instance<T> | Static<T>; | ||
export type Like<T extends Metadata = Metadata> = InstanceLike<T> | StaticLike<T>; | ||
export type Size<T extends primitive.Valid | StaticLike | InstanceLike> = T extends primitive.Valid ? primitive.Size<T> : T extends Like<infer M> ? M['size'] : number; |
@@ -22,4 +22,14 @@ export const init = Symbol('struct_init'); | ||
} | ||
export function checkInstance(arg) { | ||
if (!isInstance(arg)) { | ||
throw new TypeError((typeof arg == 'function' ? arg.name : 'object' && arg ? arg.constructor.name : arg) + ' is not a struct instance'); | ||
} | ||
} | ||
export function isStruct(arg) { | ||
return isInstance(arg) || isStatic(arg); | ||
} | ||
export function checkStruct(arg) { | ||
if (!isStruct(arg)) { | ||
throw new TypeError((typeof arg == 'function' ? arg.name : 'object' && arg ? arg.constructor.name : arg) + ' is not a struct'); | ||
} | ||
} |
import * as primitive from './internal/primitives.js'; | ||
import { symbol_metadata, init, isInstance, isStatic, isStruct, metadata, } from './internal/struct.js'; | ||
import { checkInstance, checkStruct, init, isStatic, metadata, symbol_metadata, } from './internal/struct.js'; | ||
import { capitalize } from './string.js'; | ||
@@ -11,10 +11,6 @@ export * as Struct from './internal/struct.js'; | ||
if (typeof type == 'string') { | ||
if (!primitive.isValid(type)) { | ||
throw new TypeError('Invalid primitive type: ' + type); | ||
} | ||
primitive.checkValid(type); | ||
return (+primitive.normalize(type).match(primitive.regex)[2] / 8); | ||
} | ||
if (!isStruct(type)) { | ||
throw new TypeError('Not a struct'); | ||
} | ||
checkStruct(type); | ||
const struct = isStatic(type) ? type : type.constructor; | ||
@@ -34,3 +30,3 @@ return struct[symbol_metadata(struct)][metadata].size; | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
return function __decorateStruct(target, context) { | ||
return function _decorateStruct(target, context) { | ||
context.metadata ??= {}; | ||
@@ -80,5 +76,3 @@ context.metadata[init] ||= []; | ||
export function serialize(instance) { | ||
if (!isInstance(instance)) { | ||
throw new TypeError('Can not serialize, not a struct instance'); | ||
} | ||
checkInstance(instance); | ||
const { options, members } = instance.constructor[symbol_metadata(instance.constructor)][metadata]; | ||
@@ -118,8 +112,6 @@ const buffer = new Uint8Array(sizeof(instance)); | ||
export function deserialize(instance, _buffer) { | ||
if (!isInstance(instance)) { | ||
throw new TypeError('Can not deserialize, not a struct instance'); | ||
} | ||
checkInstance(instance); | ||
const { options, members } = instance.constructor[symbol_metadata(instance.constructor)][metadata]; | ||
const buffer = new Uint8Array('buffer' in _buffer ? _buffer.buffer : _buffer); | ||
const view = new DataView(buffer.buffer); | ||
const buffer = _buffer instanceof Uint8Array ? _buffer : new Uint8Array('buffer' in _buffer ? _buffer.buffer : _buffer); | ||
const view = new DataView(buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength)); | ||
for (const [name, { type, offset, length }] of members) { | ||
@@ -126,0 +118,0 @@ for (let i = 0; i < (length || 1); i++) { |
{ | ||
"name": "utilium", | ||
"version": "0.7.0", | ||
"version": "0.7.1", | ||
"description": "Typescript utilies", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -33,1 +33,7 @@ import { capitalize } from '../string.js'; | ||
} | ||
export function checkValid(type: { toString(): string }): asserts type is Valid { | ||
if (!isValid(type)) { | ||
throw new TypeError('Not a valid primitive type: ' + type); | ||
} | ||
} |
@@ -93,2 +93,8 @@ import { ClassLike } from '../types.js'; | ||
export function checkInstance<T extends Metadata = Metadata>(arg: unknown): asserts arg is Instance<T> { | ||
if (!isInstance(arg)) { | ||
throw new TypeError((typeof arg == 'function' ? arg.name : 'object' && arg ? arg.constructor.name : arg) + ' is not a struct instance'); | ||
} | ||
} | ||
export function isStruct<T extends Metadata = Metadata>(arg: unknown): arg is Instance<T> | Static<T> { | ||
@@ -98,4 +104,10 @@ return isInstance(arg) || isStatic(arg); | ||
export function checkStruct<T extends Metadata = Metadata>(arg: unknown): asserts arg is Instance<T> | Static<T> { | ||
if (!isStruct(arg)) { | ||
throw new TypeError((typeof arg == 'function' ? arg.name : 'object' && arg ? arg.constructor.name : arg) + ' is not a struct'); | ||
} | ||
} | ||
export type Like<T extends Metadata = Metadata> = InstanceLike<T> | StaticLike<T>; | ||
export type Size<T extends primitive.Valid | StaticLike | InstanceLike> = T extends primitive.Valid ? primitive.Size<T> : T extends Like<infer M> ? M['size'] : number; |
import * as primitive from './internal/primitives.js'; | ||
import { | ||
checkInstance, | ||
checkStruct, | ||
DecoratorContext, | ||
init, | ||
InstanceLike, | ||
isStatic, | ||
MemberInit, | ||
Metadata, | ||
metadata, | ||
Options, | ||
@@ -11,7 +16,2 @@ Size, | ||
symbol_metadata, | ||
init, | ||
isInstance, | ||
isStatic, | ||
isStruct, | ||
metadata, | ||
type MemberContext, | ||
@@ -29,5 +29,3 @@ } from './internal/struct.js'; | ||
if (typeof type == 'string') { | ||
if (!primitive.isValid(type)) { | ||
throw new TypeError('Invalid primitive type: ' + type); | ||
} | ||
primitive.checkValid(type); | ||
@@ -37,5 +35,3 @@ return (+primitive.normalize(type).match(primitive.regex)![2] / 8) as Size<T>; | ||
if (!isStruct(type)) { | ||
throw new TypeError('Not a struct'); | ||
} | ||
checkStruct(type); | ||
@@ -59,3 +55,3 @@ const struct = isStatic(type) ? type : type.constructor; | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
return function __decorateStruct<const T extends StaticLike>(target: T, context: ClassDecoratorContext & DecoratorContext): T { | ||
return function _decorateStruct<const T extends StaticLike>(target: T, context: ClassDecoratorContext & DecoratorContext): T { | ||
context.metadata ??= {}; | ||
@@ -110,5 +106,3 @@ context.metadata[init] ||= []; | ||
export function serialize(instance: unknown): Uint8Array { | ||
if (!isInstance(instance)) { | ||
throw new TypeError('Can not serialize, not a struct instance'); | ||
} | ||
checkInstance(instance); | ||
const { options, members } = instance.constructor[symbol_metadata(instance.constructor)][metadata]; | ||
@@ -157,10 +151,8 @@ | ||
export function deserialize(instance: unknown, _buffer: ArrayBuffer | ArrayBufferView) { | ||
if (!isInstance(instance)) { | ||
throw new TypeError('Can not deserialize, not a struct instance'); | ||
} | ||
checkInstance(instance); | ||
const { options, members } = instance.constructor[symbol_metadata(instance.constructor)][metadata]; | ||
const buffer = new Uint8Array('buffer' in _buffer ? _buffer.buffer : _buffer); | ||
const buffer = _buffer instanceof Uint8Array ? _buffer : new Uint8Array('buffer' in _buffer ? _buffer.buffer : _buffer); | ||
const view = new DataView(buffer.buffer); | ||
const view = new DataView(buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength)); | ||
@@ -167,0 +159,0 @@ for (const [name, { type, offset, length }] of members) { |
69631
2014