Comparing version 0.7.2 to 0.7.3
@@ -1,3 +0,3 @@ | ||
import { ClassLike } from '../types.js'; | ||
import * as primitive from './primitives.js'; | ||
import type { ClassLike } from '../types.js'; | ||
import type * as primitive from './primitives.js'; | ||
export interface MemberInit { | ||
@@ -49,2 +49,7 @@ name: string; | ||
/** | ||
* Polyfill context.metadata | ||
* @see https://github.com/microsoft/TypeScript/issues/53461 | ||
*/ | ||
export declare function _polyfill_contextMetadata(target: object): void; | ||
/** | ||
* Gets a reference to Symbol.metadata, even on platforms that do not expose it globally (like Node) | ||
@@ -51,0 +56,0 @@ */ |
@@ -7,2 +7,22 @@ export const init = Symbol('struct_init'); | ||
/** | ||
* Polyfill Symbol.metadata | ||
* @see https://github.com/microsoft/TypeScript/issues/53461 | ||
*/ | ||
Symbol.metadata ??= Symbol.for('Symbol.metadata'); | ||
/** | ||
* Polyfill context.metadata | ||
* @see https://github.com/microsoft/TypeScript/issues/53461 | ||
*/ | ||
export function _polyfill_contextMetadata(target) { | ||
if (!Symbol?.metadata) { | ||
return; | ||
} | ||
Object.defineProperty(target, Symbol.metadata, { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: Object.create(null), | ||
}); | ||
} | ||
/** | ||
* Gets a reference to Symbol.metadata, even on platforms that do not expose it globally (like Node) | ||
@@ -12,2 +32,3 @@ */ | ||
const symbol_metadata = Symbol.metadata || Object.getOwnPropertySymbols(arg).find(s => s.description == 'Symbol.metadata'); | ||
_polyfill_contextMetadata(arg); | ||
if (!symbol_metadata) { | ||
@@ -26,3 +47,3 @@ throw new ReferenceError('Could not get a reference to Symbol.metadata'); | ||
if (!isInstance(arg)) { | ||
throw new TypeError((typeof arg == 'function' ? arg.name : 'object' && arg ? arg.constructor.name : arg) + ' is not a struct instance'); | ||
throw new TypeError((typeof arg == 'function' ? arg.name : typeof arg == 'object' && arg ? arg.constructor.name : arg) + ' is not a struct instance'); | ||
} | ||
@@ -35,4 +56,4 @@ } | ||
if (!isStruct(arg)) { | ||
throw new TypeError((typeof arg == 'function' ? arg.name : 'object' && arg ? arg.constructor.name : arg) + ' is not a struct'); | ||
throw new TypeError((typeof arg == 'function' ? arg.name : typeof arg == 'object' && arg ? arg.constructor.name : arg) + ' is not a struct'); | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
import { UnionToTuple } from './types.js'; | ||
import type { UnionToTuple } from './types.js'; | ||
export declare function filterObject<O extends object, R extends object>(object: O, predicate: (key: keyof O, value: O[keyof O]) => boolean): R; | ||
@@ -3,0 +3,0 @@ export declare function pick<T extends object, K extends keyof T>(object: T, ...keys: readonly K[]): Pick<T, K>; |
import * as primitive from './internal/primitives.js'; | ||
import { DecoratorContext, InstanceLike, Options, Size, StaticLike, type MemberContext } from './internal/struct.js'; | ||
import { ClassLike } from './types.js'; | ||
import type { DecoratorContext, InstanceLike, Options, Size, StaticLike } from './internal/struct.js'; | ||
import { type MemberContext } from './internal/struct.js'; | ||
import type { ClassLike } from './types.js'; | ||
export * as Struct from './internal/struct.js'; | ||
@@ -5,0 +6,0 @@ /** |
import * as primitive from './internal/primitives.js'; | ||
import { checkInstance, checkStruct, init, isStatic, metadata, symbol_metadata, } from './internal/struct.js'; | ||
import { checkInstance, checkStruct, init, isStatic, metadata, symbol_metadata } from './internal/struct.js'; | ||
import { capitalize } from './string.js'; | ||
@@ -28,3 +28,2 @@ export * as Struct from './internal/struct.js'; | ||
export function struct(options = {}) { | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
return function _decorateStruct(target, context) { | ||
@@ -31,0 +30,0 @@ context.metadata ??= {}; |
{ | ||
"name": "utilium", | ||
"version": "0.7.2", | ||
"version": "0.7.3", | ||
"description": "Typescript utilies", | ||
@@ -10,3 +10,5 @@ "main": "dist/index.js", | ||
".": "./dist/index.js", | ||
"./*": "./dist/*" | ||
"./*": "./dist/*", | ||
"./eslint": "./eslint.shared.js", | ||
"./source": "./src/*" | ||
}, | ||
@@ -16,3 +18,4 @@ "files": [ | ||
"dist", | ||
"tsconfig.json" | ||
"tsconfig.json", | ||
"scripts" | ||
], | ||
@@ -22,5 +25,5 @@ "scripts": { | ||
"format": "prettier --write .", | ||
"lint": "eslint src && tsc --noEmit", | ||
"lint": "tsc --noEmit && eslint src", | ||
"build": "tsc -p tsconfig.json", | ||
"build:docs": "typedoc src/index.ts --out docs", | ||
"build:docs": "typedoc", | ||
"prepublishOnly": "npm run build" | ||
@@ -39,9 +42,12 @@ }, | ||
"devDependencies": { | ||
"@eslint/js": "^9.12.0", | ||
"@stylistic/eslint-plugin": "^2.9.0", | ||
"@types/eslint__js": "^8.42.3", | ||
"@types/node": "^20.12.7", | ||
"@typescript-eslint/eslint-plugin": "^6.2.0", | ||
"@typescript-eslint/parser": "^6.2.0", | ||
"eslint": "^8.45.0", | ||
"eslint": "^9.12.0", | ||
"globals": "^15.10.0", | ||
"prettier": "^3.2.5", | ||
"typedoc": "^0.26.6", | ||
"typescript": "^5.5.4" | ||
"typescript": "^5.5.4", | ||
"typescript-eslint": "^8.8.0" | ||
}, | ||
@@ -48,0 +54,0 @@ "dependencies": { |
@@ -1,3 +0,3 @@ | ||
import { ClassLike } from '../types.js'; | ||
import * as primitive from './primitives.js'; | ||
import type { ClassLike } from '../types.js'; | ||
import type * as primitive from './primitives.js'; | ||
@@ -66,2 +66,24 @@ export interface MemberInit { | ||
/** | ||
* Polyfill Symbol.metadata | ||
* @see https://github.com/microsoft/TypeScript/issues/53461 | ||
*/ | ||
(Symbol as { metadata: symbol }).metadata ??= Symbol.for('Symbol.metadata'); | ||
/** | ||
* Polyfill context.metadata | ||
* @see https://github.com/microsoft/TypeScript/issues/53461 | ||
*/ | ||
export function _polyfill_contextMetadata(target: object): void { | ||
if (!Symbol?.metadata) { | ||
return; | ||
} | ||
Object.defineProperty(target, Symbol.metadata, { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: Object.create(null), | ||
}); | ||
} | ||
/** | ||
* Gets a reference to Symbol.metadata, even on platforms that do not expose it globally (like Node) | ||
@@ -71,2 +93,3 @@ */ | ||
const symbol_metadata = Symbol.metadata || Object.getOwnPropertySymbols(arg).find(s => s.description == 'Symbol.metadata'); | ||
_polyfill_contextMetadata(arg); | ||
if (!symbol_metadata) { | ||
@@ -97,3 +120,3 @@ throw new ReferenceError('Could not get a reference to Symbol.metadata'); | ||
if (!isInstance(arg)) { | ||
throw new TypeError((typeof arg == 'function' ? arg.name : 'object' && arg ? arg.constructor.name : arg) + ' is not a struct instance'); | ||
throw new TypeError((typeof arg == 'function' ? arg.name : typeof arg == 'object' && arg ? arg.constructor.name : arg) + ' is not a struct instance'); | ||
} | ||
@@ -108,3 +131,3 @@ } | ||
if (!isStruct(arg)) { | ||
throw new TypeError((typeof arg == 'function' ? arg.name : 'object' && arg ? arg.constructor.name : arg) + ' is not a struct'); | ||
throw new TypeError((typeof arg == 'function' ? arg.name : typeof arg == 'object' && arg ? arg.constructor.name : arg) + ' is not a struct'); | ||
} | ||
@@ -111,0 +134,0 @@ } |
@@ -1,6 +0,6 @@ | ||
import { UnionToTuple } from './types.js'; | ||
import type { UnionToTuple } from './types.js'; | ||
export function filterObject<O extends object, R extends object>(object: O, predicate: (key: keyof O, value: O[keyof O]) => boolean): R { | ||
const entries = <[keyof O, O[keyof O]][]>Object.entries(object); | ||
return <R>Object.fromEntries(entries.filter(([key, value]) => predicate(key, value))); | ||
const entries = Object.entries(object) as [keyof O, O[keyof O]][]; | ||
return Object.fromEntries(entries.filter(([key, value]) => predicate(key, value))) as R; | ||
} | ||
@@ -11,3 +11,3 @@ | ||
export function pick<T extends object, K extends keyof T>(object: T, ...keys: readonly K[] | readonly (readonly K[])[]): Pick<T, K> { | ||
const picked = <Pick<T, K>>{}; | ||
const picked = {} as Pick<T, K>; | ||
for (const key of keys.flat() as K[]) { | ||
@@ -14,0 +14,0 @@ picked[key] = object[key]; |
export function capitalize<T extends string>(value: T): Capitalize<T> { | ||
return <Capitalize<T>>(value.at(0)!.toUpperCase() + value.slice(1)); | ||
return (value.at(0)!.toUpperCase() + value.slice(1)) as Capitalize<T>; | ||
} | ||
export function uncapitalize<T extends string>(value: T): Uncapitalize<T> { | ||
return <Uncapitalize<T>>(value.at(0)!.toLowerCase() + value.slice(1)); | ||
return (value.at(0)!.toLowerCase() + value.slice(1)) as Uncapitalize<T>; | ||
} | ||
@@ -8,0 +8,0 @@ |
import * as primitive from './internal/primitives.js'; | ||
import { | ||
checkInstance, | ||
checkStruct, | ||
DecoratorContext, | ||
init, | ||
InstanceLike, | ||
isStatic, | ||
MemberInit, | ||
Metadata, | ||
metadata, | ||
Options, | ||
Size, | ||
StaticLike, | ||
symbol_metadata, | ||
type MemberContext, | ||
} from './internal/struct.js'; | ||
import type { DecoratorContext, InstanceLike, Member, MemberInit, Metadata, Options, Size, StaticLike } from './internal/struct.js'; | ||
import { checkInstance, checkStruct, init, isStatic, metadata, symbol_metadata, type MemberContext } from './internal/struct.js'; | ||
import { capitalize } from './string.js'; | ||
import { ClassLike } from './types.js'; | ||
import type { ClassLike } from './types.js'; | ||
export * as Struct from './internal/struct.js'; | ||
@@ -51,3 +37,2 @@ | ||
export function struct(options: Partial<Options> = {}) { | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
return function _decorateStruct<const T extends StaticLike>(target: T, context: ClassDecoratorContext & DecoratorContext): T { | ||
@@ -57,3 +42,3 @@ context.metadata ??= {}; | ||
let size = 0; | ||
const members = new Map(); | ||
const members = new Map<string, Member>(); | ||
for (const _ of context.metadata[init]) { | ||
@@ -126,3 +111,3 @@ const { name, type, length } = _; | ||
const Type = capitalize(type); | ||
const fn = <`set${typeof Type}`>('set' + Type); | ||
const fn = ('set' + Type) as `set${typeof Type}`; | ||
if (fn == 'setInt64') { | ||
@@ -183,3 +168,3 @@ view.setBigInt64(iOff, BigInt(value), !options.bigEndian); | ||
const Type = capitalize(type); | ||
const fn = <`get${typeof Type}`>('get' + Type); | ||
const fn = ('get' + Type) as `get${typeof Type}`; | ||
if (fn == 'getInt64') { | ||
@@ -186,0 +171,0 @@ object[key] = view.getBigInt64(iOff, !options.bigEndian); |
@@ -31,3 +31,3 @@ /** | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
// eslint-disable-next-line @typescript-eslint/no-empty-object-type | ||
export type RequiredKeys<T> = { [K in keyof T]-?: {} extends { [P in K]: T[K] } ? never : K }[keyof T]; | ||
@@ -34,0 +34,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
71225
2046
10