ramda#mergeDeepLeft TypeScript Examples

The following examples show how to use ramda#mergeDeepLeft. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ThingMeta.ts    From notabug with MIT License 5 votes vote down vote up
public async update(
    thingId: ThingID,
    changes: TabulatorThingChanges,
    timestamp?: number
  ): Promise<{
    counts: Partial<ThingScores>
    listingUpdates: ListingUpdate[]
  }> {
    const now = timestamp || new Date().getTime()
    const record = await this.fetch(thingId)

    if (!record.created) {
      record.created = changes.created || new Date().getTime()
    }

    if (changes.updated > record.updated || !record.updated) {
      record.updated = changes.updated || new Date().getTime()
    }

    const counts: Partial<ThingScores> = {}

    if (changes.commands) {
      counts.commands = record.counts.commands = mergeDeepLeft(
        changes.commands,
        record.scores.commands || {}
      )
    }

    for (const key of ['up', 'down', 'comment', 'replies', 'score']) {
      if (changes[key]) {
        counts[key] = record.counts[key] =
          (record.counts[key] || 0) + (changes[key] || 0)
      }
    }

    // const currentScores = record.scores
    const scores = (record.scores = calculateSortScores(record))

    const listingUpdates: ListingUpdate[] = []

    for (const sortName in scores) {
      if (!sortName) {
        continue
      }

      const sortValue = scores[sortName]

      /*
      // This optimization seems a bit buggy at the moment
      if (!changes.created && sortValue === currentScores[sortName]) {
        continue
      }
      */

      pathsForThing(record).forEach(path =>
        listingUpdates.push([
          ListingNode.soulFromPath(this.pub, `${path}/${sortName}`),
          thingId,
          sortValue,
          now
        ])
      )
    }

    record.scores = scores

    return {
      counts,
      listingUpdates
    }
  }
Example #2
Source File: describe-diff.ts    From notabug with MIT License 4 votes vote down vote up
export function describeDiff(diff: GunGraphData): TabulatorChanges | null {
  const changes: TabulatorChanges = {}

  for (const soul in diff) {
    if (!soul) {
      continue
    }

    const votesUpMatch = Schema.ThingVotesUp.route.match(soul)

    if (votesUpMatch) {
      const { _, ...votes } = diff[soul]
      const upsCount = Object.keys(votes).length
      const { thingId } = votesUpMatch
      const thingChanges: TabulatorThingChanges =
        changes[thingId] || (changes[thingId] = {})
      thingChanges.up = (thingChanges.up || 0) + upsCount
      thingChanges.score = (thingChanges.score || 0) + upsCount

      continue
    }

    const votesDownMatch = Schema.ThingVotesDown.route.match(soul)

    if (votesDownMatch) {
      const { _, ...votes } = diff[soul]
      const downsCount = Object.keys(votes).length
      const { thingId } = votesDownMatch
      const thingChanges: TabulatorThingChanges =
        changes[thingId] || (changes[thingId] = {})
      thingChanges.down = (thingChanges.down || 0) + downsCount
      thingChanges.score = (thingChanges.score || 0) - downsCount

      continue
    }

    const thingDataMatch =
      Schema.ThingData.route.match(soul) ||
      Schema.ThingDataSigned.route.match(soul)

    if (thingDataMatch) {
      const { thingId } = thingDataMatch
      const thingData = unpackNode(diff[soul])
      const { replyToId } = thingData

      if (replyToId && ThingDataNode.isCommand(thingData)) {
        const commandMap = CommentCommand.map(({
          [thingId]: thingData
        } as unknown) as any)
        const parentThingChanges: TabulatorThingChanges =
          changes[replyToId] || (changes[replyToId] = {})
        parentThingChanges.commands = mergeDeepLeft(
          commandMap,
          parentThingChanges.commands || {}
        )
      }

      continue
    }

    const thingMatch = Schema.Thing.route.match(soul)

    if (thingMatch) {
      const { thingId } = thingMatch
      const thing = diff[soul] // thing diffs can't be partial
      if (!thing) {
        continue
      }
      const { timestamp } = thing

      const opId = Thing.opId(thing)
      const replyToId = Thing.replyToId(thing)

      const thingChanges: TabulatorThingChanges =
        changes[thingId] || (changes[thingId] = {})

      thingChanges.created = timestamp

      if (timestamp > thingChanges.updated) {
        thingChanges.updated = timestamp
      }

      if (opId) {
        const opThingChanges: TabulatorThingChanges =
          changes[opId] || (changes[opId] = {})
        opThingChanges.comment = (opThingChanges.comment || 0) + 1
        if (!opThingChanges.updated || timestamp > opThingChanges.updated) {
          opThingChanges.updated = timestamp
        }
      }

      if (replyToId) {
        const parentThingChanges: TabulatorThingChanges =
          changes[replyToId] || (changes[replyToId] = {})
        parentThingChanges.replies = (parentThingChanges.replies || 0) + 1
      }

      continue
    }
  }

  return Object.keys(changes).length ? changes : null
}