@oddmash/kowrap 0.3.1 → 0.3.3
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 +2 -0
- package/dist/core/layout.js +2 -1
- package/dist/core/scoring.d.ts +1 -1
- package/dist/core/scoring.js +33 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -6,6 +6,8 @@
|
|
|
6
6
|
|
|
7
7
|
[체험하기](https://oddmash.github.io/kowrap/#playground) | [문서](https://oddmash.github.io/kowrap/docs/) | [npm](https://www.npmjs.com/package/@oddmash/kowrap)
|
|
8
8
|
|
|
9
|
+
랜딩 첫 화면에서 기본 CSS와 Kowrap의 줄바꿈 결과를 같은 글꼴과 화면 폭으로 나란히 확인할 수 있으며, 실제 어절의 이동을 반복 시연합니다. 시연은 일시 정지하거나 재생할 수 있습니다.
|
|
10
|
+
|
|
9
11
|
[](https://oddmash.github.io/kowrap/#playground)
|
|
10
12
|
|
|
11
13
|
## HTML에서 바로 실행하기
|
package/dist/core/layout.js
CHANGED
|
@@ -17,6 +17,7 @@ const greedyPlan = (text, points, options) => {
|
|
|
17
17
|
const balancedPlan = (text, points, options) => {
|
|
18
18
|
const last = points.length - 1;
|
|
19
19
|
const choices = [];
|
|
20
|
+
const nextWords = points.map(({ offset }) => text.slice(offset).match(/^\S+/u)?.[0] ?? "");
|
|
20
21
|
choices[last] = { count: 0, cost: 0, next: last, width: 0 };
|
|
21
22
|
for (let start = last - 1; start >= 0; start--) {
|
|
22
23
|
let best;
|
|
@@ -24,7 +25,7 @@ const balancedPlan = (text, points, options) => {
|
|
|
24
25
|
const tail = choices[candidate.next];
|
|
25
26
|
const choice = {
|
|
26
27
|
count: tail.count + 1,
|
|
27
|
-
cost: tail.cost + lineCost(candidate, options.width, candidate.next === last),
|
|
28
|
+
cost: tail.cost + lineCost(candidate, options.width, candidate.next === last, nextWords[candidate.next]),
|
|
28
29
|
next: candidate.next,
|
|
29
30
|
width: candidate.width,
|
|
30
31
|
};
|
package/dist/core/scoring.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import type { LineCandidate } from "./types.js";
|
|
2
|
-
export declare const lineCost: (candidate: LineCandidate, availableWidth: number, isLast: boolean) => number;
|
|
2
|
+
export declare const lineCost: (candidate: LineCandidate, availableWidth: number, isLast: boolean, nextWord?: string) => number;
|
package/dist/core/scoring.js
CHANGED
|
@@ -1,19 +1,47 @@
|
|
|
1
1
|
const sentenceEnd = /[.!?。!?…][”’"')\]]*$/u;
|
|
2
2
|
const clauseEnd = /[,;:,、;:]$/u;
|
|
3
|
-
const boundModifier = /(?:^|\s)(
|
|
4
|
-
const
|
|
3
|
+
const boundModifier = /(?:^|\s)(?:이|그|저|각|모든|어떤|이런|그런|저런|다음|해당|본|여러|다른|같은|기존|\S*(?:로운|스러운|적인|들의|에서의|로서의|으로의|와의|과의|사이의|하는|되는|있는|없는|않는|않은|했던|하던|보이는|필요한|위한|열린|닫는))$/u;
|
|
4
|
+
const technicalPrefix = /^(?:[A-Z][A-Z0-9]{1,}|[A-Z][a-z]+(?:[A-Z][a-z]+)+|[a-z]+\.[a-z]+)$/u;
|
|
5
|
+
const technicalHead = /^(?:import|export|module|runtime|client|server|layer|token|hook|input|output|API|CLI|SDK|파일|모듈|출력|레이어|토큰|컴포넌트|런타임|훅|스타일)(?:[가-힣]*|[.,;:]?)$/u;
|
|
6
|
+
const numeralModifier = /^(?:한|두|세|네|다섯|여섯|일곱|여덟|아홉|열|스무|\d+(?:[.,]\d+)?)$/u;
|
|
7
|
+
const counterHead = /^(?:개|명|건|번|권|회|종|가지|대|장|쪽|줄|단계|시간|분|초|일|개월|년|주)(?:의|이|가|을|를|은|는|에|로|으로|와|과|도|만|부터|까지|에서|에게|이나|나|씩|마다|으로는|로는)?[.,!?。!?…]?$/u;
|
|
8
|
+
const compounds = {
|
|
9
|
+
공통: ["패키지", "코드", "컴포넌트", "모듈"],
|
|
10
|
+
디자인: ["시스템", "토큰"],
|
|
11
|
+
배포: ["구조", "단위", "경계", "결과"],
|
|
12
|
+
사용: ["범위", "경험", "환경"],
|
|
13
|
+
런타임: ["의존성", "검증"],
|
|
14
|
+
퇴출: ["모션", "완료"],
|
|
15
|
+
완료: ["처리", "콜백"],
|
|
16
|
+
전체: ["스타일", "CSS", "코드"],
|
|
17
|
+
실제: ["사용", "화면", "입력"],
|
|
18
|
+
숨겨진: ["입력", "요소", "폼"],
|
|
19
|
+
키보드: ["포커스", "입력"],
|
|
20
|
+
접근: ["가능한"],
|
|
21
|
+
};
|
|
22
|
+
const protectedPair = (fragment, nextWord) => {
|
|
23
|
+
const left = fragment.match(/\S+$/u)?.[0] ?? "";
|
|
24
|
+
if (numeralModifier.test(left) && counterHead.test(nextWord))
|
|
25
|
+
return true;
|
|
26
|
+
if (technicalPrefix.test(left) && technicalHead.test(nextWord))
|
|
27
|
+
return true;
|
|
28
|
+
return Object.hasOwn(compounds, left) && compounds[left].some((head) => nextWord.startsWith(head));
|
|
29
|
+
};
|
|
30
|
+
const boundaryCost = ({ fragment, emergency }, nextWord) => {
|
|
5
31
|
if (emergency)
|
|
6
32
|
return 0.75;
|
|
7
33
|
if (sentenceEnd.test(fragment))
|
|
8
34
|
return 0;
|
|
9
35
|
if (clauseEnd.test(fragment))
|
|
10
36
|
return 0.02;
|
|
37
|
+
if (protectedPair(fragment, nextWord))
|
|
38
|
+
return 0.65;
|
|
11
39
|
if (boundModifier.test(fragment))
|
|
12
|
-
return 0.
|
|
40
|
+
return 0.45;
|
|
13
41
|
return 0.06;
|
|
14
42
|
};
|
|
15
|
-
export const lineCost = (candidate, availableWidth, isLast) => {
|
|
43
|
+
export const lineCost = (candidate, availableWidth, isLast, nextWord = "") => {
|
|
16
44
|
const slack = (availableWidth - Math.min(candidate.width, availableWidth)) / availableWidth;
|
|
17
45
|
const overflow = Math.max(0, candidate.width - availableWidth) / availableWidth;
|
|
18
|
-
return slack ** 2 + overflow ** 2 * 4 + (isLast ? 0 : boundaryCost(candidate));
|
|
46
|
+
return slack ** 2 + overflow ** 2 * 4 + (isLast ? 0 : boundaryCost(candidate, nextWord));
|
|
19
47
|
};
|