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

@msfs/electron-json-config

v1.0.0

Published

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

Downloads

3

Readme

electron-json-config

npm codecov

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

This is the 2.x.x tree.
For 1.x.x code and documentation please refer to the 1.x.x tree.
See UPGRADE.md for an upgrade guide.

This package can be used from main and renderer process.

Installation

NPM

npm install --save electron-json-config@beta

yarn

yarn add electron-json-config@beta

Usage

CommonJS

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

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

ES Modules

import { factory } from 'electron-json-config';

const config = factory();

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

Documentation

Key

type Key = string | Array<string>;

A key can be :

  • a classic string key
    eg: 'foo'
  • a dotted string multi level key
    eg: 'foo.bar'
  • an array of string representing a multi level key
    eg: ['foo', 'bar']

Storable

interface Storable {
  [key: string]: Storable | any;
}

factory(file?: string, key?: string): Conf

Description:
Create an instance of Config and returns it.
If an instance with the same key exist, returns this instance instead.

If file is specified, the configuration will be saved to that file instead of the default app.getPath('userData') + '/config.json'.

If key is specified, the requested instance will be saved under une given key instead of the default userData.

Examples:

// file: app.getPath('userData') + '/config.json'
// key: 'userData'
factory();

// file: '/data/test.json'
// key: '/data/test.json'
factory('/data/test.json');

// file: '/data/test.json'
// key: 'test'
factory('/data/test.json', 'test');

// file: app.getPath('userData') + '/config.json'
// key: 'test'
factory(undefined, 'test');

Parameters: | Name | Type | Default | | ------- | -------- | ------------------------------------------ | | file? | string | app.getPath('userData') + '/config.json' | | key? | string | key || file || 'userData' |

Returns: void

Config

The config class is a set of wrappers and helpers providing access to configuration and file synchronization.

new Config(file: string, data: Storable): Config

Parameters: | Name | Type | | ------ | ---------- | | file | string | | data | Storable |

Returns: Config

get file(): string

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

Returns: string

all(): Storable

Description: Returns all the data currently saved.

Returns: Storable

delete(key: Key): void

Description: Removes the key and its value from the config file.

Parameters: | Name | Type | | ----- | ----- | | key | Key |

Returns: void

deleteBulk(keys: Array<Key>): void

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

Parameters: | Name | Type | | ------ | --------------- | | keys | Array<Key> |

Returns: void

get<T>(key: Key, defaultValue?: T): T | undefined

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

Parameters: | Name | Type | | --------------- | ----- | | key | Key | | defaultValue? | T |

Returns: T | undefined

has(key: Key): boolean

Description: Checks if a key exists.

Parameters: | Name | Type | | ----- | ----- | | key | Key |

Returns: boolean

keys(key?: Key): Array<string>

Description: 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.

Parameters: | Name | Type | | ------ | ----- | | key? | Key |

Returns: Array<string>

purge(): void

Description: Removes all data from the config file.

Returns: void

set<T>(key: Key, value: Storable | T): void

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

Parameters: | Name | Type | | ------- | --------------- | | key | Key | | value | Storable | T |

Returns: void

setBulk<T>(items: { [key:string]: Storable | T }): void

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

Parameters: | Name | Type | | ------- | ------------------------------------ | | items | { [key: string]: Storable | T } |

Returns: void