1.3.1 • Published 3 years ago

cfg-lite v1.3.1

Weekly downloads
6
License
MIT
Repository
github
Last release
3 years ago

Cfg Lite

Configration or data file encryption is cumbersome, but it is essential.

CfgLite makes this behavior easy.

Encrypts all files with different values.

Install

npm

npm install cfg-lite

yarn

yarn add cfg-lite

Usage

File Init

If the file does not exist, it is new created. And if the file extension does not exist, .cfg is automatically recognized.

You can specify a unique value of up to 32 characters.

import CfgLite from 'cfg-lite';

const cfg = new CfgLite('/path/to/file.cfg');
const uniqueCfg = new CfgLite('/path/to/file', 'MyUniqueKey');

Get

All keys are separated by periods. Therefore, the key cannot contain periods.

If the value doesn't exist, it returns undefined like optional chaining.

The object is returned as a deep copy.

/*
cfg data
{
	key1: 'hello',
}
*/
cfg.get('key1'); // 'hello'
cfg.get('key1.key2.key-key'); // undefined

Set

/*
cfg data
{
}
*/

cfg.set('key1.key2', 55); // ok
cfg.set('key3.key4.key5', cfg.get('key1')); // ok

/*
result cfg data
{
	key1: {
		key2: 55
	},
	key3: {
		key4: {
			key5: {
				key1: {
					key2: 55
				},
			},
		},
	}
}
*/

Merge

Overwrite the object with new values.

/*
cfg data
{
	key1: {
		key2: 11,
		key3: 22
	}
}
*/

cfg.merge('key1', { key2: 33, key4: 55 });

/*
result cfg data
{
	key1: {
		key2: 33,
		key3: 22,
		key4: 55
	}
}
*/

Delete

/*
cfg data
{
	key1: 'hello'
}
*/

cfg.delete('key1');

/*
result cfg data
{
}
*/

Or you can clear all values.

cfg.deleteAll();

Save

It is inefficient to write a file every time there is a new change. To save the changes to a file you need to use the following function.

cfg.save();
cfg.save('new.cfg'); // New save, Leave before cfg file
cfg.save('new.cfg', true); // New save, Remove before cfg file

Result

{
	"a": {
		"b": {
			"c": "Hello"
		},
		"c": 55
	},
	"c": {
		"d": {
			"e": "Hello"
		}
	},
	"d": true
}

npm.io

Command line

Global install

npm install -g cfg-lite

Decoding commed

cfg [cfg file] [key]

Thanks.

1.3.1

3 years ago

1.3.0

3 years ago

1.2.0

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.2

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.1

4 years ago

1.0.0

4 years ago