Skip to content

Instantly share code, notes, and snippets.

@mckennatim
Created July 17, 2014 03:30
Show Gist options
  • Save mckennatim/cd32e003964d4da4be98 to your computer and use it in GitHub Desktop.
Save mckennatim/cd32e003964d4da4be98 to your computer and use it in GitHub Desktop.
<meta name="description" content="Snch client and server" />
<title>ng-blank</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.js" ></script>
<script src="main.js"></script>
<body ng-app="SyncApp">
<div ng-controller="SyncCtrl">
<br>
<table>
<caption><b>sync clients with server</b> M=(C\(P\S))U(S\(P\C)) <br>
<small>(B\A) = the set of elements in B, but not in A.</small></caption>
<thead>
<tr>
<th width="15" valign="top">P <br>prior on client</th>
<th width="15" valign="top">C <br>current on client</th>
<th width="15" valign="top">S <br>server updated by other clients </th>
<th width="15" valign="top">(P\C) <br>relative complement (P-C) </th>
<th width="15" valign="top">(P\S) <br>relative complement (P-S) </th>
<th width="15" valign="top">(S\(P\C) <br>relative complement (S-(P-C)) </th>
<th width="15" valign="top">(C\(P\S)) <br>relative complement (C-(P-S)</th>
<th width="15" valign="top"> (C\(P\S)) U (S\(P\C))<br>union (S-(P-C) U (C-(P-S)</th>
</tr>
</thead>
<tbody>
<tr>
<td><ul id="thePList">
<li ng-repeat="pi in p">{{pi}} </li>
</ul></td>
<td><ul id="theCList">
<li ng-repeat="ci in c">{{ci}} </li>
</ul></td>
<td><ul id="theSList">
<li ng-repeat="si in s">{{si}} </li>
</ul></td>
<td><ul id="thePCList">
<li ng-repeat="pci in pc">{{pci.product}} </li>
</ul></td>
<td><ul id="thePSList">
<li ng-repeat="psi in ps">{{psi.product}} </li>
</ul></td>
<td><ul id="theSPCList">
<li ng-repeat="spci in spc">{{spci.product}} </li>
</ul></td>
<td><ul id="theCPSList">
<li ng-repeat="cpsi in cps">{{cpsi.product}} </li>
</ul></td>
<td><ul id="theMO2List">
<li ng-repeat="mo2i in mo2">{{mo2i.product}} </li>
</ul></td>
</tr>
</tbody>
</table>
<pre>
{{op}} <br>
{{c2}} <br>
{{s2}} <br>
{{jmo2}}
</pre>
<h4>refs</h4>
<a href="http://www.cs.umd.edu/class/fall2005/cmsc250/exam/ex1/node5.html">http://www.cs.umd.edu/class/fall2005/cmsc250/exam/ex1/node5.html</a><br>
<a href="http://en.wikipedia.org/wiki/Complement_(set_theory)#Relative_complement">http://en.wikipedia.org/wiki/Complement_(set_theory)#Relative_complement</a><br>
<a href="http://stackoverflow.com/questions/18383636/compare-2-arrays-of-objects-with-underscore-to-find-the-differnce">http://stackoverflow.com/questions/18383636/compare-2-arrays-of-objects-with-underscore-to-find-the-differnce by Jonathan Naguin</a><br>
<a href="http://stackoverflow.com/questions/7146217/merge-2-arrays-of-objects">http://stackoverflow.com/questions/7146217/merge-2-arrays-of-objects by Guya</a>
</div>
</body>
/*
Write an algorithm that will find the first duplicate in a list on the page.
For example we would return 4, for the list above.
*/
var app = angular.module("SyncApp", []);
app.controller("SyncCtrl", function($scope, SyncService){
var c = $scope.c = SyncService.c() ;
var c2 = $scope.c2 = SyncService.c2() ;
//console.log( c2);
var s = $scope.s = SyncService.s() ;
var s2 = $scope.s2 = SyncService.s2() ;
var p = $scope.p = SyncService.p() ;
var m = $scope.m = SyncService.merge(p,c,s);
//console.log(m)
var oc = $scope.oc = SyncService.a2o(c)
var os = $scope.os = SyncService.a2o(s)
var op = $scope.op = SyncService.a2o(p)
var ps = $scope.ps = SyncService.difference(op,s2, 'product');
var pc = $scope.pc = SyncService.difference(op,c2, 'product');
var spc = $scope.spc = SyncService.difference(s2,pc, 'product');
var cps = $scope.cps = SyncService.difference(c2, ps, 'product');
var mo2 = $scope.mo2 = SyncService.mergeObjs(op, c2, s2);
$scope.jmo2 = JSON.stringify(mo2, undefined, 2);
});
app.factory("SyncService", function(){
return {
s2: function(){
return [{"product":"bananas", "done" : false},
{"product":"cantelope", "done" : false},
{"product":"daipers", "done" : false},
{"product":"dog food",
"done" : false,
"tags" : [
"produce"
],
"amt" : {
"qty" : 3,
"unit" : "3lb bag"
}
},
{"product":"fish sticks", "done" : false},
{"product":"gerkins", "done" : false}]
},
c2: function(){
return [{"product":"anise", "done" : false},
{"product":"apples", "done" : false},
{"product":"bananas",
"done" : false,
"tags" : [
"produce"
],
"amt" : {
"qty" : 3,
"unit" : "3lb bag"
}
},
{"product":"cauliflower", "done" : false},
{"product":"fennel", "done" : false},
{"product":"frogs legs", "done" : false}]
},
c: function(){
return ['anise',
'apples',
'bananas',
'cauliflower',
'fennel',
'frogs legs',
]
},
p: function(){
return ['apples',
'bananas',
'cantelope',
'dog food',
'fennel'
]
},
s: function(){
return ['bananas',
'cantelope',
'daipers',
'dog food',
'fish sticks',
'gerkins'
]
},
merge: function(p,c,s){
// (C\(P\S))U(S\(P\C))
var ps = _.difference(p,s);
var pc = _.difference(p,c);
var cps = _.difference(c,ps);
var spc = _.difference(s,pc);
var u = _.union(cps, spc);
return u
},
a2o: function(arr){
var obj = _.map(arr, function(x){
return {product: x, done: false}
})
return obj
},
difference: function(array){
var prop =arguments[2];
var rest = Array.prototype.concat.apply(Array.prototype, Array.prototype.slice.call(arguments, 1));
var containsEquals = function(obj, target) {
if (obj == null) return false;
return _.any(obj, function(value) {
return value[prop] === target[prop];
});
};
return _.filter(array, function(value){
return ! containsEquals(rest, value);
});
},
union: function (arr1, arr2, prop) {
_.each(arr2, function(arr2obj) {
var arr1obj = _.find(arr1, function(arr1obj) {
return arr1obj[prop] === arr2obj[prop];
});
arr1obj ? _.extend(arr1obj, arr2obj) : arr1.push(arr2obj);
});
},
mergeObjs: function(p,c,s){
// (C\(P\S))U(S\(P\C))
var ps = this.difference(p,s, 'product');
var pc = this.difference(p,c, 'product' );
var cps = this.difference(c,ps, 'product');
var spc = this.difference(s,pc, 'product');
this.union(spc, cps, 'product');
return spc
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment