Skip to content

Commit dfa6570

Browse files
authored
Rename @cached to @memoized (#4)
1 parent e629833 commit dfa6570

File tree

3 files changed

+35
-32
lines changed

3 files changed

+35
-32
lines changed

src/__tests__/helper.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { describe, it, expect, vi } from 'vitest';
22

3-
import { cached } from '../utils/helper';
3+
import { memoized } from '../utils/helper';
44

5-
describe('cached decorator', () => {
5+
describe('memoized decorator', () => {
66
it('should evaluate the getter only once per instance', () => {
77
const compute = vi.fn(() => 42);
88

99
class Test {
10-
@cached
10+
@memoized
1111
get value() {
1212
return compute();
1313
}
@@ -17,17 +17,17 @@ describe('cached decorator', () => {
1717

1818
// first access: compute
1919
expect(a.value).toBe(42);
20-
// second access: cached
20+
// second access: memoized
2121
expect(a.value).toBe(42);
2222

2323
expect(compute).toHaveBeenCalledTimes(1);
2424
});
2525

26-
it('should cache per instance, not shared across instances', () => {
26+
it('should memoize per instance, not shared across instances', () => {
2727
let callCount = 0;
2828

2929
class Test {
30-
@cached
30+
@memoized
3131
get value() {
3232
callCount++;
3333
return callCount;
@@ -42,7 +42,7 @@ describe('cached decorator', () => {
4242
const b1 = b.value;
4343

4444
expect(a1).toBe(1);
45-
expect(a2).toBe(1); // cached on same instance
45+
expect(a2).toBe(1); // memoized on same instance
4646
expect(b1).toBe(2); // separate instance → separate evaluation
4747
expect(callCount).toBe(2);
4848
});
@@ -51,7 +51,7 @@ describe('cached decorator', () => {
5151
class Test {
5252
public _count = 0;
5353

54-
@cached
54+
@memoized
5555
get value() {
5656
this._count++;
5757
return this._count;

src/core/韻鏡位置.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { defaultLogger, 音韻地位 } from 'tshet-uinh';
22

33
import { 母位置名稱, 轉名稱列表, 轉呼 } from '../utils/常量';
4-
import { cached } from '../utils/helper';
4+
import { memoized } from '../utils/helper';
55

66
// import { 等韻搭配, 鈍音母 } from './音韻屬性常量';
77
/** 依可搭配的等列出各韻 */
@@ -57,27 +57,27 @@ class 韻鏡位置 {
5757

5858
/**
5959
* 轉的名稱(如「內轉第一圖」)。
60-
*/
61-
@cached
60+
*/
61+
@memoized
6262
get 轉名稱() {
6363
return `${轉名稱列表[this.轉號 - 1]}圖`;
6464
}
6565

6666
/**
6767
* 坐標的字串表示(如「(1,1,1)」)。
68-
*/
69-
@cached
68+
*/
69+
@memoized
7070
get 坐標() {
7171
const { 轉號, 上位, 右位 } = this;
7272
return `(${轉號},${上位},${右位})`;
7373
}
7474

7575
/**
7676
* 韻鏡等(「一二三四」)。
77-
*
78-
* 僅由上位決定,與切韻等未必一致。
79-
*/
80-
@cached
77+
*
78+
* 僅由上位決定,與切韻等未必一致。
79+
*/
80+
@memoized
8181
get 韻鏡等() {
8282
const { 上位 } = this;
8383
const 韻鏡等 = ((上位 - 1) % 4) + 1;
@@ -91,7 +91,7 @@ class 韻鏡位置 {
9191
*
9292
* 與《韻鏡》標註的韻亦未必一致,因為存在一些特殊情況,例如《韻鏡》原書存在刪、山韻排反的情況,此處均已進行修正。
9393
*/
94-
@cached
94+
@memoized
9595
get () {
9696
const { 轉號, 上位, 右位 } = this;
9797
return 轉號上位右位2韻(轉號, 上位, 右位);
@@ -100,7 +100,7 @@ class 韻鏡位置 {
100100
/**
101101
* 切韻等(「一二三四」)。
102102
*/
103-
@cached
103+
@memoized
104104
get 切韻等() {
105105
const { 轉號, 上位, 右位, 韻鏡等,} = this;
106106

@@ -137,7 +137,7 @@ class 韻鏡位置 {
137137
/**
138138
* 韻鏡母。
139139
*/
140-
@cached
140+
@memoized
141141
get () {
142142
const { 右位, 韻鏡等, 切韻等 } = this;
143143

@@ -223,7 +223,7 @@ class 韻鏡位置 {
223223
*
224224
* 與《韻鏡》標註的呼未必一致,例如包含 `null`,又如《韻鏡》原書第四轉標註為「內轉第四開合」,而此處為「開」。
225225
*/
226-
@cached
226+
@memoized
227227
get () {
228228
const { 轉號, 轉名稱,,} = this;
229229
if ([...'幫滂並明'].includes() || [...'模侯尤'].includes()) {
@@ -241,7 +241,7 @@ class 韻鏡位置 {
241241
*
242242
* 由上位推得,並根據「去聲寄此」的韻鏡標註修正。
243243
*/
244-
@cached
244+
@memoized
245245
get () {
246246
const { 轉號, 上位 } = this;
247247
const raw聲 = [...'平上去入'][Math.floor((上位 - 1) / 4)];
@@ -256,7 +256,7 @@ class 韻鏡位置 {
256256
/**
257257
* 切韻類(A/B/C/`null`)。
258258
*/
259-
@cached
259+
@memoized
260260
get () {
261261
const { 韻鏡等, 切韻等,,} = this;
262262
if (切韻等 !== '三' || !鈍音母.includes()) {
@@ -298,7 +298,7 @@ class 韻鏡位置 {
298298
/**
299299
* 韻鏡位置描述(如「外轉第四十二圖·舌音第一位·入聲位·韻鏡一等」)。
300300
*/
301-
@cached
301+
@memoized
302302
get 描述() {
303303
const { 上位, 右位, 轉名稱, 韻鏡等 } = this;
304304
const raw聲 = [...'平上去入'][Math.floor((上位 - 1) / 4)];

src/utils/helper.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,32 @@
11
type AnyGetter<T, R> = (this: T) => R;
22

33
/**
4-
* Cache a getter result per-instance.
5-
* - Stores cache in a non-enumerable, per-decorator Symbol to avoid collisions.
4+
* Memoize a getter result per instance.
5+
* - Stores the memoized value in a non-enumerable, per-decorator Symbol to avoid collisions.
66
* - Works with TS 5.x / new decorators (ClassGetterDecoratorContext).
77
* - Pure getters only: if underlying state changes, you must provide invalidation yourself.
88
*/
9-
export function cached<T extends object, R>(originalGetter: AnyGetter<T, R>, context: ClassGetterDecoratorContext<T, R>): AnyGetter<T, R> {
9+
export function memoized<T extends object, R>(
10+
originalGetter: AnyGetter<T, R>,
11+
context: ClassGetterDecoratorContext<T, R>,
12+
): AnyGetter<T, R> {
1013
if (context.kind !== 'getter') {
11-
throw new TypeError('@cached can only be applied to a getter.');
14+
throw new TypeError('@memoized can only be applied to a getter.');
1215
}
1316

1417
// One unique key per decorated getter (per class definition).
15-
const cacheKey = Symbol(`@cached:${String(context.name)}`);
18+
const memoKey = Symbol(`@memoized:${String(context.name)}`);
1619

1720
return function (this: T): R {
1821
// Use hasOwnProperty so prototype chain doesn't interfere.
19-
if (Object.prototype.hasOwnProperty.call(this, cacheKey)) {
20-
return (this as any)[cacheKey] as R;
22+
if (Object.prototype.hasOwnProperty.call(this, memoKey)) {
23+
return (this as any)[memoKey] as R;
2124
}
2225

2326
const value = originalGetter.call(this);
2427

2528
// Define as non-enumerable to avoid polluting iteration/serialization/debug output.
26-
Object.defineProperty(this, cacheKey, {
29+
Object.defineProperty(this, memoKey, {
2730
value,
2831
writable: true, // allow manual override if needed
2932
configurable: true, // allow manual deletion to invalidate

0 commit comments

Comments
 (0)