The KeyValues
class is responsible for managing key-value pairs and storing them in a JSON file. It provides methods for setting, getting, checking existence, and removing key-value pairs. The class uses the Functions
class to handle file operations and data manipulation.
// Create a new instance of KeyValues with custom options
const keyValues = new KeyValues({
fileName: 'config.json',
prettify: true
});
-- or --
/**
* Default Options
*
* {
* atomicSave: true,
* fileName: 'keyvalues.json',
* prettify: false,
* numSpaces: 2,
* }
*/
const keyValues = new KeyValues()
// Set a key-value pair
await keyValues.set('color.name', 'sapphire');
// Get the value at a specific key path
const value = await keyValues.get('color.name');
// Check if a key path exists
const exists = await keyValues.has('color.name');
// Remove a key-value pair
await keyValues.unset('color.name');
- Manage key-value pairs and store them in a JSON file
- Set and get values at specific key paths
- Check if a key path exists
- Remove key-value pairs
constructor(options?: Partial<Options>)
: Initializes a new instance of theKeyValues
class with optional custom options.file(): string
: Returns the path to the JSON file.reset(): void
: Resets the configuration of theKeyValues
instance to default options.has(keyPath: KeyPath): Promise<boolean>
: Checks if a key path exists asynchronously.hasSync(keyPath: KeyPath): boolean
: Checks if a key path exists synchronously.get<T extends valueTypes>(keyPath?: KeyPath): Promise<T>
: Gets the value at a specific key path asynchronously.getSync<T extends valueTypes>(keyPath?: KeyPath): T
: Gets the value at a specific key path synchronously.set<T extends valueTypes>(...args: [Types<T>] | [KeyPath, T]): Promise<void>
: Sets a value at a specific key path asynchronously.setSync<T extends valueTypes>(...args: [Types<T>] | [KeyPath, T]): void
: Sets a value at a specific key path synchronously.unset(keyPath?: KeyPath): Promise<void>
: Removes a key-value pair at a specific key path asynchronously.unsetSync(keyPath?: KeyPath): void
: Removes a key-value pair at a specific key path synchronously.
options: Options
: The configuration options for theKeyValues
instance.fnc: Functions
: An instance of theFunctions
class used for file operations and data manipulation.
- lodash: The Lodash library exported as Node.js modules.
- write-file-atomic: Atomically and asynchronously writes data to a file, replacing the file if it already exists. data can be a string or a buffer
Please make sure to read the Contributing Guide before making a pull request.
Thank you to all the people who already contributed to project!
Made with contrib.rocks.
That said, there's a bunch of ways you can contribute to this project, like by:
- 🪲 Reporting a bug
- 📄 Improving this documentation
- 🚨 Sharing this project and recommending it to your friends
- 💵 Supporting this project on GitHub Sponsors or Ko-fi
- 🌟 Giving a star on this repository
If you appreciate that, please consider donating to the Developer.