npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

text-different

v1.2.1

Published

比较代码(文本)的不同,并高亮展示的组件。

Downloads

23

Readme

text-different

中文文档

Effect

Compare text differences and generate a difference tree.
text-different-for-html: Provide native methods to display code.
text-different-for-react: Provide React components to display code.

demo

How to use

<script src="text-different/build/text-different.js"></script>
<script>
const oldText = 'Old text';
const newText = 'New text';

const tree = textDifferent(oldText, newText);
</script>

or

import textDifferent from 'text-different';
const oldText = 'Old text';
const newText = 'New text';

const tree = textDifferent(oldText, newText);

Result

After comparing the texts, a text tree is generated, for example:

const tree = [
  {
    status: 0,
    text: 'string'
  },
  {
    status: 1,
    text: 'string'
  },
  {
    status: 2,
    text: 'string'
  },
  {
    status: 3,
    text: [
      {
        status: 0,
        text: 'string'
      },
      {
        status: 1,
        text: 'string'
      },
      {
        status: 2,
        text: 'string'
      },
      {
        status: 0,
        text: 'string'
      }
    ]
  },
  {
    status: 0,
    text: 'string'
  },
]
  • When status is 0, there is no change in the text of the paragraph
  • When status is 1, it means that the text is deleted in the old text.
  • When status is 2, it means that the text is added in the new text.
  • When status is 3, it means that the text is modified in the text, where the text attribute is an array indicating which text in the text is being modified (added, deleted). Each data in the array has the attributes status and text.

Output analysis result

This method only provides a text parsing tree. If you want to output a parsed result, you can view example.

demo

text-different-for-html

Depends on highlightjs, you have to install it.

Sample code

<html>
  <head>
    <link rel="stylesheet" href="highlightjs/styles/xcode.css">
    <link rel="stylesheet" href="text-different/build/style/text-different.css">
  </head>
  <body>
    <div id=root></div>
    <script src="highlightjs/highlight.pack.js"></script>
    <script src="text-different/build/text-different.js"></script>
    <script src="text-different/build/text-different-for-html.js"></script>
    <script>
      const tdfh = new TextDifferentForHtml(
        document.getElementById('root'),   // The dom used to render the display code
        'python'                           // Type of code
      );
      tdfh.render({
        oldCode: 'def func():\n  x = 5',   // Old code
        newCode: 'def func():\n  x = 32',  // New code
        hasLineNumber: true                // Whether to display the line number
      });
    </script>
  </body>
</html>

Methods

  • TextDifferentForHtml:Initialization class
    parameter:
    • element { Element }:The dom used to render the display code
    • type { string }:Type of code
  • .render { Function }:Rendering code parameter:
    • object:
      • oldCode { string }:Old code
      • newCode { string }:New code

text-different-for-react

Rely on highlightjs,react,react-dom,prop-types, you must install them.

Sample code

import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import 'highlightjs/styles/xcode.css';
import 'text-different/build/style/text-different.css';
import TextDifferentForReact from 'text-different/lib/dom/react';

ReactDOM.render(
  <TextDifferentForReact type="python"
    oldCode={ 'def func():\n  x = 5' }
    newCode={ 'def func():\n  x = 32' }
  />,
  document.getElementById('root')
);

Parameter

| parameter | description | type | | --- | --- | --- | | type | Type of code | string | | oldCode | Old code | string | | newCode | New code | string | | hasLineNumber | Whether to display the line number | boolean |