Skip to content

Commit

Permalink
Add package alfa-string (#1572)
Browse files Browse the repository at this point in the history
* Factor indent in alfa-string

* Add documentation

* Refactor normalize in alfa-string

* Add new dependency graphs

* Clean up

* Clean up

* Try disabling cache

* Revert

* Add debug info

* Add debug info

* Actually add files

* Clean up
  • Loading branch information
Jym77 authored Feb 13, 2024
1 parent f51e4f2 commit e449c31
Show file tree
Hide file tree
Showing 43 changed files with 269 additions and 205 deletions.
5 changes: 5 additions & 0 deletions .changeset/eight-dancers-itch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@siteimprove/alfa-string": minor
---

**Added:** Package `@siteimprove/alfa-string` grouping low-level string manipulations is now available.
1 change: 1 addition & 0 deletions config/package-dependency-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ export default {
"@siteimprove/alfa-math",
"@siteimprove/alfa-parser",
"@siteimprove/alfa-rectangle",
"@siteimprove/alfa-string",
"@siteimprove/alfa-test",
"@siteimprove/alfa-time",
],
Expand Down
200 changes: 106 additions & 94 deletions docs/dependency-graph.dot

Large diffs are not rendered by default.

Binary file modified docs/dependency-graph.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions docs/review/api/alfa-string.api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## API Report File for "@siteimprove/alfa-string"

> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/).
```ts

// @public (undocumented)
type String_2 = globalThis.String;

// @public (undocumented)
namespace String_2 {
function indent(input: string): string;
function normalize(input: string, toLowerCase?: boolean): string;
}
export { String_2 as String }

// (No @packageDocumentation comment for this package)

```
1 change: 1 addition & 0 deletions packages/alfa-act/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@siteimprove/alfa-result": "workspace:^0.73.0",
"@siteimprove/alfa-sarif": "workspace:^0.73.0",
"@siteimprove/alfa-sequence": "workspace:^0.73.0",
"@siteimprove/alfa-string": "workspace:^0.73.0",
"@siteimprove/alfa-thunk": "workspace:^0.73.0",
"@siteimprove/alfa-trilean": "workspace:^0.73.0",
"@siteimprove/alfa-tuple": "workspace:^0.73.0"
Expand Down
7 changes: 2 additions & 5 deletions packages/alfa-act/src/diagnostic.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Equatable } from "@siteimprove/alfa-equatable";
import { Hash, Hashable } from "@siteimprove/alfa-hash";
import { Serializable } from "@siteimprove/alfa-json";
import { String } from "@siteimprove/alfa-string";

import * as json from "@siteimprove/alfa-json";

Expand All @@ -11,7 +12,7 @@ export class Diagnostic
implements Equatable, Hashable, Serializable<Diagnostic.JSON>
{
public static of(message: string): Diagnostic {
return new Diagnostic(normalize(message));
return new Diagnostic(String.normalize(message, false));
}

protected readonly _message: string;
Expand Down Expand Up @@ -58,7 +59,3 @@ export namespace Diagnostic {

export const empty = Diagnostic.of("No extra information");
}

function normalize(input: string): string {
return input.trim().replace(/\s+/g, " ");
}
1 change: 1 addition & 0 deletions packages/alfa-act/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
{ "path": "../alfa-result" },
{ "path": "../alfa-sarif" },
{ "path": "../alfa-sequence" },
{ "path": "../alfa-string" },
{ "path": "../alfa-thunk" },
{ "path": "../alfa-trilean" },
{ "path": "../alfa-tuple" }
Expand Down
1 change: 1 addition & 0 deletions packages/alfa-aria/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@siteimprove/alfa-selector": "workspace:^0.73.0",
"@siteimprove/alfa-sequence": "workspace:^0.73.0",
"@siteimprove/alfa-set": "workspace:^0.73.0",
"@siteimprove/alfa-string": "workspace:^0.73.0",
"@siteimprove/alfa-style": "workspace:^0.73.0",
"@siteimprove/alfa-table": "workspace:^0.73.0",
"@siteimprove/alfa-thunk": "workspace:^0.73.0",
Expand Down
7 changes: 2 additions & 5 deletions packages/alfa-aria/src/node/container.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { None, Option } from "@siteimprove/alfa-option";
import { String } from "@siteimprove/alfa-string";

import * as dom from "@siteimprove/alfa-dom";

Expand Down Expand Up @@ -62,7 +63,7 @@ export class Container extends Node<"container"> {
public toString(): string {
return [
"container",
...this._children.map((child) => indent(child.toString())),
...this._children.map((child) => String.indent(child.toString())),
].join("\n");
}
}
Expand All @@ -75,7 +76,3 @@ export namespace Container {
role: Role.Name | null;
}
}

function indent(input: string): string {
return input.replace(/^/gm, " ");
}
7 changes: 2 additions & 5 deletions packages/alfa-aria/src/node/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Iterable } from "@siteimprove/alfa-iterable";
import { None, Option } from "@siteimprove/alfa-option";
import { Predicate } from "@siteimprove/alfa-predicate";
import { Refinement } from "@siteimprove/alfa-refinement";
import { String } from "@siteimprove/alfa-string";

import * as dom from "@siteimprove/alfa-dom";

Expand Down Expand Up @@ -110,7 +111,7 @@ export class Element extends Node<"element"> {
this._role.map((role) => role.name).getOr("element"),
...this._name.map((name) => `"${name}"`),
].join(" "),
...this._children.map((child) => indent(child.toString())),
...this._children.map((child) => String.indent(child.toString())),
].join("\n");
}
}
Expand All @@ -125,7 +126,3 @@ export namespace Element {
attributes: Array<Attribute.JSON>;
}
}

function indent(input: string): string {
return input.replace(/^/gm, " ");
}
1 change: 1 addition & 0 deletions packages/alfa-aria/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
{ "path": "../alfa-selector" },
{ "path": "../alfa-sequence" },
{ "path": "../alfa-set" },
{ "path": "../alfa-string" },
{ "path": "../alfa-style" },
{ "path": "../alfa-table" },
{ "path": "../alfa-test" },
Expand Down
1 change: 1 addition & 0 deletions packages/alfa-dom/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"@siteimprove/alfa-sarif": "workspace:^0.73.0",
"@siteimprove/alfa-selective": "workspace:^0.73.0",
"@siteimprove/alfa-sequence": "workspace:^0.73.0",
"@siteimprove/alfa-string": "workspace:^0.73.0",
"@siteimprove/alfa-trampoline": "workspace:^0.73.0",
"@siteimprove/alfa-tree": "workspace:^0.73.0"
},
Expand Down
8 changes: 3 additions & 5 deletions packages/alfa-dom/src/node/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { Array } from "@siteimprove/alfa-array";
import { Device } from "@siteimprove/alfa-device";
import { Iterable } from "@siteimprove/alfa-iterable";
import { None, Option } from "@siteimprove/alfa-option";
import { String } from "@siteimprove/alfa-string";
import { Trampoline } from "@siteimprove/alfa-trampoline";

import { Node } from "../node";
import { Sheet } from "../style/sheet";
import { Element } from "./element";
Expand Down Expand Up @@ -76,7 +78,7 @@ export class Document extends Node<"document"> {

public toString(): string {
const children = this._children
.map((child) => indent(child.toString()))
.map((child) => String.indent(child.toString()))
.join("\n");

return `#document${children === "" ? "" : `\n${children}`}`;
Expand Down Expand Up @@ -151,7 +153,3 @@ export namespace Document {
});
}
}

function indent(input: string): string {
return input.replace(/^/gm, " ");
}
7 changes: 2 additions & 5 deletions packages/alfa-dom/src/node/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { None, Option, Some } from "@siteimprove/alfa-option";
import { Predicate } from "@siteimprove/alfa-predicate";
import { Rectangle } from "@siteimprove/alfa-rectangle";
import { Sequence } from "@siteimprove/alfa-sequence";
import { String } from "@siteimprove/alfa-string";
import { Trampoline } from "@siteimprove/alfa-trampoline";

import { Namespace } from "../namespace";
Expand Down Expand Up @@ -341,7 +342,7 @@ export class Element<N extends string = string>
return value.trim();
})
.filter(not(isEmpty))
.map(indent)
.map(String.indent)
.join("\n");

return `<${name}${attributes}>${
Expand Down Expand Up @@ -519,7 +520,3 @@ export namespace Element {

export const { inputType } = helpers;
}

function indent(input: string): string {
return input.replace(/^/gm, " ");
}
7 changes: 2 additions & 5 deletions packages/alfa-dom/src/node/fragment.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Device } from "@siteimprove/alfa-device";
import { String } from "@siteimprove/alfa-string";
import { Trampoline } from "@siteimprove/alfa-trampoline";

import { Iterable } from "@siteimprove/alfa-iterable";
Expand Down Expand Up @@ -38,7 +39,7 @@ export class Fragment extends Node<"fragment"> {

public toString(): string {
const children = this._children
.map((child) => indent(child.toString()))
.map((child) => String.indent(child.toString()))
.join("\n");

return `#document-fragment${children === "" ? "" : `\n${children}`}`;
Expand Down Expand Up @@ -93,7 +94,3 @@ export namespace Fragment {
});
}
}

function indent(input: string): string {
return input.replace(/^/gm, " ");
}
7 changes: 2 additions & 5 deletions packages/alfa-dom/src/node/shadow.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { None, Option } from "@siteimprove/alfa-option";
import { String } from "@siteimprove/alfa-string";
import { Trampoline } from "@siteimprove/alfa-trampoline";

import { Device } from "@siteimprove/alfa-device";
Expand Down Expand Up @@ -100,7 +101,7 @@ export class Shadow extends Node<"shadow"> {

public toString(): string {
const children = this._children
.map((child) => indent(child.toString()))
.map((child) => String.indent(child.toString()))
.join("\n");

return `#shadow-root (${this._mode})${
Expand Down Expand Up @@ -184,7 +185,3 @@ export namespace Shadow {
});
}
}

function indent(input: string): string {
return input.replace(/^/gm, " ");
}
7 changes: 2 additions & 5 deletions packages/alfa-dom/src/style/rule/font-face.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { String } from "@siteimprove/alfa-string";
import { Trampoline } from "@siteimprove/alfa-trampoline";

import { Block } from "../block";
Expand Down Expand Up @@ -36,7 +37,7 @@ export class FontFaceRule extends Rule<"font-face"> {
public toString(): string {
const style = this._style.toString();

return `@font-face {${style === "" ? "" : `\n${indent(style)}\n`}}`;
return `@font-face {${style === "" ? "" : `\n${String.indent(style)}\n`}}`;
}
}

Expand All @@ -59,7 +60,3 @@ export namespace FontFaceRule {
return Trampoline.done(FontFaceRule.of(json.style.map(Declaration.from)));
}
}

function indent(input: string): string {
return input.replace(/^/gm, " ");
}
7 changes: 2 additions & 5 deletions packages/alfa-dom/src/style/rule/keyframe.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { String } from "@siteimprove/alfa-string";
import { Trampoline } from "@siteimprove/alfa-trampoline";

import { Block } from "../block";
Expand Down Expand Up @@ -47,7 +48,7 @@ export class KeyframeRule extends Rule<"keyframe"> {
const style = this._style.toString();

return `@keyframe ${this._key} {${
style === "" ? "" : `\n${indent(style)}\n`
style === "" ? "" : `\n${String.indent(style)}\n`
}}`;
}
}
Expand All @@ -74,7 +75,3 @@ export namespace KeyframeRule {
);
}
}

function indent(input: string): string {
return input.replace(/^/gm, " ");
}
7 changes: 2 additions & 5 deletions packages/alfa-dom/src/style/rule/keyframes.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { String } from "@siteimprove/alfa-string";
import { Trampoline } from "@siteimprove/alfa-trampoline";

import { Rule } from "../rule";
Expand Down Expand Up @@ -32,7 +33,7 @@ export class KeyframesRule extends GroupingRule<"keyframes"> {

public toString(): string {
const rules = this._rules
.map((rule) => indent(rule.toString()))
.map((rule) => String.indent(rule.toString()))
.join("\n\n");

return `@keyframes ${this._name} {${rules === "" ? "" : `\n${rules}\n`}}`;
Expand Down Expand Up @@ -60,7 +61,3 @@ export namespace KeyframesRule {
);
}
}

function indent(input: string): string {
return input.replace(/^/gm, " ");
}
7 changes: 2 additions & 5 deletions packages/alfa-dom/src/style/rule/media.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Lexer } from "@siteimprove/alfa-css";
import { Feature } from "@siteimprove/alfa-css-feature";
import { Iterable } from "@siteimprove/alfa-iterable";
import { String } from "@siteimprove/alfa-string";
import { Trampoline } from "@siteimprove/alfa-trampoline";

import { Rule } from "../rule";
Expand Down Expand Up @@ -36,7 +37,7 @@ export class MediaRule extends ConditionRule<"media"> {

public toString(): string {
const rules = join(
map(this._rules, (rule) => indent(rule.toString())),
map(this._rules, (rule) => String.indent(rule.toString())),
"\n\n",
);

Expand All @@ -63,7 +64,3 @@ export namespace MediaRule {
);
}
}

function indent(input: string): string {
return input.replace(/^/gm, " ");
}
7 changes: 2 additions & 5 deletions packages/alfa-dom/src/style/rule/page.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { String } from "@siteimprove/alfa-string";
import { Trampoline } from "@siteimprove/alfa-trampoline";

import { Block } from "../block";
Expand Down Expand Up @@ -47,7 +48,7 @@ export class PageRule extends Rule<"page"> {
const style = this._style.toString();

return `@page ${this._selector} {${
style === "" ? "" : `\n${indent(style)}\n`
style === "" ? "" : `\n${String.indent(style)}\n`
}}`;
}
}
Expand All @@ -74,7 +75,3 @@ export namespace PageRule {
);
}
}

function indent(input: string): string {
return input.replace(/^/gm, " ");
}
7 changes: 2 additions & 5 deletions packages/alfa-dom/src/style/rule/style.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { String } from "@siteimprove/alfa-string";
import { Trampoline } from "@siteimprove/alfa-trampoline";

import { Block } from "../block";
Expand Down Expand Up @@ -57,7 +58,7 @@ export class StyleRule extends Rule<"style"> {
public toString(): string {
const style = this._style.toString();

return `${this._selector} {${style === "" ? "" : `\n${indent(style)}\n`}}`;
return `${this._selector} {${style === "" ? "" : `\n${String.indent(style)}\n`}}`;
}
}

Expand All @@ -83,7 +84,3 @@ export namespace StyleRule {
);
}
}

function indent(input: string): string {
return input.replace(/^/gm, " ");
}
Loading

0 comments on commit e449c31

Please sign in to comment.