Socket
Socket
Sign inDemoInstall

electron-json-config

Package Overview
Dependencies
0
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    electron-json-config

Simply set and get configuration from a json file for your Electron app


Version published
Weekly downloads
203
increased by17.34%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

electron-json-config

Simply set and get configuration from a json file for your Electron app

The config file (config.json) is located in the path returned by app.getPath('userData'). This package can be used from browser and renderer process.

Usage

const config = require('electron-json-config');

config.set('foo', 'bar');
console.log(config.get('foo')); // shows 'bar'

Documentation

All key can be a classic key (eg: foo) or a multiple level key with levels separated by . (eg: foo.bar)

.file()

Returns the name of the file the config is stored in.
Returns: String

.set(key, value)

Sets a key with the specified value. Overwrites the value, if the key already exists..

ParametersTypeOptionalDescription
keyStringThe key to set
value*  The value to set under the key

Returns: void
Example:

config.set('foo', 'bar'); // Sets 'bar' under 'foo' key
config.set('anArray', [1, 2]); // Sets [1, 2] under 'anArray' key
config.set('the.answer', 42); // Sets 42 under 'answer' under 'the'

.setBulk(items)

Like .set() but sets multiple keys in a single call.

ParametersTypeOptionalDescription
itemsObjectAn object whose attributes will become keys

Returns: void
Example:

// Sets 'bar' under 'foo' key
// Sets 42 under 'answer' under 'the'
config.setBulk({
  'foo': 'bar',
  'the.answer': 42,
});

.has(key)

Checks if a key exists.

ParametersTypeOptionalDescription
keyStringThe name of a key to test existence

Returns: Boolean
Example:

config.set('foo', 'bar');

config.has('foo'); // true
config.has('bar'); // false

.get(key[, defaultValue])

Returns the value associated with the key, undefined otherwise.
You can specify a default value returned in case the key does not exists.

ParametersTypeOptionalDescription
keyStringThe name of the key to get
defaultValue*The value to return in case value does not exists

Returns: *
Example:

config.set('foo', 'bar'); // Sets 'bar' under 'foo' key

config.get('foo');        // Returns 'bar'
config.get('bar', 42);    // Returns 42

.keys([key])

If key is omitted, returns an array containing all keys in the config file.
If key is provided, returns an array containing all sub keys in the key object.

ParametersTypeOptionalDescription
keyStringThe name of a key to get sub keys

Returns: Array<String>
Example:

config.setBulk({
  'foo': 'bar',
  'the.answer': 42,
});

config.keys();      // Returns ['foo', 'the']
config.keys('the'); // Returns ['answer']

.all()

Returns an object with all the data currently saved.

Returns: Object
Example:

config.setBulk({
  'foo': 'bar',
  'the.answer': 42,
});

config.all();
/*
{
  'foo': 'bar',
  'the': {
    'answer': 42
  }
}
*/

.delete(key)

Removes the key and its value from the config file.

ParametersTypeOptionalDescription
keyStringThe name of a key to delete

Returns: void
Example:

config.set('foo', 'bar'); // Sets 'bar' under 'foo' key
config.delete('foo');     // Removes key 'bar' and its value

.deleteBulk(keys)

Removes all the keys specified and theirs value from the config file.

ParametersTypeOptionalDescription
keysArray<String>An array of keys to remove

Returns: void
Example:

config.setBulk({
  'foo': 'bar',
  'the.answer': 42,
});

// Remove keys 'foo' and 'answer'
config.deleteBulk(['foo', 'answer']);

.purge()

Removes all data from the config file.

Returns: void
Example:

config.purge(); // All keys are removed

Keywords

FAQs

Last updated on 10 May 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc