Skip to content

Commit

Permalink
linting fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
rgrannell1 committed Jul 1, 2023
1 parent 5376b8a commit bd07f98
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 38 deletions.
6 changes: 3 additions & 3 deletions src/prism.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { RegExpMatchArrayIndexed, StringPrism } from "./types.ts";

/*
/**
* A prism that matches a regular expression (at most once) against a string.
*
* - view: returns the matched string, or null if no match
Expand Down Expand Up @@ -48,7 +48,7 @@ export function MaybeMatch(pattern: RegExp) {
}();
}

/*
/**
* A prism that matches a regular expression with capture groups (at most once) against a string.
` *
* - view: returns the matched capture-group, or null if no match
Expand Down Expand Up @@ -122,7 +122,7 @@ export function MaybeGroupMatch(pattern: RegExp, index: number) {
return whole;
}

let [groupStart, groupEnd] = matches.indices[index];
const [groupStart, groupEnd] = matches.indices[index];

return whole.slice(0, groupStart) +
newPart +
Expand Down
36 changes: 22 additions & 14 deletions src/traversal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ Deno.test({
},
});


Deno.test({
name: "EachMatch.view: if the pattern never matches, .view returns no parts",
fn() {
Expand All @@ -34,35 +33,41 @@ Deno.test({
});

Deno.test({
name: "EachMatch.view: if the pattern matches each character, .view acts as split",
name:
"EachMatch.view: if the pattern matches each character, .view acts as split",
fn() {
const traversal = SubEdit.EachMatch(/[0-9]/gd);

for (const whole of Peach.Array.from(sampleNumbers, 100)()) {
assertEquals(traversal.view(whole), whole.split(''));
assertEquals(traversal.view(whole), whole.split(""));
}
},
});

Deno.test({
name: "EachMatch.modify: if the input is empty, .modify acts as an identity function",
name:
"EachMatch.modify: if the input is empty, .modify acts as an identity function",
fn() {
const traversal = SubEdit.EachMatch(/[a-z]+/gd);

assertEquals(traversal.modify(() => {
throw Error('bang')
}, ""), "");
assertEquals(
traversal.modify(() => {
throw Error("bang");
}, ""),
"",
);
},
});

Deno.test({
name: "EachMatch.modify: if the pattern never matches, .modify acts as an identity function",
name:
"EachMatch.modify: if the pattern never matches, .modify acts as an identity function",
fn() {
const traversal = SubEdit.EachMatch(/[a-z]+/gd);

for (const whole of Peach.Array.from(sampleNumbers, 100)()) {
const updated = traversal.modify(() => {
throw Error('bang')
throw Error("bang");
}, whole);

assertEquals(updated, whole);
Expand All @@ -71,7 +76,8 @@ Deno.test({
});

Deno.test({
name: "EachMatch.modify: elementwise uppercasing is the same as String.toUpperCase",
name:
"EachMatch.modify: elementwise uppercasing is the same as String.toUpperCase",
fn() {
const traversal = SubEdit.EachMatch(/[a-z]+/gd);

Expand All @@ -86,19 +92,21 @@ Deno.test({
});

Deno.test({
name: "EachMatch.modify: elementwise uppercasing is the same as String.toUpperCase",
name:
"EachMatch.modify: elementwise uppercasing is the same as String.toUpperCase",
fn() {
const traversal = SubEdit.EachMatch(/polo/gd);
const dialogue = Peach.String.from(
Peach.Logic.oneOf(Peach.Number.uniform, ['marco', 'polo']),
Peach.Number.uniform(1, 10))
Peach.Logic.oneOf(Peach.Number.uniform, ["marco", "polo"]),
Peach.Number.uniform(1, 10),
);

for (const whole of Peach.Array.from(dialogue, 100)()) {
const updated = traversal.modify((polo: string) => {
return polo.toUpperCase();
}, whole);

const replaced = whole.replace(/polo/g, 'POLO')
const replaced = whole.replace(/polo/g, "POLO");
assertEquals(updated, replaced);
}
},
Expand Down
20 changes: 10 additions & 10 deletions src/traversal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
Traversal,
} from "./types.ts";

/*
/**
* A traversal that matches a regular expression against a string.
*
* - view: returns an array of all matched strings
Expand All @@ -25,7 +25,7 @@ export function EachMatch(pattern: RegExp): StringTraversal {

return new class extends StringTraversal {
view(whole: string) {
let parts: string[] = [];
const parts: string[] = [];

for (const match of whole.matchAll(pattern)) {
parts.push(match[0]);
Expand All @@ -34,7 +34,7 @@ export function EachMatch(pattern: RegExp): StringTraversal {
return parts;
}
indices(whole: string): number[][] {
let slices: number[][] = [];
const slices: number[][] = [];

for (const match of whole.matchAll(pattern)) {
const matchIndices = (match as RegExpMatchArrayIndexed).indices[0];
Expand All @@ -45,12 +45,12 @@ export function EachMatch(pattern: RegExp): StringTraversal {
}

modify(modifier: (part: string) => string, whole: string) {
let parts: string[] = [];
const parts: string[] = [];
let start = 0;

for (const match of whole.matchAll(pattern)) {
const text = match[0];
const boundaries = (match as any).indices[0];
const boundaries = (match as RegExpMatchArrayIndexed).indices[0];

// push text from the previous match up to this match
parts.push(whole.slice(start, boundaries[0]));
Expand All @@ -69,7 +69,7 @@ export function EachMatch(pattern: RegExp): StringTraversal {
}();
}

/*
/**
* A traversal that matches a regular expression's capture-group against a string.
*
* - view: returns an array of all matched strings
Expand All @@ -93,7 +93,7 @@ export function EachGroupMatch(

return new class extends StringTraversal {
view(whole: string) {
let parts: string[] = [];
const parts: string[] = [];

for (const match of whole.matchAll(pattern)) {
parts.push(match[index]);
Expand All @@ -102,7 +102,7 @@ export function EachGroupMatch(
return parts;
}
indices(whole: string): number[][] {
let slices: number[][] = [];
const slices: number[][] = [];

for (const match of whole.matchAll(pattern)) {
const matchIndices = (match as RegExpMatchArrayIndexed).indices[index];
Expand All @@ -112,12 +112,12 @@ export function EachGroupMatch(
return slices;
}
modify(modifier: (part: string) => string, whole: string) {
let parts: string[] = [];
const parts: string[] = [];
let start = 0;

for (const match of whole.matchAll(pattern)) {
const text = match[index];
const boundaries = (match as any).indices[index];
const boundaries = (match as RegExpMatchArrayIndexed).indices[index];

// push text from the previous match up to this match
parts.push(whole.slice(start, boundaries[0]));
Expand Down
22 changes: 11 additions & 11 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/**
* A prism is an optic that:
* - may or may not return a part
* - may or may not update a whole, depending if the part was found
Expand All @@ -12,7 +12,7 @@ export interface Prism<Whole, Part> {
): Prism<Whole, SubPart>;
}

/*
/**
* Partial implementation of the prism interface, leaving view and set abstract
*/
export abstract class AbstractPrism<Whole, Part> implements Prism<Whole, Part> {
Expand All @@ -32,11 +32,11 @@ export abstract class AbstractPrism<Whole, Part> implements Prism<Whole, Part> {
composePrism<SubPart>(
secondPrism: Prism<Part, SubPart>,
): Prism<Whole, SubPart> {
let self = this;
const self = this;

return new class extends AbstractPrism<Whole, SubPart>
implements Prism<Whole, SubPart> {
/*
/**
* Retrieve the part from the first, then from the second. Account for nulls.
*/
view(whole: Whole): SubPart | null {
Expand All @@ -49,7 +49,7 @@ export abstract class AbstractPrism<Whole, Part> implements Prism<Whole, Part> {
return secondPrism.view(firstPart as Part);
}

/*
/**
* Retrieve the part from the first. Update it with a second prism,
* and update with the newly updated part
*/
Expand All @@ -69,7 +69,7 @@ export abstract class AbstractPrism<Whole, Part> implements Prism<Whole, Part> {
}
}

/*
/**
* A traversal is an optic (sorry if this is misnamed!) that:
* - views 0...n parts
* - modifies the whole at the site of 0...n parts
Expand All @@ -80,7 +80,7 @@ export interface Traversal<Whole, Part> {
composePrism<SubPart>(prism: Prism<Part, SubPart>): Traversal<Whole, SubPart>;
}

/*
/**
* Partial implementation of the traversal interface, leaving view and modify abstract
*/
export abstract class AbstractTraversal<Whole, Part>
Expand All @@ -91,7 +91,7 @@ export abstract class AbstractTraversal<Whole, Part>
composePrism<SubPart>(
prism: Prism<Part, SubPart>,
): Traversal<Whole, SubPart> {
let self = this;
const self = this;

return new class extends AbstractTraversal<Whole, SubPart>
implements Traversal<Whole, SubPart> {
Expand All @@ -101,7 +101,7 @@ export abstract class AbstractTraversal<Whole, Part>

const subparts: SubPart[] = [];

for (let part of parts) {
for (const part of parts) {
const subpart = prism.view(part);

// prisms can return null; do not preserve it
Expand Down Expand Up @@ -131,7 +131,7 @@ export abstract class AbstractTraversal<Whole, Part>
composeTraversal<SubPart>(
secondTraversal: Traversal<Part, SubPart>,
): Traversal<Whole, SubPart> {
let self = this;
const self = this;

return new class extends AbstractTraversal<Whole, SubPart>
implements Traversal<Whole, SubPart> {
Expand All @@ -141,7 +141,7 @@ export abstract class AbstractTraversal<Whole, Part>

let subparts: SubPart[] = [];

for (let part of parts) {
for (const part of parts) {
subparts = subparts.concat(secondTraversal.view(part));
}

Expand Down

0 comments on commit bd07f98

Please sign in to comment.