use-undoable
Advanced tools
Comparing version 4.0.0 to 5.0.0
@@ -1,2 +0,2 @@ | ||
export declare const payloadError: (func: string) => never; | ||
export declare const invalidBehavior: (behavior: string) => never; | ||
export declare const payloadError: (func: string) => Error; | ||
export declare const invalidBehaviorError: (behavior: string) => Error; |
@@ -22,6 +22,6 @@ var react = require('react'); | ||
var payloadError = function payloadError(func) { | ||
throw new Error("NoPayloadError: " + func + " requires a payload."); | ||
return new Error("NoPayloadError: " + func + " requires a payload."); | ||
}; | ||
var invalidBehavior = function invalidBehavior(behavior) { | ||
throw new Error("Mutation behavior must be one of: mergePastReversed, mergePast, keepFuture, or destroyFuture. Not: " + behavior); | ||
var invalidBehaviorError = function invalidBehaviorError(behavior) { | ||
return new Error("Mutation behavior must be one of: mergePastReversed, mergePast, keepFuture, or destroyFuture. Not: " + behavior); | ||
}; | ||
@@ -55,4 +55,4 @@ | ||
if (!payload && payload !== '' && payload !== 0) { | ||
payloadError("mutate"); | ||
if (!payload || payload === undefined) { | ||
throw payloadError("mutate"); | ||
} | ||
@@ -108,3 +108,3 @@ | ||
if (!behaviorMap.hasOwnProperty(behavior)) invalidBehavior(behavior); | ||
if (!behaviorMap.hasOwnProperty(behavior)) throw invalidBehaviorError(behavior); | ||
return behaviorMap[behavior]; | ||
@@ -146,4 +146,9 @@ }; | ||
var transform = function transform(action) { | ||
action.payload = typeof action.payload === "function" ? action.payload(present) : action.payload; | ||
return action; | ||
}; | ||
var update = function update() { | ||
return mutate(state, action); | ||
return mutate(state, transform(action)); | ||
}; | ||
@@ -155,3 +160,3 @@ | ||
past: [], | ||
present: payload, | ||
present: payload || state.present, | ||
future: [] | ||
@@ -165,3 +170,3 @@ }; | ||
if (!payload) { | ||
payloadError("resetInitialState"); | ||
throw payloadError("resetInitialState"); | ||
} | ||
@@ -264,3 +269,3 @@ | ||
return typeof payload === "function" ? update(payload(state.present), mutationBehavior, ignoreAction) : update(payload, mutationBehavior, ignoreAction); | ||
return update(payload, mutationBehavior, ignoreAction); | ||
}, [state]); | ||
@@ -267,0 +272,0 @@ |
@@ -22,6 +22,6 @@ import { useReducer, useCallback } from 'react'; | ||
var payloadError = function payloadError(func) { | ||
throw new Error("NoPayloadError: " + func + " requires a payload."); | ||
return new Error("NoPayloadError: " + func + " requires a payload."); | ||
}; | ||
var invalidBehavior = function invalidBehavior(behavior) { | ||
throw new Error("Mutation behavior must be one of: mergePastReversed, mergePast, keepFuture, or destroyFuture. Not: " + behavior); | ||
var invalidBehaviorError = function invalidBehaviorError(behavior) { | ||
return new Error("Mutation behavior must be one of: mergePastReversed, mergePast, keepFuture, or destroyFuture. Not: " + behavior); | ||
}; | ||
@@ -55,4 +55,4 @@ | ||
if (!payload && payload !== '' && payload !== 0) { | ||
payloadError("mutate"); | ||
if (!payload || payload === undefined) { | ||
throw payloadError("mutate"); | ||
} | ||
@@ -108,3 +108,3 @@ | ||
if (!behaviorMap.hasOwnProperty(behavior)) invalidBehavior(behavior); | ||
if (!behaviorMap.hasOwnProperty(behavior)) throw invalidBehaviorError(behavior); | ||
return behaviorMap[behavior]; | ||
@@ -146,4 +146,9 @@ }; | ||
var transform = function transform(action) { | ||
action.payload = typeof action.payload === "function" ? action.payload(present) : action.payload; | ||
return action; | ||
}; | ||
var update = function update() { | ||
return mutate(state, action); | ||
return mutate(state, transform(action)); | ||
}; | ||
@@ -155,3 +160,3 @@ | ||
past: [], | ||
present: payload, | ||
present: payload || state.present, | ||
future: [] | ||
@@ -165,3 +170,3 @@ }; | ||
if (!payload) { | ||
payloadError("resetInitialState"); | ||
throw payloadError("resetInitialState"); | ||
} | ||
@@ -264,3 +269,3 @@ | ||
return typeof payload === "function" ? update(payload(state.present), mutationBehavior, ignoreAction) : update(payload, mutationBehavior, ignoreAction); | ||
return update(payload, mutationBehavior, ignoreAction); | ||
}, [state]); | ||
@@ -267,0 +272,0 @@ |
import type { Action, State } from "./types"; | ||
declare const mutate: (state: State, action: Action) => State; | ||
declare const mutate: <T>(state: State<T>, action: Action<T>) => State<T>; | ||
export { mutate }; |
import type { Action, State } from "./types"; | ||
export declare const reducer: (state: State, action: Action) => State; | ||
export declare const reducer: <T>(state: State<T>, action: Action<T>) => State<T>; |
export declare type ActionType = "undo" | "redo" | "update" | "reset" | "resetInitialState"; | ||
export declare type HistoryLimit = number | "infinium" | "infinity"; | ||
export declare type MutationBehavior = "mergePastReversed" | "mergePast" | "destroyFuture" | "keepFuture"; | ||
export interface Action { | ||
export interface Action<T> { | ||
type: ActionType; | ||
payload?: any; | ||
payload?: T; | ||
behavior?: MutationBehavior; | ||
@@ -13,6 +13,6 @@ historyLimit?: HistoryLimit; | ||
} | ||
export interface State { | ||
past: any[]; | ||
present: any; | ||
future: any[]; | ||
export interface State<T> { | ||
past: T[]; | ||
present: T; | ||
future: T[]; | ||
} | ||
@@ -19,0 +19,0 @@ export interface Options { |
{ | ||
"name": "use-undoable", | ||
"version": "4.0.0", | ||
"version": "5.0.0", | ||
"description": "React hook for undo/redo functionality without the hassle.", | ||
@@ -5,0 +5,0 @@ "private": false, |
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
66586
545