horae-configure
v0.1.0
Published
"horae" is a lightweight TypeScript library that provides methods to easily read JSON files and manipulate data using dot-notation and array index property access.
Maintainers
Readme
Introduction
"horae" is a lightweight TypeScript library that provides methods to easily read JSON files and manipulate data using dot-notation and array index property access.
Features
- Read JSON files effortlessly.
- Get and set nested properties using dot-notation (e.g.
position.x) or array index syntax (e.g.items[0].name). - Check if a specific property exists using
has. - Delete properties with
delete. - Reset all data with
clear. - Reload data from file with
reload. - Safe property access with
getOrDefault. - Save updated data back to the file with
save.
Installation
npm install horae-configureyarn add horae-configureHow to use?
Prepare a JSON file in the root of your project:
{
"position": {
"x": 1,
"y": 2
},
"items": [
{ "name": "foo" }
]
}Initialize a Horae instance with the filename (without extension):
import { Horae } from 'horae-configure';
const horae = new Horae<{
position: { x: number; y: number };
items: { name: string }[];
}>('config');get
Retrieve a value using dot-notation or array index syntax.
horae.get('position.x'); // 1
horae.get('items[0].name'); // "foo"
horae.get('position.z'); // undefinedhas
Check whether a property exists.
horae.has('position.x'); // true
horae.has('position.z'); // falseset
Set a value at a given path. Intermediate objects are created automatically.
horae.set('position.x', 100);
horae.set('profile.address.city', 'Taipei');delete
Remove a property at a given path.
horae.delete('position.x');
horae.has('position.x'); // false
horae.has('position.y'); // trueclear
Reset all data to an empty object.
horae.clear();
horae.has('position'); // falsereload
Discard in-memory changes and re-read data from the file.
horae.set('position.x', 999);
horae.reload();
horae.get('position.x'); // back to original value from filegetOrDefault
Return the value if it exists, otherwise return the provided default.
horae.getOrDefault('position.x', 0); // 1
horae.getOrDefault('position.z', 0); // 0save
Persist the current in-memory data back to the JSON file.
horae.set('position.x', 1000);
horae.save();The file will be updated to:
{
"position": {
"x": 1000,
"y": 2
}
}