Skip to content

Instantly share code, notes, and snippets.

@savelichalex
Created September 4, 2017 10:07
Show Gist options
  • Save savelichalex/18572e0e46a1e8ca27181c5ca5cd7c9a to your computer and use it in GitHub Desktop.
Save savelichalex/18572e0e46a1e8ca27181c5ca5cd7c9a to your computer and use it in GitHub Desktop.
const normalizeChatAsync = ({ Messages, Users, ...rest }) => {
let messagesIds = [];
let usersIds = [];
return new Promise((resolve) => {
Promise.all(
Messages.map(
mes => Promise.resolve(mes)
.then(mes => mes.id)
)
).then(mesIds => {
messagesIds = mesIds;
if (usersIds.length > 0) {
resolve({
...rest,
Messages: messagesIds,
Users: usersIds,
enitities: {
Messages,
Users,
},
});
}
});
Promise.all(
Users.map(
user => Promise.resolve(user)
.then(user => user.id)
)
).then(uIds =>
Object.keys(
uIds.reduce((acc, id) => {
if (acc[id] == null) {
acc[id] = true;
return acc;
}
return acc;
}, {})
)
).then(uIds => {
usersIds = uIds;
if (messagesIds.length > 0) {
resolve({
...rest,
Messages: messagesIds,
Users: usersIds,
enitities: {
Messages,
Users,
},
});
}
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment