codecombat/app/lib/deltas.coffee

158 lines
5.7 KiB
CoffeeScript
Raw Normal View History

2014-04-12 00:11:52 -04:00
###
Good-to-knows:
dataPath: an array of keys that walks you up a JSON object that's being patched
ex: ['scripts', 0, 'description']
deltaPath: an array of keys that walks you up a JSON Diff Patch object.
ex: ['scripts', '_0', 'description']
###
module.exports.expandDelta = (delta, left, schema) ->
flattenedDeltas = flattenDelta(delta)
(expandFlattenedDelta(fd, left, schema) for fd in flattenedDeltas)
2014-04-12 00:11:52 -04:00
flattenDelta = (delta, dataPath=null, deltaPath=null) ->
# takes a single jsondiffpatch delta and returns an array of objects with
return [] unless delta
dataPath ?= []
deltaPath ?= []
return [{dataPath:dataPath, deltaPath: deltaPath, o:delta}] if _.isArray delta
2014-04-12 00:11:52 -04:00
results = []
affectingArray = delta._t is 'a'
for deltaIndex, childDelta of delta
continue if deltaIndex is '_t'
dataIndex = if affectingArray then parseInt(deltaIndex.replace('_', '')) else deltaIndex
results = results.concat flattenDelta(
childDelta, dataPath.concat([dataIndex]), deltaPath.concat([deltaIndex]))
results
expandFlattenedDelta = (delta, left, schema) ->
# takes a single flattened delta and converts into an object that can be
# easily formatted into something human readable.
delta.action = '???'
o = delta.o # the raw jsondiffpatch delta
2014-04-12 00:11:52 -04:00
if _.isArray(o) and o.length is 1
delta.action = 'added'
delta.newValue = o[0]
2014-04-12 00:11:52 -04:00
if _.isArray(o) and o.length is 2
delta.action = 'modified'
delta.oldValue = o[0]
delta.newValue = o[1]
2014-04-12 00:11:52 -04:00
if _.isArray(o) and o.length is 3 and o[1] is 0 and o[2] is 0
delta.action = 'deleted'
delta.oldValue = o[0]
2014-04-12 00:11:52 -04:00
if _.isPlainObject(o) and o._t is 'a'
delta.action = 'modified-array'
2014-04-12 00:11:52 -04:00
if _.isPlainObject(o) and o._t isnt 'a'
delta.action = 'modified-object'
2014-04-14 14:14:09 -04:00
if _.isArray(o) and o.length is 3 and o[2] is 3
2014-04-12 00:11:52 -04:00
delta.action = 'moved-index'
delta.destinationIndex = o[1]
delta.originalIndex = delta.dataPath[delta.dataPath.length-1]
2014-04-12 00:11:52 -04:00
if _.isArray(o) and o.length is 3 and o[1] is 0 and o[2] is 2
delta.action = 'text-diff'
delta.unidiff = o[0]
2014-04-12 00:11:52 -04:00
humanPath = []
parentLeft = left
parentSchema = schema
2014-04-12 00:11:52 -04:00
for key, i in delta.dataPath
# TODO: A more comprehensive way of getting child schemas
childSchema = parentSchema?.items or parentSchema?.properties?[key] or {}
childLeft = parentLeft?[key]
2014-04-12 00:11:52 -04:00
humanKey = null
childData = if i is delta.dataPath.length-1 and delta.action is 'added' then o[0] else childLeft
humanKey ?= childData.name or childData.id if childData
humanKey ?= "#{childSchema.title} ##{key+1}" if childSchema.title and _.isNumber(key)
humanKey ?= "#{childSchema.title}" if childSchema.title
humanKey ?= _.string.titleize key
humanPath.push humanKey
parentLeft = childLeft
parentSchema = childSchema
2014-04-12 00:11:52 -04:00
delta.humanPath = humanPath.join(' :: ')
delta.schema = childSchema
delta.left = childLeft
delta.right = jsondiffpatch.patch childLeft, delta.o unless delta.action is 'moved-index'
2014-04-12 00:11:52 -04:00
delta
2014-04-12 00:11:52 -04:00
module.exports.makeJSONDiffer = ->
hasher = (obj) -> obj.name || obj.id || obj._id || JSON.stringify(_.keys(obj))
jsondiffpatch.create({objectHash:hasher})
module.exports.getConflicts = (headDeltas, pendingDeltas) ->
# headDeltas and pendingDeltas should be lists of deltas returned by interpretDelta
# Returns a list of conflict objects with properties:
# headDelta
# pendingDelta
# The deltas that have conflicts also have conflict properties pointing to one another.
2014-04-12 00:11:52 -04:00
headPathMap = groupDeltasByAffectingPaths(headDeltas)
pendingPathMap = groupDeltasByAffectingPaths(pendingDeltas)
paths = _.keys(headPathMap).concat(_.keys(pendingPathMap))
2014-04-12 00:11:52 -04:00
# Here's my thinking:
# A) Conflicts happen when one delta path is a substring of another delta path
# B) A delta from one self-consistent group cannot conflict with another
# So, sort the paths, which will naturally make conflicts adjacent,
# and if one is identified, one path is from the headDeltas, the other is from pendingDeltas
# This is all to avoid an O(nm) brute force search.
2014-04-12 00:11:52 -04:00
conflicts = []
paths.sort()
for path, i in paths
continue if i + 1 is paths.length
nextPath = paths[i+1]
if nextPath.startsWith path
headDelta = (headPathMap[path] or headPathMap[nextPath])[0].delta
pendingDelta = (pendingPathMap[path] or pendingPathMap[nextPath])[0].delta
conflicts.push({headDelta:headDelta, pendingDelta:pendingDelta})
pendingDelta.conflict = headDelta
headDelta.conflict = pendingDelta
return conflicts if conflicts.length
groupDeltasByAffectingPaths = (deltas) ->
metaDeltas = []
for delta in deltas
conflictPaths = []
if delta.action is 'moved-index'
# every other action affects just the data path, but moved indexes affect a swath
indices = [delta.originalIndex, delta.destinationIndex]
indices.sort()
for index in _.range(indices[0], indices[1]+1)
conflictPaths.push delta.dataPath.slice(0, delta.dataPath.length-1).concat(index)
else
conflictPaths.push delta.dataPath
for path in conflictPaths
metaDeltas.push {
delta: delta
path: (item.toString() for item in path).join('/')
}
_.groupBy metaDeltas, 'path'
module.exports.pruneConflictsFromDelta = (delta, conflicts) ->
# the jsondiffpatch delta mustn't include any dangling nodes,
# or else things will get removed which shouldn't be, or errors will occur
for conflict in conflicts
prunePath delta, conflict.pendingDelta.deltaPath
if _.isEmpty delta then undefined else delta
prunePath = (delta, path) ->
if path.length is 1
delete delta[path]
else
prunePath delta[path[0]], path.slice(1)
keys = (k for k in _.keys(delta[path[0]]) when k isnt '_t')
delete delta[path[0]] if keys.length is 0