From 35199b41b229d2c43734b80d27a2486b67f3a65a Mon Sep 17 00:00:00 2001 From: h1alexbel Date: Tue, 19 Mar 2024 13:57:51 +0300 Subject: [PATCH] feat(#49): lines, hash-split --- src/blob-path.ts | 4 ++-- src/hash-split.ts | 40 ++++++++++++++++++++++++++++++++++++++++ src/lines.ts | 40 ++++++++++++++++++++++++++++++++++++++++ src/pdd.ts | 7 +++++-- src/ranged.ts | 11 ++++++----- 5 files changed, 93 insertions(+), 9 deletions(-) create mode 100644 src/hash-split.ts create mode 100644 src/lines.ts diff --git a/src/blob-path.ts b/src/blob-path.ts index 2a64d03..0efab16 100644 --- a/src/blob-path.ts +++ b/src/blob-path.ts @@ -39,8 +39,8 @@ export class BlobPath implements Scalar { const match = this.body?.match(pattern); let path; if (match) { - const full = match[1]; - path = full.split("#")[0]; + path = match[1]; + // path = full.split("#")[0]; } else { throw new Error( `Asset body ${this.body} does not contain puzzle blob, regex: ${pattern}` diff --git a/src/hash-split.ts b/src/hash-split.ts new file mode 100644 index 0000000..3987236 --- /dev/null +++ b/src/hash-split.ts @@ -0,0 +1,40 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2023-2024 Tracehub.git + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +/** + * Split it by hash. + */ +export class HashSplit implements Scalar { + + /** + * Ctor. + * @param full Full path + */ + constructor(private readonly full: string | undefined) { + } + + value(): string | undefined { + return this.full?.split("#")[0]; + } +} diff --git a/src/lines.ts b/src/lines.ts new file mode 100644 index 0000000..3af9ecc --- /dev/null +++ b/src/lines.ts @@ -0,0 +1,40 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2023-2024 Tracehub.git + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +/** + * Lines. + */ +export class Lines implements Scalar { + + /** + * Ctor. + * @param body Report body + */ + constructor(private readonly body: string | undefined) { + } + + value(): string { + return "1-5"; + } +} diff --git a/src/pdd.ts b/src/pdd.ts index fd039e4..a367026 100644 --- a/src/pdd.ts +++ b/src/pdd.ts @@ -25,6 +25,8 @@ import {Ranged} from "./ranged"; import {Blob} from "./blob"; import {Octokit} from "@octokit/rest"; import {BlobPath} from "./blob-path"; +import {Lines} from "./lines"; +import {HashSplit} from "./hash-split"; /** * PDD routine. @@ -48,9 +50,10 @@ export class Pdd { * Run it. */ async run() { + const path = new BlobPath(this.body).value(); const puzzle = await new Ranged( - new Blob(this.github, this.issue, new BlobPath(this.body)), - "1-5" + new Blob(this.github, this.issue, new HashSplit(path)), + new Lines(path) ).value(); console.log(puzzle); } diff --git a/src/ranged.ts b/src/ranged.ts index df208e9..7b05956 100644 --- a/src/ranged.ts +++ b/src/ranged.ts @@ -43,13 +43,14 @@ export class Ranged implements Scalar> { */ async value() { const content = await this.origin.value(); + const value = this.range.value(); let result; - if (this.range.includes('-')) { - const start = this.range.split("-")[0] - 1; - const end = this.range.split("-")[1]; + if (value.includes('-')) { + const start = value.split("-")[0] - 1; + const end = value.split("-")[1]; result = content.splice(start, end).join("\r\n"); - } else if (parseInt(this.range)) { - result = content[parseInt(this.range) - 1]; + } else if (parseInt(value)) { + result = content[parseInt(value) - 1]; } return result; }