npm

esc-fmt-same-string-edges
TypeScript icon, indicating that this package has built-in type declarations

0.3.3 • Public • Published

esc-fmt-same-string-edges

Format 2 strings, human readable, easy compare, highlight beginning/middle/end (if applicable) same/different parts, algorithm/function, visualize/highlight, match unchanged context. Fast and minimal, zero dependencies, supply your own formatting functions, cli, terminal, html, React, xml.

Installation

  1. npm install --save esc-fmt-same-string-edges

Example usage

import format from 'esc-fmt-same-string-edges';
import colors from 'esc-colors';

const old = 'Hello World';
const new = 'H3110 W0r1d';
const [ fmtdOld, fmtdNew ] = format(old, new, colors.green, colors.red);
console.log('fmtdOld', fmtdOld);
console.log('fmtdNew', fmtdNew);

Demo CLI

import format from 'esc-fmt-same-string-edges';
import colors from 'esc-colors';

const demo2 = (a, b) => console.log(`${format(a, b, colors.green, colors.red                ).join('\n')}\n`);
const demo3 = (a, b) => console.log(`${format(a, b, colors.green, colors.red, colors.magenta).join('\n')}\n`);

demo2('/foo/bar/baz/qux.txt', '/foo/bar/daz/qux.txt');
demo3('/foo/bar/baz/qux.txt', '/foo/bar/daz/qux.txt');
demo3('/foo/bar/baz/baz/qux.txt', '/foo/bar/baz/qux.txt');
demo3('/foo/bar/baz/qux.txt', '/foo/bar/baz/baz/qux.txt');
demo3('/foo/bar/baz/qux.txt', '/xyz/bar/baz/qux.txt');
demo3('/foo/bar.txt', '#foo/bar.txt');
demo3('/foo/bar/baz/qux.txt', '/zoo/bar/baz/qux.txt');
demo3('/foo/bar/baz/qux.txt', '/zoo/bar/baz/qux.xml');
demo2('Hello World', 'H3110 W0r1d');
demo3('Hello World', 'H3110 W0r1d');

image

Demo HTML

import format from 'esc-fmt-same-string-edges';

// html example - make your own HTML, XML, React, etc
const fmtHtmlSameF = s => `<span class='same'>${s}</span>`;
const fmtHtmlDiffF = s => `<span class='diff'>${s}</span>`;
const demoH = (a, b) => console.log(`${format(a, b, fmtHtmlSameF, fmtHtmlDiffF).join('\n')}\n`);

console.log('/foo/bar/baz.txt\n/foo/xyz/baz.zip\nFormat as HTML');

demoH('/foo/bar/baz.txt', '/foo/xyz/baz.zip');

image

Format HTML - CLI example (you could also format HTML with HTML or whatever)

// format the HTML
import colors from 'esc-colors';

console.log('HTML difference hard to see?');
const [ fmtdOldHtml, fmtdNewHtml ] = format('/foo/bar/baz.txt', '/foo/xyz/baz.zip', fmtHtmlSameF, fmtHtmlDiffF);
const demo2 = (a, b) => console.log(`${format(a, b, colors.green, colors.red).join('\n')}\n`);
demo2(fmtdOldHtml, fmtdNewHtml);

image

What it does

If your strings are the same length then every character can be formatted according to your same/different formatting function. (with as little formatting as possible)

If your strings are different lengths then the parts of the start and end (respectively) that are the same will be formatted as such and the middle that is different will be formatted as such. (the different part can also be at the beginning or end of the strings)

Say thanks

Star the repo https://github.com/softwarecreations/esc-fmt-same-string-edges

PR's or issues

Welcome

License

MIT

Package Sidebar

Install

npm i esc-fmt-same-string-edges

Weekly Downloads

4

Version

0.3.3

License

MIT

Unpacked Size

8.14 kB

Total Files

5

Last publish

Collaborators

  • softwarecreations