1.0.8 • Published 5 years ago

node-prefs v1.0.8

Weekly downloads
3
License
ISC
Repository
github
Last release
5 years ago

Node-prefs

User preferences and configuration settings persistence feature for node.js apps.

Overview

Node-prefs module lets you easily persist and retrieve user preferences and configuration settings in your node.js app. Install and include it in your project, so you can focus on your app development.

All user preferences and configuration settings are stored locally in a JSON file with a specified file path and file name.

Installation

npm install node-prefs

or

npm install --save node-prefs


Usage

const NodePrefs = require('node-prefs');
const prefs = new NodePrefs({
  fileName: "config.js",
  defaults: {
    window: {
      width: 600,
      height: 300
    }
  }
});

prefs.set('foo', 'bar');
console.log(prefs.get('foo'));
//=> bar

console.log(prefs.get("window"));
//=> { width: 600, height: 300 }

// use dot-notation to access nested properties
prefs.set('window.width', 700);
console.log(prefs.get("window"));
//=> { width: 700, height: 300 }
console.log(prefs.get('window.width'));
//=> 700

prefs.delete('foo');
console.log(prefs.get('foo'));
//=> undefined

API

  • Constructor:

new NodePrefs(mOptions)

Creates an instance of NodePrefs.

Parameters:

TypeNameDescription
ObjectmOptionsA JSON-like object containing instance options
StringmOptions.fileNameThe name of the file where preferences are stored
StringmOptions.sFilePathThe full path to the settings data file.
ObjectmOptions.defaultsA set of default settings and/or preferences

  • Properties:

TypeNameDescriptionRead-onlyStatic
ObjectdefaultsThe default values for this instance.YesNo
StringpathThe full path to the configuration data file.YesNo
Numbersizehe number of entries in the settings file (same as length).YesNo
Numberlengthhe number of entries in the settings file (same as size).YesNo

  • Methods:

NodePrefs.parseDataFile(sFilePath, mDefaults)

Reads the settings data file and returns its content as a JSON object.

Parameters:

TypeNameDescription
StringsFilePathThe full path to the settings data file.
ObjectmDefaultsA set of default values to return if it fails loading the settings data file.

Returns: Object - A JSON-like object containing the settings and user preferences iub the data file.


NodePrefs.flattenObject(oObj, sSeparator)

Flattens nested objects into a single-depth object. For example:

{ foo: 'bar', baz: { foo: 'bar' } }

will turn into:

{ foo: 'bar', 'baz.foo': 'bar' }

Parameters:

TypeNameDescription
ObjectoObjThe object (with nested objects) to flatten.
StringsSeparatorA string to use as separator between the keys. By default, the separator is . (dot).

Returns: Object - The flatten object.


NodePrefs.prototype.clear()

Removes all the settings in the settings list.

Parameters: None

Returns: NodePrefs - Self-reference for method chaining calls.


NodePrefs.prototype.delete(sKey)

Removes the specified settings item from the settings list.

Parameters:

TypeNameDescription
StringsKeyThe settings item to remove.

Returns: NodePrefs - Self-reference for method chaining calls.


NodePrefs.prototype.entries()

Returns a array containing all the [key, value] pairs for each settings item in the settings list.

Parameters: None

Returns: Array - The [key, value] pairs array.


NodePrefs.prototype.forEach(fCallback, thisArg)

Executes the given function once for each key-value pair in the settings list.

Parameters:

TypeNameDescription
FunctionfCallbackThe function to execute for each key-value pair.
ObjectthisArgThe value of this when executing the callback function.

Returns: NodePrefs - Self-reference for method chaining calls.


NodePrefs.prototype.has(sKey)

Returns whether the settings list contains a settings item with the given key or not.

Parameters:

TypeNameDescription
StringsKeyThe key to check the settings list for.

Returns: Boolean - true if the settings list contains a settings item with given key, or false otherwise.


NodePrefs.prototype.get(sKey)

Gets the value of the settings item referenced by the given key in the settings list, or the whole list if no key is given.

Parameters:

TypeNameDescription
StringsKeyThe key of a settings item in the settings list.

Returns: any - The value of the settings item referenced by the key in the settings list, or the whole list if no key is given.


NodePrefs.prototype.set(sKey, sValue)

Sets the given value as the value of the settings item referenced by the given key in the settings list.

Parameters:

TypeNameDescription
StringsKeyThe key of a settings item in the settings list.
anysValueThe value to assign to the settings item referenced by the key in the settings list.

Returns: NodePrefs - Self-reference for method chaining calls.


NodePrefs.prototype.keys()

Returns the names of all enumerable settings and preferences of this object.

Parameters: None

Returns: String[] - The names of the enumerable settings and preferences.


NodePrefs.prototype.values()

Returns the names of all enumerable settings and preferences of this object.

Parameters: None

Returns: String[] - The values of the enumerable settings and preferences.


Version

1.0.7