Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
duart38 committed Mar 16, 2021
1 parent 2031ced commit db800b6
Show file tree
Hide file tree
Showing 9 changed files with 203 additions and 192 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Documentation
</a>

## Road 🗺

- [x] Add ARGS (deno.args) carpenter
- [x] Add OS (deno.build) carpenter
- [x] Add STRING carpenter
Expand Down Expand Up @@ -66,9 +67,9 @@ STRING("hello")
.contains("hello").and().contains("x") // false
.or().isOfSize(5) // true
.do(() => console.log("OH WOW!!!!")) // true because of OR operator
;

STRING("hello")
.adapt((prev: string)=>prev += " world") // adapts the string.. you may continue with chaining after this
.adapt((prev: string) => prev += " world") // adapts the string.. you may continue with chaining after this
.getValue(); // gets the current object value.

```
2 changes: 1 addition & 1 deletion builder/ARGS.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {Builder} from "../types/Builder.ts";
import type { Builder } from "../types/Builder.ts";
/**
* Deno command line argument specific code builder.
* @example ARGS.on("-x")
Expand Down
132 changes: 66 additions & 66 deletions builder/ARRAY.ts
Original file line number Diff line number Diff line change
@@ -1,98 +1,98 @@
import { STRING, STR } from "../mod.ts";
import {WritableBuilder} from "../types/Builder.ts";
import {LOGICAL, Logical} from "../types/Logical.ts";
import { STR, STRING } from "../mod.ts";
import { WritableBuilder } from "../types/Builder.ts";
import { LOGICAL, Logical } from "../types/Logical.ts";

export class ARR<I> implements WritableBuilder<Array<I>, ARR<I>>, Logical{
private value: I[]
private lastStackMatched: boolean;
private previousLogical: LOGICAL;
constructor(val: I[]){
this.value = val;
this.lastStackMatched = true;
this.previousLogical = LOGICAL.AND;
}
private computeWithPrevious(onCurrent: boolean): boolean {
switch (this.previousLogical) {
case LOGICAL.AND:
return this.lastStackMatched && onCurrent;
case LOGICAL.OR:
return this.lastStackMatched || onCurrent;
}
export class ARR<I> implements WritableBuilder<Array<I>, ARR<I>>, Logical {
private value: I[];
private lastStackMatched: boolean;
private previousLogical: LOGICAL;
constructor(val: I[]) {
this.value = val;
this.lastStackMatched = true;
this.previousLogical = LOGICAL.AND;
}
private computeWithPrevious(onCurrent: boolean): boolean {
switch (this.previousLogical) {
case LOGICAL.AND:
return this.lastStackMatched && onCurrent;
case LOGICAL.OR:
return this.lastStackMatched || onCurrent;
}
}

and(): this {
this.previousLogical = LOGICAL.AND;
return this;
}
and(): this {
this.previousLogical = LOGICAL.AND;
return this;
}

or(): this {
this.previousLogical = LOGICAL.OR;
return this;
}
or(): this {
this.previousLogical = LOGICAL.OR;
return this;
}

/**
/**
* Appends regardless of result of the previous chain items.
* @param toAdd
*/
public append(...toAdd: I[]): this{
this.value.push(...toAdd);
return this;
}
public append(...toAdd: I[]): this {
this.value.push(...toAdd);
return this;
}

/**
/**
* Appends only if the previous chain items result in a "true".
* @param toAdd
*/
public thenAppend(...toAdd: I[]): this{
if(this.lastStackMatched) this.value.push(...toAdd);
return this;
}
public thenAppend(...toAdd: I[]): this {
if (this.lastStackMatched) this.value.push(...toAdd);
return this;
}

/**
/**
* Checks if the current array length is of size 'n'
* @param n
*/
public isOfSize(n: number): this{
this.lastStackMatched = this.computeWithPrevious(this.value.length == n);
return this;
}
public isOfSize(n: number): this {
this.lastStackMatched = this.computeWithPrevious(this.value.length == n);
return this;
}

/**
/**
* Checks if the array includes all the provided parameters.
* @param toCheck
* @returns
*/
public contains(...toCheck: I[]): this{
this.lastStackMatched = this.computeWithPrevious(toCheck.every((x)=> this.value.includes(x)));
return this;
}
public contains(...toCheck: I[]): this {
this.lastStackMatched = this.computeWithPrevious(
toCheck.every((x) => this.value.includes(x)),
);
return this;
}

/**
/**
* join the array together into a string
* @param separator
* @returns a string carpenter
* @see STR
*/
public join(separator: string | undefined): STR {
return STRING(this.value.join(separator));
}
public join(separator: string | undefined): STR {
return STRING(this.value.join(separator));
}

public do(run: (currentValue: I[]) => void|I[]): this{
if(this.lastStackMatched){
this.value = run(this.value) || this.value;
}
return this;
public do(run: (currentValue: I[]) => void | I[]): this {
if (this.lastStackMatched) {
this.value = run(this.value) || this.value;
}
return this;
}

public else(run: (currentValue: I[]) => void|I[]): this{
if(!this.lastStackMatched){
this.value = run(this.value) || this.value;
}
return this;
public else(run: (currentValue: I[]) => void | I[]): this {
if (!this.lastStackMatched) {
this.value = run(this.value) || this.value;
}


return this;
}
}
export function ARRAY<I>(val: I[]): ARR<I> {
return new ARR(val);
}
export function ARRAY<I>(val: I[]): ARR<I>{
return new ARR(val);
}
2 changes: 1 addition & 1 deletion builder/OS.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
type OSName = "darwin" | "linux" | "windows";
import type {Builder} from "../types/Builder.ts";
import type { Builder } from "../types/Builder.ts";
/**
* Deno operating system specific code builder.
* @example OS.on("darwin")
Expand Down
23 changes: 13 additions & 10 deletions builder/STRING.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ARRAY, ARR } from "../mod.ts";
import {WritableBuilder} from "../types/Builder.ts";
import {LOGICAL, Logical} from "../types/Logical.ts";
export class STR implements WritableBuilder<string, STR>, Logical{
import { ARR, ARRAY } from "../mod.ts";
import { WritableBuilder } from "../types/Builder.ts";
import { LOGICAL, Logical } from "../types/Logical.ts";
export class STR implements WritableBuilder<string, STR>, Logical {
private value: string;
private lastStackMatched;
private previousLogical: LOGICAL;
Expand Down Expand Up @@ -70,7 +70,10 @@ export class STR implements WritableBuilder<string, STR>, Logical{
* @returns ARR<string>
* @see ARR
*/
public split(separator: string | RegExp, limit?: number | undefined): ARR<string>{
public split(
separator: string | RegExp,
limit?: number | undefined,
): ARR<string> {
return ARRAY(this.value.split(separator, limit));
}

Expand Down Expand Up @@ -114,9 +117,9 @@ export class STR implements WritableBuilder<string, STR>, Logical{
* @returns
*/
public do(run: (stored: string) => void | string): this {
if (this.lastStackMatched){
const t = run(this.value);
if(t) this.value = t;
if (this.lastStackMatched) {
const t = run(this.value);
if (t) this.value = t;
}
return this;
}
Expand All @@ -126,7 +129,7 @@ export class STR implements WritableBuilder<string, STR>, Logical{
* Unlike the do method, this method will always run regardless of the evaluation before it.
* @param run {(prev: string)=>string} method that adapts the string
*/
public adapt(run: (prev: string)=>string): this {
public adapt(run: (prev: string) => string): this {
this.value = run(this.value);
return this;
}
Expand All @@ -140,7 +143,7 @@ export class STR implements WritableBuilder<string, STR>, Logical{
public else(run: (stored: string) => void | string): this {
if (!this.lastStackMatched) {
const t = run(this.value);
if(t) this.value = t;
if (t) this.value = t;
}
return this;
}
Expand Down
4 changes: 2 additions & 2 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { OS } from "./builder/OS.ts";
export { ARGS } from "./builder/ARGS.ts";
export { STRING, STR } from "./builder/STRING.ts";
export { ARRAY, ARR } from "./builder/ARRAY.ts";
export { STR, STRING } from "./builder/STRING.ts";
export { ARR, ARRAY } from "./builder/ARRAY.ts";
Loading

0 comments on commit db800b6

Please sign in to comment.