From 28af705279aeaf49105262d003e39153288b5646 Mon Sep 17 00:00:00 2001 From: h1alexbel Date: Wed, 20 Mar 2024 17:50:58 +0300 Subject: [PATCH] feat(#49): split.ts --- src/blob.ts | 13 +++++-------- src/pdd.ts | 3 ++- src/ranged.ts | 9 +++++---- src/split.ts | 40 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 52 insertions(+), 13 deletions(-) create mode 100644 src/split.ts diff --git a/src/blob.ts b/src/blob.ts index 78af0f8..11fccaf 100644 --- a/src/blob.ts +++ b/src/blob.ts @@ -27,7 +27,7 @@ import {Base64} from "js-base64"; /** * Code tree GitHub blob. */ -export class Blob implements Scalar> { +export class Blob implements Scalar> { /** * Ctor. @@ -45,7 +45,7 @@ export class Blob implements Scalar> { /** * As text. */ - async value(): Promise { + async value(): Promise { const {data} = await this.github.repos.get( { owner: this.issue.owner, @@ -58,11 +58,8 @@ export class Blob implements Scalar> { ref: data.default_branch, path: String(this.path.value()) }); - const encoded = JSON.parse(JSON.stringify(response.data)).content; - const decoded = Base64.decode(encoded); - - console.log(decoded); - - return decoded.split('\n'); + return Base64.decode( + JSON.parse(JSON.stringify(response.data)).content + ); } } diff --git a/src/pdd.ts b/src/pdd.ts index 3ff8a04..566e8e6 100644 --- a/src/pdd.ts +++ b/src/pdd.ts @@ -27,6 +27,7 @@ import {Octokit} from "@octokit/rest"; import {BlobPath} from "./blob-path"; import {Lines} from "./lines"; import {HashSplit} from "./hash-split"; +import {Split} from "./split"; /** * PDD routine. @@ -55,7 +56,7 @@ export class Pdd { const path = new BlobPath(this.body).value(); const full = new HashSplit(path); const content = await new Blob(this.github, this.issue, full).value(); - const puzzle = await new Ranged(content, new Lines(path)).value(); + const puzzle = await new Ranged(new Split(content), new Lines(path)).value(); console.log(full.value()); console.log(content); diff --git a/src/ranged.ts b/src/ranged.ts index 647f13c..81c772a 100644 --- a/src/ranged.ts +++ b/src/ranged.ts @@ -29,11 +29,11 @@ export class Ranged implements Scalar> { /** * Ctor. - * @param content Content + * @param origin Origin * @param range Range */ constructor( - private readonly content: string[], + private readonly origin: Scalar, private readonly range: any ) { } @@ -42,14 +42,15 @@ export class Ranged implements Scalar> { * Blob as text. */ async value() { + const content = this.origin.value(); const value = this.range.value(); let result; if (value.includes('-')) { const start = value.split("-")[0] - 1; const end = value.split("-")[1]; - result = this.content.splice(start, end).join("\r\n"); + result = content.splice(start, end).join("\r\n"); } else if (parseInt(value)) { - result = this.content[parseInt(value) - 1]; + result = content[parseInt(value) - 1]; } return result; } diff --git a/src/split.ts b/src/split.ts new file mode 100644 index 0000000..f94fec9 --- /dev/null +++ b/src/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. + */ + +/** + * Text in a split way. + */ +export class Split implements Scalar { + + /** + * Ctor. + * @param text Text to split + */ + constructor(private readonly text: string) { + } + + value(): string[] { + return this.text.split('\n'); + } +}