SimpleTwitter
function Twitter() { this.userMap = new Map() }; Twitter.prototype.createUser = function(userId){ let userObj = {} userObj.funs = [] userObj.msgList = [] this.userMap.set(userId,userObj) } Twitter.prototype.createTwitter = function(userId, tweetId){ let msgObj = {} msgObj.author = userId msgObj.tweetId = tweetId msgObj.time = +new Date() return msgObj } Twitter.prototype.postTwitter = function(userId, tweetId) { if(this.userMap.has(userId)){ let userObj = this.userMap.get(userId) let msgObj = this.createTwitter(userId, tweetId) userObj.msgList.unshift(msgObj) userObj.funs.map((item)=>{ let funsObj = this.userMap.get(item) funsObj.msgList.unshift(msgObj) this.userMap.set(item,funsObj) }) }else{ this.createUser(userId) let msgObj = this.createTwitter(userId,tweetId) let userObj = this.userMap.get(userId) userObj.msgList.unshift(msgObj) this.userMap.set(userId,userObj) } }; Twitter.prototype.getNewsFeed = function(userId) { let newsFeedIDArr = [] if(this.userMap.has(userId)){ let userObj = this.userMap.get(userId) let newsFeedArr = userObj.msgList.length > 10 ? userObj.msgList.slice(0,10) : userObj.msgList newsFeedIDArr = newsFeedArr.map((item)=> { return item.tweetId }) } return newsFeedIDArr }; Twitter.prototype.follow = function(followerId, followeeId) { if(!this.userMap.has(followerId)){ this.createUser(followerId) } if(!this.userMap.has(followeeId)){ this.createUser(followeeId) } let followerIdMsgList = this.userMap.get(followerId).msgList let followeeIdMsgList = this.userMap.get(followeeId).msgList followerIdMsgList = followerIdMsgList.concat(followeeIdMsgList) followerIdMsgList.sort((a,b)=>b.time-a.time) let followerIdObj = this.userMap.get(followerId) followerIdObj.msgList = followerIdMsgList this.userMap.set(followerId,followerIdObj) let userObj = this.userMap.get(followeeId) userObj.funs.push(followerId) this.userMap.set(followeeId,userObj) }; Twitter.prototype.unfollow = function(followerId, followeeId) { let followeeIdObj = this.userMap.get(followeeId) followeeIdObj.funs = followeeIdObj.funs.filter((item)=>{ return item != followerId }) this.userMap.set(followeeId,followeeIdObj) let followerIdObj = this.userMap.get(followerId) let msgList = [] for(let i = 0;i < followerIdObj.msgList.length;i++){ let item = followerIdObj.msgList[i] if(item.author != followeeId){ msgList.push(item) } } followerIdObj.msgList = msgList this.userMap.set(followerId,followerIdObj) };
以自己现在的努力程度,还没有资格和别人拼天赋