Skip to content

Commit

Permalink
Diff-ing algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
Swizz committed Nov 7, 2017
0 parents commit e58cafc
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# IDE
.idea/
.vscode/

# Logs
*.log*
yarn.lock
package-lock.json

# Misc
examples
notes.md
node_modules
coverage
*.xml

# Dist
*.gz
dist/
35 changes: 35 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "trdis",
"version": "0.0.0",
"description": "The most known way to travel in space and time",
"license": "MIT",
"repository": "Swizz/trdis",
"author": "Quentin Gerodel",
"keywords": [
"trdis",
"timetravel",
"object",
"state",
"do",
"undo",
"diff",
"json"
],
"main": "dist/trdis.js",
"module": "src/index.js",
"files": [
"src",
"dist"
],
"scripts": {
"build": "npm run bundle && npm run minify",
"bundle": "rollup -i src/index.js -o dist/trdis.js -m -f umd -n trdis",
"format": "prettier --semi false --write 'src/**/*.js'",
"minify": "uglifyjs dist/trdis.js -o dist/trdis.js --mangle --compress --source-map"
},
"devDependencies": {
"prettier": "^1.8.1",
"rollup": "^0.50.0",
"uglify-js": "^3.1.8"
}
}
27 changes: 27 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export function diff(from, to) {
var done = {}
var patch = {
do: {},
undo: {}
}

for (var key in to) {
if (key in done || from[key] === to[key]) continue

patch.do[key] = to[key]
patch.undo[key] = from[key]

done[key] = true
}

for (var key in from) {
if (key in done || from[key] === to[key]) continue

patch.do[key] = to[key]
patch.undo[key] = from[key]

done[key] = true
}

return patch
}

0 comments on commit e58cafc

Please sign in to comment.