Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

tdiff

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tdiff

  • 0.4.0
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

TDiff

  • Source
  • Issues
  • Documentation

Description

Calculates the differences between two tree-like structures. Similar to Rubys built-in TSort module.

Features

  • Provides the {TDiff} mixin.
  • Provides the {TDiff::Unordered} mixin for unordered diffing.
  • Allows custom node equality and traversal logic by overriding the {TDiff#tdiff_equal} and {TDiff#tdiff_each_child} methods.
  • Implements the Longest Common Subsequence (LCS) algorithm.

Examples

Diff two HTML documents:

require 'nokogiri'
require 'tdiff'

class Nokogiri::XML::Node

  include TDiff

  def tdiff_equal(node)
    if (self.text? && node.text?)
      self.text == node.text
    elsif (self.respond_to?(:root) && node.respond_to?(:root))
      self.root.tdiff_equal(node.root)
    elsif (self.respond_to?(:name) && node.respond_to?(:name))
      self.name == node.name
    else
      false
    end
  end

  def tdiff_each_child(node,&block)
    node.children.each(&block)
  end

end

doc1 = Nokogiri::HTML('<div><p>one</p> <p>three</p></div>')
doc2 = Nokogiri::HTML('<div><p>one</p> <p>two</p> <p>three</p></div>')

doc1.at('div').tdiff(doc2.at('div')) do |change,node|
  puts "#{change} #{node.to_html}".ljust(30) + node.parent.path
end

Output

+ <p>one</p>                  /html/body/div
+                             /html/body/div
  <p>one</p>                  /html/body/div
                              /html/body/div
  <p>three</p>                /html/body/div
- one                         /html/body/div/p[1]
+ two                         /html/body/div/p[2]
  three                       /html/body/div/p[2]

Requirements

Install

$ gem install tdiff

See {file:LICENSE.txt} for details.

FAQs

Package last updated on 25 Jan 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc