@hlw-zbzs/gift 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.ts ADDED
@@ -0,0 +1,7 @@
1
+ /**
2
+ * @file @hlw-zbzs/gift - 多平台直播礼物数据与查询工具包
3
+ */
4
+
5
+ export type * from './types'
6
+ export * from './src/platforms'
7
+ export * from './src/core'
package/package.json ADDED
@@ -0,0 +1,64 @@
1
+ {
2
+ "name": "@hlw-zbzs/gift",
3
+ "version": "1.0.1",
4
+ "description": "Multi-Platform Live Stream Gift Data and Utilities (Douyin, Kuaishou, WeChat Channels, Bilibili, TikTok)",
5
+ "main": "index.ts",
6
+ "types": "index.ts",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./index.ts",
10
+ "import": "./index.ts",
11
+ "require": "./index.ts",
12
+ "default": "./index.ts"
13
+ },
14
+ "./dy": {
15
+ "types": "./src/platforms/dy.ts",
16
+ "import": "./src/platforms/dy.ts",
17
+ "require": "./src/platforms/dy.ts",
18
+ "default": "./src/platforms/dy.ts"
19
+ },
20
+ "./ks": {
21
+ "types": "./src/platforms/ks.ts",
22
+ "import": "./src/platforms/ks.ts",
23
+ "require": "./src/platforms/ks.ts",
24
+ "default": "./src/platforms/ks.ts"
25
+ },
26
+ "./sph": {
27
+ "types": "./src/platforms/sph.ts",
28
+ "import": "./src/platforms/sph.ts",
29
+ "require": "./src/platforms/sph.ts",
30
+ "default": "./src/platforms/sph.ts"
31
+ },
32
+ "./bili": {
33
+ "types": "./src/platforms/bili.ts",
34
+ "import": "./src/platforms/bili.ts",
35
+ "require": "./src/platforms/bili.ts",
36
+ "default": "./src/platforms/bili.ts"
37
+ },
38
+ "./tk": {
39
+ "types": "./src/platforms/tk.ts",
40
+ "import": "./src/platforms/tk.ts",
41
+ "require": "./src/platforms/tk.ts",
42
+ "default": "./src/platforms/tk.ts"
43
+ },
44
+ "./*": {
45
+ "types": "./src/*.ts",
46
+ "import": "./src/*.ts",
47
+ "require": "./src/*.ts",
48
+ "default": "./src/*.ts"
49
+ }
50
+ },
51
+ "files": [
52
+ "index.ts",
53
+ "src/"
54
+ ],
55
+ "dependencies": {},
56
+ "devDependencies": {
57
+ "@types/node": "^22.14.1",
58
+ "typescript": "^5.8.3"
59
+ },
60
+ "publishConfig": {
61
+ "access": "public"
62
+ },
63
+ "license": "MIT"
64
+ }
package/src/core.ts ADDED
@@ -0,0 +1,93 @@
1
+ /**
2
+ * @file 礼物数据管理与查询工具模块
3
+ */
4
+
5
+ import type { PlatformType, GiftItem } from './types'
6
+ import { PLATFORM_GIFTS } from './platforms'
7
+
8
+ const giftMap = new Map<PlatformType, Map<string | number, GiftItem>>()
9
+ const nameMap = new Map<PlatformType, Map<string, GiftItem>>()
10
+
11
+ function initStore() {
12
+ for (const [platform, gifts] of Object.entries(PLATFORM_GIFTS)) {
13
+ const pKey = platform as PlatformType
14
+ const idLookup = new Map<string | number, GiftItem>()
15
+ const nameLookup = new Map<string, GiftItem>()
16
+
17
+ for (const gift of gifts) {
18
+ idLookup.set(gift.id, gift)
19
+ idLookup.set(String(gift.id), gift)
20
+ nameLookup.set(gift.name, gift)
21
+ }
22
+
23
+ giftMap.set(pKey, idLookup)
24
+ nameMap.set(pKey, nameLookup)
25
+ }
26
+ }
27
+
28
+ initStore()
29
+
30
+ /**
31
+ * 获取指定平台的礼物列表
32
+ */
33
+ export function getGifts(platform: PlatformType = 'dy'): GiftItem[] {
34
+ return PLATFORM_GIFTS[platform] || []
35
+ }
36
+
37
+ /**
38
+ * 根据 ID 查询礼物
39
+ */
40
+ export function getById(id: number | string, platform: PlatformType = 'dy'): GiftItem | undefined {
41
+ const map = giftMap.get(platform)
42
+ return map?.get(id) || map?.get(String(id))
43
+ }
44
+
45
+ /**
46
+ * 根据礼物名称查询礼物
47
+ */
48
+ export function getByName(name: string, platform: PlatformType = 'dy'): GiftItem | undefined {
49
+ const map = nameMap.get(platform)
50
+ return map?.get(name)
51
+ }
52
+
53
+ /**
54
+ * 智能查找礼物 (优先ID,其次名称)
55
+ */
56
+ export function findGift(query: number | string, platform: PlatformType = 'dy'): GiftItem | undefined {
57
+ if (query === undefined || query === null || query === '') return undefined
58
+ const map = giftMap.get(platform)
59
+ const byId = map?.get(query) || map?.get(String(query))
60
+ if (byId) return byId
61
+ if (typeof query === 'string') {
62
+ return nameMap.get(platform)?.get(query)
63
+ }
64
+ return undefined
65
+ }
66
+
67
+ /**
68
+ * 关键字搜索礼物列表
69
+ */
70
+ export function searchGifts(keyword: string, platform: PlatformType = 'dy'): GiftItem[] {
71
+ if (!keyword) return getGifts(platform)
72
+ const kw = keyword.toLowerCase().trim()
73
+ const gifts = getGifts(platform)
74
+ return gifts.filter((item) => item.name.toLowerCase().includes(kw))
75
+ }
76
+
77
+ /**
78
+ * 动态注册或覆盖平台礼物数据
79
+ */
80
+ export function setGifts(platform: PlatformType, gifts: GiftItem[]): void {
81
+ PLATFORM_GIFTS[platform] = gifts
82
+ const idLookup = new Map<string | number, GiftItem>()
83
+ const nameLookup = new Map<string, GiftItem>()
84
+
85
+ for (const gift of gifts) {
86
+ idLookup.set(gift.id, gift)
87
+ idLookup.set(String(gift.id), gift)
88
+ nameLookup.set(gift.name, gift)
89
+ }
90
+
91
+ giftMap.set(platform, idLookup)
92
+ nameMap.set(platform, nameLookup)
93
+ }
@@ -0,0 +1,62 @@
1
+ [
2
+ {
3
+ "id": 31036,
4
+ "name": "小花花",
5
+ "price": 1,
6
+ "image": "https://s1.hdslb.com/bfs/live/8b40d0470890e7d573995383af8a8ae074d485d9.png"
7
+ },
8
+ {
9
+ "id": 31164,
10
+ "name": "粉丝团灯牌",
11
+ "price": 1,
12
+ "image": "https://s1.hdslb.com/bfs/live/e051dfd4557678f8edcac4993ed00a0935cbd9cc.png"
13
+ },
14
+ {
15
+ "id": 1,
16
+ "name": "辣条",
17
+ "price": 1,
18
+ "image": "https://s1.hdslb.com/bfs/live/8b40d0470890e7d573995383af8a8ae074d485d9.png"
19
+ },
20
+ {
21
+ "id": 34315,
22
+ "name": "喜欢你",
23
+ "price": 99,
24
+ "image": "https://s1.hdslb.com/bfs/live/5d940270c22a17fa2ff17a0713b4383b6febd945.png"
25
+ },
26
+ {
27
+ "id": 31122,
28
+ "name": "星愿水晶球",
29
+ "price": 1000,
30
+ "image": "https://s1.hdslb.com/bfs/live/288536798081e855e8f645bed6a2d2d27f411ee5.png"
31
+ },
32
+ {
33
+ "id": 10003,
34
+ "name": "舰长",
35
+ "price": 1980,
36
+ "image": "https://i0.hdslb.com/bfs/activity-plat/static/20220125/7f8dc1e417a6c1d6a509a66770dc060e/ohMHzbhJdN.png"
37
+ },
38
+ {
39
+ "id": 35566,
40
+ "name": "落日飞车",
41
+ "price": 2000,
42
+ "image": "https://s1.hdslb.com/bfs/live/93939d794de3e967b8564dc46487097124a192a0.png"
43
+ },
44
+ {
45
+ "id": 34998,
46
+ "name": "小电视飞船",
47
+ "price": 29990,
48
+ "image": "https://s1.hdslb.com/bfs/live/48884b1b1d3bf9df185f43ff6651748040faf5de.png"
49
+ },
50
+ {
51
+ "id": 10002,
52
+ "name": "提督",
53
+ "price": 19980,
54
+ "image": "https://i0.hdslb.com/bfs/activity-plat/static/20220125/7f8dc1e417a6c1d6a509a66770dc060e/ohMHzbhJdN.png"
55
+ },
56
+ {
57
+ "id": 10001,
58
+ "name": "总督",
59
+ "price": 199980,
60
+ "image": "https://i0.hdslb.com/bfs/activity-plat/static/20220125/7f8dc1e417a6c1d6a509a66770dc060e/ohMHzbhJdN.png"
61
+ }
62
+ ]