mirror of
https://github.com/yume-chan/ya-webadb.git
synced 2025-10-04 18:29:23 +02:00
feat(struct): add missing int8
and int16
types
This commit is contained in:
parent
4db6bd2b5d
commit
b6531e821f
3 changed files with 53 additions and 12 deletions
|
@ -215,18 +215,18 @@ export default class Struct<
|
|||
* Merges (flats) another `Struct`'s fields and extra fields into this one.
|
||||
*/
|
||||
public fields<TOther extends Struct<any, any, any, any>>(
|
||||
struct: TOther
|
||||
other: TOther
|
||||
): Struct<
|
||||
TValue & TOther['valueType'],
|
||||
TInit & TOther['initType'],
|
||||
TExtra & TOther['extraType'],
|
||||
TPostDeserialized
|
||||
> {
|
||||
for (const field of struct._fields) {
|
||||
for (const field of other._fields) {
|
||||
this._fields.push(field);
|
||||
}
|
||||
this._size += struct._size;
|
||||
Object.assign(this._extra, struct._extra);
|
||||
this._size += other._size;
|
||||
Object.assign(this._extra, other._extra);
|
||||
return this as any;
|
||||
}
|
||||
|
||||
|
@ -245,6 +245,23 @@ export default class Struct<
|
|||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends an `int8` field to the `Struct`
|
||||
*/
|
||||
public int8<
|
||||
TName extends PropertyKey,
|
||||
TTypeScriptType = (typeof NumberFieldType)['Uint8']['valueType']
|
||||
>(
|
||||
name: TName,
|
||||
_typescriptType?: TTypeScriptType,
|
||||
) {
|
||||
return this.number(
|
||||
name,
|
||||
NumberFieldType.Int8,
|
||||
_typescriptType
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends an `uint8` field to the `Struct`
|
||||
*/
|
||||
|
@ -262,6 +279,23 @@ export default class Struct<
|
|||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends an `int16` field to the `Struct`
|
||||
*/
|
||||
public int16<
|
||||
TName extends PropertyKey,
|
||||
TTypeScriptType = (typeof NumberFieldType)['Uint16']['valueType']
|
||||
>(
|
||||
name: TName,
|
||||
_typescriptType?: TTypeScriptType,
|
||||
) {
|
||||
return this.number(
|
||||
name,
|
||||
NumberFieldType.Int16,
|
||||
_typescriptType
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends an `uint16` field to the `Struct`
|
||||
*/
|
||||
|
@ -409,11 +443,14 @@ export default class Struct<
|
|||
};
|
||||
|
||||
/**
|
||||
* Adds some extra fields into every Struct instance.
|
||||
* Adds some extra fields into every Struct value.
|
||||
*
|
||||
* Extra fields will not affect serialize or deserialize process.
|
||||
*
|
||||
* Multiple calls to `extra` will merge all values together.
|
||||
*
|
||||
* @param value
|
||||
* An object containing anything you want to add to the result object. Accessors and methods are also allowed.
|
||||
*/
|
||||
public extra<T extends Record<
|
||||
// This trick disallows any keys that are already in `TValue`
|
||||
|
|
|
@ -7,8 +7,12 @@ export type DataViewSetters =
|
|||
{ [TKey in keyof DataView]: TKey extends `set${string}` ? TKey : never }[keyof DataView];
|
||||
|
||||
export class NumberFieldType<TTypeScriptType extends number | bigint = number | bigint> {
|
||||
public static readonly Int8 = new NumberFieldType<number>(1, 'getInt8', 'setInt8');
|
||||
|
||||
public static readonly Uint8 = new NumberFieldType<number>(1, 'getUint8', 'setUint8');
|
||||
|
||||
public static readonly Int16 = new NumberFieldType<number>(2, 'getInt16', 'setUint16');
|
||||
|
||||
public static readonly Uint16 = new NumberFieldType<number>(2, 'getUint16', 'setUint16');
|
||||
|
||||
public static readonly Int32 = new NumberFieldType<number>(4, 'getInt32', 'setInt32');
|
||||
|
|
|
@ -33,15 +33,15 @@ export type Overwrite<TBase extends object, TNew extends object> =
|
|||
*/
|
||||
export type OmitNever<T> = Pick<T, { [K in keyof T]: [T[K]] extends [never] ? never : K }[keyof T]>;
|
||||
|
||||
/**
|
||||
* Generates a type. Useful in generic type inference.
|
||||
*/
|
||||
export function placeholder<T>(): T {
|
||||
return undefined as unknown as T;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract keys of fields in `T` that has type `TValue`
|
||||
*/
|
||||
export type KeysOfType<T, TValue> =
|
||||
{ [TKey in keyof T]: T[TKey] extends TValue ? TKey : never }[keyof T];
|
||||
|
||||
/**
|
||||
* Returns a (fake) value of the given type.
|
||||
*/
|
||||
export function placeholder<T>(): T {
|
||||
return undefined as unknown as T;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue