Created
October 30, 2018 20:17
-
-
Save shklqm/b3396748874aa4d864be97c919ff7d91 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import moment from "moment-timezone"; | |
export const mapObjectsToArray = (objects) => { | |
let index = -1, result = Array(objects.size); | |
Object.keys(objects).forEach((objectId)=>{ | |
result[++index] = [objects[objectId]["createdAt"], objects[objectId]]; | |
}); | |
return result; | |
}; | |
export const unmapObjectsFromArray = (objectsArrays) => { | |
let result = []; | |
objectsArrays.forEach((_objectArray)=>{ | |
result.push(_objectArray[1]); | |
}); | |
return result; | |
}; | |
export const reorderObjects = (targetObjects) => { | |
let parsedObjects = {}; | |
let result = []; | |
if (targetObjects){ | |
Object.keys(targetObjects).forEach((targetObjectId) => { | |
let parentId = targetObjects[targetObjectId]["parent"]; | |
if(parentId == null){ | |
if(!(targetObjectId in parsedObjects)){ | |
parsedObjects[targetObjectId] = targetObjects[targetObjectId]; | |
parsedObjects[targetObjectId]["replies"] = []; | |
} | |
} | |
else{ | |
if(!(parentId in parsedObjects)){ | |
parsedObjects[parentId] = targetObjects[parentId]; | |
parsedObjects[parentId]["replies"] = []; | |
} | |
parsedObjects[parentId]["replies"].push(targetObjects[targetObjectId]); | |
} | |
}); | |
//sorting inner targetObjects | |
Object.keys(parsedObjects).forEach((targetObjectId) => { | |
if(parsedObjects[targetObjectId]["replies"].length){ | |
parsedObjects[targetObjectId]["replies"] = unmapObjectsFromArray( | |
mapObjectsToArray(parsedObjects[targetObjectId]["replies"]).sort( | |
(a, b) => moment(a[1].createdAt) - moment(b[1].createdAt) | |
) | |
); | |
} | |
}); | |
//sorting outer targetObjects | |
Object.keys(parsedObjects).forEach((targetObjectId) => { | |
result = unmapObjectsFromArray(mapObjectsToArray(parsedObjects) | |
.sort((a, b) => moment(a[1].createdAt) - moment(b[1].createdAt))); | |
}); | |
} | |
return result; | |
}; |
Output example
[
{
"id":85384,
"content":"Lores Ipsum",
"parent":null,
"createdAt":"2018-10-23T09:27:13.161522Z",
"replies":[
{
"id":85569,
"content":"Lores Ipsum",
"parent":85384,
"createdAt":"2018-10-26T14:18:57.130353Z"
},
{
"id":85570,
"content":"Lores Ipsum",
"parent":85384,
"createdAt":"2018-10-26T14:41:43.940199Z"
}
]
},
{
"id":85385,
"content":"Lores Ipsum",
"parent":null,
"createdAt":"2018-10-23T09:27:16.995098Z",
"replies":[
]
},
{
"id":85400,
"content":"Lores Ipsum",
"parent":null,
"createdAt":"2018-10-23T09:27:17.041750Z",
"replies":[
]
},
{
"id":85415,
"content":"Lores Ipsum",
"parent":null,
"createdAt":"2018-10-23T09:27:17.096840Z",
"replies":[
]
},
{
"id":85426,
"content":"Lores Ipsum",
"parent":null,
"createdAt":"2018-10-23T09:27:17.128810Z",
"replies":[
]
}
]
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Input example