[feat] add diffLines function

This commit is contained in:
Jay 2020-09-14 11:24:45 +08:00
parent a8f36b2923
commit 0dbb651a74
7 changed files with 115 additions and 8 deletions

View File

@ -1,11 +1,14 @@
const test = require('ava')
const { diff } = require('../index')
const { diffChars } = require('../index')
test('diff', (t) => {
test('diff chars', (t) => {
const left = 'asd'
const right = 'asd'
const result = diff(left, right)
console.log(result)
const result = diffChars(left, right)
t.is(result.length, 1)
t.is(result[0].count, 3)
t.is(result[0].value, 'asd')
t.is(result[0].added, false)
t.is(result[0].removed, false)
})

5
index.d.ts vendored
View File

@ -2,3 +2,8 @@ export const diffChars: (
left: string,
right: string,
) => [{ count: number; value: string; added: boolean; removed: boolean }]
export const diffLines: (
left: string,
right: string,
) => [{ count: number; value: string; added: boolean; removed: boolean }]

View File

@ -1,12 +1,12 @@
const path = require("path");
const { loadBinding } = require("@node-rs/helper");
try {
// __dirname means load native addon from current dir
// 'index' means native addon name is `index`
// the value of this two arguments was decided by `build` script in `package.json`
module.exports = loadBinding(path.join(__dirname, "index", "@mtfos/rs-diff"));
module.exports = loadBinding(__dirname, "index", "@mtfos/rs-diff");
} catch (e) {
console.log(e);
try {
module.exports = require(`@mtfos/rs-diff-${process.platform}`);
} catch (e) {

12
package-lock.json generated
View File

@ -99,6 +99,18 @@
}
}
},
"@mtfos/rs-diff-darwin": {
"version": "0.0.6",
"resolved": "https://npm.trj.tw/@mtfos%2frs-diff-darwin/-/rs-diff-darwin-0.0.6.tgz",
"integrity": "sha512-Qhu9vfTdUoIGcJl4uuaeb4fjU64RNozqmfGZzdMtzruOZmI0sLZJInBm4ZjtVXGjowrtuzeLtqNB+e0a0cbCRA==",
"optional": true
},
"@mtfos/rs-diff-linux": {
"version": "0.0.6",
"resolved": "https://npm.trj.tw/@mtfos%2frs-diff-linux/-/rs-diff-linux-0.0.6.tgz",
"integrity": "sha512-wQwnstELnuVH/6BXtrgDG7SmJtlM4UO13MK0Q+zUFvr7IOdOVdX8oqet/4WEOtful7iid2KlYWnu0xXJG2XIyQ==",
"optional": true
},
"@node-rs/helper": {
"version": "0.4.0",
"resolved": "https://registry.npmjs.org/@node-rs/helper/-/helper-0.4.0.tgz",

View File

@ -50,4 +50,4 @@
"@mtfos/rs-diff-darwin": "^0.0.6",
"@mtfos/rs-diff-linux": "^0.0.6"
}
}
}

View File

@ -17,6 +17,7 @@ register_module!(example, init);
fn init(module: &mut Module) -> Result<()> {
module.create_named_method("diffChars", diff_char)?;
module.create_named_method("diffLines", diff_line)?;
Ok(())
}
@ -32,7 +33,34 @@ fn diff_char(ctx: CallContext) -> Result<JsObject> {
let mut idx = 0;
for it in res.iter() {
let mut item = ctx.env.create_object().unwrap();
item.set_named_property(
"count",
ctx.env.create_int64(it.count.try_into().unwrap()).unwrap(),
)?;
item.set_named_property("value", ctx.env.create_string(&it.value).unwrap())?;
item.set_named_property("added", ctx.env.get_boolean(it.added).unwrap())?;
item.set_named_property("removed", ctx.env.get_boolean(it.removed).unwrap())?;
obj.set_element(idx, item)?;
idx += 1;
}
Ok(obj)
}
#[js_function(2)]
fn diff_line(ctx: CallContext) -> Result<JsObject> {
let str1: String = ctx.get::<JsString>(0)?.try_into()?;
let str2: String = ctx.get::<JsString>(1)?.try_into()?;
let res = mdiff::diff_lines(str1.as_str(), str2.as_str());
let mut obj = ctx.env.create_array_with_length(res.len()).unwrap();
let mut idx = 0;
for it in res.iter() {
let mut item = ctx.env.create_object().unwrap();
item.set_named_property(
"count",
ctx.env.create_int64(it.count.try_into().unwrap()).unwrap(),

View File

@ -13,6 +13,14 @@ pub enum Mode {
RIGHT,
}
fn get_line_res(s: &String, mode: Mode) -> Res {
Res {
count: s.split('\n').count(),
added: if mode == Mode::RIGHT { true } else { false },
removed: if mode == Mode::LEFT { true } else { false },
value: s.clone(),
}
}
fn get_res(s: &String, mode: Mode) -> Res {
Res {
count: s.chars().count(),
@ -64,3 +72,54 @@ pub fn diff_chars(s1: &str, s2: &str) -> Vec<Res> {
result
}
pub fn diff_lines(s1: &str, s2: &str) -> Vec<Res> {
let mut result: Vec<Res> = Vec::new();
let mut mode = Mode::INIT;
let mut s: String = String::new();
for d in diff::lines(s1, s2) {
match d {
diff::Result::Left(l) => {
if mode != Mode::LEFT && mode != Mode::INIT {
result.push(get_line_res(&s, mode));
s.clear();
}
mode = Mode::LEFT;
if s.len() > 0 {
s.push('\n')
}
s.push_str(l);
}
diff::Result::Right(r) => {
if mode != Mode::RIGHT && mode != Mode::INIT {
result.push(get_line_res(&s, mode));
s.clear();
}
mode = Mode::RIGHT;
if s.len() > 0 {
s.push('\n')
}
s.push_str(r);
}
diff::Result::Both(l, _) => {
if mode != Mode::BOTH && mode != Mode::INIT {
result.push(get_line_res(&s, mode));
s.clear()
}
mode = Mode::BOTH;
if s.len() > 0 {
s.push('\n')
}
s.push_str(l);
}
}
}
if s.len() > 0 {
result.push(get_line_res(&s, mode));
s.clear();
}
result
}