Skip to content

Commit 43ad787

Browse files
committed
refactor: Use pre- defined BaseLine type
instead of writing `type Line = Page["lines"][number]`
1 parent 31cd346 commit 43ad787

File tree

6 files changed

+18
-25
lines changed

6 files changed

+18
-25
lines changed

browser/websocket/applyCommit.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { CommitNotification } from "./websocket-types.ts";
2-
import type { Page } from "@cosense/types/rest";
2+
import type { BaseLine } from "@cosense/types/rest";
33
import { getUnixTimeFromId } from "./id.ts";
4-
type Line = Page["lines"][number];
54

65
export interface ApplyCommitProp {
76
/** changesの作成日時
@@ -19,10 +18,10 @@ export interface ApplyCommitProp {
1918
* @param changes 適用するcommits
2019
*/
2120
export const applyCommit = (
22-
lines: readonly Line[],
21+
lines: readonly BaseLine[],
2322
changes: CommitNotification["changes"],
2423
{ updated, userId }: ApplyCommitProp,
25-
): Line[] => {
24+
): BaseLine[] => {
2625
const newLines = [...lines];
2726
const getPos = (lineId: string) => {
2827
const position = newLines.findIndex(({ id }) => id === lineId);

browser/websocket/patch.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import type { Change, DeletePageChange, PinChange } from "./wrap.ts";
22
import { makeChanges } from "./makeChanges.ts";
3-
import type { Page } from "@cosense/types/rest";
3+
import type { BaseLine, Page } from "@cosense/types/rest";
44
import { push, type PushError, type PushOptions } from "./push.ts";
55
import { suggestUnDupTitle } from "./suggestUnDupTitle.ts";
66
import type { Result } from "option-t/plain_result";
77

88
export type PatchOptions = PushOptions;
9-
type Line = Page["lines"][number];
109

1110
export interface PatchMetadata extends Page {
1211
/** 書き換えを再試行した回数
@@ -29,7 +28,7 @@ export const patch = (
2928
project: string,
3029
title: string,
3130
update: (
32-
lines: Line[],
31+
lines: BaseLine[],
3332
metadata: PatchMetadata,
3433
) => string[] | undefined | Promise<string[] | undefined>,
3534
options?: PatchOptions,

browser/websocket/updateCodeBlock.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Page } from "@cosense/types/rest";
1+
import type { BaseLine } from "@cosense/types/rest";
22
import type {
33
DeleteChange,
44
InsertChange,
@@ -12,8 +12,6 @@ import { countBodyIndent, extractFromCodeTitle } from "./_codeBlock.ts";
1212
import { push, type PushError, type PushOptions } from "./push.ts";
1313
import type { Result } from "option-t/plain_result";
1414

15-
type Line = Page["lines"][number];
16-
1715
export interface UpdateCodeBlockOptions extends PushOptions {
1816
/** `true`でデバッグ出力ON */
1917
debug?: boolean;
@@ -35,7 +33,7 @@ export const updateCodeBlock = (
3533
): Promise<Result<string, PushError>> => {
3634
const newCodeBody = getCodeBody(newCode);
3735
const bodyIndent = countBodyIndent(target);
38-
const oldCodeWithoutIndent: Line[] = target.bodyLines.map((e) => {
36+
const oldCodeWithoutIndent: BaseLine[] = target.bodyLines.map((e) => {
3937
return { ...e, text: e.text.slice(bodyIndent) };
4038
});
4139

browser/websocket/updateCodeFile.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Page } from "@cosense/types/rest";
1+
import type { BaseLine } from "@cosense/types/rest";
22
import type {
33
DeleteChange,
44
InsertChange,
@@ -10,7 +10,6 @@ import { diff, toExtendedChanges } from "../../deps/onp.ts";
1010
import { countBodyIndent } from "./_codeBlock.ts";
1111
import { push, type PushError, type PushOptions } from "./push.ts";
1212
import type { Result } from "option-t/plain_result";
13-
type Line = Page["lines"][number];
1413

1514
/** コードブロックの上書きに使う情報のinterface */
1615
export interface SimpleCodeFile {
@@ -74,7 +73,7 @@ export const updateCodeFile = (
7473
project,
7574
title,
7675
(page) => {
77-
const lines: Line[] = page.lines;
76+
const lines: BaseLine[] = page.lines;
7877
const codeBlocks = getCodeBlocks({ project, title, lines }, {
7978
filename: codeFile.filename,
8079
});
@@ -104,7 +103,7 @@ export const updateCodeFile = (
104103
/** TinyCodeBlocksの配列からコード本文をフラットな配列に格納して返す \
105104
* その際、コードブロックの左側に存在していたインデントは削除する
106105
*/
107-
const flatCodeBodies = (codeBlocks: readonly TinyCodeBlock[]): Line[] => {
106+
const flatCodeBodies = (codeBlocks: readonly TinyCodeBlock[]): BaseLine[] => {
108107
return codeBlocks.map((block) => {
109108
const indent = countBodyIndent(block);
110109
return block.bodyLines.map((body) => {
@@ -117,7 +116,7 @@ const flatCodeBodies = (codeBlocks: readonly TinyCodeBlock[]): Line[] => {
117116
function* makeCommits(
118117
_codeBlocks: readonly TinyCodeBlock[],
119118
codeFile: SimpleCodeFile,
120-
lines: Line[],
119+
lines: BaseLine[],
121120
{ userId, insertPositionIfNotExist, isInsertEmptyLineInTail }: {
122121
userId: string;
123122
insertPositionIfNotExist: Required<

rest/getCodeBlocks.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
import type { Page } from "@cosense/types/rest";
1+
import type { BaseLine } from "@cosense/types/rest";
22
import { assertEquals } from "@std/assert";
33
import { assertSnapshot } from "@std/testing/snapshot";
44
import { getCodeBlocks } from "./getCodeBlocks.ts";
5-
type Line = Page["lines"][number];
65

76
// https://scrapbox.io/takker/コードブロック記法
87
const project = "takker";
98
const title = "コードブロック記法";
10-
const sample: Line[] = [
9+
const sample: BaseLine[] = [
1110
{
1211
"id": "63b7aeeb5defe7001ddae116",
1312
"text": "コードブロック記法",

rest/getCodeBlocks.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import type { Page } from "@cosense/types/rest";
1+
import type { BaseLine } from "@cosense/types/rest";
22
import {
33
type CodeTitle,
44
extractFromCodeTitle,
55
} from "../browser/websocket/_codeBlock.ts";
6-
type Line = Page["lines"][number];
76

87
/** pull()から取れる情報で構成したコードブロックの最低限の情報 */
98
export interface TinyCodeBlock {
@@ -14,13 +13,13 @@ export interface TinyCodeBlock {
1413
lang: string;
1514

1615
/** タイトル行 */
17-
titleLine: Line;
16+
titleLine: BaseLine;
1817

1918
/** コードブロックの中身(タイトル行を含まない) */
20-
bodyLines: Line[];
19+
bodyLines: BaseLine[];
2120

2221
/** コードブロックの真下の行(無ければ`null`) */
23-
nextLine: Line | null;
22+
nextLine: BaseLine | null;
2423

2524
/** コードブロックが存在するページの情報 */
2625
pageInfo: { projectName: string; pageTitle: string };
@@ -46,7 +45,7 @@ export interface GetCodeBlocksFilter {
4645
* @return コードブロックの配列
4746
*/
4847
export const getCodeBlocks = (
49-
target: { project: string; title: string; lines: Line[] },
48+
target: { project: string; title: string; lines: BaseLine[] },
5049
filter?: GetCodeBlocksFilter,
5150
): TinyCodeBlock[] => {
5251
const codeBlocks: TinyCodeBlock[] = [];

0 commit comments

Comments
 (0)