npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-prefs

v1.0.8

Published

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

Downloads

20

Readme

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:

|Type|Name|Description| |:---|:---|:----------| |Object|mOptions|A JSON-like object containing instance options| |String|mOptions.fileName|The name of the file where preferences are stored| |String|mOptions.sFilePath|The full path to the settings data file.| |Object|mOptions.defaults|A set of default settings and/or preferences|


  • Properties:

|Type|Name|Description|Read-only|Static| |:---|:---|:---|:---|:---| |Object|defaults|The default values for this instance.|Yes|No| |String|path|The full path to the configuration data file.|Yes|No| |Number|size|he number of entries in the settings file (same as length).|Yes|No| |Number|length|he number of entries in the settings file (same as size).|Yes|No|


  • Methods:

NodePrefs.parseDataFile(sFilePath, mDefaults)

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

Parameters:

|Type|Name|Description| |:---|:---|:----------| |String|sFilePath|The full path to the settings data file.| |Object|mDefaults|A 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:

|Type|Name|Description| |:---|:---|:----------| |Object|oObj|The object (with nested objects) to flatten.| |String|sSeparator|A 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:

|Type|Name|Description| |:---|:---|:----------| |String|sKey|The 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:

|Type|Name|Description| |:---|:---|:----------| |Function|fCallback|The function to execute for each key-value pair.| |Object|thisArg|The 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:

|Type|Name|Description| |:---|:---|:----------| |String|sKey|The 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:

|Type|Name|Description| |:---|:---|:----------| |String|sKey|The 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:

|Type|Name|Description| |:---|:---|:----------| |String|sKey|The key of a settings item in the settings list.| |any|sValue|The 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