@dan-uni/dan-any 0.0.5 → 0.1.0

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,24 @@ 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 | Extra;
146
+ extraStr: string;
147
+ DMID: string;
148
+ }
131
149
  /**
132
150
  * 所有 number/bigint 值设为0自动转换为默认
133
151
  */
@@ -137,7 +155,7 @@ export declare class UniDM {
137
155
  */
138
156
  FCID: string;
139
157
  /**
140
- * 弹幕出现位置(单位ms)
158
+ * 弹幕出现位置(单位s;精度为ms,即保留三位小数)
141
159
  */
142
160
  progress: number;
143
161
  /**
@@ -219,7 +237,7 @@ export declare class UniDM {
219
237
  */
220
238
  FCID: string,
221
239
  /**
222
- * 弹幕出现位置(单位ms)
240
+ * 弹幕出现位置(单位s;精度为ms,即保留三位小数)
223
241
  */
224
242
  progress?: number,
225
243
  /**
@@ -291,23 +309,7 @@ export declare class UniDM {
291
309
  * - Artplayer: style不为空时,将其JSON.stringify()存入
292
310
  */
293
311
  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;
312
+ static create(args?: Partial<UniDMObj>): UniDM;
311
313
  get extra(): Extra;
312
314
  get isFrom3rdPlatform(): boolean;
313
315
  /**
@@ -316,8 +318,16 @@ export declare class UniDM {
316
318
  * @description 同一FCID下唯一
317
319
  */
318
320
  toDMID(): string;
321
+ minify(): Partial<UniDMObj> & Pick<UniDMObj, "FCID">;
319
322
  downgradeAdvcancedDan(): this | undefined;
320
- static transCtime(oriCtime: ctime): Date;
323
+ /**
324
+ * 将各种类型的时间进行格式化
325
+ * @param oriCtime
326
+ * @param tsUnit 当`oriCtime`为数字类型表时间戳时的单位;
327
+ * 为 毫秒(ms)/秒(s)
328
+ * @returns {Date}
329
+ */
330
+ static transCtime(oriCtime: ctime, tsUnit?: 'ms' | 's'): Date;
321
331
  static transMode(oriMode: number, fmt: 'bili' | 'dplayer' | 'artplayer' | 'ddplay'): Modes;
322
332
  static fromBili(args: DMBili, SPMO?: string, cid?: bigint): UniDM;
323
333
  static fromBiliCommand(args: DMBiliCommand, SPMO?: string, cid?: bigint): UniDM;
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.1.0",
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,20 @@ 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
+ })
62
+
63
+ describe('其它', () => {
64
+ const pool = UniPool.fromBiliXML(xml)
65
+ it('最小化', () => {
66
+ console.info(pool.minify())
67
+ })
68
+ })
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,16 @@ 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
+ }
114
+ minify() {
115
+ return this.dans.map((d) => d.minify())
116
+ }
89
117
  convert2(format: DM_format) {
90
118
  switch (format) {
91
119
  case 'danuni.json':
@@ -242,6 +242,25 @@ 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 | Extra
260
+ extraStr: string
261
+ DMID: string
262
+ }
263
+
245
264
  /**
246
265
  * 所有 number/bigint 值设为0自动转换为默认
247
266
  */
@@ -256,7 +275,7 @@ export class UniDM {
256
275
  */
257
276
  public FCID: string,
258
277
  /**
259
- * 弹幕出现位置(单位ms)
278
+ * 弹幕出现位置(单位s;精度为ms,即保留三位小数)
260
279
  */
261
280
  public progress: number = 0,
262
281
  /**
@@ -341,43 +360,31 @@ export class UniDM {
341
360
  if (pool < Pools.Def || pool > Pools.Ix) this.pool = Pools.Def
342
361
  // if (attr < 0 || attr > 0b111) this.attr = 0
343
362
  if (!DMID) DMID = this.toDMID()
363
+
364
+ this.progress = Number.parseFloat(progress.toFixed(3))
344
365
  }
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
- ) {
364
- return new UniDM(
365
- args.FCID || ID.fromNull().toString(),
366
- args.progress,
367
- args.mode,
368
- args.fontsize,
369
- args.color,
370
- args.senderID,
371
- args.content,
372
- args.ctime,
373
- args.weight,
374
- args.pool,
375
- args.attr,
376
- args.platform,
377
- args.SPMO,
378
- args.extra,
379
- args.DMID,
380
- )
366
+ static create(args?: Partial<UniDMObj>) {
367
+ return args
368
+ ? new UniDM(
369
+ args.FCID || ID.fromNull().toString(),
370
+ args.progress,
371
+ args.mode,
372
+ args.fontsize,
373
+ args.color,
374
+ args.senderID,
375
+ args.content,
376
+ args.ctime,
377
+ args.weight,
378
+ args.pool,
379
+ args.attr,
380
+ args.platform,
381
+ args.SPMO,
382
+ typeof args.extra === 'object'
383
+ ? JSON.stringify(args.extra)
384
+ : args.extra || args.extraStr,
385
+ args.DMID,
386
+ )
387
+ : new UniDM(ID.fromNull().toString())
381
388
  }
382
389
  get extra() {
383
390
  const extra = JSON.parse(this.extraStr || '{}')
@@ -396,15 +403,47 @@ export class UniDM {
396
403
  toDMID() {
397
404
  return createDMID(this.content, this.senderID, this.ctime)
398
405
  }
406
+ minify() {
407
+ type UObj = Partial<UniDMObj> & Pick<UniDMObj, 'FCID'>
408
+ const def: UObj = UniDM.create(),
409
+ dan: UObj = UniDM.create(this)
410
+ // const prototypes = Object.getOwnPropertyNames(this)
411
+ for (const key in dan) {
412
+ const k = key as keyof UObj,
413
+ v = dan[k]
414
+ // if (key in prototypes) continue
415
+ if (key === 'FCID') continue
416
+ else if (!v) delete dan[k]
417
+ else if (v === def[k]) delete dan[k]
418
+ else {
419
+ if (k === 'attr' && Array.isArray(v) && v.length === 0) delete dan[k]
420
+ if (k === 'extraStr' && v === '{}') delete dan[k]
421
+ }
422
+ }
423
+ return JSON.parse(JSON.stringify(dan)) as UObj
424
+ }
399
425
  downgradeAdvcancedDan() {
400
426
  if (!this.extra) return this
401
427
  else {
402
428
  // TODO 分别对 mode7/8/9 command artplayer等正常播放器无法绘制的弹幕做降级处理
403
429
  }
404
430
  }
405
- static transCtime(oriCtime: ctime) {
406
- if (typeof oriCtime === 'number') return new Date(oriCtime)
407
- else if (typeof oriCtime === 'bigint') return new Date(Number(oriCtime))
431
+ /**
432
+ * 将各种类型的时间进行格式化
433
+ * @param oriCtime
434
+ * @param tsUnit 当`oriCtime`为数字类型表时间戳时的单位;
435
+ * 为 毫秒(ms)/秒(s)
436
+ * @returns {Date}
437
+ */
438
+ static transCtime(oriCtime: ctime, tsUnit?: 'ms' | 's'): Date {
439
+ function isMsTs(ts: number | bigint) {
440
+ if (tsUnit === 'ms') return true
441
+ else if (tsUnit === 's') return false
442
+ else return ts < 100000000
443
+ }
444
+ if (typeof oriCtime === 'number' || typeof oriCtime === 'bigint')
445
+ if (isMsTs(oriCtime)) return new Date(Number(oriCtime))
446
+ else return new Date(Number(oriCtime) * 1000)
408
447
  else if (typeof oriCtime === 'string') {
409
448
  if (/^\d+n$/.test(oriCtime))
410
449
  return new Date(Number(oriCtime.slice(0, -1)))
@@ -522,7 +561,7 @@ export class UniDM {
522
561
  // color: args.color,
523
562
  senderID: senderID.toString(),
524
563
  // content: args.content,
525
- ctime: this.transCtime(args.ctime),
564
+ ctime: this.transCtime(args.ctime, 's'),
526
565
  weight: args.weight ? args.weight : pool === Pools.Ix ? 1 : 0,
527
566
  pool,
528
567
  attr: DMAttrUtils.fromBin(args.attr, 'bili'),
package/CHANGELOG.md DELETED
@@ -1,9 +0,0 @@
1
- ## 0.0.5 (2025-01-23)
2
-
3
-
4
- ### Features
5
-
6
- * **dan-any:** add `.assign()` method to help merge dans ([d7c7a86](https://github.com/ani-uni/danuni/commit/d7c7a86dd6363051083cadae2ef775fcd0c93275))
7
-
8
-
9
-