Skip to content

Commit

Permalink
feat(#49): split.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
h1alexbel committed Mar 20, 2024
1 parent a9feda0 commit 28af705
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 13 deletions.
13 changes: 5 additions & 8 deletions src/blob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {Base64} from "js-base64";
/**
* Code tree GitHub blob.
*/
export class Blob implements Scalar<Promise<string[]>> {
export class Blob implements Scalar<Promise<string>> {

/**
* Ctor.
Expand All @@ -45,7 +45,7 @@ export class Blob implements Scalar<Promise<string[]>> {
/**
* As text.
*/
async value(): Promise<string[]> {
async value(): Promise<string> {
const {data} = await this.github.repos.get(
{
owner: this.issue.owner,
Expand All @@ -58,11 +58,8 @@ export class Blob implements Scalar<Promise<string[]>> {
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
);
}
}
3 changes: 2 additions & 1 deletion src/pdd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand Down
9 changes: 5 additions & 4 deletions src/ranged.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ export class Ranged implements Scalar<Promise<any>> {

/**
* Ctor.
* @param content Content
* @param origin Origin
* @param range Range
*/
constructor(
private readonly content: string[],
private readonly origin: Scalar<string[]>,
private readonly range: any
) {
}
Expand All @@ -42,14 +42,15 @@ export class Ranged implements Scalar<Promise<any>> {
* 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;
}
Expand Down
40 changes: 40 additions & 0 deletions src/split.ts
Original file line number Diff line number Diff line change
@@ -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<string[]> {

/**
* Ctor.
* @param text Text to split
*/
constructor(private readonly text: string) {
}

value(): string[] {
return this.text.split('\n');
}
}

0 comments on commit 28af705

Please sign in to comment.