made with requirebin
Created
July 31, 2015 20:30
-
-
Save dhritzkiv/da9a5fd6b17c7d41aba5 to your computer and use it in GitHub Desktop.
requirebin sketch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var rfc6902 = require("rfc6902"); | |
var starting = { | |
arr: [] | |
}; | |
var goal = { | |
arr: [1,2,3,4,5,6] | |
}; | |
var diff = rfc6902.createPatch(starting, goal); | |
//note the values are in backwards order | |
console.log(diff[0]);//{op: "add", path: "/arr/-", value: 6}; | |
/////// | |
var target = { | |
arr: [] | |
}; | |
var result = rfc6902.applyPatch(target, diff); | |
console.log(target.arr[0]);//is 6, should be 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require=function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}({rfc6902:[function(require,module,exports){(function(global){(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.rfc6902=f()}})(function(){var define,module,exports;return function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}({1:[function(_dereq_,module,exports){"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.diffAny=diffAny;var _equal=_dereq_("./equal");function pushAll(array,xs){return Array.prototype.push.apply(array,xs)}function _subtract(a,b){var obj={};for(var add_key in a){obj[add_key]=1}for(var del_key in b){delete obj[del_key]}return Object.keys(obj)}function _intersection(xs){var obj={};xs.forEach(function(x){for(var key in x){obj[key]=(obj[key]||0)+1}});var threshold=xs.length;for(var key in obj){if(obj[key]<threshold){delete obj[key]}}return Object.keys(obj)}function objectType(object){if(object===undefined){return"undefined"}if(object===null){return"null"}if(Array.isArray(object)){return"array"}return typeof object}function diffArrays(input,output,ptr){var memo={"0,0":{operations:[],cost:0}};function dist(i,j){var memoized=memo[[i,j]];if(memoized===undefined){if((0,_equal.compare)(input[i-1],output[j-1])){memoized=dist(i-1,j-1)}else{var directions=[];if(i>0){directions.push({dist:dist(i-1,j),type:"deletion"})}if(j>0){directions.push({dist:dist(i,j-1),type:"insertion"})}if(i>0&&j>0){directions.push({dist:dist(i-1,j-1),type:"substitution"})}var best=directions.sort(function(a,b){return a.dist.cost-b.dist.cost})[0];var operations=[];if(best.type==="deletion"){operations.push({op:"remove",path:ptr.add(i-1).toString()})}else if(best.type==="insertion"){var col=j-1;var path=ptr.add(col<input.length?col:"-");operations.push({op:"add",path:path.toString(),value:output[j-1]})}else{operations.push({op:"replace",path:ptr.add(j-1).toString(),value:output[j-1]})}memoized={operations:best.dist.operations.concat(operations),cost:best.dist.cost+1}}memo[[i,j]]=memoized}return memoized}var end=dist(input.length,output.length);return end.operations.reverse()}function diffObjects(input,output,ptr){var operations=[];_subtract(input,output).forEach(function(key){operations.push({op:"remove",path:ptr.add(key).toString()})});_subtract(output,input).forEach(function(key){operations.push({op:"add",path:ptr.add(key).toString(),value:output[key]})});_intersection([input,output]).forEach(function(key){pushAll(operations,diffAny(input[key],output[key],ptr.add(key)))});return operations}function diffValues(input,output,ptr){var operations=[];if(!(0,_equal.compare)(input,output)){operations.push({op:"replace",path:ptr.toString(),value:output})}return operations}function diffAny(input,output,ptr){var input_type=objectType(input);var output_type=objectType(output);if(input_type=="array"&&output_type=="array"){return diffArrays(input,output,ptr)}if(input_type=="object"&&output_type=="object"){return diffObjects(input,output,ptr)}return diffValues(input,output,ptr)}},{"./equal":2}],2:[function(_dereq_,module,exports){"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.compare=compare;function _zip(a,b){var zipped=[];for(var i=0,l=Math.min(a.length,b.length);i<l;i++){zipped.push([a[i],b[i]])}return zipped}function _compareArrays(left,right){if(left.length!==right.length)return false;return _zip(left,right).every(function(pair){return compare(pair[0],pair[1])})}function _compareObjects(left,right){var left_keys=Object.keys(left);var right_keys=Object.keys(right);if(!_compareArrays(left_keys,right_keys))return false;return left_keys.every(function(key){return compare(left[key],right[key])})}function compare(left,right){if(left===right)return true;if(Array.isArray(left)&&Array.isArray(right)){return _compareArrays(left,right)}if(Object(left)===left&&Object(right)===right){return _compareObjects(left,right)}return false}},{}],3:[function(_dereq_,module,exports){"use strict";Object.defineProperty(exports,"__esModule",{value:true});var _get=function get(_x,_x2,_x3){var _again=true;_function:while(_again){var object=_x,property=_x2,receiver=_x3;desc=parent=getter=undefined;_again=false;if(object===null)object=Function.prototype;var desc=Object.getOwnPropertyDescriptor(object,property);if(desc===undefined){var parent=Object.getPrototypeOf(object);if(parent===null){return undefined}else{_x=parent;_x2=property;_x3=receiver;_again=true;continue _function}}else if("value"in desc){return desc.value}else{var getter=desc.get;if(getter===undefined){return undefined}return getter.call(receiver)}}};function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function")}}function _inherits(subClass,superClass){if(typeof superClass!=="function"&&superClass!==null){throw new TypeError("Super expression must either be null or a function, not "+typeof superClass)}subClass.prototype=Object.create(superClass&&superClass.prototype,{constructor:{value:subClass,enumerable:false,writable:true,configurable:true}});if(superClass)Object.setPrototypeOf?Object.setPrototypeOf(subClass,superClass):subClass.__proto__=superClass}var MissingError=function(_Error){_inherits(MissingError,_Error);function MissingError(path){_classCallCheck(this,MissingError);_get(Object.getPrototypeOf(MissingError.prototype),"constructor",this).call(this,"Value required at path: "+path);this.name=this.constructor.name;this.path=path}return MissingError}(Error);exports.MissingError=MissingError;var InvalidOperationError=function(_Error2){_inherits(InvalidOperationError,_Error2);function InvalidOperationError(op){_classCallCheck(this,InvalidOperationError);_get(Object.getPrototypeOf(InvalidOperationError.prototype),"constructor",this).call(this,"Invalid operation: "+op);this.name=this.constructor.name;this.op=op}return InvalidOperationError}(Error);exports.InvalidOperationError=InvalidOperationError;var TestError=function(_Error3){_inherits(TestError,_Error3);function TestError(actual,expected){_classCallCheck(this,TestError);_get(Object.getPrototypeOf(TestError.prototype),"constructor",this).call(this,"Test failed: "+actual+" != "+expected);this.name=this.constructor.name;this.actual=actual;this.expected=expected}return TestError}(Error);exports.TestError=TestError},{}],4:[function(_dereq_,module,exports){"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.applyPatch=applyPatch;exports.patch=patch;exports.createPatch=createPatch;exports.diff=diff;function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{"default":obj}}var _errors=_dereq_("./errors");var _pointer=_dereq_("./pointer");var _patch=_dereq_("./patch");var _diff=_dereq_("./diff");var _package=_dereq_("./package");var _package2=_interopRequireDefault(_package);var version=_package2["default"].version;exports.version=version;function applyPatch(object,patch){return patch.map(function(operation){var operationFunction=_patch.operationFunctions[operation.op];if(operationFunction===undefined){return new _errors.InvalidOperationError(operation.op)}return operationFunction(object,operation)})}function patch(object,patch){console.error("rfc6902.patch(object, patch) has been deprecated. Use rfc6902.applyPatch(object, patch) instead.");return applyPatch(object,patch)}function createPatch(input,output){var ptr=new _pointer.Pointer;var operations=(0,_diff.diffAny)(input,output,ptr);operations.forEach(function(operation){operation.path=operation.path.toString()});return operations}function diff(input,output){console.error("rfc6902.diff(input, output) has been deprecated. Use rfc6902.createPatch(input, output) instead.");return createPatch(input,output)}},{"./diff":1,"./errors":3,"./package":5,"./patch":6,"./pointer":7}],5:[function(_dereq_,module,exports){module.exports={name:"rfc6902",version:"1.0.4",description:"Complete implementation of RFC6902 (patch and diff)",keywords:["json","patch","diff","rfc6902"],homepage:"https://github.com/chbrown/rfc6902",repository:"git://github.com/chbrown/rfc6902.git",author:"Christopher Brown <io@henrian.com> (http://henrian.com)",license:"MIT",main:"./rfc6902.js",dependencies:{},devDependencies:{babel:"*",babelify:"*",browserify:"*",derequire:"*","js-yaml":"*",mocha:"*"},scripts:{test:"make test"}}},{}],6:[function(_dereq_,module,exports){"use strict";Object.defineProperty(exports,"__esModule",{value:true});var _pointer=_dereq_("./pointer");var _equal=_dereq_("./equal");var _errors=_dereq_("./errors");function _add(object,key,value){if(Array.isArray(object)){if(key=="-"){object.push(value)}else{object.splice(key,0,value)}}else{object[key]=value}}function _remove(object,key){if(Array.isArray(object)){object.splice(key,1)}else{delete object[key]}}function add(object,operation){var endpoint=_pointer.Pointer.fromJSON(operation.path).evaluate(object);if(endpoint.parent===undefined){return new _errors.MissingError(operation.path)}_add(endpoint.parent,endpoint.key,operation.value);return null}function remove(object,operation){var endpoint=_pointer.Pointer.fromJSON(operation.path).evaluate(object);if(endpoint.value===undefined){return new _errors.MissingError(operation.path)}_remove(endpoint.parent,endpoint.key);return null}function replace(object,operation){var endpoint=_pointer.Pointer.fromJSON(operation.path).evaluate(object);if(endpoint.value===undefined)return new _errors.MissingError(operation.path);endpoint.parent[endpoint.key]=operation.value;return null}function move(object,operation){var from_endpoint=_pointer.Pointer.fromJSON(operation.from).evaluate(object);if(from_endpoint.value===undefined)return new _errors.MissingError(operation.from);var endpoint=_pointer.Pointer.fromJSON(operation.path).evaluate(object);if(endpoint.parent===undefined)return new _errors.MissingError(operation.path);_remove(from_endpoint.parent,from_endpoint.key);_add(endpoint.parent,endpoint.key,from_endpoint.value);return null}function copy(object,operation){var from_endpoint=_pointer.Pointer.fromJSON(operation.from).evaluate(object);if(from_endpoint.value===undefined)return new _errors.MissingError(operation.from);var endpoint=_pointer.Pointer.fromJSON(operation.path).evaluate(object);if(endpoint.parent===undefined)return new _errors.MissingError(operation.path);_remove(from_endpoint.parent,from_endpoint.key);_add(endpoint.parent,endpoint.key,from_endpoint.value);return null}function test(object,operation){var endpoint=_pointer.Pointer.fromJSON(operation.path).evaluate(object);var result=(0,_equal.compare)(endpoint.value,operation.value);if(!result)return new _errors.TestError(endpoint.value,operation.value);return null}var operationFunctions={add:add,remove:remove,replace:replace,move:move,copy:copy,test:test};exports.operationFunctions=operationFunctions},{"./equal":2,"./errors":3,"./pointer":7}],7:[function(_dereq_,module,exports){"use strict";Object.defineProperty(exports,"__esModule",{value:true});var _createClass=function(){function defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||false;descriptor.configurable=true;if("value"in descriptor)descriptor.writable=true;Object.defineProperty(target,descriptor.key,descriptor)}}return function(Constructor,protoProps,staticProps){if(protoProps)defineProperties(Constructor.prototype,protoProps);if(staticProps)defineProperties(Constructor,staticProps);return Constructor}}();function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function")}}function unescape(token){return token.replace(/~1/g,"/").replace(/~0/g,"~")}function escape(token){return token.replace(/~/g,"~0").replace(/\//g,"~1")}var Pointer=function(){function Pointer(tokens){_classCallCheck(this,Pointer);this.tokens=tokens||[""]}_createClass(Pointer,[{key:"toString",value:function toString(){return this.tokens.map(escape).join("/")}},{key:"evaluate",value:function evaluate(object){var parent=null;var token=null;for(var i=1,l=this.tokens.length;i<l;i++){parent=object;token=this.tokens[i];object=(parent||{})[token]}return{parent:parent,key:token,value:object}}},{key:"push",value:function push(token){this.tokens.push(token)}},{key:"add",value:function add(token){var tokens=this.tokens.concat(String(token));return new Pointer(tokens)}}],[{key:"fromJSON",value:function fromJSON(path){var tokens=path.split("/").map(unescape);if(tokens[0]!=="")throw new Error("Invalid JSON Pointer: "+path);return new Pointer(tokens)}}]);return Pointer}();exports.Pointer=Pointer},{}]},{},[4])(4)})}).call(this,typeof global!=="undefined"?global:typeof self!=="undefined"?self:typeof window!=="undefined"?window:{})},{}]},{},[]);var rfc6902=require("rfc6902");var starting={arr:[]};var goal={arr:[1,2,3,4,5,6]};var diff=rfc6902.createPatch(starting,goal);console.log(diff[0]);var target={arr:[]};var result=rfc6902.applyPatch(target,diff);console.log(target.arr[0]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "requirebin-sketch", | |
"version": "1.0.0", | |
"dependencies": { | |
"rfc6902": "1.0.5" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- contents of this file will be placed inside the <body> --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- contents of this file will be placed inside the <head> --> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment