circular-history 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/README.md ADDED
@@ -0,0 +1,121 @@
1
+ # Circular History
2
+
3
+ Data structure that holds a fixed amount of items of a specific data type. It's designed more for managing history, such as undo-redo functionality, therefore it allows you to overwrite the items after moving backward in order to discard the "redo" history.
4
+
5
+ ## Usage
6
+
7
+ ### 1. Create an instance of `CircularHistory` with a specified capacity and data type.
8
+
9
+ ```javascript
10
+ var history = new CircularHistory(5, "string");
11
+ ```
12
+
13
+ ### 2. Commit new items to the history.
14
+
15
+ ```javascript
16
+ history.commit("First");
17
+ history.commit("Second");
18
+ ```
19
+
20
+ Keep in mind that after moving backward, committing a new item will overwrite the items ahead of the current index.
21
+ It was designed this way to facilitate undo-redo functionality by discarding the "redo" history when a new action is taken.
22
+
23
+ When the capacity is reached, the oldest items will be overwritten in a circular manner.
24
+
25
+ ### 3. Get the current item.
26
+
27
+ ```javascript
28
+ var currentItem = history.current();
29
+ ```
30
+
31
+ Returns either the current item or `CircularHistory.FLAGS.empty` if current index is `-1` which means there are no committed items yet or you went backwards beyond the first committed item (basically an empty state).
32
+
33
+ ### 4. Move backward and forward in the history.
34
+
35
+ ```javascript
36
+ history.moveBackward();
37
+ history.moveForward();
38
+ ```
39
+
40
+ Moving backward and forward will adjust the current index accordingly and allow you to navigate within specific range which is determined by the number of committed items before navigation. If the number of committed items exceeds the capacity, the range will be limited to the capacity.
41
+
42
+ ### 5. Clear the history.
43
+
44
+ ```javascript
45
+ history.clear();
46
+ ```
47
+
48
+ ### 6. Get the history array.
49
+
50
+ ```javascript
51
+ var historyArray = history.dump();
52
+ ```
53
+
54
+ This will return an array of all committed items in the history. If capacity has not been reached, the array will contain items with `undefined` values for uncommitted slots.
55
+
56
+ If you want to get only the committed items, you can pass `true` as an argument.
57
+
58
+ ```javascript
59
+ var committedItems = history.dump(true);
60
+ ```
61
+
62
+ ### 7. Get the current index.
63
+
64
+ ```javascript
65
+ var currentIndex = history.getCurrentIndex();
66
+ ```
67
+
68
+ ### 8. Determine if start/end has been reached
69
+
70
+ ```javascript
71
+ var isAtStart = history.isStartReached();
72
+ var isAtEnd = history.isEndReached();
73
+ ```
74
+
75
+ ## Running tests
76
+
77
+ 1. `pnpm install`
78
+
79
+ 2. `pnpm test`
80
+
81
+ ## Usage example
82
+
83
+ Imagine that you have a drawing application. You have a layer where you can draw
84
+ shapes and you can create a snapshot of the layer's state to keep track of changes.
85
+
86
+ ```javascript
87
+ var history = new CircularHistory(50, "string");
88
+
89
+ function commitHistorySnapshot() {
90
+ const snapshot = drawLayer.toJSON();
91
+ if (!snapshot) return;
92
+ history.commit(snapshot);
93
+ }
94
+
95
+ function restoreHistorySnapshot(snapshot) {
96
+ if (!snapshot) return;
97
+
98
+ if (snapshot === CircularHistory.FLAGS.empty) {
99
+ drawLayer.clear();
100
+ drawLayer.redraw();
101
+ return;
102
+ }
103
+
104
+ drawLayer.unmount();
105
+
106
+ const restoredLayer = new Layer(snapshot);
107
+ drawLayer = restoredLayer;
108
+
109
+ restoredLayer.redraw();
110
+ }
111
+
112
+ function undo() {
113
+ history.moveBackward();
114
+ restoreHistorySnapshot(history.current());
115
+ }
116
+
117
+ function redo() {
118
+ history.moveForward();
119
+ restoreHistorySnapshot(history.current());
120
+ }
121
+ ```
@@ -0,0 +1,31 @@
1
+ export declare const FLAGS: {
2
+ readonly empty: unique symbol;
3
+ };
4
+
5
+ export declare type CircularHistoryType = number | string | bigint | boolean | symbol | object;
6
+
7
+ type DataType = "number" | "string" | "bigint" | "boolean" | "symbol" | "object";
8
+
9
+ export declare class CircularHistory<T extends CircularHistoryType = CircularHistoryType> {
10
+ constructor(capacity: number, dataType: DataType);
11
+
12
+ commit(value: T): void;
13
+
14
+ current(): T | typeof FLAGS.empty;
15
+
16
+ moveBackward(): void;
17
+
18
+ moveForward(): void;
19
+
20
+ clear(): void;
21
+
22
+ dump(discardHoles?: boolean): T[];
23
+
24
+ getCurrentIndex(): number;
25
+
26
+ isStartReached(): boolean;
27
+
28
+ isEndReached(): boolean;
29
+
30
+ static readonly FLAGS: typeof FLAGS;
31
+ }
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ var g=Object.defineProperty;var a=(e,t)=>g(e,"name",{value:t,configurable:!0});var y=["number","string","bigint","boolean","symbol","object"],p=0,f=-1,v={empty:Symbol("empty")},s=a(e=>y.includes(e),"isTypeAllowed"),c=a(e=>{var t=typeof e,n=t==="object"&&e!==null;return n||s(t)},"isValueTypeAllowed"),l=a((e,t)=>c(e)&&typeof e===t,"canCommit"),d=a(e=>e===void 0,"isItemEmpty"),u=a((e,t)=>e%t,"makeIndex"),r=new WeakMap;function i(e,t){if(typeof e!="number"||e<=0||!Number.isInteger(e))throw new Error(`Capacity must be a positive integer. Got "${e}".`);if(!s(t))throw new Error(`"${t}" is not allowed`);r.set(this,{navigationUpperBound:p,navigationIndex:p,pointer:f,capacity:e,dataType:t,buffer:new Array(e)})}a(i,"CircularHistory");i.prototype.commit=function(e){var t=r.get(this),n=t.dataType;if(!l(e,n))throw new Error(`Type of ${e} is invalid. Expected "${n}", got "${e===null?"null":typeof e}".`);var o=t.capacity;t.pointer===t.capacity-1?(t.navigationUpperBound=o-1,t.navigationIndex=o-1):t.navigationIndex=++t.navigationUpperBound,t.buffer[u(++t.pointer,o)]=e};i.prototype.current=function(){var e=r.get(this),t=e.buffer,n=e.pointer;if(n===f)return v.empty;var o=t[u(n,e.capacity)];return d(o)?v.empty:o};i.prototype.moveBackward=function(){var e=r.get(this);e.navigationIndex===p||e.pointer===f||(e.pointer--,e.navigationIndex--)};i.prototype.moveForward=function(){var e=r.get(this);e.navigationIndex!==e.navigationUpperBound&&(e.pointer++,e.navigationIndex++)};i.prototype.clear=function(){var e=r.get(this);e.navigationIndex=p,e.navigationUpperBound=p,e.pointer=f,e.buffer=new Array(e.capacity)};i.prototype.dump=function(e=!1){var t=r.get(this),n=[...t.buffer];return e?n.filter(o=>!d(o)):n};i.prototype.getCurrentIndex=function(){var e=r.get(this);return u(e.pointer,e.capacity)};i.prototype.isStartReached=function(){var e=r.get(this);return e.navigationIndex===p};i.prototype.isEndReached=function(){var e=r.get(this);return e.navigationIndex===e.navigationUpperBound};i.FLAGS=v;export{i as CircularHistory};
@@ -0,0 +1,21 @@
1
+ {
2
+ "name": "circular-history",
3
+ "type": "module",
4
+ "version": "1.0.2",
5
+ "description": "Data structure with a fixed number of items of a specific type. Supports adding elements, moving backward and forward with wrapping. Suitable for undo/redo implementations such as history management.",
6
+ "main": "index.js",
7
+ "types": "index.d.ts",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "https://github.com/zavvdev/circular-history"
11
+ },
12
+ "keywords": [
13
+ "javascript",
14
+ "data-structures",
15
+ "circular-buffer",
16
+ "circular-history",
17
+ "history-management"
18
+ ],
19
+ "author": "Ihor ZAVIRIUKHA <ihor.zaviriukha@protonmail.com>",
20
+ "license": "MIT"
21
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "circular-history",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "description": "Data structure with a fixed number of items of a specific type. Supports adding elements, moving backward and forward with wrapping. Suitable for undo/redo implementations such as history management.",