Created
July 9, 2018 12:53
-
-
Save immigration9/20d6a97a54e46d04b0fc7cf056da5773 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Gist for creating Fake JSON file