Skip to content
This repository was archived by the owner on Jul 1, 2023. It is now read-only.

Commit 026c091

Browse files
Brian Hsufacebook-github-bot
Brian Hsu
authored andcommitted
Add sections to support string separated by comma
Summary: While working with the StringDiff library, I found that if the data input doesn't contains `\n`, using ::lines() won't provide the desired result. Considering there should be quite some amount of strings built with comma separated way, this should enable more use cases to do the comparison correctly. Reviewed By: fredemmott Differential Revision: D26116594 fbshipit-source-id: 77e587e7ca7b10aeff936c0237a157da29664a2e
1 parent 95ce7eb commit 026c091

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/StringDiff.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
/** Concrete instance of `Diff` for comparing sequences of strings.
1717
*
1818
* You can directly pass in vectors of strings to the constructor, or you can
19-
* use `::lines()` or `::characters()` for convenience in the common cases.
19+
* use `::lines()`, `::characters()` or `::commaSeparatedValues()` for convenience in the common cases.
2020
*
2121
* @see `getDiff()` to get a `vec<DiffOp<string>>`
2222
* @see `getUnifiedDiff()` to get a diff suitable for `patch`
@@ -34,6 +34,10 @@ public static function characters(string $a, string $b): this {
3434
return new self(Str\split($a, ''), Str\split($b, ''));
3535
}
3636

37+
public static function commaSeparatedValues(string $a, string $b): this {
38+
return new self(Str\split($a, ','), Str\split($b, ','));
39+
}
40+
3741
public function getHunks(int $context): vec<vec<DiffOp<string>>> {
3842
$hunks = vec[];
3943

tests/StringDiffTest.php

+14
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,20 @@ public function testDiffCharacters(): void {
4848
);
4949
}
5050

51+
public function testDiffCommaSeparatedValues(): void {
52+
$diff = StringDiff::commaSeparatedValues('a,b,c', 'a,b,d')->getDiff();
53+
expect(C\count($diff))->toEqual(4);
54+
55+
expect($diff[0])->toBeInstanceOf(DiffKeepOp::class);
56+
expect($diff[1])->toBeInstanceOf(DiffKeepOp::class);
57+
expect($diff[2])->toBeInstanceOf(DiffDeleteOp::class);
58+
expect($diff[3])->toBeInstanceOf(DiffInsertOp::class);
59+
60+
expect(Vec\map($diff, $op ==> $op->getContent()))->toEqual(
61+
vec['a', 'b', 'c', 'd'],
62+
);
63+
}
64+
5165
public static function provideExamples(): vec<(string)> {
5266
return Vec\map(
5367
/* HH_FIXME[4107] using directly because this is open source */

0 commit comments

Comments
 (0)