Skip to content

Commit 40e13ed

Browse files
committed
Support encoding.
1 parent 18b7be8 commit 40e13ed

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/components/JSONFormatter/JSONFormatter.jsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { MuiThemeProvider, createMuiTheme } from '@material-ui/core/styles';
33
import { UnControlled as CodeMirror } from 'react-codemirror2'
44
import SnappyJS from 'snappyjs'
55
import { Buffer } from "buffer";
6-
import { decode } from "@msgpack/msgpack";
6+
import { decode, encode } from "@msgpack/msgpack";
77

88
const zlib = require('zlib');
99
require('codemirror/mode/javascript/javascript');
@@ -83,11 +83,25 @@ export class JSONFormatter extends Component {
8383
input = input.replace(/\r?\n|\r/g, "");
8484
// check whether the input is a base64 string and then try use snappy to decompress it
8585
var base64regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/;
86+
var intRegex = /^[0-9]+$/;
8687
if (base64regex.test(input)) {
8788
var uncompressed = SnappyJS.uncompress(Buffer.from(input, 'base64'));
8889
let utf8decoder = new TextDecoder()
8990
jsonString = utf8decoder.decode(uncompressed);
9091
}
92+
else if (intRegex.test(input)) {
93+
var intValue = parseInt(input, 10); // base 10
94+
var ids = [0, intValue]; // Example: Wrap it in an array
95+
var encodedBuffer = encode(ids);
96+
var base64WithoutPadding = Buffer.from(encodedBuffer).toString('base64').replace(/=+$/, '');
97+
98+
var json = {};
99+
json["Enterprise"] = 'E_' + base64WithoutPadding;
100+
json["Organization"] = 'O_' + base64WithoutPadding;
101+
json["User"] = 'U_' + base64WithoutPadding;
102+
json["Repository"] = 'R_' + base64WithoutPadding;
103+
jsonString = JSON.stringify(json, null, 4);
104+
}
91105
else {
92106
var parts = input.split('_', 2);
93107
if (parts.length !== 2) {

0 commit comments

Comments
 (0)