feat: Update module exports and enhance TypeScript configuration

This commit is contained in:
2025-12-27 04:55:54 +00:00
parent b3875a111f
commit 2d80d53b70
12 changed files with 75 additions and 40 deletions
+7
View File
@@ -2,8 +2,15 @@
"name": "@sammo-ts/logic",
"private": true,
"version": "0.0.0",
"type": "module",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
},
"scripts": {
"build": "tsc -p tsconfig.json",
"dev": "node -e \"console.log('dev not configured')\"",
+10 -10
View File
@@ -1,10 +1,10 @@
export * from './domain/entities';
export * from './ports/rng';
export * from './ports/world';
export * from './triggers';
export * from './util/BytesLike';
export * from './util/convertBytesLikeToArrayBuffer';
export * from './util/convertBytesLikeToUint8Array';
export * from './util/LiteHashDRBG';
export * from './util/RNG';
export * from './util/RandUtil';
export * from './domain/entities.js';
export * from './ports/rng.js';
export * from './ports/world.js';
export * from './triggers/index.js';
export * from './util/BytesLike.js';
export * from './util/convertBytesLikeToArrayBuffer.js';
export * from './util/convertBytesLikeToUint8Array.js';
export * from './util/LiteHashDRBG.js';
export * from './util/RNG.js';
export * from './util/RandUtil.js';
+1 -1
View File
@@ -7,7 +7,7 @@ import type {
GeneralId,
Nation,
NationId,
} from '../domain/entities';
} from '../domain/entities.js';
export interface GeneralRepository<GeneralType extends General = General> {
getById(id: GeneralId): Promise<GeneralType | null>;
@@ -1,5 +1,5 @@
import type { GeneralTriggerState } from '../domain/entities';
import { type GeneralActionContext, GeneralTriggerCaller } from './general';
import type { GeneralTriggerState } from '../domain/entities.js';
import { type GeneralActionContext, GeneralTriggerCaller } from './general.js';
export interface GeneralActionModule<TriggerState extends GeneralTriggerState = GeneralTriggerState> {
getName?(): string;
+4 -4
View File
@@ -1,7 +1,7 @@
import type { General, GeneralTriggerState } from '../domain/entities';
import type { RandomGenerator } from '../ports/rng';
import type { WorldStateRepository } from '../ports/world';
import { TriggerCaller, type Trigger } from './core';
import type { General, GeneralTriggerState } from '../domain/entities.js';
import type { RandomGenerator } from '../ports/rng.js';
import type { WorldStateRepository } from '../ports/world.js';
import { TriggerCaller, type Trigger } from './core.js';
export interface GeneralActionLogSink {
push(message: string): void;
+3 -3
View File
@@ -1,3 +1,3 @@
export * from './core';
export * from './general';
export * from './general-action';
export * from './core.js';
export * from './general.js';
export * from './general-action.js';
+5 -5
View File
@@ -1,9 +1,9 @@
import type { RNG } from './RNG';
import type { RNG } from './RNG.js';
import { sha512 } from 'js-sha512';
import { convertBytesLikeToUint8Array } from './convertBytesLikeToUint8Array';
import type { BytesLike } from './BytesLike';
import { convertBytesLikeToUint8Array } from './convertBytesLikeToUint8Array.js';
import type { BytesLike } from './BytesLike.js';
const maxRngSupportBit = 53;
const maxInt = 0x1f_ffff_ffff_ffff; // NOTE: b 0, 10000110011, 11...11
@@ -111,7 +111,7 @@ export class LiteHashDRBG implements RNG {
protected genNextBlock(): void {
this.hq.setUint32(this.hqIdxPos, this.stateIdx, true);
const digest = sha512.arrayBuffer(this.hq.buffer);
const digest = sha512.arrayBuffer(this.hq.buffer as ArrayBuffer);
this.buffer = digest;
this.bufferIdx = 0;
this.stateIdx += 1;
@@ -181,7 +181,7 @@ export class LiteHashDRBG implements RNG {
return result;
}
result[bytes - 1] &= 0xff >> (8 - headBits);
result[bytes - 1]! &= 0xff >> (8 - headBits);
return result;
}
+10 -6
View File
@@ -1,4 +1,4 @@
import type { RNG } from './RNG';
import type { RNG } from './RNG.js';
// RNG 유틸리티 모음
export class RandUtil {
@@ -25,8 +25,8 @@ export class RandUtil {
}
public nextBit(): boolean {
const view = new DataView(this.rng.nextBits(1) as ArrayBufferLike);
return view.getUint8(0) != 0;
const bits = this.rng.nextBits(1);
return bits[0]! != 0;
}
public nextBool(prob = 0.5): boolean {
@@ -57,7 +57,10 @@ export class RandUtil {
if (srcIdx === destIdx) {
continue;
}
[result[srcIdx], result[destIdx]] = [result[destIdx], result[srcIdx]];
const srcValue = result[srcIdx]!;
const destValue = result[destIdx]!;
result[srcIdx] = destValue;
result[destIdx] = srcValue;
}
return result;
@@ -71,14 +74,15 @@ export class RandUtil {
throw new Error('Empty items');
}
const idx = this.rng.nextInt(items.length - 1);
return items[idx];
return items[idx]!;
}
if (items instanceof Set) {
return this.choice(Array.from(items.values()));
}
return items[this.choice(Array.from(Object.keys(items)))];
const key = this.choice(Array.from(Object.keys(items))) as keyof typeof items;
return items[key]!;
}
public choiceUsingWeight(items: Record<string | number, number>): string | number {
@@ -1,11 +1,22 @@
import type { BytesLike } from './BytesLike';
import type { BytesLike } from './BytesLike.js';
export function convertBytesLikeToArrayBuffer(data: BytesLike, encodeUTF8 = true): ArrayBuffer {
if (data instanceof ArrayBuffer) {
return data;
}
if (data instanceof Uint8Array) {
return data.buffer;
if (
data.byteOffset === 0
&& data.byteLength === data.buffer.byteLength
&& data.buffer instanceof ArrayBuffer
) {
return data.buffer;
}
return data.slice().buffer;
}
if (data instanceof DataView) {
const view = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
return view.slice().buffer;
}
if (typeof (data) === 'string') {
if (encodeUTF8) {
@@ -13,5 +24,5 @@ export function convertBytesLikeToArrayBuffer(data: BytesLike, encodeUTF8 = true
}
return new Uint8Array(data.split('').map((s) => s.codePointAt(0) as number)).buffer;
}
return data.buffer;
throw new Error('Unsupported BytesLike');
}
@@ -1,4 +1,4 @@
import type { BytesLike } from './BytesLike';
import type { BytesLike } from './BytesLike.js';
export function convertBytesLikeToUint8Array(data: BytesLike, encodeUTF8 = true): Uint8Array {
if (data instanceof Uint8Array) {
+4 -4
View File
@@ -1,9 +1,9 @@
import chai, { assert } from 'chai';
import chaiBytes from 'chai-bytes';
import { bufferByteSize, LiteHashDRBG } from '../src/util/LiteHashDRBG';
import { RandUtil } from '../src/util/RandUtil';
import { convertBytesLikeToArrayBuffer } from '../src/util/convertBytesLikeToArrayBuffer';
import { convertBytesLikeToUint8Array as toBytes } from '../src/util/convertBytesLikeToUint8Array';
import { bufferByteSize, LiteHashDRBG } from '../src/util/LiteHashDRBG.js';
import { RandUtil } from '../src/util/RandUtil.js';
import { convertBytesLikeToArrayBuffer } from '../src/util/convertBytesLikeToArrayBuffer.js';
import { convertBytesLikeToUint8Array as toBytes } from '../src/util/convertBytesLikeToUint8Array.js';
import _ from 'lodash-es';
chai.use(chaiBytes);
+14 -1
View File
@@ -3,7 +3,20 @@
"compilerOptions": {
"outDir": "dist",
"rootDir": "src",
"composite": true
"composite": true,
"module": "NodeNext",
"moduleResolution": "NodeNext",
"target": "ES2023",
"lib": ["ES2024"],
"verbatimModuleSyntax": true,
"noUncheckedIndexedAccess": true,
"exactOptionalPropertyTypes": true,
"noImplicitOverride": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"useUnknownInCatchVariables": true,
"noUnusedLocals": true,
"noUnusedParameters": true
},
"include": ["src"]
}