• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

DrHyde / perl-modules-Algorithm-Diff-JSON / 14206718881

01 Apr 2025 09:48PM CUT coverage: 100.0%. Remained the same
14206718881

push

github

web-flow
Merge pull request #23 from DrHyde/dependabot/github_actions/actions/download-artifact-4.2.1

Bump actions/download-artifact from 4.1.9 to 4.2.1

13 of 13 relevant lines covered (100.0%)

6.15 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/lib/Algorithm/Diff/JSON.pm
1
package Algorithm::Diff::JSON;
2

3
use strict;
1✔
4
use warnings;
1✔
5

6
use Algorithm::Diff qw(diff);
1✔
7
use Cpanel::JSON::XS qw(encode_json);
1✔
8

9
use Sub::Exporter -setup  => { exports => [ 'json_diff' ] };
1✔
10

11
our $VERSION = '1.000';
12

13
sub json_diff {
14
    my @changes = ();
4✔
15

16
    foreach my $diff (map { @{$_} } diff(@_)) {
4✔
17
        my($action, $this_line, $content) = @{$diff};
21✔
18
        if(defined($changes[$this_line])) {
21✔
19
            $changes[$this_line] = {
20
                change => {
21
                    add    => $content,
22
                    remove => $changes[$this_line]->{remove}
23
                }
24
            };
6✔
25
        } elsif($action eq '+') {
26
            $changes[$this_line] = { add  => $content };
5✔
27
        } elsif($action eq '-') {
28
            $changes[$this_line] = { remove => $content };
10✔
29
        }
30
    }
31
    return encode_json([
32
        map { defined($changes[$_]) ? { element => $_, %{$changes[$_]} } : () }
4✔
33
        0 .. $#changes
34
    ]);
35
}
36

37
=head1 NAME
38

39
Algorithm::Diff::JSON - find the differences between two lists and report on them in JSON
40

41
=head1 SYNOPSIS
42

43
This perl code:
44

45
    use Algorithm::Diff::JSON qw(json_diff);
46

47
    my $json = json_diff(
48
        [0,      1, 2, 3, 4, 5,      6],
49
        ['zero', 1, 2, 3,    5, 5.5, 6]
50
    );
51

52
will generate this JSON:
53

54
    [
55
        { "element": 0, "change": { "remove": 0, "add": "zero" } },
56
        { "element": 4, "remove": 4 },
57
        { "element": 5, "add": 5.5 }
58
    ]
59

60
(well, an ugly, minimised, equivalent version of that JSON anyway)
61

62
=head1 FUNCTIONS
63

64
There is only one function, which is a simple wrapper around L<Algorithm::Diff>'s
65
C<diff> function:
66

67
=head2 json_diff
68

69
This takes two list-ref arguments. It returns a JSON array describing the
70
changes needed to transform the first into the second.
71

72
This function may be exported. If you want to export it with a different name
73
then you can do so:
74

75
    use Algorithm::Diff::JSON 'json_diff' => { -as => 'something_else };
76

77
Each element in the returned array is a hash. Hashes always have:
78

79
=over
80

81
=item element
82

83
The element number, as given to us by C<Algorithm::Diff>
84

85
=back
86

87
and will also have exactly one of the following keys:
88

89
=over
90

91
=item add
92

93
The content to add at this location
94

95
=item remove
96

97
The content to remove from this location
98

99
=item change
100

101
A hash of both ...
102

103
=over
104

105
=item add
106

107
The content to add at this location
108

109
=item remove
110

111
The content which that replaces at this location
112

113
=back
114

115
=back
116

117
=head1 FEEDBACK
118

119
I welcome feedback about my code, including constructive criticism, bug
120
reports, documentation improvements, and feature requests. The best bug reports
121
include files that I can add to the test suite, which fail with the current
122
code in my git repo and will pass once I've fixed the bug
123

124
Feature requests are far more likely to get implemented if you submit a patch
125
yourself.
126

127
=head1 SOURCE CODE REPOSITORY
128

129
L<git://github.com/DrHyde/perl-modules-Algorithm-Diff-JSON.git>
130

131
=head1 SEE ALSO
132

133
L<Text::Diff>
134

135
L<Algorithm::Diff>
136

137
=head1 AUTHOR, LICENCE and COPYRIGHT
138

139
Copyright 2020 David Cantrell E<lt>F<david@cantrell.org.uk>E<gt>
140

141
This software is free-as-in-speech software, and may be used, distributed, and
142
modified under the terms of either the GNU General Public Licence version 2 or
143
the Artistic Licence. It's up to you which one you use. The full text of the
144
licences can be found in the files GPL2.txt and ARTISTIC.txt, respectively.
145

146
=head1 CONSPIRACY
147

148
This module is also free-as-in-mason software.
149

150
=cut
151

152
1;
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc