Skip to content

Instantly share code, notes, and snippets.

@Zejnilovic
Last active July 29, 2019 14:10
Show Gist options
  • Save Zejnilovic/60cb9d6b9956ae8a83824fc99f564f74 to your computer and use it in GitHub Desktop.
Save Zejnilovic/60cb9d6b9956ae8a83824fc99f564f74 to your computer and use it in GitHub Desktop.
Smart JSON Editor transformer for Decimal(38,18)
// https://github.com/SmartJSONEditor/PublicDocuments/wiki/ValueTransformers
var ValueTransformer = function () {
this.displayName = "Decimal(38,18)";
this.shortDescription = "https://spark.apache.org/docs/2.4.0/api/java/org/apache/spark/sql/types/Decimal.html"
this.transform = function (inputValue, jsonValue, arrayIndex, parameters, info) {
var result = '';
var characters = '0123456789';
var charactersLength = characters.length;
var precision = 38;
var scale = 18;
var afterDot = false;
var actualScale = 0
for ( var i = 0; i < precision; i++ ) {
if (!afterDot && i > 0 && (i == 10 || (Math.random() * charactersLength) > 8)) {
result += '.';
afterDot = true
}
result += characters.charAt(Math.floor(Math.random() * charactersLength));
if (afterDot) {
actualScale += 1
if (Math.random() * charactersLength > 8) {
break;
}
}
}
if (actualScale < 18) {
result += "0".repeat(18 - actualScale)
}
return result;
};
}
function sjeClass() {
return new ValueTransformer();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment