@dan-uni/dan-any 0.0.5 → 0.0.7

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.
@@ -128,6 +128,23 @@ export declare enum Pools {
128
128
  Ix = 3
129
129
  }
130
130
  export type ctime = string | number | bigint | Date;
131
+ export interface UniDMObj {
132
+ FCID: string;
133
+ progress: number;
134
+ mode: Modes;
135
+ fontsize: number;
136
+ color: number;
137
+ senderID: string;
138
+ content: string;
139
+ ctime: Date;
140
+ weight: number;
141
+ pool: Pools;
142
+ attr: DMAttr[];
143
+ platform: platfrom | string;
144
+ SPMO: string;
145
+ extra: string;
146
+ DMID: string;
147
+ }
131
148
  /**
132
149
  * 所有 number/bigint 值设为0自动转换为默认
133
150
  */
@@ -291,23 +308,7 @@ export declare class UniDM {
291
308
  * - Artplayer: style不为空时,将其JSON.stringify()存入
292
309
  */
293
310
  extraStr?: string | undefined, DMID?: string | undefined);
294
- static create(args: Partial<{
295
- FCID: string;
296
- progress: number;
297
- mode: Modes;
298
- fontsize: number;
299
- color: number;
300
- senderID: string;
301
- content: string;
302
- ctime: Date;
303
- weight: number;
304
- pool: Pools;
305
- attr: DMAttr[];
306
- platform: platfrom | string;
307
- SPMO: string;
308
- extra: string;
309
- DMID: string;
310
- }>): UniDM;
311
+ static create(args: Partial<UniDMObj>): UniDM;
311
312
  get extra(): Extra;
312
313
  get isFrom3rdPlatform(): boolean;
313
314
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dan-uni/dan-any",
3
- "version": "0.0.5",
3
+ "version": "0.0.7",
4
4
  "description": "A danmaku transformer lib, supporting danmaku from different platforms.",
5
5
  "keywords": [
6
6
  "bangumi",
package/src/index.test.ts CHANGED
@@ -3,9 +3,7 @@ import { describe, it } from 'vitest'
3
3
 
4
4
  import { UniPool } from './index'
5
5
 
6
- describe('转化自', () => {
7
- it('bili(xml)', () => {
8
- const xml = `<i>
6
+ const xml = `<i>
9
7
  <chatserver>chat.bilibili.com</chatserver>
10
8
  <chatid>1156756312</chatid>
11
9
  <mission>0</mission>
@@ -28,8 +26,10 @@ describe('转化自', () => {
28
26
  <d p="13.462,1,25,16777215,1686302133,0,3cab672c,1335558106620590080">哈哈哈</d>
29
27
  <d p="13.481,1,25,16777215,1686297342,0,ce67fafd,1335517923728804864">?</d>
30
28
  <d p="13.499,1,25,16777215,1686301548,3,2848bf1c,1335553202649003264">不喜欢</d>
31
- </i>`,
32
- pool = UniPool.fromBiliXML(xml)
29
+ </i>`
30
+ describe('转化自', () => {
31
+ it('bili(xml)', () => {
32
+ const pool = UniPool.fromBiliXML(xml)
33
33
  console.info(xml)
34
34
  console.info(pool)
35
35
  })
@@ -49,3 +49,13 @@ describe('转化自', () => {
49
49
  console.info(pool)
50
50
  })
51
51
  })
52
+
53
+ describe('共通值', () => {
54
+ const pool = UniPool.fromBiliXML(xml)
55
+ it('获取shared', () => {
56
+ console.info(pool.shared)
57
+ })
58
+ it('按pool分组', () => {
59
+ console.info(pool.split('pool'))
60
+ })
61
+ })
package/src/index.ts CHANGED
@@ -72,8 +72,26 @@ export type DM_format =
72
72
  | 'artplayer.json'
73
73
  | 'ddplay.json'
74
74
 
75
+ type shareItems = Partial<
76
+ Pick<
77
+ UniDMTools.UniDMObj,
78
+ 'FCID' | 'senderID' | 'platform' | 'SPMO' | 'pool' | 'mode'
79
+ >
80
+ >
81
+
75
82
  export class UniPool {
76
- constructor(public dans: UniDM[]) {}
83
+ readonly shared: shareItems = {}
84
+ constructor(public dans: UniDM[]) {
85
+ function isShared(key: keyof UniDMTools.UniDMObj) {
86
+ return new Set(dans.map((d) => d[key])).size === 1
87
+ }
88
+ if (isShared('FCID')) this.shared.FCID = dans[0].FCID
89
+ if (isShared('senderID')) this.shared.senderID = dans[0].senderID
90
+ if (isShared('platform')) this.shared.platform = dans[0].platform
91
+ if (isShared('SPMO')) this.shared.SPMO = dans[0].SPMO
92
+ if (isShared('pool')) this.shared.pool = dans[0].pool
93
+ if (isShared('mode')) this.shared.mode = dans[0].mode
94
+ }
77
95
  static create() {
78
96
  return new UniPool([])
79
97
  }
@@ -86,6 +104,13 @@ export class UniPool {
86
104
  return new UniPool([...this.dans, ...dans])
87
105
  } else return this
88
106
  }
107
+ split(key: keyof shareItems) {
108
+ if (this.shared[key]) return [this]
109
+ const set = new Set(this.dans.map((d) => d[key]))
110
+ return [...set].map((v) => {
111
+ return new UniPool(this.dans.filter((d) => d[key] === v))
112
+ })
113
+ }
89
114
  convert2(format: DM_format) {
90
115
  switch (format) {
91
116
  case 'danuni.json':
@@ -242,6 +242,24 @@ export type ctime = string | number | bigint | Date
242
242
  // dateStr = 'dateStr',
243
243
  // }
244
244
 
245
+ export interface UniDMObj {
246
+ FCID: string
247
+ progress: number
248
+ mode: Modes
249
+ fontsize: number
250
+ color: number
251
+ senderID: string
252
+ content: string
253
+ ctime: Date
254
+ weight: number
255
+ pool: Pools
256
+ attr: DMAttr[]
257
+ platform: platfrom | string
258
+ SPMO: string
259
+ extra: string
260
+ DMID: string
261
+ }
262
+
245
263
  /**
246
264
  * 所有 number/bigint 值设为0自动转换为默认
247
265
  */
@@ -342,25 +360,7 @@ export class UniDM {
342
360
  // if (attr < 0 || attr > 0b111) this.attr = 0
343
361
  if (!DMID) DMID = this.toDMID()
344
362
  }
345
- static create(
346
- args: Partial<{
347
- FCID: string
348
- progress: number
349
- mode: Modes
350
- fontsize: number
351
- color: number
352
- senderID: string
353
- content: string
354
- ctime: Date
355
- weight: number
356
- pool: Pools
357
- attr: DMAttr[]
358
- platform: platfrom | string
359
- SPMO: string
360
- extra: string
361
- DMID: string
362
- }>,
363
- ) {
363
+ static create(args: Partial<UniDMObj>) {
364
364
  return new UniDM(
365
365
  args.FCID || ID.fromNull().toString(),
366
366
  args.progress,