@dan-uni/dan-any 0.0.7 → 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.
@@ -142,7 +142,8 @@ export interface UniDMObj {
142
142
  attr: DMAttr[];
143
143
  platform: platfrom | string;
144
144
  SPMO: string;
145
- extra: string;
145
+ extra: string | Extra;
146
+ extraStr: string;
146
147
  DMID: string;
147
148
  }
148
149
  /**
@@ -154,7 +155,7 @@ export declare class UniDM {
154
155
  */
155
156
  FCID: string;
156
157
  /**
157
- * 弹幕出现位置(单位ms)
158
+ * 弹幕出现位置(单位s;精度为ms,即保留三位小数)
158
159
  */
159
160
  progress: number;
160
161
  /**
@@ -236,7 +237,7 @@ export declare class UniDM {
236
237
  */
237
238
  FCID: string,
238
239
  /**
239
- * 弹幕出现位置(单位ms)
240
+ * 弹幕出现位置(单位s;精度为ms,即保留三位小数)
240
241
  */
241
242
  progress?: number,
242
243
  /**
@@ -308,7 +309,7 @@ export declare class UniDM {
308
309
  * - Artplayer: style不为空时,将其JSON.stringify()存入
309
310
  */
310
311
  extraStr?: string | undefined, DMID?: string | undefined);
311
- static create(args: Partial<UniDMObj>): UniDM;
312
+ static create(args?: Partial<UniDMObj>): UniDM;
312
313
  get extra(): Extra;
313
314
  get isFrom3rdPlatform(): boolean;
314
315
  /**
@@ -317,8 +318,16 @@ export declare class UniDM {
317
318
  * @description 同一FCID下唯一
318
319
  */
319
320
  toDMID(): string;
321
+ minify(): Partial<UniDMObj> & Pick<UniDMObj, "FCID">;
320
322
  downgradeAdvcancedDan(): this | undefined;
321
- 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;
322
331
  static transMode(oriMode: number, fmt: 'bili' | 'dplayer' | 'artplayer' | 'ddplay'): Modes;
323
332
  static fromBili(args: DMBili, SPMO?: string, cid?: bigint): UniDM;
324
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.7",
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
@@ -59,3 +59,10 @@ describe('共通值', () => {
59
59
  console.info(pool.split('pool'))
60
60
  })
61
61
  })
62
+
63
+ describe('其它', () => {
64
+ const pool = UniPool.fromBiliXML(xml)
65
+ it('最小化', () => {
66
+ console.info(pool.minify())
67
+ })
68
+ })
package/src/index.ts CHANGED
@@ -111,6 +111,9 @@ export class UniPool {
111
111
  return new UniPool(this.dans.filter((d) => d[key] === v))
112
112
  })
113
113
  }
114
+ minify() {
115
+ return this.dans.map((d) => d.minify())
116
+ }
114
117
  convert2(format: DM_format) {
115
118
  switch (format) {
116
119
  case 'danuni.json':
@@ -256,7 +256,8 @@ export interface UniDMObj {
256
256
  attr: DMAttr[]
257
257
  platform: platfrom | string
258
258
  SPMO: string
259
- extra: string
259
+ extra: string | Extra
260
+ extraStr: string
260
261
  DMID: string
261
262
  }
262
263
 
@@ -274,7 +275,7 @@ export class UniDM {
274
275
  */
275
276
  public FCID: string,
276
277
  /**
277
- * 弹幕出现位置(单位ms)
278
+ * 弹幕出现位置(单位s;精度为ms,即保留三位小数)
278
279
  */
279
280
  public progress: number = 0,
280
281
  /**
@@ -359,25 +360,31 @@ export class UniDM {
359
360
  if (pool < Pools.Def || pool > Pools.Ix) this.pool = Pools.Def
360
361
  // if (attr < 0 || attr > 0b111) this.attr = 0
361
362
  if (!DMID) DMID = this.toDMID()
363
+
364
+ this.progress = Number.parseFloat(progress.toFixed(3))
362
365
  }
363
- static create(args: Partial<UniDMObj>) {
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.7 (2025-01-26)
2
-
3
-
4
- ### Features
5
-
6
- * **dan-any:** add `.split(key)` method to split UniPool by shared value ([2568c76](https://github.com/ani-uni/danuni/commit/2568c7645f95c6b564bf22cfef2ca1a05d96f8ba))
7
-
8
-
9
-