A universal but simple node library to implement user settings, originally built to work with Electron.js
npm install node-user-settings
npm i node-user-settings --save
`
$3
---
$3
- For now, callback-based methods naming are weird, I know. I am going to fix it in a major release; in the future.
- You can specify an optional filename in which settings would be saved or retrieved. An optional filename isn't required and can be left blank.
- A new preference file would be created after using the optionalFileName argument, with the persisted preference in it.
- Adding a sync as suffix to the promise-based functions, provides you with it's synchronous alternatives.
- preferenceFileName is no longer deprecated as of v1.5.0. Use preferenceDir, fileName and fileExt to set the file name to be used to persist settings, if you want to split the preference directory, filename and extension.
- Specifying only preferenceFileName in options is the same as calling settings.setDefaultPreferenceFilePath()
- Leaving fileExt config option blank but setting a fileName, results in a file with the .json file extension.
- It is recommended that you only initialize the API once and then pass the initialized instance around using Dependency Injection. Even though you don't do this, it's still possible that the API would work as you want because Node JS automatically caches a module after requiring them. But again, I wouldn't recommend you do that!
Setup and Initialization 🛠️
Initialization using exported method
`javascript
const settings = require("node-user-settings)([options]);
`
In case you don't want to initialize options using exported method, initialize it this way
`javascript
const settings = require("node-user-settings").defaults;
`
Please note: You must call settings.setDefaultPreferenceFilePath() after using .defaults, else an error would be thrown
$3
#### Type: Object
$3
#### preferenceFileName
The filename (with extension) used to persist preference
#### preferenceFileDir
The directory used to persist preference
#### fileName
The file name (without extension) used to persist preference
#### fileExt
The file extension of the file used to persist preference
Example
For Non-Electron JS users 💡
---
#### Splitting paths into different units
`javascript
// preferenceFileName is optional, it defaults to a Settings.json file
const settings = require("node-user-settings")({
preferenceFileDir: "path/to/save/preference",
preferenceFileName: "Settings.json",
fileName: "Settings",
fileExt: "json"
});
`
#### Merging paths into a single unit
`javascript
// using only preferenceFileName, it is required for you to input the full path to the file
const settings = require("node-user-settings")({
preferenceFileName: "path/to/save/preference/Settings.json"
});
`
For Electron JS Users 💡
---
#### Splitting paths into different units
`javascript
// preferenceFileName is optional, it defaults to a Settings.json file
const { app } = require("electron");
const { join } = require("path");
const settings = require("node-user-settings")({
/ this is the recommended path to persist preference /
preferenceFileDir: join(app.getPath("userData"), "User", "Preferences"),
/ preferenceFileName is optional, it defaults to a Settings.json file /
preferenceFileName: "Settings.json",
fileName: "Settings",
fileExt: "json"
});
`
#### Merging paths into a single unit
`javascript
const { app } = require("electron");
const { join } = require("path");
// using only preferenceFileName, it is required for you to input the full path to the file
const settings = require("node-user-settings")({
/ this is the recommended path to persist preference /
preferenceFileName: join(app.getPath("userData"), "User", "Preferences", "Settings.json")
});
`
The _settings_ variable where module was imported and stored, is what would be used to call the following methods listed below
General Utility Method 💡
$3
Gets the default save path to the preference
#### Returns
_A String_. The default save path to the preference
Example
`javascript
const path = settings.getDefaultPreferenceFilePath();
console.log(path);
`
$3
Gets the optional save path to the preference, if an optional file path was previously specified
Please note: You mostly never need to use this method at all! Never include it in production code,
use only in development mode
#### Returns
_A String_. The temporary save path to the preference
Example
`javascript
const tmpPath = settings.getTempPreferenceOptionalFilePath();
console.log(tmpPath);
`
$3
Sets the default path to the preference file
_Note_: This method is synchronous, and it is to be used in initialization only. Even though you tried to re-set the default save path, it would throw an error, so you should use it to explicitly set the default preference file path immediately after importing or _requiring_ the module
$3
---
$3
#### Type: String
The file path to persist preference
#### Returns
_A String_. The default save path to the preference
Example
`javascript
const path = settings.setDefaultPreferenceFilePath("path/to/save/preference");
console.log(path);
`
Promise-based Method 💡
$3
Gets a value asynchronously
$3
---
$3
#### Type: String
The key in the preference in which it's value would be retrieved
$3
#### Type:String
An optional filename used to persist the settings. This can be left _null_
$3
#### Type: String
A default value to be used if that key has never been set
#### Returns
_Promise that resolves to a string_. The value which was mapped to the key specified
Example
`javascript
const value = await settings.getState("key", "a-default-value" optionalFileName);
console.log(value);
`
$3
Sets a value asynchronously
$3
---
$3
#### Type: String
The key in the preference in which it's value would be retrieved
$3
#### Type: String
The value to be set and mapped to the key
$3
#### Type: String
An optional filename used to persist the settings. This can be left _null_
#### Returns
_Promise that resolves to a Boolean_. indicating if the operation was successful or not
Example
`javascript
const isSet = await settings.setState("key", "value", optionalFileName);
console.log(Is value set: ${isSet});
`
$3
Sets multiple value simultaneously and asynchronously
$3
---
$3
#### Type: Object
A map with the key-value pair to be persisted
$3
#### Type: String
An optional filename used to persist the settings. This can be left _null_
#### Returns
_A Promise that resolves an Array_. A list of all the values that were persisted / set
Example
`javascript
// the states to be persisted
const map = {
moduleName: "node-user-settings",
version: "1.0.0",
author: "noahweasley"
};
let persisted = await settings.setStates(map, optionalFileName);
console.log(This values were set: ${Array.toString(persisted)});
`
$3
Gets multiple value simultaneously and asynchronously
$3
---
$3
#### Type: Array
An array of keys of which values would be retrieved
$3
#### Type: String
An optional filename used to persist the settings. This can be left _null_
#### Returns
_A Promise that resolves an Array_. A list of all the values that were retrieved
Example
`javascript
// the states to be retrieved
let states = ["key1", "key2", "key3"];
let [value1, value2, value3] = await settings.getStates(states, optionalFileName);
`
$3
Asynchronously checks if a key exists
$3
---
$3
#### Type: String
The key in the preference in which it's value would be checked for it's existence
$3
#### Type: String
An optional filename used to do the check. This can be left _null_
#### Returns
_Promise that resolves to a Boolean_. indicating if the key exists in the persisted preference
Example
`javascript
let hasKey = await settings.hasKey("key", optionalFileName);
console.log(Is this key available: ${hasKey ? YES : NO});
`
$3
Asynchronously deletes a single entry
$3
---
$3
#### Type: String
The key in the preference in which it's value would be deleted
$3
#### Type: String
An optional filename used to persist the settings. This can be left _null_
#### Returns
_Promise that resolves to a Boolean_. indicating if the key was successfully deleted
Example
`javascript
let isDeleted = await settings.deleteKey("key", optionalFileName);
console.log(Is key deleted? ${isDeleted ? YES : NO});
`
$3
Asynchronously deletes the preference file
$3
---
$3
#### Type: String
An optional filename in which the corresponding file would be deleted. This can be left _null_
#### Returns
_Promise that resolves to a Boolean_. indicating if the file was successfully deleted
Example
`javascript
let isDeleted = await settings.deleteFile(optionalFileName);
console.log(Is file deleted? ${isDeleted ? YES : NO});
`
$3
Asynchronously replaces all data in preference
$3
---
$3
#### Type: Object
A JSON object to be serialized and persisted
$3
#### Type: String
An optional filename used to persist the settings. This can be left _null_
#### Returns
_A Boolean_. indicating if it was persisted successfully
Example
`javascript
const mockData = {
moduleName: "node-user-settings",
version: "1.0.0",
author: "noahweasley"
};
const isPersisted = await settings.serialize(mockData, optionalFileName);
console.log(Is data changed ? ${isPersisted ? YES : NO});
`
$3
Asynchronously retrieves all the data in preference
$3
---
$3
#### Type: String
An optional filename used to persist the settings. This can be left _null_
#### Returns
_Promise that resolves to A String_. The persisted object as it exists in preference
Example
`javascript
const data = await settings.deserialize(optionalFileName);
console.log(data);
`
Callback-based Method 💡
$3
Gets a value asynchronously
$3
---
$3
#### Type: String
The key in the preference in which it's value would be retrieved
$3
#### Type: String
A default value to be used if that key has never been set
$3
#### Type: String
An optional filename used to persist the settings. This can be left _null_
$3
#### Type: Function
A Node-Js qualified callback with any error that occurred as the first argument and a String as the second argument, representing the value which was mapped to the key specified
Example
`javascript
settings.getState_c("key", "a-default-value", optionalFileName, function (err, value) {
if (err) console.log(err);
else console.log(value);
});
`
$3
Sets a value asynchronously
$3
---
$3
#### Type: String
The key in the preference in which it's value would be set
$3
#### Type: String
The value to be set and mapped to the key
$3
#### Type: String
An optional filename used to persist the settings. This can be left _null_
$3
#### Type: Function
A Node-Js qualified callback with any error that occurred as the first argument and a Boolean as the second argument, indicating if the operation was successful or not
Example
`javascript
settings.setState_c("key", "value", optionalFileName, function (err, isSet) {
if (err) console.error(err);
else console.log(Is value set: ${isSet});
});
`
$3
Sets multiple value simultaneously and asynchronously
$3
---
$3
#### Type: Object
A map with the key-value pair to be persisted
$3
#### Type: String
An optional filename used to persist the settings. This can be left _null_
$3
#### Type: Function
A Node-Js qualified callback with any error that occurred as the first argument and an Array as the second argument, representing a list of all the values that were persisted / set
Example
`javascript
// the states to be persisted
const map = {
moduleName: "node-user-settings",
version: "1.0.0",
author: "noahweasley"
};
settings.setStates_c(map, optionalFileName, function (err, persisted) {
if (err) console.error(err);
else console.log(This values were set: ${Array.toString(persisted)});
});
`
$3
Gets multiple value simultaneously and asynchronously
$3
---
$3
#### Type: Array
An array of keys of which values would be retrieved
$3
#### Type: String
An optional filename used to persist the settings. This can be left _null_
$3
#### Type: Function
A Node-Js qualified callback with any error that occurred as the first argument and an Array as the second argument, representing list of all the values that were retrieved
Example
`javascript
// the states to be retrieved
let states = ["key1", "key2", "key3"];
settings.getStates_c(states, optionalFileName, function (err, values) {
let [value1, value2, value3] = values;
// do something with this array of values
});
`
$3
Asynchronously checks if a key exists
$3
---
$3
#### Type: String
The key in the preference in which it's value would be checked for it's existence
$3
#### Type: String
An optional filename used to do the check. This can be left _null_
$3
#### Type: Function
A Node-Js qualified callback with any error that occurred as the first argument and a Boolean as the second argument, indicating if the key exists in the persisted preference
Example
`javascript
settings.hasKey_c("key", optionalFileName, function (err, hasKey) {
if (err) console.error(err);
else console.log(Is this key available: ${hasKey ? YES : NO});
});
`
$3
Asynchronously deletes a single entry
$3
---
$3
#### Type: String
The key in the preference in which it's value would be deleted
$3
#### Type: String
An optional filename used to persist the settings. This can be left _null_
$3
#### Type: Function
A Node-Js qualified callback with any error that occurred as the first argument and a Boolean as the second argument, indicating if the key was successfully deleted
Example
`javascript
settings.deleteKey_c("key", optionalFileName, function (err, isDeleted) {
if (err) console.error(err);
else console.log(Is key deleted? ${isDeleted ? YES : NO});
});
`
$3
Asynchronously deletes the preference file
$3
---
$3
#### Type: String
An optional filename in which the corresponding file would be deleted. This can be left _null_
$3
#### Type: Function
A Node-Js qualified callback with any error that occurred as the first argument and a Boolean as the second argument, indicating if the file was successfully deleted
Example
`javascript
settings.deleteFile_c(optionalFileName, function (err, isDeleted) {
if (err) console.error(err);
else console.log(Is file deleted? ${isDeleted ? YES : NO});
});
`
$3
Asynchronously replaces all data in preference
$3
---
$3
#### Type: Object
A JSON object to be serialized and persisted
$3
#### Type: String
An optional filename used to persist the settings. This can be left _null_
$3
#### Type: Function
A Node-Js qualified callback with any error that occurred as the first argument and a Boolean as the second argument, indicating if the file was successfully persisted
Example
`javascript
const mockData = {
moduleName: "node-user-settings",
version: "1.0.0",
author: "noahweasley"
};
settings.serialize_c(mockData, optionalFileName, function (err, isPersisted) {
if (err) console.error(err);
else console.log(Is data changed ? ${isPersisted ? YES : NO});
});
`
$3
Asynchronously retrieves all the data in preference
$3
---
$3
#### Type: String
An optional filename used to persist the settings. This can be left _null_
$3
#### Type: Function
A Node-Js qualified callback with any error that occurred as the first argument and a String as the second argument, representing the data that was deserialized and retrieved
Example
`javascript
settings.deserialize_c(optionalFileName, function (err, data) {
if (err) console.error(err);
else console.log(data);
});
`
Synchronous Method 💡
$3
Gets a value synchronously
$3
---
$3
#### Type: String
The key in the preference in which it's value would be retrieved
$3
The default value to be retrieved if that key has never been set
#### Type: String
The key in the preference in which it's value would be retrieved
$3
#### Type: String
An optional filename used to persist the settings. This can be left _null_
#### Returns
_A String_. The value which was mapped to the key specified
Example
`javascript
const value = settings.getStateSync("key", "a-default-value", optionalFileName);
console.log(value);
`
$3
Sets a value synchronously
$3
---
$3
#### Type: String
The key in the preference in which it's value would be retrieved
$3
#### Type: String
The value to be set and mapped to the key
$3
#### Type: String
An optional filename used to persist the settings. This can be left _null_
#### Returns
_A Boolean_. indicating if the operation was successful or not
Example
`javascript
const isSet = settings.setStateSync("key", "value", optionalFileName);
console.log(Is value set: ${isSet});
`
$3
Sets multiple value simultaneously and synchronously
$3
---
$3
#### Type: Object
A map with the key-value pair to be persisted
$3
#### Type: String
An optional filename used to persist the settings. This can be left _null_
#### Returns
_An Array_. A list of all the values that were persisted / set
Example
`javascript
// the states to be persisted
const map = {
moduleName: "node-user-settings",
version: "1.0.0",
author: "noahweasley"
};
let persisted = settings.setStatesSync(map, optionalFileName);
console.log(This values were set: ${Array.toString(persisted)});
`
$3
Gets multiple value simultaneously and synchronously
$3
---
$3
#### Type: Array
An array of keys of which values would be retrieved
$3
#### Type: String
An optional filename used to persist the settings. This can be left _null_
#### Returns
_An Array_. A list of all the values that were retrieved
Example
`javascript
// the states to be retrieved
let states = ["key1", "key2", "key3"];
let [value1, value2, value3] = settings.getStatesSync(states, optionalFileName);
`
$3
Synchronously checks if a key exists
$3
---
$3
#### Type: String
The key in the preference in which it's value would be checked for it's existence
$3
#### Type: String
An optional filename used to do the check. This can be left _null_
#### Returns
_A Boolean_. indicating if the key exists in the persisted preference
Example
`javascript
let hasKey = settings.hasKeySync("key", optionalFileName);
console.log(Is this key available: ${hasKey ? YES : NO});
`
$3
Synchronously deletes a single entry
$3
---
$3
#### Type: String
The key in the preference in which it's value would deleted
$3
#### Type: String
An optional filename used to persist the settings. This can be left _null_
#### Returns
_A Boolean_. indicating if the key was successfully deleted
Example
`javascript
let isDeleted = settings.deleteKeySync("key", optionalFileName);
console.log(Is key deleted? ${isDeleted ? YES : NO});
`
$3
Synchronously deletes the preference file
$3
---
$3
#### Type: String
An optional filename in which the corresponding file would be deleted. This can be left _null_
#### Returns
_A Boolean_. indicating if the file was successfully deleted
Example
`javascript
let isDeleted = settings.deleteFileSync(optionalFileName);
console.log(Is file deleted? ${isDeleted ? YES : NO});
`
$3
Synchronously replaces all data in preference
$3
---
$3
#### Type: Object
A JSON object to be serialized and persisted
$3
#### Type: String
An optional filename used to persist the settings. This can be left _null_
Example
`javascript
const mockData = {
moduleName: "node-user-settings",
version: "1.0.0",
author: "noahweasley"
};
const isPersisted = settings.serializeSync(mockData, optionalFileName);
console.log(Is data changed ? ${isPersisted ? YES : NO});
`
$3
Synchronously retrieves all the data in preference
$3
---
$3
#### Type: String
An optional filename used to persist the settings. This can be left _null_
#### Returns
_A String_. The persisted object as it exists in preference
Example
`javascript
const data = settings.deserializeSync(optionalFileName);
console.log(data);
``