A lightweight utility for managing nested state with command-driven operations. Perform atomic updates and batch operations with ease.
Install via npm:
npm install chalkityarn add chalkitimport { Chalkit } from "chalkit";
// Initialize store
const store = {};
const chalkit = new Chalkit(store, { marker: "$", divider: "_" });// Set a value in a deep path using the divider
chalkit.apply("user_profile_name$set", { name: "John" });
// Merge values into a deep path
chalkit.apply("user_profile_details$merge", {
age: 30,
email: "[email protected]",
});// Clear a value in a deep path
chalkit.apply("user_profile_name$remove");// Append to an array in a deep path
chalkit.apply("user_profile_tags$arrayAppend", {
data: ["typescript", "javascript"],
});// Toggle an item in an array in a deep path
chalkit.apply("user_profile_favorites$arrayToggle", "item1");// Remove an item from an array in a deep path
chalkit.apply("user_profile_tags$arrayRemove", { item: "typescript" });// Set an item in a collection at a deep path
chalkit.apply("organization_members$itemSet", {
id: "user1",
data: { name: "John", age: 30 },
});// Merge an item in a collection at a deep path
chalkit.apply("organization_members$itemMerge", {
id: "user1",
data: { email: "[email protected]" },
});// Delete an item from a collection at a deep path
chalkit.apply("organization_members$itemDelete", "user1");chalkit.batch([
{ command: "user_profile_name$set", payload: { name: "John" } },
{ command: "settings_theme$set", payload: { theme: "dark" } },
{
command: "user_profile_tags$arrayAppend",
payload: { data: ["typescript"] },
},
]);try {
chalkit.batch([
{ command: "user_profile_name$set", payload: { name: "John" } },
{ command: "settings_invalid$set", payload: { theme: "dark" } }, // Invalid operation
]);
} catch (error) {
console.log("Batch failed, all changes rolled back");
}This project is licensed under the MIT License - see the LICENSE file for details.