Skip to content

Commit

Permalink
adding transform diff
Browse files Browse the repository at this point in the history
  • Loading branch information
nmaguiar committed Mar 17, 2024
1 parent ce0a777 commit 5893137
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 3 deletions.
25 changes: 25 additions & 0 deletions src/docs/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ These options will change the parsed input data included any filters provided.
| arraytomapkey | String | For arraytomap=true defines the name of the map property that will be each element key (see arraytomapkeepkey) |
| cmlt | Boolean | If true will accumulate the input values into an output array (useful with loop) |
| correcttypes | Boolean | If true will try to convert alpha-numeric field values with just numbers to number fields, string date fields to dates and boolean fields |
| diff | String | A JSON/SLON map with a 'a' path and a 'b' path to compare and provide diff data |
| flatmap | Boolean | If true a map structure will be flat to just one level |
| jsonschema | String | The JSON schema file to use for validation returning a map with a boolean valid and errors if exist |
| jsonschemacmd | String | Alternative option to 'jsonschema' to retrieve the JSON schema data to use for validation returning a map with a boolean valid and errors if exist |
Expand Down Expand Up @@ -141,6 +142,8 @@ List of available formats to use with the _output_ option:

> For 'template' check https://github.com/OpenAF/openaf-opacks/blob/master/oafproc/docs/TEMPLATE.md
> For 'log' you can use 'logtheme' or the environment variable 'OAFP_LOGTHEME' with a JSON/SLON map with the colors to use '(errorLevel: red, warnLevel: yellow, timestamp: bold)'
---

## 🧾 JSON input options
Expand Down Expand Up @@ -277,6 +280,28 @@ List of options to use when _cmlt=true_:

---

## 🧾 Diff transform options

List of options to use when _diff=..._:

| Option | Type | Description |
|--------|------|-------------|
| difftheme | String | A JSON/SLON map with the colors to use if color = true: "(added: GREEN, removed: RED, common: FAINT, linenum: ITALIC, linediv: FAINT, linesep: '|')" |
| diffnlines | Boolean | If true will append each line with a line number of the final result of the differences between 'a' and 'b' (just for rough reference) |
| diffwords | Boolean | If true and the input is text based will perform the diff at the word level |
| diffwordswithspace | Boolean | If true and the input is text based will perform the diff at the word + spaces level |
| difflines | Boolean | If true and the input is text based will perform the diff at the lines level |
| diffsentences | Boolean | If true and the input is text based will perfom the diff at the sentence level |
| diffchars | Boolean | If true and the input is text based will perform the diff at the char level |

> If color=true a visual colored diff will be output insted of an array of differences
> If both inputs are array based and color=false (or not provided) the comparition will be performed at the array elements level
> The contents of 'difftheme' can also be provided through the 'OAFP_DIFFTHEME' environment variable
---

## 🧾 Log output options

List of options to use when _out=log_:
Expand Down
13 changes: 10 additions & 3 deletions src/include/outputFns.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,25 @@ var _outputFns = new Map([
var _arr = r
if (isMap(r)) _arr = [ r ]
if (isArray(_arr)) {
if (isUnDef(params.logtheme) && isDef(getEnv("OAFP_LOGTHEME"))) params.logtheme = getEnv("OAFP_LOGTHEME")
let _lt = _fromJSSLON(_$(params.logtheme, "logtheme").isString().default(""))
_lt = merge({
errorLevel: "RED,BOLD",
warnLevel : "YELLOW",
timestamp : "BOLD"
}, _lt)
_arr.forEach(_r => {
if (isMap(_r)) {
let d = (isDef(_r["@timestamp"]) ? _r["@timestamp"] : __)
let l = (isDef(_r.level) ? _r.level : __)
let m = (isDef(_r.message) ? _r.message : __)
let lineC
if (isDef(l)) {
if (l.toLowerCase().indexOf("err") >= 0) lineC = "RED,BOLD"
if (l.toLowerCase().indexOf("warn") >= 0) lineC = "YELLOW"
if (l.toLowerCase().indexOf("err") >= 0) lineC = _lt.errorLevel
if (l.toLowerCase().indexOf("warn") >= 0) lineC = _lt.warnLevel
}
if (isDef(d) && d.length > 24) d = d.substring(0, 23) + "Z"
if (isDef(m) || isDef(d)) print(ansiColor("BOLD", d) + (isDef(l) ? " | " + ansiColor(lineC, l) : "") + " | " + ansiColor(lineC, m))
if (isDef(m) || isDef(d)) print(ansiColor(_lt.timestamp, d) + (isDef(l) ? " | " + ansiColor(lineC, l) : "") + " | " + ansiColor(lineC, m))
}
})
}
Expand Down
85 changes: 85 additions & 0 deletions src/include/transformFns.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,91 @@ var _transformFns = {
}
}
},
"diff": _r => {
var _d = _fromJSSLON(params.diff)
if (isMap(_d)) {
if (isUnDef(_d.filea) || isUnDef(_d.fileb) || isUnDef(_d.a) || isUnDef(_d.b)) _exit(-1, "diff.a path and diff.b path are required.")

loadDiff()
let _d1 = $path(_r, _d.a), _d2 = $path(_r, _d.b), _dt = __
if (toBoolean(params.color)) {
if (isUnDef(params.difftheme) && isDef(getEnv("OAFP_DIFFTHEME"))) params.difftheme = getEnv("OAFP_DIFFTHEME")
_dt = _fromJSSLON(_$(params.difftheme, "difftheme").isString().default(""))
_dt = merge({
added : "GREEN",
removed: "RED",
common : "FAINT",
linenum: "ITALIC",
linediv: "FAINT",
linesep: "|"
}, _dt)
}

let _f = (s, e1) => {
if (toBoolean(params.color)) {
if (isUnDef(e1)) e1 = ""
var _o = new Set()
if (isArray(s)) {
let _c = 1
let _ssl = toBoolean(params.diffnlines), _mnl = 0
if (_ssl) {
s.forEach(v => {
_mnl += v.value.split("\n").length
})
_mnl = String(_mnl).length+1
}
let _sl = inc => {
let _o
if (_ssl && e1 != "") {
_o = ansiColor(_dt.linenum, (inc > 0 ? $ft("% " + _mnl + "d", _c) : " ".repeat(_mnl)) ) + ansiColor(_dt.linediv, `${_dt.linesep}`)
} else {
_o = ""
}
_c += inc
return _o
}
s.forEach((sr, i) => {
var _v = sr.value
if (isString(_v)) {
if (e1 != "") {
_v = _v.split(e1)
if (_v[_v.length - 1] == "") _v.pop()
} else {
_v = [ _v ]
}
}
_o.add( (sr.added ? _v.map(_l => _sl(1) + ansiColor(_dt.added, (e1 != "" ? "+" : "") + _l) ).join(ansiColor("RESET", e1)) :
sr.removed ? _v.map(_l => _sl(0) + ansiColor(_dt.removed, (e1 != "" ? "-" : "") + _l) ).join(ansiColor("RESET", e1)) :
_v.map(_l => _sl(1) + ansiColor(_dt.common, (e1 != "" ? " " : "") + _l) ).join(ansiColor("RESET", e1)) ))
})
}
return Array.from(_o).join(ansiColor("RESET", e1))
}

return $from(s).select({count:__,added:false,removed:false,value:[]})
}

if (isString(_d1) || isString(_d2)) {
if (toBoolean(params.diffwords)) {
return _f(JsDiff.diffWords(_d1, _d2, _d.options))
} else if (toBoolean(params.diffwordswithspace)) {
return _f(JsDiff.diffWordsWithSpace(_d1, _d2, _d.options))
} else if (toBoolean(params.difflines)) {
return _f(JsDiff.diffLines(_d1, _d2, _d.options), "\n")
} else if (toBoolean(params.diffsentences)) {
return _f(JsDiff.diffSentences(_d1, _d2, _d.options), "\n")
} else {
return _f(JsDiff.diffChars(_d1, _d2, _d.options))
}
} else {
if (isArray(_d1) && isArray(_d2) && !toBoolean(params.color)) {
return _f(JsDiff.diffArrays(_d1, _d2, _d.options))
} else {
return _f(JsDiff.diffJson(_d1, _d2, _d.options), "\n")
}
}
}
},
"jsonschemagen" : _r => {
if (toBoolean(params.jsonschemagen)) {
ow.loadObj()
Expand Down

0 comments on commit 5893137

Please sign in to comment.