Skip to content

Instantly share code, notes, and snippets.

@gngenius02
Last active July 1, 2021 18:29
Show Gist options
  • Save gngenius02/dfccb0ff4b08c765bc1d904467ffe965 to your computer and use it in GitHub Desktop.
Save gngenius02/dfccb0ff4b08c765bc1d904467ffe965 to your computer and use it in GitHub Desktop.
{"_id":"60d90843f2de503b38cedd21","asks":[{"price":"34335.41","volume":"2.945558"},{"price":"34336.91","volume":"1.957166"},{"price":"34336.92","volume":"0.198337"},{"price":"34337.14","volume":"0.58016"},{"price":"34337.18","volume":"0.062053"},{"price":"34340.25","volume":"0.089595"},{"price":"34340.62","volume":"0.093975"},{"price":"34341.99","volume":"0.224002"},{"price":"34342","volume":"0.64"},{"price":"34342.83","volume":"0.187001"}],"bids":[{"price":"34335.4","volume":"0.487535"},{"price":"34333.82","volume":"0.040469"},{"price":"34332.59","volume":"0.004369"},{"price":"34329.53","volume":"0.000295"},{"price":"34326.78","volume":"0.251112"},{"price":"34326.77","volume":"0.4"},{"price":"34325.02","volume":"0.317457"},{"price":"34325.01","volume":"0.19687"},{"price":"34325","volume":"0.001"},{"price":"34323.03","volume":"0.307214"}],"seq":553600,"time":"2021-06-27T16: 22: 42.984-07: 00"}
function getObjectType(obj) {
return Object.prototype.toString.call(obj)
}
function isObjArray(obj) {
return getObjectType(obj) === '[object Array]'
}
function isObjFunction(obj) {
return obj === '[object Function]'
}
function getLength(obj) {
return isObjArray(obj) ? obj.length : Object.keys(obj).length
}
function isObject(obj) {
return ['[object Array]', '[object Object]'].indexOf(obj) < 0
}
function compare(item1, item2) {
const itemType = getObjectType(item1)
if (isObject(itemType)) {
if (itemType !== getObjectType(item2)) return false
if (isObjFunction(itemType) && item1.toString() !== item2.toString()) return false
return true
}
if (!isEqual(item1, item2)) return false
return true
}
function isEqual(value, other) {
const type = getObjectType(value)
const valueLen = getLength(value)
if (type !== getObjectType(other) || isObject(type) || valueLen !== getLength(other)) return false
if (isObjArray(type)) {
for (let i = 0; i < valueLen; i++) {
if (!compare(value[i], other[i])) return false
}
}
for (const key of Object.keys(value)) {
if (!compare(value[key], other[key])) return false
}
return true
}
module.exports = isEqual
const data = require('./data.json');
const {highorderFn,loopFn} = require('./process');
const isEqual = require('./checkArrayEqual');
// Print the first entry of the first array for each of the functions.
// Could also print everything out instead.
console.log(highorderFn(data).sort()[0][0]);
console.log(loopFn(data).sort()[0][0]);
console.log("DO THEY MATCH: ", isEqual(highorderFn(data), loopFn(data)))
function loopFn(response) {
const dataArray = []
for (let type in response) {
if (['bids', 'asks'].includes(type)) {
const typeArray = []
for (let row of response[type]) {
typeArray.push({ ...row, type })
}
dataArray.push(typeArray)
}
}
return dataArray
}
const highorderFn = (response) => Object.keys(response).filter((key) => ['bids', 'asks'].includes(key)).map((type) => response[type].map((row) => ({ ...row, type })));
module.exports = {highorderFn, loopFn}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment