Skip to content

Commit b1e5198

Browse files
Nuno Miguel AguiarNuno Miguel Aguiar
authored andcommitted
Add new transformation functions: field2byte, field2date, field2si, and val2icon
1 parent ba87a5c commit b1e5198

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

data/completion.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,12 @@ complete:
502502
desc: If true and the input is text based will perfom the diff at the sentence level
503503
- name: diffchars=
504504
desc: If true and the input is text based will perform the diff at the char level
505+
- name: field2byte=
506+
desc: A comma delimited list of fields whose value should be converted to a byte abbreviation
507+
- name: field2date=
508+
desc: A comma delimited list of fields whose value should be converted to date values
509+
- name: field2si=
510+
desc: A comma delimited list of fields whose value should be converted to a SI abbreviation
505511
- name: flatmap=
506512
desc: If true a map structure will be flat to just one level -optionally flatmapsep=[char] to use a different separator that '.'-
507513
- name: getlist=
@@ -609,3 +615,5 @@ complete:
609615
desc: A .py file with function code to manipulate each input array record as 'args'. Returns the transformed 'args' record
610616
- name: xrfn=
611617
desc: A javascript code, receiving each input array record as 'args' and return it's code evaluation
618+
- name: val2icon=
619+
desc: If true will transform undefined, null and boolean values to emoticons

src/docs/USAGE.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ These options will change the parsed input data included any filters provided.
112112
| 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 |
113113
| denormalize | String | Reverses 'normalize' given a JSON/SLON map with a normalize schema (see OpenAF's ow.ai.normalize.withSchema) |
114114
| diff | String | A JSON/SLON map with a 'a' path and a 'b' path to compare and provide diff data |
115+
| field2byte | String | A comma delimited list of fields whose value should be converted to a byte abbreviation |
116+
| field2date | String | A comma delimited list of fields whose value should be converted to date values |
117+
| field2si | String | A comma delimited list of fields whose value should be converted to a SI abbreviation |
115118
| flatmap | Boolean | If true a map structure will be flat to just one level (optionally flatmapsep=[char] to use a different separator that '.') |
116119
| getlist | Number | If true will try to find the first array on the input value (if number will stop only after the number of checks) |
117120
| forcearray | Boolean | If true and if the input is map it will force it to be an array with that map as the only element |
@@ -136,6 +139,7 @@ These options will change the parsed input data included any filters provided.
136139
| sortmapkeys | Boolean | If true the resulting map keys will be sorted |
137140
| spacekeys | String | Replaces spaces in keys with the provided string (for example, helpful to xml output) |
138141
| trim | Boolean | If true all the strings of the result map/list will be trimmed |
142+
| val2icon | Boolean | If true will transform undefined, null and boolean values to emoticons |
139143
| xjs | String | A .js file with function code manipulating an input 'args'. Returns the transformed 'args' variable. |
140144
| xpy | String | A .py file with Python function code manipulating an input 'args'. Returns the transformed 'args' variable. |
141145
| xfn | String | A javascript code, receiving input as 'args' and return it's code evaluation. |

src/include/transformFns.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,5 +527,48 @@ var _transformFns = {
527527
return pForEach(_r, _f)
528528
}
529529
return _r
530+
},
531+
"val2icon": _r => {
532+
if (toBoolean(params.val2icon)) {
533+
ow.loadFormat()
534+
traverse(_r, (aK, aV, aP, aO) => {
535+
if (isUnDef(aV) || isNull(aV)) {
536+
aO[aK] = "🕳️"
537+
} else {
538+
if (isBoolean(aV)) {
539+
aO[aK] = aV ? "✅" : "❌"
540+
}
541+
}
542+
})
543+
}
544+
return _r
545+
},
546+
"field2date": _r => {
547+
let _lst = params.field2date.split(",").map(r => r.trim())
548+
traverse(_r, (aK, aV, aP, aO) => {
549+
if (_lst.indexOf(aP.length > 0 && !aP.startsWith("[") ? aP.substring(1) + "." + aK : aK) >= 0 && isNumber(aV) && aV > 0) {
550+
try { aO[aK] = new Date(aV) } catch(e) {}
551+
}
552+
})
553+
return _r
554+
},
555+
"field2si": _r => {
556+
let _lst = params.field2si.split(",").map(r => r.trim())
557+
traverse(_r, (aK, aV, aP, aO) => {
558+
if (_lst.indexOf(aP.length > 0 && !aP.startsWith("[") ? aP.substring(1) + "." + aK : aK) >= 0 && isNumber(aV)) {
559+
aO[aK] = ow.format.toAbbreviation(aV)
560+
}
561+
})
562+
return _r
563+
},
564+
"field2byte": _r => {
565+
let _lst = params.field2byte.split(",").map(r => r.trim())
566+
traverse(_r, (aK, aV, aP, aO) => {
567+
if (_lst.indexOf(aP.length > 0 && !aP.startsWith("[") ? aP.substring(1) + "." + aK : aK) >= 0 && isNumber(aV)) {
568+
aO[aK] = ow.format.toBytesAbbreviation(aV)
569+
}
570+
})
571+
return _r
530572
}
573+
531574
}

0 commit comments

Comments
 (0)