Skip to content

Commit

Permalink
feat: Add sort_semver and sort_by_semver functions to $path
Browse files Browse the repository at this point in the history
  • Loading branch information
nmaguiar committed Jun 3, 2024
1 parent fa87814 commit 7b5cefa
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
17 changes: 16 additions & 1 deletion js/openaf.js
Original file line number Diff line number Diff line change
Expand Up @@ -4424,7 +4424,7 @@ var $from = function(a) {
* range(count), ranges(count, start, step)\
* inc(name), dec(name), getc(name), unset(obj, name)\
* k2a(map, keyre, outkey, removeNulls), geta(nameOrPath, arrayIndex)\
* sql_format(sql, options)\
* sql_format(sql, options), sort_semver(arrayVersions), sort_by_semver(arrayMaps, jmespathStringToVersionField)\
* \
* Custom functions:\
* $path(2, "example(@)", { example: { _func: (a) => { return Number(a) + 10; }, _signature: [ { types: [ $path().number ] } ] } });\
Expand Down Expand Up @@ -4774,6 +4774,21 @@ const $path = function(aObj, aPath, customFunctions) {
sql_format: {
_func: ar => ow.loadFormat().sqlFormat(ar[0], af.fromJSSLON(ar[1])),
_signature: [ { types: [ jmespath.types.string ] }, { types: [ jmespath.types.string ] } ]
},
sort_semver: {
_func: ar => ow.loadFormat().sortSemanticVersions(ar[0]),
_signature: [ { types: [ jmespath.types.array ] } ]
},
sort_by_semver: {
_func: ar => {
ow.loadFormat()
return ar[0].sort((a, b) => {
var aVal = $path(a, ar[1])
var bVal = $path(b, ar[1])
return new org.semver4j.Semver(aVal).isEqualTo(bVal) ? 0 : new org.semver4j.Semver(aVal).isGreaterThan(bVal) ? 1 : -1
})
},
_signature: [ { types: [ jmespath.types.array ] }, { types: [ jmespath.types.string ] } ]
}
}, customFunctions)

Expand Down
13 changes: 13 additions & 0 deletions js/owrap.format.js
Original file line number Diff line number Diff line change
Expand Up @@ -2081,6 +2081,19 @@ OpenWrap.format.prototype.percentile = function(aArray, aPercentile) {
}
}

/**
* <odoc>
* <key>ow.format.sortSemanticVersions(aArray) : Array</key>
* Will sort the provided aArray of semantic versions (e.g. 1.0.0, 1.0.1, 1.1.0, 2.0.0, ...)\
* (available after ow.loadFormat())
* </odoc>
*/
OpenWrap.format.prototype.sortSemanticVersions = function(anArray) {
return anArray.sort((a, b) => {
return new org.semver4j.Semver(a).isEqualTo(b) ? 0 : new org.semver4j.Semver(a).isGreaterThan(b) ? 1 : -1
})
}

OpenWrap.format.prototype.toBase64 = function(aString) { return af.fromBytes2String(af.toBase64Bytes(aString)); }
OpenWrap.format.prototype.fromBase64 = function(aString) { return af.fromBytes2String(af.fromBase64(aString)); }
OpenWrap.format.prototype.md2 = md2;
Expand Down
Binary file added lib/semver4j-5.3.0.jar
Binary file not shown.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,11 @@
<artifactId>sql-formatter</artifactId>
<version>2.0.5</version>
</dependency>
<dependency>
<groupId>org.semver4j</groupId>
<artifactId>semver4j</artifactId>
<version>5.3.0</version>
</dependency>
</dependencies>

</project>

0 comments on commit 7b5cefa

Please sign in to comment.