Skip to content

Instantly share code, notes, and snippets.

@immigration9
Created July 9, 2018 12:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save immigration9/20d6a97a54e46d04b0fc7cf056da5773 to your computer and use it in GitHub Desktop.
Save immigration9/20d6a97a54e46d04b0fc7cf056da5773 to your computer and use it in GitHub Desktop.
const comma = ",";
const empty = "";
const cursor = ["object", "array", "none"];
class FakeJsonGen {
constructor() {
this.out = ""
this.timebase = new Date().getTime();
this.timeInterval = 5000;
this.hasKey = false;
this.hasValue = false;
this.cursor = cursor[2];
this.buffer = "";
}
setKVStatus = (k, v) => {
this.hasKey = k;
this.hasValue = v;
}
startObject = () => {
if (this.out.slice(-1) === "}" || this.out.slice(-1) === "]") {
this.buffer = this.buffer.concat(`${comma}`);
}
this.buffer = this.buffer.concat("{");
return this;
}
endObject = () => {
this.buffer = this.buffer.concat("}");
this.setKVStatus(false, false);
this.out = this.out.concat(this.buffer);
this.buffer = "";
return this;
}
startArray = () => {
this.buffer = this.buffer.concat("[");
return this;
}
setArrayValue = (value) => {
if (typeof value === Number) {
this.buffer = this.buffer.concat(value);
} else {
this.buffer = this.buffer.concat(`"${value}"`);
}
return this;
}
endArray = () => {
this.buffer = this.buffer.concat("]");
return this;
}
attachKV = (key, value) => {
if (typeof value === 'object') {
if (value.hasOwnProperty("buffer")) {
this.buffer = this.buffer.concat(`"${key}":"${value.buffer}"`);
}
} else {
this.buffer = this.buffer.concat(`"${key}":"${value}"`);
}
return this;
}
setKey = (key) => {
if (!this.hasKey) {
this.buffer = this.buffer.concat(`"${key}":`);
this.setKVStatus(true, false);
}
return this;
}
setValue = (value) => {
if (this.hasKey && !this.hasValue) {
this.buffer = this.buffer.concat(`"${value}"`);
this.setKVStatus(true, true);
}
return this;
}
returnOut = () => {
console.log(this.out);
}
}
const fake = new FakeJsonGen();
fake.startObject();
fake.attachKV("foo", fake.startObject());
fake.attachKV("bar", "foobar");
fake.endObject().endObject();
fake.returnOut();
@immigration9
Copy link
Author

Gist for creating Fake JSON file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment