Skip to content

Instantly share code, notes, and snippets.

@Akkuma
Created October 22, 2015 13:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Akkuma/8c64bbd4d6b3ad7328b7 to your computer and use it in GitHub Desktop.
Save Akkuma/8c64bbd4d6b3ad7328b7 to your computer and use it in GitHub Desktop.
!function(t){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var e;e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,e.jiff=t()}}(function(){return function t(e,r,n){function o(i,u){if(!r[i]){if(!e[i]){var c="function"==typeof require&&require;if(!u&&c)return c(i,!0);if(a)return a(i,!0);var p=Error("Cannot find module '"+i+"'");throw p.code="MODULE_NOT_FOUND",p}var f=r[i]={exports:{}};e[i][0].call(f.exports,function(t){var r=e[i][1][t];return o(r?r:t)},f,f.exports,t,e,r,n)}return r[i].exports}for(var a="function"==typeof require&&require,i=0;i<n.length;i++)o(n[i]);return o}({1:[function(t,e,r){function n(t,e,r){return a(t,e,"",o(r,[])).patch}function o(t,e){return"object"==typeof t?{patch:e,hash:f(h,t.hash,b),makeContext:f(h,t.makeContext,l),invertible:!(t.invertible===!1)}:{patch:e,hash:f(h,t,b),makeContext:l,invertible:!0}}function a(t,e,r,n){return Array.isArray(t)&&Array.isArray(e)?u(t,e,r,n):g(t)&&g(e)?i(t,e,r,n):p(t,e,r,n)}function i(t,e,r,n){var o,i,u=Object.keys(e),c=n.patch;for(o=u.length-1;o>=0;--o){i=u[o];var p=r+"/"+x(i);void 0!==t[i]?a(t[i],e[i],p,n):c.push({op:"add",path:p,value:e[i]})}for(u=Object.keys(t),o=u.length-1;o>=0;--o)if(i=u[o],void 0===e[i]){var f=r+"/"+x(i);n.invertible&&c.push({op:"test",path:f,value:t[i]}),c.push({op:"remove",path:f})}return n}function u(t,e,r,n){var o=v.map(n.hash,t),a=v.map(n.hash,e),i=s.compare(o,a);return c(t,e,r,n,i)}function c(t,e,r,n,o){var i=0;return s.reduce(function(n,o,u,c){var p,f,l=n.patch,h=r+"/"+(c+i);return o===s.REMOVE?(p=l[l.length-1],f=n.makeContext(c,t),n.invertible&&l.push({op:"test",path:h,value:t[c],context:f}),void 0!==p&&"add"===p.op&&p.path===h?(p.op="replace",p.context=f):l.push({op:"remove",path:h,context:f}),i-=1):o===s.ADD?(l.push({op:"add",path:h,value:e[u],context:n.makeContext(c,t)}),i+=1):a(t[c],e[u],h,n),n},n,o)}function p(t,e,r,n){return t!==e&&(n.invertible&&n.patch.push({op:"test",path:r,value:t}),n.patch.push({op:"replace",path:r,value:e})),n}function f(t,e,r){return t(e)?e:r}function l(){return void 0}function h(t){return"function"==typeof t}/** @license MIT License (c) copyright 2010-2014 original author or authors */
var s=t("./lib/lcs"),v=t("./lib/array"),y=t("./lib/jsonPatch"),d=t("./lib/inverse"),m=t("./lib/jsonPointer"),x=m.encodeSegment;r.diff=n,r.patch=y.apply,r.patchInPlace=y.applyInPlace,r.inverse=d,r.clone=y.clone,r.InvalidPatchOperationError=t("./lib/InvalidPatchOperationError"),r.TestFailedError=t("./lib/TestFailedError"),r.PatchNotInvertibleError=t("./lib/PatchNotInvertibleError");var g=y.isValidObject,b=y.defaultHash},{"./lib/InvalidPatchOperationError":2,"./lib/PatchNotInvertibleError":3,"./lib/TestFailedError":4,"./lib/array":5,"./lib/inverse":9,"./lib/jsonPatch":10,"./lib/jsonPointer":11,"./lib/lcs":13}],2:[function(t,e){function r(t){Error.call(this),this.name=this.constructor.name,this.message=t,"function"==typeof Error.captureStackTrace&&Error.captureStackTrace(this,this.constructor)}e.exports=r,r.prototype=Object.create(Error.prototype),r.prototype.constructor=r},{}],3:[function(t,e){function r(t){Error.call(this),this.name=this.constructor.name,this.message=t,"function"==typeof Error.captureStackTrace&&Error.captureStackTrace(this,this.constructor)}e.exports=r,r.prototype=Object.create(Error.prototype),r.prototype.constructor=r},{}],4:[function(t,e){function r(t){Error.call(this),this.name=this.constructor.name,this.message=t,"function"==typeof Error.captureStackTrace&&Error.captureStackTrace(this,this.constructor)}e.exports=r,r.prototype=Object.create(Error.prototype),r.prototype.constructor=r},{}],5:[function(t,e,r){function n(t,e){var r=e.length,n=Array(r+1);n[0]=t;for(var o=0;r>o;++o)n[o+1]=e[o];return n}function o(t){for(var e=t.length-1,r=Array(e),n=0;e>n;++n)r[n]=t[n+1];return r}function a(t,e){for(var r=Array(e.length),n=0;n<e.length;++n)r[n]=t(e[n]);return r}/** @license MIT License (c) copyright 2010-2014 original author or authors */
r.cons=n,r.tail=o,r.map=a},{}],6:[function(t,e){function r(t){return null==t||"object"!=typeof t?t:Array.isArray(t)?n(t):o(t)}function n(t){for(var e=t.length,n=Array(e),o=0;e>o;++o)n[o]=r(t[o]);return n}function o(t){for(var e,n=Object.keys(t),o={},a=0,i=n.length;i>a;++a)e=n[a],o[e]=r(t[e]);return o}/** @license MIT License (c) copyright 2010-2014 original author or authors */
e.exports=r},{}],7:[function(t,e){function r(t,e,r){if(t.path===r.path)throw new TypeError("cannot commute "+t.op+","+r.op+" with identical object paths");return[r,t]}function n(t,e,r,n){return e.length===n.length?a(t,e,r,n):(e.length>n.length?(e=i(r,n,t,e,-1),t.path=p.absolute(p.join(e))):(n=i(t,e,r,n,1),r.path=p.absolute(p.join(n))),[r,t])}function o(t,e,r){return p.isValidArrayIndex(t[r])&&p.isValidArrayIndex(e[r])}function a(t,e,r,n){var o,a=e.length-1,i=+e[a],u=+n[a];return u>i?"add"===t.op||"copy"===t.op?(o=n.slice(),o[a]=Math.max(0,u-1),r.path=p.absolute(p.join(o))):"remove"===t.op&&(o=n.slice(),o[a]=u+1,r.path=p.absolute(p.join(o))):"add"===r.op||"copy"===r.op?(o=e.slice(),o[a]=i+1,t.path=p.absolute(p.join(o))):i>u&&"remove"===r.op&&(o=e.slice(),o[a]=Math.max(0,i-1),t.path=p.absolute(p.join(o))),[r,t]}function i(t,e,r,n,o){var a=e.length-1,i=+e[a],u=+n[a],c=n.slice();return i>u?c:("add"===t.op||"copy"===t.op?c[a]=Math.max(0,u-o):"remove"===t.op&&(c[a]=Math.max(0,u+o)),c)}function u(t,e){var r=t.length,n=e.length;if(0===r||0===n||2>r&&2>n)return[];for(var o=r===n?r-1:Math.min(r,n),a=0;o>a&&t[a]===e[a];)++a;return t.slice(0,a)}function c(t){return"remove"===t.op?{op:t.op,path:t.path}:"copy"===t.op||"move"===t.op?{op:t.op,path:t.path,from:t.from}:{op:t.op,path:t.path,value:t.value}}var p=t("./jsonPointer");e.exports=function(t,e){var a=p.parse(t.path),i=p.parse(e.path),f=u(a,i),l=o(a,i,f.length),h=c(t),s=c(e);return 0!==f.length||l?l?n(h,a,s,i):r(h,a,s,i):[s,h]}},{"./jsonPointer":11}],8:[function(t,e){function r(t,e){return t===e?!0:Array.isArray(t)&&Array.isArray(e)?n(t,e):"object"==typeof t&&"object"==typeof e?o(t,e):!1}function n(t,e){if(t.length!==e.length)return!1;for(var n=0;n<t.length;++n)if(!r(t[n],e[n]))return!1;return!0}function o(t,e){if(null===t&&null!==e||null!==t&&null===e)return!1;var n=Object.keys(t),o=Object.keys(e);if(n.length!==o.length)return!1;for(var a,i=0;i<n.length;++i)if(a=n[i],!(a in e&&r(t[a],e[a])))return!1;return!0}e.exports=r},{}],9:[function(t,e){function r(t,e,r,o){var a=n[e.op];return void 0!==a&&"function"==typeof a.inverse?a.inverse(t,e,r,o):1}var n=t("./patches");e.exports=function(t){var e,n,o=[];for(e=t.length-1;e>=0;e-=n)n=r(o,t[e],e,t);return o}},{"./patches":14}],10:[function(t,e,r){function n(t,e,r){return o(t,c(e),r)}function o(t,e,r){if(r||(r=f),!Array.isArray(t))return e;for(var n,o,a=0;a<t.length;++a){if(o=t[a],n=u[o.op],void 0===n)throw new p("invalid op "+JSON.stringify(o));e=n.apply(e,o,r)}return e}function a(t){return i(t)?JSON.stringify(t):t}function i(t){return null!==t&&"[object Object]"===Object.prototype.toString.call(t)}/** @license MIT License (c) copyright 2010-2014 original author or authors */
var u=t("./patches"),c=t("./clone"),p=t("./InvalidPatchOperationError");r.apply=n,r.applyInPlace=o,r.clone=c,r.isValidObject=i,r.defaultHash=a;var f={}},{"./InvalidPatchOperationError":2,"./clone":6,"./patches":14}],11:[function(t,e,r){function n(t,e,r,n){if("string"==typeof e){if(""===e)return{target:t,key:void 0};if(e===v)return{target:t,key:""};var o,a=t,i=void 0!==n;return s(e,function(e){return null==t?(a=null,!1):(o=Array.isArray(t)?i?h(r,l(e),t,n):"-"===e?e:l(e):e,a=t,void(t=t[o]))}),null===a?void 0:{target:a,key:o}}}function o(t){return t[0]===v?t:v+t}function a(t){return t.join(v)}function i(t){var e=[];return s(t,e.push.bind(e)),e}function u(t,e){return 0===e.indexOf(t)&&e[t.length]===v}function c(t){return t.replace(m,v).replace(w,x)}function p(t){return t.replace(g,b).replace(y,d)}function f(t){return E.test(t)}function l(t){if(f(t))return+t;throw new SyntaxError("invalid array index "+t)}function h(t,e,r,n){var o=e;if(0>o)throw Error("array index out of bounds "+o);if(void 0!==n&&"function"==typeof t&&(o=t(e,r,n),0>o))throw Error("could not find patch context "+n);return o}/** @license MIT License (c) copyright 2010-2014 original author or authors */
var s=t("./jsonPointerParse");r.find=n,r.join=a,r.absolute=o,r.parse=i,r.contains=u,r.encodeSegment=p,r.decodeSegment=c,r.parseArrayIndex=l,r.isValidArrayIndex=f;var v="/",y=/\//g,d="~1",m=/~1/g,x="~",g=/~/g,b="~0",w=/~0/g,E=/^(0|[1-9]\d*)$/},{"./jsonPointerParse":12}],12:[function(t,e){function r(t,e){var r,u,c,p;for(r=t.charAt(0)===o?1:0,u="",n.lastIndex=r;c=n.exec(t);)if(p=c[0],u+=t.slice(r,n.lastIndex-p.length),r=n.lastIndex,p===o){if(e(u)===!1)return t;u=""}else u+=p===i?o:a;return u+=t.slice(r),e(u),t}/** @license MIT License (c) copyright 2010-2014 original author or authors */
e.exports=r;var n=/\/|~1|~0/g,o="/",a="~",i="~1"},{}],13:[function(t,e,r){function n(t,e){var r=t.length,n=e.length,o=a(t,e),p=r>o&&n>o?i(t,e,o):0,f=p+o-1;r-=f,n-=f;for(var l=c(r,n),h=r-1;h>=0;--h)for(var s=n-1;s>=0;--s)l[s][h]=u(l,t,e,o,h,s);return{prefix:o,matrix:l,suffix:p}}function o(t,e,r){var n,o,a,i,u=r.matrix,c=r.prefix;for(n=0;c>n;++n)e=t(e,s,n,n);for(a=n,c=u.length,n=0,o=0;c>n;)switch(i=u[n][o].type,e=t(e,i,n+a,o+a),i){case s:++n,++o;break;case f:++o;break;case h:++n}for(n+=a,o+=a,c=r.suffix,a=0;c>a;++a)e=t(e,s,n+a,o+a);return e}function a(t,e){for(var r=0,n=Math.min(t.length,e.length);n>r&&t[r]===e[r];)++r;return r}function i(t,e){for(var r=t.length-1,n=e.length-1,o=Math.min(r,n),a=0;o>a&&t[r-a]===e[n-a];)++a;return a}function u(t,e,r,n,o,a){return e[o+n]===r[a+n]?{value:t[a+1][o+1].value,type:s}:t[a][o+1].value<t[a+1][o].value?{value:t[a][o+1].value+1,type:f}:{value:t[a+1][o].value+1,type:h}}function c(t,e){var r,n,o,a=[];for(o=a[e]=[],n=0;t>n;++n)o[n]={value:t-n,type:f};for(r=0;e>r;++r)a[r]=[],a[r][t]={value:e-r,type:h};return a[e][t]={value:0,type:s},a}/** @license MIT License (c) copyright 2010-2014 original author or authors */
r.compare=n,r.reduce=o;var p,f,l,h,s;r.REMOVE=p=f=-1,r.ADD=l=h=1,r.EQUAL=s=0},{}],14:[function(t,e,r){function n(t,e,r){var n,o,a=N(t,e.path,r.findContext,e.context),i=a.target;if(Array.isArray(i)?(n=V(a.key),o=i[n]):o=void 0===a.key?a.target:a.target[a.key],!O(o,e.value))throw new T("test failed "+JSON.stringify(e));return t}function o(t,e){return t.push(e),1}function a(t,e){if(t.path===e.path&&"remove"===e.op)throw new TypeError("Can't commute test,remove -> remove,test for same path");return"test"===e.op||"replace"===e.op?[e,t]:I(t,e)}function i(t,e,r){var n=N(t,e.path,r.findContext,e.context);if(E(n))throw new S("path does not exist "+e.path);if(void 0===e.value)throw new S("missing value");var o=P(e.value);return void 0===n.key?o:(u(n,o),t)}function u(t,e){var r=t.target;if(Array.isArray(r))if("-"===t.key)r.push(e);else{if(t.key>r.length)throw new S("target of add outside of array bounds");r.splice(t.key,0,e)}else{if(!j(r))throw new S("target of add must be an object or array "+t.key);r[t.key]=e}}function c(t,e){var r=e.context;return void 0!==r&&(r={before:r.before,after:C.cons(e.value,r.after)}),t.push({op:"test",path:e.path,value:e.value,context:r}),t.push({op:"remove",path:e.path,context:r}),1}function p(t,e){if(t.path===e.path&&"remove"===e.op)throw new TypeError("Can't commute add,remove -> remove,add for same path");return I(t,e)}function f(t,e,r){var n=N(t,e.path,r.findContext,e.context);if(E(n)||k(n))throw new S("path does not exist "+e.path);if(void 0===e.value)throw new S("missing value");var o=P(e.value);if(void 0===n.key)return o;var a=n.target;return Array.isArray(a)?a[V(n.key)]=o:a[n.key]=o,t}function l(t,e,r,n){var o=n[r-1];if(void 0===o||"test"!==o.op||o.path!==e.path)throw new M("cannot invert replace w/o test");var a=o.context;return void 0!==a&&(a={before:a.before,after:C.cons(o.value,C.tail(a.after))}),t.push({op:"test",path:o.path,value:e.value}),t.push({op:"replace",path:o.path,value:o.value}),2}function h(t,e){if(t.path===e.path&&"remove"===e.op)throw new TypeError("Can't commute replace,remove -> remove,replace for same path");return"test"===e.op||"replace"===e.op?[e,t]:I(t,e)}function s(t,e,r){var n=N(t,e.path,r.findContext,e.context);if(E(n)||void 0===n.target[n.key])throw new S("path does not exist "+e.path);return v(n),t}function v(t){var e,r=t.target;if(Array.isArray(r))return e=r.splice(V(t.key),1),e[0];if(j(r))return e=r[t.key],delete r[t.key],e;throw new S("target of remove must be an object or array")}function y(t,e,r,n){var o=n[r-1];if(void 0===o||"test"!==o.op||o.path!==e.path)throw new M("cannot invert remove w/o test");var a=o.context;return void 0!==a&&(a={before:a.before,after:C.tail(a.after)}),t.push({op:"add",path:o.path,value:o.value,context:a}),2}function d(t,e){return t.path===e.path&&"remove"===e.op?[e,t]:I(t,e)}function m(t,e,r){if(A.contains(e.path,e.from))throw new S("move.from cannot be ancestor of move.path");var n=N(t,e.path,r.findContext,e.context),o=N(t,e.from,r.findContext,e.fromContext);return u(n,v(o)),t}function x(t,e){return t.push({op:"move",path:e.from,context:e.fromContext,from:e.path,fromContext:e.context}),1}function g(t,e){if(t.path===e.path&&"remove"===e.op)throw new TypeError("Can't commute move,remove -> move,replace for same path");return I(t,e)}function b(t,e,r){var n=N(t,e.path,r.findContext,e.context),o=N(t,e.from,r.findContext,e.fromContext);if(E(o)||k(o))throw new S("copy.from must exist");var a,i=o.target;return a=Array.isArray(i)?i[V(o.key)]:i[o.key],u(n,P(a)),t}function w(t,e){throw new M("cannot invert "+e.op)}function E(t){return void 0===t||null==t.target&&void 0!==t.key}function k(t){return void 0!==t.key&&void 0===t.target[t.key]}function j(t){return null!==t&&"object"==typeof t}var A=t("./jsonPointer"),P=t("./clone"),O=t("./deepEquals"),I=t("./commutePaths"),C=t("./array"),T=t("./TestFailedError"),S=t("./InvalidPatchOperationError"),M=t("./PatchNotInvertibleError"),N=A.find,V=A.parseArrayIndex;r.test={apply:n,inverse:o,commute:a},r.add={apply:i,inverse:c,commute:p},r.remove={apply:s,inverse:y,commute:d},r.replace={apply:f,inverse:l,commute:h},r.move={apply:m,inverse:x,commute:g},r.copy={apply:b,inverse:w,commute:p}},{"./InvalidPatchOperationError":2,"./PatchNotInvertibleError":3,"./TestFailedError":4,"./array":5,"./clone":6,"./commutePaths":7,"./deepEquals":8,"./jsonPointer":11}]},{},[1])(1)});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment