This project is archived and no longer maintained. Use the follow repo for updated utilities: llorcadev-utils.
A small TypeScript utility to reset object fields to default values depending on their type (string, number, boolean, array, object, etc.).
It works in-place (mutates the object) which makes it ideal for use with reactive stores (e.g., Vue’s reactive or ref).
If you like the repo, please share it with your dev friends and give it a star. It makes me grow and inspires me to keep creating new things. Thanks.
- Reset all object fields in-place based on type:
string → ''number → 0boolean → falsearray → []object → recurse(default)
- Works recursively for nested objects.
- Allows custom default values by type.
- Allows custom default values by key.
- Supports skipping keys you don’t want to reset.
Just copy install:
yarn add llorcadev-utilsimport { useFieldReseter } from "llorcadev-utils";
const product = {
title: "T-Shirt",
price: 25,
active: true,
tags: ["new", "discount"],
meta: {
created_at: "2025-09-07",
stock: 15,
},
};
// Reset all fields in-place
useFieldReseter(product);
console.log(product);
/*
{
title: '',
price: 0,
active: false,
tags: [],
meta: {
created_at: '',
stock: 0
}
}
*/You can override the default reset values:
useFieldReseter(product, {
rules: {
string: "", // default for strings
number: 0, // default for numbers
boolean: false, // default for booleans
byKey: {
price: 1, // 👈 force "price" to reset to 1
status: "draft", // 👈 force "status" to reset to 'draft'
},
},
});Skip specific keys from being reset:
useFieldReseter(product, {
skip: ["status", "meta"],
});| Option | Type | Default | Description |
|---|---|---|---|
skip |
string[] |
[] |
Array of keys to leave unchanged. |
rules |
ResetRules |
see below | Object defining reset values by type or by key. |
{
string?: any; // default: ''
number?: any; // default: 0
boolean?: any; // default: false
array?: any; // default: []
object?: "recurse" | object; // default: "recurse"
null?: any; // default: null
undefined?: any; // default: null
byKey?: Record<string, any>; // overrides by specific key
}- In-place mutation: the object reference remains the same → works perfectly with Vue’s reactivity system.
- If you prefer a cloning (immutable) version, you can easily adapt it by using
Object.fromEntriesinstead of mutating.
MIT