Comparing version 1.3.3 to 1.3.4
# Changelog | ||
### `V1.3.3` - 2020-08-20 | ||
* type: Improved TypeScript support. | ||
* type: improved TypeScript support. | ||
@@ -6,0 +6,0 @@ ### `V1.3.2` - 2020-08-20 |
@@ -46,2 +46,17 @@ 'use strict'; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
function safeStringify(obj) { | ||
var indent = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 2; | ||
var cache = []; | ||
var retVal = JSON.stringify(obj, function (key, value) { | ||
return (typeof value === 'undefined' ? 'undefined' : _typeof(value)) === 'object' && value !== null ? cache.includes(value) ? undefined // Duplicate reference found, discard key | ||
: cache.push(value) && value // Store value in our collection | ||
: value; | ||
}, indent); | ||
cache = null; | ||
return retVal; | ||
} | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
@@ -84,3 +99,3 @@ | ||
for (var i = 0; i < chunks.length; i++) { | ||
var chunkStr = JSON.stringify(chunks[i]); | ||
var chunkStr = safeStringify(chunks[i]); | ||
var hashKey = hashFunc(chunkStr); | ||
@@ -87,0 +102,0 @@ hashes.push(hashKey); |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var murmurHash2=function(e){for(var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,t=e.length,i=n^t,r=0,s=void 0;t>=4;)s=1540483477*(65535&(s=255&e.charCodeAt(r)|(255&e.charCodeAt(++r))<<8|(255&e.charCodeAt(++r))<<16|(255&e.charCodeAt(++r))<<24))+((1540483477*(s>>>16)&65535)<<16),i=1540483477*(65535&i)+((1540483477*(i>>>16)&65535)<<16)^(s=1540483477*(65535&(s^=s>>>24))+((1540483477*(s>>>16)&65535)<<16)),t-=4,++r;switch(t){case 3:i^=(255&e.charCodeAt(r+2))<<16;case 2:i^=(255&e.charCodeAt(r+1))<<8;case 1:i=1540483477*(65535&(i^=255&e.charCodeAt(r)))+((1540483477*(i>>>16)&65535)<<16)}return i=1540483477*(65535&(i^=i>>>13))+((1540483477*(i>>>16)&65535)<<16),(i^=i>>>15)>>>0},hashFunc=murmurHash2,_extends=Object.assign||function(e){for(var n=1;n<arguments.length;n++){var t=arguments[n];for(var i in t)Object.prototype.hasOwnProperty.call(t,i)&&(e[i]=t[i])}return e};function _toConsumableArray(e){if(Array.isArray(e)){for(var n=0,t=Array(e.length);n<e.length;n++)t[n]=e[n];return t}return Array.from(e)}var defaultRule={toRecord:function(e){return{chunks:[_extends({},e,{children:void 0})],children:e.children}},fromRecord:function(e){var n=e.chunks,t=e.children;return _extends({},n[0],{children:t})}},state2Record=function e(n,t){for(var i=arguments.length>2&&void 0!==arguments[2]?arguments[2]:[],r=arguments.length>3&&void 0!==arguments[3]?arguments[3]:null,s=arguments.length>4&&void 0!==arguments[4]?arguments[4]:-1,h=i.findIndex(function(e){return(0,e.match)(n)}),o=(i[h]||defaultRule).toRecord(n),u=o.chunks,a=o.children,l=a,c=[],d=0;d<u.length;d++){var f=JSON.stringify(u[d]),g=hashFunc(f);c.push(g),t[g]=f}if(-1!==s&&Array.isArray(r&&r.children)){var $=[].concat(_toConsumableArray(r.children));return $[s]=e(l[s],t,i),{hashes:c,ruleIndex:h,children:$}}return{hashes:c,ruleIndex:h,children:a&&a.map(function(n){return e(n,t,i)})}},record2State=function e(n,t){var i=arguments.length>2&&void 0!==arguments[2]?arguments[2]:[],r=n.hashes,s=n.ruleIndex,h=n.children,o=r.map(function(e){return JSON.parse(t[e])});return(i[s]||defaultRule).fromRecord({chunks:o,children:h&&h.map(function(n){return e(n,t,i)})})},_createClass=function(){function e(e,n){for(var t=0;t<n.length;t++){var i=n[t];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,i.key,i)}}return function(n,t,i){return t&&e(n.prototype,t),i&&e(n,i),n}}();function _classCallCheck(e,n){if(!(e instanceof n))throw new TypeError("Cannot call a class as a function")}var noop=function(){},History=function(){function e(){var n=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{initialState:void 0,rules:[],delay:50,maxLength:100,onChange:noop,useChunks:!0};_classCallCheck(this,e),this.rules=n.rules||[],this.delay=n.delay||50,this.maxLength=n.maxLength||100,this.useChunks=void 0===n.useChunks||n.useChunks,this.onChange=noop,this.$index=-1,this.$records=[],this.$chunks={},this.$pending={state:null,pickIndex:null,onResolves:[],timer:null},this.$debounceTime=null,void 0!==n.initialState&&this.pushSync(n.initialState),n.onChange&&(this.onChange=n.onChange)}return _createClass(e,[{key:"get",value:function(){var e=this.$records[this.$index],n=void 0;return n=e?this.useChunks?record2State(e,this.$chunks,this.rules):e:null,this.onChange(n),n}},{key:"pushSync",value:function(e){var n=this,t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:-1,i=this.$records[this.$index]||null,r=this.useChunks?state2Record(e,this.$chunks,this.rules,i,t):e;this.$index++,this.$records[this.$index]=r;for(var s=this.$index+1;s<this.$records.length;s++)this.$records[s]=null;return this.$index>=this.maxLength&&(this.$records[this.$index-this.maxLength]=null),this.$pending.timer&&(clearTimeout(this.$pending.timer),this.$pending.state=null,this.$pending.pickIndex=null,this.$pending.timer=null,this.$debounceTime=null,this.$pending.onResolves.forEach(function(e){return e(n)}),this.$pending.onResolves=[]),this.onChange(e),this}},{key:"push",value:function(e){var n=this,t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:-1,i=+new Date,r=function(){return n.$pending.state=e,n.$pending.pickIndex=t,n.$debounceTime=i,new Promise(function(e,t){n.$pending.onResolves.push(e),n.$pending.timer=setTimeout(function(){n.pushSync(n.$pending.state,n.$pending.pickIndex)},n.delay)})};return null===this.$pending.timer?r():i-this.$debounceTime<this.delay?(clearTimeout(this.$pending.timer),this.$pending.timer=null,r()):Promise.reject(new Error("Invalid push ops"))}},{key:"undo",value:function(){return this.hasUndo&&this.$index--,this}},{key:"redo",value:function(){return this.hasRedo&&this.$index++,this}},{key:"reset",value:function(){var e=this;return this.$index=-1,this.$records.forEach(function(e){null}),Object.keys(this.$chunks).forEach(function(n){e.$chunks[n]=null}),this.$records=[],this.$chunks={},clearTimeout(this.$pending.timer),this.$pending={state:null,pickIndex:null,onResolves:[],timer:null},this.$debounceTime=null,this}},{key:"hasRedo",get:function(){if(this.$index===this.$records.length-1)return!1;for(var e=!1,n=this.$index+1;n<this.$records.length;n++)null!==this.$records[n]&&(e=!0);return e}},{key:"hasUndo",get:function(){var e=Math.max(this.$records.length-this.maxLength,0);return this.$index>e}},{key:"length",get:function(){return Math.min(this.$records.length,this.maxLength)}}]),e}();exports.History=History; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var murmurHash2=function(e){for(var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,t=e.length,i=n^t,r=0,s=void 0;t>=4;)s=1540483477*(65535&(s=255&e.charCodeAt(r)|(255&e.charCodeAt(++r))<<8|(255&e.charCodeAt(++r))<<16|(255&e.charCodeAt(++r))<<24))+((1540483477*(s>>>16)&65535)<<16),i=1540483477*(65535&i)+((1540483477*(i>>>16)&65535)<<16)^(s=1540483477*(65535&(s^=s>>>24))+((1540483477*(s>>>16)&65535)<<16)),t-=4,++r;switch(t){case 3:i^=(255&e.charCodeAt(r+2))<<16;case 2:i^=(255&e.charCodeAt(r+1))<<8;case 1:i=1540483477*(65535&(i^=255&e.charCodeAt(r)))+((1540483477*(i>>>16)&65535)<<16)}return i=1540483477*(65535&(i^=i>>>13))+((1540483477*(i>>>16)&65535)<<16),(i^=i>>>15)>>>0},hashFunc=murmurHash2,_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e};function safeStringify(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:2,t=[],i=JSON.stringify(e,function(e,n){return"object"===(void 0===n?"undefined":_typeof(n))&&null!==n?t.includes(n)?void 0:t.push(n)&&n:n},n);return t=null,i}var _extends=Object.assign||function(e){for(var n=1;n<arguments.length;n++){var t=arguments[n];for(var i in t)Object.prototype.hasOwnProperty.call(t,i)&&(e[i]=t[i])}return e};function _toConsumableArray(e){if(Array.isArray(e)){for(var n=0,t=Array(e.length);n<e.length;n++)t[n]=e[n];return t}return Array.from(e)}var defaultRule={toRecord:function(e){return{chunks:[_extends({},e,{children:void 0})],children:e.children}},fromRecord:function(e){var n=e.chunks,t=e.children;return _extends({},n[0],{children:t})}},state2Record=function e(n,t){for(var i=arguments.length>2&&void 0!==arguments[2]?arguments[2]:[],r=arguments.length>3&&void 0!==arguments[3]?arguments[3]:null,s=arguments.length>4&&void 0!==arguments[4]?arguments[4]:-1,o=i.findIndex(function(e){return(0,e.match)(n)}),h=(i[o]||defaultRule).toRecord(n),u=h.chunks,l=h.children,a=l,c=[],d=0;d<u.length;d++){var f=safeStringify(u[d]),g=hashFunc(f);c.push(g),t[g]=f}if(-1!==s&&Array.isArray(r&&r.children)){var p=[].concat(_toConsumableArray(r.children));return p[s]=e(a[s],t,i),{hashes:c,ruleIndex:o,children:p}}return{hashes:c,ruleIndex:o,children:l&&l.map(function(n){return e(n,t,i)})}},record2State=function e(n,t){var i=arguments.length>2&&void 0!==arguments[2]?arguments[2]:[],r=n.hashes,s=n.ruleIndex,o=n.children,h=r.map(function(e){return JSON.parse(t[e])});return(i[s]||defaultRule).fromRecord({chunks:h,children:o&&o.map(function(n){return e(n,t,i)})})},_createClass=function(){function e(e,n){for(var t=0;t<n.length;t++){var i=n[t];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,i.key,i)}}return function(n,t,i){return t&&e(n.prototype,t),i&&e(n,i),n}}();function _classCallCheck(e,n){if(!(e instanceof n))throw new TypeError("Cannot call a class as a function")}var noop=function(){},History=function(){function e(){var n=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{initialState:void 0,rules:[],delay:50,maxLength:100,onChange:noop,useChunks:!0};_classCallCheck(this,e),this.rules=n.rules||[],this.delay=n.delay||50,this.maxLength=n.maxLength||100,this.useChunks=void 0===n.useChunks||n.useChunks,this.onChange=noop,this.$index=-1,this.$records=[],this.$chunks={},this.$pending={state:null,pickIndex:null,onResolves:[],timer:null},this.$debounceTime=null,void 0!==n.initialState&&this.pushSync(n.initialState),n.onChange&&(this.onChange=n.onChange)}return _createClass(e,[{key:"get",value:function(){var e=this.$records[this.$index],n=void 0;return n=e?this.useChunks?record2State(e,this.$chunks,this.rules):e:null,this.onChange(n),n}},{key:"pushSync",value:function(e){var n=this,t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:-1,i=this.$records[this.$index]||null,r=this.useChunks?state2Record(e,this.$chunks,this.rules,i,t):e;this.$index++,this.$records[this.$index]=r;for(var s=this.$index+1;s<this.$records.length;s++)this.$records[s]=null;return this.$index>=this.maxLength&&(this.$records[this.$index-this.maxLength]=null),this.$pending.timer&&(clearTimeout(this.$pending.timer),this.$pending.state=null,this.$pending.pickIndex=null,this.$pending.timer=null,this.$debounceTime=null,this.$pending.onResolves.forEach(function(e){return e(n)}),this.$pending.onResolves=[]),this.onChange(e),this}},{key:"push",value:function(e){var n=this,t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:-1,i=+new Date,r=function(){return n.$pending.state=e,n.$pending.pickIndex=t,n.$debounceTime=i,new Promise(function(e,t){n.$pending.onResolves.push(e),n.$pending.timer=setTimeout(function(){n.pushSync(n.$pending.state,n.$pending.pickIndex)},n.delay)})};return null===this.$pending.timer?r():i-this.$debounceTime<this.delay?(clearTimeout(this.$pending.timer),this.$pending.timer=null,r()):Promise.reject(new Error("Invalid push ops"))}},{key:"undo",value:function(){return this.hasUndo&&this.$index--,this}},{key:"redo",value:function(){return this.hasRedo&&this.$index++,this}},{key:"reset",value:function(){var e=this;return this.$index=-1,this.$records.forEach(function(e){null}),Object.keys(this.$chunks).forEach(function(n){e.$chunks[n]=null}),this.$records=[],this.$chunks={},clearTimeout(this.$pending.timer),this.$pending={state:null,pickIndex:null,onResolves:[],timer:null},this.$debounceTime=null,this}},{key:"hasRedo",get:function(){if(this.$index===this.$records.length-1)return!1;for(var e=!1,n=this.$index+1;n<this.$records.length;n++)null!==this.$records[n]&&(e=!0);return e}},{key:"hasUndo",get:function(){var e=Math.max(this.$records.length-this.maxLength,0);return this.$index>e}},{key:"length",get:function(){return Math.min(this.$records.length,this.maxLength)}}]),e}();exports.History=History; | ||
//# sourceMappingURL=stateshot.min.js.map |
{ | ||
"name": "stateshot", | ||
"version": "1.3.3", | ||
"version": "1.3.4", | ||
"main": "dist/stateshot.js", | ||
@@ -5,0 +5,0 @@ "repository": "git@github.com:doodlewind/stateshot.git", |
@@ -255,3 +255,4 @@ /* global test expect jest */ | ||
const state = getState() | ||
void new History({ initialState: state, onChange }) | ||
// eslint-disable-next-line no-new | ||
new History({ initialState: state, onChange }) | ||
expect(onChange.mock.calls.length).toBe(0) | ||
@@ -258,0 +259,0 @@ }) |
import { hashFunc } from './hash' | ||
import { safeStringify } from './utils' | ||
@@ -22,3 +23,3 @@ const defaultRule = { | ||
for (let i = 0; i < chunks.length; i++) { | ||
const chunkStr = JSON.stringify(chunks[i]) | ||
const chunkStr = safeStringify(chunks[i]) | ||
const hashKey = hashFunc(chunkStr) | ||
@@ -25,0 +26,0 @@ hashes.push(hashKey) |
@@ -214,1 +214,13 @@ /* global test expect */ | ||
}) | ||
test('support circular structure to JSON', () => { | ||
const parent = { children: [], name: 'foo' } | ||
const child = { parent, name: 'boo' } | ||
parent.children.push(child) | ||
const state = { parent } | ||
const chunks = {} | ||
const record = state2Record(state, chunks) | ||
const resultState = record2State(record, chunks) | ||
expect(resultState.parent.name).toEqual(parent.name) | ||
expect(resultState.parent.children[0].name).toEqual(child.name) | ||
}) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
76944
15
1097