Skip to content

Commit

Permalink
add increaseCorrection time-range
Browse files Browse the repository at this point in the history
  • Loading branch information
downmix committed Jan 2, 2024
1 parent ff91f8f commit 1a1f422
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 5 deletions.
6 changes: 6 additions & 0 deletions src/number-util/number-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ export namespace NumberUtil {
if (isNaN(floatValue)) {
throw new Error(`unable to convert "${floatValue}" to number type`);
}
if (decimal === 0) {
return Math.round(floatValue);
}
const factor = Math.pow(10, decimal);
return Math.round(floatValue * factor);
};
Expand All @@ -50,6 +53,9 @@ export namespace NumberUtil {
if (isNaN(value)) {
throw new Error(`unable to convert "${value}" to number type`);
}
if (decimal === 0) {
return value;
}
return value / Math.pow(10, decimal);
};
}
47 changes: 44 additions & 3 deletions src/time-range/time-range.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ describe('TimeRange Util', () => {
});

it('decimal merge to one', async () => {
const timeRange = new TimeRange();
const timeRange = new TimeRange([], 1);
timeRange.add({
start: 1.1,
end: 11.1,
Expand All @@ -146,7 +146,7 @@ describe('TimeRange Util', () => {
});

it('cale decimal points in the totalPlayTime', async () => {
const timeRange1 = new TimeRange();
const timeRange1 = new TimeRange([], 1);
timeRange1.add({
start: 99.9,
end: 100.1,
Expand All @@ -155,7 +155,7 @@ describe('TimeRange Util', () => {
// native JS 100.1 - 99.9 = 0.19999999999998863
expect(timeRange1.totalPlayTime()).toEqual(0.2);

const timeRange2 = new TimeRange();
const timeRange2 = new TimeRange([], 5);
timeRange2.add({
start: 0,
end: 59.08332,
Expand Down Expand Up @@ -212,6 +212,47 @@ describe('TimeRange Util', () => {
});
timeRange5.merge();
expect(timeRange5.totalPlayTime()).toEqual(3600000000.99);

const timeRange6 = new TimeRange();
timeRange6.add({
start: 0,
end: 100,
interval: 100,
});
timeRange6.add({
start: 90,
end: 60 * 60 * 1000000 + 0.99, // 1,000,000 hours
interval: 60 * 60 * 1000000 + 0.99,
});
timeRange6.merge();
expect(timeRange6.totalPlayTime()).toEqual(3600000001);

const timeRange8 = new TimeRange();
timeRange8.add({
start: 0,
end: 59.08332,
interval: 59.08332,
});
timeRange8.add({
start: 227.26884,
end: 230.481,
interval: 6.7765600000000035,
});
expect(timeRange8.totalPlayTime()).toEqual(62);
});

it('increased correction totalPlayTime', async () => {
const timeRange1 = new TimeRange();
expect(timeRange1.increaseCorrection(100)).toEqual(100);

const timeRange2 = new TimeRange(); // 0.01%
expect(timeRange2.increaseCorrection(10000)).toEqual(10001);

const timeRange3 = new TimeRange([], 0, 0.001); // 0.1%
expect(timeRange3.increaseCorrection(20000)).toEqual(20020);

const timeRange4 = new TimeRange([], 0, 0.01); // 1%
expect(timeRange4.increaseCorrection(30000)).toEqual(30300);
});
});
});
Expand Down
14 changes: 12 additions & 2 deletions src/time-range/time-range.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import { NumberUtil } from '../number-util';
import { TimeSection } from './time-range.interface';

/**
* @params loadSection: TimeSection 배열
* @params decimalPlaces: TimeSection 계산시 소수점 자리수를 설정하여 반올림 처리 (default 0)
* @params correction: 모든 클립 playtime의 오차 보정 수치 (default 0.01%)
*/
export class TimeRange {
private section!: Array<TimeSection>;
readonly decimalPlaces: number;
constructor(loadSection: Array<TimeSection> = [], decimalPlaces = 5) {
decimalPlaces: number;
correction: number;
constructor(loadSection: Array<TimeSection> = [], decimalPlaces = 0, correction = 0.0001) {
this.section = loadSection;
this.decimalPlaces = decimalPlaces;
this.correction = correction;
}
add(piece: TimeSection) {
this.section.push(piece);
Expand Down Expand Up @@ -56,4 +63,7 @@ export class TimeRange {
}, 0);
return NumberUtil.decimalRoundDown(result, this.decimalPlaces);
}
increaseCorrection(allPlayTime: number) {
return allPlayTime + Math.round(allPlayTime * this.correction);
}
}

0 comments on commit 1a1f422

Please sign in to comment.