Skip to content

Commit

Permalink
simplify laziness of children sequences
Browse files Browse the repository at this point in the history
  • Loading branch information
Baptouuuu committed Jun 26, 2024
1 parent 9a7f910 commit bcaadb8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 26 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## [Unreleased]

### Changed

- Requires `innmind/immutable:~5.7`
- `Document` and `Element` children `Sequence` is no longer forced to be lazy, it depends on the kind of `Sequence` you use when building them

## 7.6.0 - 2023-10-22

### Changed
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
"require": {
"php": "~8.2",
"innmind/immutable": "^4.7.1|~5.0",
"innmind/immutable": "~5.7",
"innmind/filesystem": "~7.0"
},
"autoload": {
Expand Down
26 changes: 12 additions & 14 deletions src/Element/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public function mapChild(callable $map): self
public function prependChild(Node $child): self
{
$element = clone $this;
$element->children = Sequence::lazyStartingWith($child)->append($this->children);
$element->children = $this->children->prepend(Sequence::of($child));

return $element;
}
Expand Down Expand Up @@ -202,20 +202,18 @@ public function toString(): string
public function asContent(): Content
{
return Content::ofLines(
Sequence::lazyStartingWith(Content\Line::of(Str::of($this->openingTag())))
->append(
$this
->children
->flatMap(
static fn($node) => match (true) {
$node instanceof AsContent => $node->asContent()->lines(),
default => Content::ofString($node->toString())->lines(),
},
)
->map(static fn($line) => $line->map(
static fn($string) => $string->prepend(' '), // to correctly indent the file
)),
$this
->children
->flatMap(
static fn($node) => match (true) {
$node instanceof AsContent => $node->asContent()->lines(),
default => Content::ofString($node->toString())->lines(),
},
)
->map(static fn($line) => $line->map(
static fn($string) => $string->prepend(' '), // to correctly indent the file
))
->prepend(Sequence::of(Content\Line::of(Str::of($this->openingTag()))))
->add(Content\Line::of(Str::of($this->closingTag()))),
);
}
Expand Down
20 changes: 9 additions & 11 deletions src/Node/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function mapChild(callable $map): self
public function prependChild(Node $child): Node
{
$document = clone $this;
$document->children = Sequence::lazyStartingWith($child)->append($this->children);
$document->children = $this->children->prepend(Sequence::of($child));

return $document;
}
Expand Down Expand Up @@ -149,19 +149,17 @@ public function toString(): string
public function asContent(): Content
{
return Content::ofLines(
Sequence::lazyStartingWith(Content\Line::of(Str::of($this->tag())))
->append($this->type->match(
$this
->children
->flatMap(static fn($node) => match (true) {
$node instanceof AsContent => $node->asContent()->lines(),
default => Content::ofString($node->toString())->lines(),
})
->prepend($this->type->match(
static fn($type) => Sequence::of(Content\Line::of(Str::of($type->toString()))),
static fn() => Sequence::of(),
))
->append(
$this
->children
->flatMap(static fn($node) => match (true) {
$node instanceof AsContent => $node->asContent()->lines(),
default => Content::ofString($node->toString())->lines(),
}),
),
->prepend(Sequence::of(Content\Line::of(Str::of($this->tag())))),
);
}

Expand Down

0 comments on commit bcaadb8

Please sign in to comment.