unitmaster 1.0.0 → 1.2.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.
package/README.md CHANGED
@@ -6,7 +6,7 @@ TypeScript 기반의 강력하고 유연한 단위 변환 라이브러리입니
6
6
  ## 설치 (Installation)
7
7
 
8
8
  ```bash
9
- npm install unitconvert
9
+ npm install unitmaster
10
10
  ```
11
11
 
12
12
  ## 주요 기능 (Features)
@@ -24,7 +24,7 @@ npm install unitconvert
24
24
  Byte, KB, MB, GB, TB, PB, EB, ZB, YB 등을 지원합니다. (1024 배수 기준)
25
25
 
26
26
  ```typescript
27
- import { ComputerCapacityConverter } from 'unitconvert';
27
+ import { ComputerCapacityConverter } from 'unitmaster';
28
28
 
29
29
  const converter = new ComputerCapacityConverter();
30
30
 
@@ -36,8 +36,8 @@ console.log(converter.convertTo(1024, 'Byte', 'KB')); // 1
36
36
  console.log(converter.convertTo(1, 'MB', 'Byte')); // 1048576
37
37
 
38
38
  // 2. 최적 단위 자동 포맷팅
39
- console.log(converter.formatBest(1048576)); // "1 MB"
40
- console.log(converter.formatBest(2048)); // "2 KB"
39
+ console.log(converter.formatBest(1048576)); // { value: 1, unit: 'MB' }
40
+ console.log(converter.formatBest(2048)); // { value: 2, unit: 'KB' }
41
41
  ```
42
42
 
43
43
  ### 2. 거리 변환 (Distance)
@@ -45,7 +45,7 @@ console.log(converter.formatBest(2048)); // "2 KB"
45
45
  mm, cm(10), m(100), km(1000) 단위를 지원합니다.
46
46
 
47
47
  ```typescript
48
- import { DistanceConverter } from 'unitconvert';
48
+ import { DistanceConverter } from 'unitmaster';
49
49
 
50
50
  const distConverter = new DistanceConverter();
51
51
 
@@ -53,7 +53,8 @@ const distConverter = new DistanceConverter();
53
53
  console.log(distConverter.convertTo(1, 'km', 'm')); // 1000
54
54
 
55
55
  // 100 cm -> 1 m
56
- console.log(distConverter.formatBest(100, undefined)); // "10 cm" 아니라 최적 단위인 "1 m"로 변환 (로직에 따라 다름)
56
+ // 100 cm -> 1 m
57
+ console.log(distConverter.formatBest(100, undefined)); // { value: 1, unit: 'm' }
57
58
  // formatBest는 값이 1 이상이 되는 가장 큰 단위를 찾습니다.
58
59
  ```
59
60
 
@@ -63,10 +64,10 @@ console.log(distConverter.formatBest(100, undefined)); // "10 cm" 가 아니라
63
64
  - **PowerUsageConverter**: Wh, kWh, MWh, GWh, TWh (1000 배수)
64
65
 
65
66
  ```typescript
66
- import { PowerCapacityConverter } from 'unitconvert';
67
+ import { PowerCapacityConverter } from 'unitmaster';
67
68
 
68
69
  const powerConverter = new PowerCapacityConverter();
69
- console.log(powerConverter.formatBest(1500)); // "1.5 kW" (기본 단위 W 기준)
70
+ console.log(powerConverter.formatBest(1500)); // { value: 1.5, unit: 'kW' } (기본 단위 W 기준)
70
71
  ```
71
72
 
72
73
  ## 고급 사용법 (Advanced)
@@ -86,7 +87,7 @@ converter.registerUnit([{ unit: 'BB', unitValue: 1024 }]);
86
87
  `UnitConverter`를 상속받아 새로운 도메인의 변환기를 만들 수 있습니다.
87
88
 
88
89
  ```typescript
89
- import { UnitConverter } from 'unitconvert';
90
+ import { UnitConverter } from 'unitmaster';
90
91
 
91
92
  class TimeConverter extends UnitConverter {
92
93
  constructor() {
@@ -101,7 +102,7 @@ class TimeConverter extends UnitConverter {
101
102
  }
102
103
 
103
104
  const timeConverter = new TimeConverter();
104
- console.log(timeConverter.formatBest(3600)); // "1 Hour"
105
+ console.log(timeConverter.formatBest(3600)); // { value: 1, unit: 'Hour' }
105
106
  ```
106
107
 
107
108
  ### 복합 입력값 처리
@@ -2,9 +2,9 @@
2
2
  * @file UnitConverter.ts
3
3
  * @author 김영찬
4
4
  * @since 2026-01-20
5
- * @description 단위 변환 기능 및 최적 단위 표시 기능을 제공하는 메인 클래스입니다.
5
+ * @description 단위 변환 기능 및 최적 단위 표시 기능을 제공하는 메인 클래스입니다. (상대적 배수 로직 적용)
6
6
  */
7
- import { UnitDefinition, ComplexValueInput } from './types';
7
+ import { UnitDefinition, ComplexValueInput, UnitOutput } from './types';
8
8
  /**
9
9
  * 단위 변환기 클래스
10
10
  */
@@ -12,18 +12,18 @@ export declare class UnitConverter {
12
12
  private units;
13
13
  /**
14
14
  * 생성자
15
- * @description 기본 컴퓨터 용량 단위(Byte, KB, MB, GB, TB, PB)를 초기화합니다.
15
+ * @description 단위 목록으로 초기화합니다.
16
16
  */
17
17
  constructor();
18
- /**
19
- * 기본 컴퓨터 용량 단위 등록
20
- */
21
- private registerDefaultUnits;
22
18
  /**
23
19
  * 단위 등록 (Rule 5)
24
- * @param units 단위 정의 목록
20
+ * @param units 단위 정의 목록 (기존 단위 목록 뒤에 추가됨)
25
21
  */
26
22
  registerUnit(units: UnitDefinition[]): void;
23
+ /**
24
+ * 단위 목록을 절대 값(Base 기준)으로 변환하여 반환
25
+ */
26
+ private getAbsoluteUnits;
27
27
  /**
28
28
  * 특정 단위로 값 변환 (Rule 1, Rule 4)
29
29
  * @param value 값
@@ -36,16 +36,12 @@ export declare class UnitConverter {
36
36
  * 입력 값을 받아 최적의 단위로 변환 (Rule 1, Rule 2, Rule 3)
37
37
  * @param input 복합 입력 값 ({value, unit[]}) 또는 단순 값
38
38
  * @param fixedUnits 사용할 단위 목록 (없으면 등록된 단위 사용)
39
- * @returns 변환된 문자열 (예: "100 MB")
40
- */
41
- formatBest(input: number, fixedUnits?: UnitDefinition[]): string;
42
- formatBest(input: ComplexValueInput): string;
43
- /**
44
- * 단위 정의 검색
39
+ * @returns 변환된 결과 객체 { value, unit }
45
40
  */
46
- private findUnit;
41
+ formatBest(input: number, fixedUnits?: UnitDefinition[]): UnitOutput;
42
+ formatBest(input: ComplexValueInput): UnitOutput;
47
43
  /**
48
- * 현재 등록된 단위 목록 반환
44
+ * 현재 등록된 단위 목록 반환 (원본, 상대적 값 포함)
49
45
  */
50
46
  getUnits(): UnitDefinition[];
51
47
  }
@@ -3,7 +3,7 @@
3
3
  * @file UnitConverter.ts
4
4
  * @author 김영찬
5
5
  * @since 2026-01-20
6
- * @description 단위 변환 기능 및 최적 단위 표시 기능을 제공하는 메인 클래스입니다.
6
+ * @description 단위 변환 기능 및 최적 단위 표시 기능을 제공하는 메인 클래스입니다. (상대적 배수 로직 적용)
7
7
  */
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
9
  exports.UnitConverter = void 0;
@@ -13,33 +13,37 @@ exports.UnitConverter = void 0;
13
13
  class UnitConverter {
14
14
  /**
15
15
  * 생성자
16
- * @description 기본 컴퓨터 용량 단위(Byte, KB, MB, GB, TB, PB)를 초기화합니다.
16
+ * @description 단위 목록으로 초기화합니다.
17
17
  */
18
18
  constructor() {
19
19
  this.units = [];
20
- this.registerDefaultUnits();
21
- }
22
- /**
23
- * 기본 컴퓨터 용량 단위 등록
24
- */
25
- registerDefaultUnits() {
26
- this.units = [
27
- { unit: 'Byte', unitValue: 1 },
28
- { unit: 'KB', unitValue: 1024 },
29
- { unit: 'MB', unitValue: 1024 * 1024 },
30
- { unit: 'GB', unitValue: 1024 * 1024 * 1024 },
31
- { unit: 'TB', unitValue: 1024 * 1024 * 1024 * 1024 },
32
- { unit: 'PB', unitValue: 1024 * 1024 * 1024 * 1024 * 1024 }
33
- ];
20
+ // 기본 단위 없음 (서브 클래스 또는 registerUnit으로 등록)
34
21
  }
35
22
  /**
36
23
  * 단위 등록 (Rule 5)
37
- * @param units 단위 정의 목록
24
+ * @param units 단위 정의 목록 (기존 단위 목록 뒤에 추가됨)
38
25
  */
39
26
  registerUnit(units) {
40
27
  this.units = [...this.units, ...units];
41
- // unitValue 기준으로 오름차순 정렬 (계산 편의성)
42
- this.units.sort((a, b) => a.unitValue - b.unitValue);
28
+ // 정렬하지 않음 (순서가 계산 로직에 중요함)
29
+ }
30
+ /**
31
+ * 단위 목록을 절대 값(Base 기준)으로 변환하여 반환
32
+ */
33
+ getAbsoluteUnits(units) {
34
+ let currentMultiplier = 1;
35
+ return units.map((u, index) => {
36
+ if (index === 0) {
37
+ currentMultiplier = u.unitValue;
38
+ }
39
+ else {
40
+ currentMultiplier *= u.unitValue;
41
+ }
42
+ return {
43
+ unit: u.unit,
44
+ unitValue: currentMultiplier
45
+ };
46
+ });
43
47
  }
44
48
  /**
45
49
  * 특정 단위로 값 변환 (Rule 1, Rule 4)
@@ -49,67 +53,54 @@ class UnitConverter {
49
53
  * @returns 변환된 값
50
54
  */
51
55
  convertTo(value, fromUnit, toUnit) {
52
- const fromDef = this.findUnit(fromUnit);
53
- const toDef = this.findUnit(toUnit);
56
+ const absoluteUnits = this.getAbsoluteUnits(this.units);
57
+ const fromDef = absoluteUnits.find(u => u.unit === fromUnit);
58
+ const toDef = absoluteUnits.find(u => u.unit === toUnit);
54
59
  if (!fromDef || !toDef) {
55
60
  throw new Error(`Unit not found: ${!fromDef ? fromUnit : toUnit}`);
56
61
  }
57
- // 기본 단위 값으로 변환 후 목표 단위로 변환
58
- // 예: 1 KB (1024) -> MB (1024*1024)
59
- // 1 * 1024 / (1024*1024) = 0.0009765625
60
62
  const baseValue = value * fromDef.unitValue;
61
63
  return baseValue / toDef.unitValue;
62
64
  }
63
65
  formatBest(input, fixedUnits) {
64
66
  let value;
65
- let unitDefs;
67
+ let originalUnitDefs;
66
68
  if (typeof input === 'object' && 'unit' in input) {
67
- // Rule 2 Case
68
- // 입력값은 기본 단위(unitValue:1 또는 가장 작은 단위) 기준이라고 가정하거나,
69
- // 아니면 입력 구조에 대한 명확한 정의가 필요함.
70
- // 여기서는 입력된 value가 제공된 unit[0] (혹은 unitValue=1)이 아니라,
71
- // 제공된 unit 리스트를 '사용할 단위 체계'로 보고,
72
- // value는 그 체계의 가장 작은 단위(혹은 기준 값)라고 가정함.
73
- // *주의*: 요구사항의 예시가 모호하므로, 값은 Base Unit 기준이라고 가정.
74
69
  value = input.value;
75
- unitDefs = input.unit;
76
- // 정렬
77
- unitDefs.sort((a, b) => a.unitValue - b.unitValue);
70
+ originalUnitDefs = input.unit;
78
71
  }
79
72
  else {
80
73
  value = input;
81
- unitDefs = fixedUnits || this.units;
74
+ originalUnitDefs = fixedUnits || this.units;
75
+ }
76
+ if (originalUnitDefs.length === 0) {
77
+ return { value, unit: '' };
82
78
  }
83
- if (unitDefs.length === 0)
84
- return `${value}`;
85
- // 가장 큰 단위부터 확인하여 값이 >= 1 인지 확인 (혹은 가장 적절한 단위)
86
- // 오름차순 정렬되어 있다고 가정
87
- // 예: 1000 Byte -> Byte (1000 >= 1)
88
- // 1024 Byte -> KB (1024 >= 1024) => 1 KB
89
- // 뒤(큰 단위)에서부터 탐색
90
- for (let i = unitDefs.length - 1; i >= 0; i--) {
91
- const unit = unitDefs[i];
92
- if (value >= unit.unitValue) {
79
+ // 절대 값으로 변환하여 계산
80
+ const absoluteUnits = this.getAbsoluteUnits(originalUnitDefs);
81
+ // 가장 큰 단위부터 확인
82
+ for (let i = absoluteUnits.length - 1; i >= 0; i--) {
83
+ const unit = absoluteUnits[i];
84
+ // 값이 해당 단위의 '단위 값'보다 크거나 같으면 사용
85
+ // 단, 절대값 기준
86
+ if (Math.abs(value) >= unit.unitValue) {
93
87
  const converted = value / unit.unitValue;
94
- // 정수로 떨어지거나, 소수점 2자리까지 표시
95
- const formatted = Number.isInteger(converted) ? converted : converted.toFixed(2);
96
- return `${formatted} ${unit.unit}`;
88
+ const formattedValue = Number.isInteger(converted)
89
+ ? converted
90
+ : parseFloat(converted.toFixed(2));
91
+ return { value: formattedValue, unit: unit.unit };
97
92
  }
98
93
  }
99
- // 가장 작은 단위보다도 작을 경우, 가장 작은 단위로 표시
100
- const smallest = unitDefs[0];
94
+ // 가장 작은 단위보다도 작을 경우 (가장 작은 단위로 표시)
95
+ const smallest = absoluteUnits[0];
101
96
  const converted = value / smallest.unitValue;
102
- const formatted = Number.isInteger(converted) ? converted : converted.toFixed(2);
103
- return `${formatted} ${smallest.unit}`;
104
- }
105
- /**
106
- * 단위 정의 검색
107
- */
108
- findUnit(unitName) {
109
- return this.units.find(u => u.unit === unitName);
97
+ const formattedValue = Number.isInteger(converted)
98
+ ? converted
99
+ : parseFloat(converted.toFixed(2));
100
+ return { value: formattedValue, unit: smallest.unit };
110
101
  }
111
102
  /**
112
- * 현재 등록된 단위 목록 반환
103
+ * 현재 등록된 단위 목록 반환 (원본, 상대적 값 포함)
113
104
  */
114
105
  getUnits() {
115
106
  return this.units;
@@ -0,0 +1,10 @@
1
+ /**
2
+ * @file ComputerCapacityConverter.ts
3
+ * @author 김영찬
4
+ * @since 2026-01-20
5
+ * @description 컴퓨터 용량 단위 변환기 구현체
6
+ */
7
+ import { UnitConverter } from '../UnitConverter';
8
+ export declare class ComputerCapacityConverter extends UnitConverter {
9
+ constructor();
10
+ }
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ /**
3
+ * @file ComputerCapacityConverter.ts
4
+ * @author 김영찬
5
+ * @since 2026-01-20
6
+ * @description 컴퓨터 용량 단위 변환기 구현체
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.ComputerCapacityConverter = void 0;
10
+ const UnitConverter_1 = require("../UnitConverter");
11
+ class ComputerCapacityConverter extends UnitConverter_1.UnitConverter {
12
+ constructor() {
13
+ super();
14
+ this.registerUnit([
15
+ { unit: 'Byte', unitValue: 1 },
16
+ { unit: 'KB', unitValue: 1024 },
17
+ { unit: 'MB', unitValue: 1024 },
18
+ { unit: 'GB', unitValue: 1024 },
19
+ { unit: 'TB', unitValue: 1024 },
20
+ { unit: 'PB', unitValue: 1024 },
21
+ { unit: 'EB', unitValue: 1024 },
22
+ { unit: 'ZB', unitValue: 1024 },
23
+ { unit: 'YB', unitValue: 1024 }
24
+ ]);
25
+ }
26
+ }
27
+ exports.ComputerCapacityConverter = ComputerCapacityConverter;
@@ -0,0 +1,10 @@
1
+ /**
2
+ * @file DistanceConverter.ts
3
+ * @author 김영찬
4
+ * @since 2026-01-20
5
+ * @description 거리(Distance) 단위 변환기 (mm, cm, m, km)
6
+ */
7
+ import { UnitConverter } from '../UnitConverter';
8
+ export declare class DistanceConverter extends UnitConverter {
9
+ constructor();
10
+ }
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ /**
3
+ * @file DistanceConverter.ts
4
+ * @author 김영찬
5
+ * @since 2026-01-20
6
+ * @description 거리(Distance) 단위 변환기 (mm, cm, m, km)
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.DistanceConverter = void 0;
10
+ const UnitConverter_1 = require("../UnitConverter");
11
+ class DistanceConverter extends UnitConverter_1.UnitConverter {
12
+ constructor() {
13
+ super();
14
+ this.registerUnit([
15
+ { unit: 'mm', unitValue: 1 },
16
+ { unit: 'cm', unitValue: 10 }, // 1 cm = 10 mm
17
+ { unit: 'm', unitValue: 100 }, // 1 m = 100 cm
18
+ { unit: 'km', unitValue: 1000 } // 1 km = 1000 m
19
+ ]);
20
+ }
21
+ }
22
+ exports.DistanceConverter = DistanceConverter;
@@ -0,0 +1,10 @@
1
+ /**
2
+ * @file PowerCapacityConverter.ts
3
+ * @author 김영찬
4
+ * @since 2026-01-20
5
+ * @description 전력 용량(Power Capacity) 단위 변환기 (W, kW 등)
6
+ */
7
+ import { UnitConverter } from '../UnitConverter';
8
+ export declare class PowerCapacityConverter extends UnitConverter {
9
+ constructor();
10
+ }
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ /**
3
+ * @file PowerCapacityConverter.ts
4
+ * @author 김영찬
5
+ * @since 2026-01-20
6
+ * @description 전력 용량(Power Capacity) 단위 변환기 (W, kW 등)
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.PowerCapacityConverter = void 0;
10
+ const UnitConverter_1 = require("../UnitConverter");
11
+ class PowerCapacityConverter extends UnitConverter_1.UnitConverter {
12
+ constructor() {
13
+ super();
14
+ this.registerUnit([
15
+ { unit: 'W', unitValue: 1 },
16
+ { unit: 'kW', unitValue: 1000 },
17
+ { unit: 'MW', unitValue: 1000 },
18
+ { unit: 'GW', unitValue: 1000 },
19
+ { unit: 'TW', unitValue: 1000 }
20
+ ]);
21
+ }
22
+ }
23
+ exports.PowerCapacityConverter = PowerCapacityConverter;
@@ -0,0 +1,10 @@
1
+ /**
2
+ * @file PowerUsageConverter.ts
3
+ * @author 김영찬
4
+ * @since 2026-01-20
5
+ * @description 전력 사용량(Power Usage) 단위 변환기 (Wh, kWh 등)
6
+ */
7
+ import { UnitConverter } from '../UnitConverter';
8
+ export declare class PowerUsageConverter extends UnitConverter {
9
+ constructor();
10
+ }
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ /**
3
+ * @file PowerUsageConverter.ts
4
+ * @author 김영찬
5
+ * @since 2026-01-20
6
+ * @description 전력 사용량(Power Usage) 단위 변환기 (Wh, kWh 등)
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.PowerUsageConverter = void 0;
10
+ const UnitConverter_1 = require("../UnitConverter");
11
+ class PowerUsageConverter extends UnitConverter_1.UnitConverter {
12
+ constructor() {
13
+ super();
14
+ this.registerUnit([
15
+ { unit: 'Wh', unitValue: 1 },
16
+ { unit: 'kWh', unitValue: 1000 },
17
+ { unit: 'MWh', unitValue: 1000 },
18
+ { unit: 'GWh', unitValue: 1000 },
19
+ { unit: 'TWh', unitValue: 1000 }
20
+ ]);
21
+ }
22
+ }
23
+ exports.PowerUsageConverter = PowerUsageConverter;
package/dist/index.d.ts CHANGED
@@ -6,3 +6,7 @@
6
6
  */
7
7
  export * from './types';
8
8
  export * from './UnitConverter';
9
+ export * from './converters/ComputerCapacityConverter';
10
+ export * from './converters/PowerCapacityConverter';
11
+ export * from './converters/PowerUsageConverter';
12
+ export * from './converters/DistanceConverter';
package/dist/index.js CHANGED
@@ -22,3 +22,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
22
22
  Object.defineProperty(exports, "__esModule", { value: true });
23
23
  __exportStar(require("./types"), exports);
24
24
  __exportStar(require("./UnitConverter"), exports);
25
+ __exportStar(require("./converters/ComputerCapacityConverter"), exports);
26
+ __exportStar(require("./converters/PowerCapacityConverter"), exports);
27
+ __exportStar(require("./converters/PowerUsageConverter"), exports);
28
+ __exportStar(require("./converters/DistanceConverter"), exports);
package/dist/types.d.ts CHANGED
@@ -14,7 +14,7 @@ export interface UnitDefinition {
14
14
  */
15
15
  unit: string;
16
16
  /**
17
- * 단위 값 (기준 단위 대비 배수)
17
+ * 단위 값 (이전 단위 기준 배수, 첫 번째 단위는 절대 값)
18
18
  */
19
19
  unitValue: number;
20
20
  }
@@ -32,3 +32,17 @@ export interface ComplexValueInput {
32
32
  */
33
33
  unit: UnitDefinition[];
34
34
  }
35
+ /**
36
+ * 단위 변환 출력 구조
37
+ * @interface UnitOutput
38
+ */
39
+ export interface UnitOutput {
40
+ /**
41
+ * 변환된 값
42
+ */
43
+ value: number;
44
+ /**
45
+ * 단위 명칭
46
+ */
47
+ unit: string;
48
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "unitmaster",
3
- "version": "1.0.0",
3
+ "version": "1.2.0",
4
4
  "description": "Unit Converter Library",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -8,8 +8,9 @@
8
8
  "dist"
9
9
  ],
10
10
  "scripts": {
11
- "build": "tsc",
12
- "test": "mocha -r ts-node/register test/**/*.test.ts"
11
+ "build": "npm version minor && tsc",
12
+ "test": "mocha -r ts-node/register test/**/*.test.ts",
13
+ "publish": "npm version minor && npm publish"
13
14
  },
14
15
  "keywords": [
15
16
  "unit",