Skip to content

Commit ea68758

Browse files
committed
v1.0.6
1 parent c0eea7a commit ea68758

File tree

8 files changed

+352
-253
lines changed

8 files changed

+352
-253
lines changed

.github/ISSUE_TEMPLATE/feature_request.yml

Lines changed: 47 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,52 @@ description: Suggest an idea or feature.
33
labels:
44
- enhancement
55
- needs-triage
6+
67
body:
8+
- type: markdown
9+
attributes:
10+
value: 'Please read and follow the instructions before submitting an issue:'
11+
12+
- type: markdown
13+
attributes:
14+
value: |
15+
- Read all our documentation, especially the [README](https://github.com/heliomarpm/keyvalues-storage/blob/master/README.md). It may contain information that helps you solve your issue.
16+
- Ensure your issue isn't already [reported](https://github.com/heliomarpm/keyvalues-storage/issues?utf8=%E2%9C%93&q=is%3Aissue).
17+
18+
- type: markdown
19+
attributes:
20+
value: '⚠️👆 Feel free to these instructions before submitting the issue 👆⚠️'
21+
22+
- type: textarea
23+
id: description
24+
attributes:
25+
label: 'Is your feature request related to a problem? Please describe.'
26+
description: A clear and concise description of what the problem is.
27+
placeholder: I'm always frustrated when [...]
28+
validations:
29+
required: true
30+
31+
- type: textarea
32+
id: solution
33+
attributes:
34+
label: Describe the solution you'd like
35+
description: A clear and concise description of what you want to happen.
36+
validations:
37+
required: false
38+
39+
- type: textarea
40+
id: alternative
41+
attributes:
42+
label: Describe alternatives you've considered
43+
description: A clear and concise description of any alternative solutions or features you've considered.
44+
validations:
45+
required: false
746

8-
- type: markdown
9-
attributes:
10-
value: |
11-
- Read all our documentation, especially the [README](https://github.com/heliomarpm/keyvalues-storage/blob/master/README.md). It may contain information that helps you solve your issue.
12-
- Ensure your issue isn't already [reported](https://github.com/heliomarpm/keyvalues-storage/issues?utf8=%E2%9C%93&q=is%3Aissue).
13-
- type: markdown
14-
attributes:
15-
value: '⚠️👆 Feel free to these instructions before submitting the issue 👆⚠️'
16-
- type: textarea
17-
id: description
18-
attributes:
19-
label: 'Is your feature request related to a problem? Please describe.'
20-
description: A clear and concise description of what the problem is.
21-
placeholder: I'm always frustrated when [...]
22-
validations:
23-
required: true
24-
- type: textarea
25-
id: solution
26-
attributes:
27-
label: Describe the solution you'd like
28-
description: A clear and concise description of what you want to happen.
29-
validations:
30-
required: false
31-
- type: textarea
32-
id: alternative
33-
attributes:
34-
label: Describe alternatives you've considered
35-
description: A clear and concise description of any alternative solutions or features you've considered.
36-
validations:
37-
required: false
38-
- type: textarea
39-
id: additional-context
40-
attributes:
41-
label: 'Additional context/Screenshots'
42-
description: Add any other context or screenshots about the feature request here.
43-
render: bash
44-
validations:
45-
required: false
47+
- type: textarea
48+
id: additional-context
49+
attributes:
50+
label: 'Additional context/Screenshots'
51+
description: Add any other context or screenshots about the feature request here.
52+
render: bash
53+
validations:
54+
required: false

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
node_modules
22
localdb
3+
data
34
dist
45
lib
56
tmp

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@heliomarpm/kvs",
3-
"version": "1.0.5",
3+
"version": "1.0.6",
44
"description": "A simple and robust KeyValues Storage's library",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
@@ -36,7 +36,7 @@
3636
"build": "tsc",
3737
"test": "jest --detectOpenHandles",
3838
"test:c": "jest --coverage",
39-
"commit": "node ./cmd/modules/release/index.js"
39+
"pub:release": "node ./cmd/modules/release/index.js"
4040
},
4141
"dependencies": {
4242
"lodash": "^4.17.21",
@@ -54,4 +54,4 @@
5454
"@types/lodash": "^4.14.194",
5555
"@types/write-file-atomic": "^4.0.0"
5656
}
57-
}
57+
}

src/internal/JsonFileHelper.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import writeFileAtomic from 'write-file-atomic';
44

55
import './types';
66

7+
export const DEFAULT_DIR_NAME = 'localdb';
8+
export const DEFAULT_FILE_NAME = 'keyvalues.json';
9+
710
export class JsonFileHelper {
811

912
options: Options;
@@ -21,7 +24,8 @@ export class JsonFileHelper {
2124
* @internal
2225
*/
2326
private getJsonDirPath(): string {
24-
return this.options.dir ?? path.resolve('localdb')
27+
let dir = (this.options.dir ?? path.resolve(DEFAULT_DIR_NAME)).trim();
28+
return dir === '' ? './' : dir;
2529
}
2630

2731
/**
@@ -33,8 +37,10 @@ export class JsonFileHelper {
3337
*/
3438
public getJsonFilePath(): string {
3539
const dir = this.getJsonDirPath();
40+
let fileName = this.options.fileName.trim();
41+
fileName = fileName === '' ? DEFAULT_FILE_NAME : fileName;
3642

37-
return path.join(dir, this.options.fileName);
43+
return path.join(dir, fileName);
3844
}
3945

4046
/**
@@ -48,7 +54,7 @@ export class JsonFileHelper {
4854
const filePath = this.getJsonFilePath();
4955

5056
return new Promise((resolve, reject) => {
51-
fs.stat(filePath, (err) => {
57+
fs.stat(filePath, (err: any) => {
5258
if (err) {
5359
if (err.code === 'ENOENT') {
5460
this.saveKeyValues({}).then(resolve, reject);
@@ -93,10 +99,10 @@ export class JsonFileHelper {
9399
const dirPath = this.getJsonDirPath();
94100

95101
return new Promise((resolve, reject) => {
96-
fs.stat(dirPath, (err) => {
102+
fs.stat(dirPath, (err: any) => {
97103
if (err) {
98104
if (err.code === 'ENOENT') {
99-
fs.mkdir(dirPath, { recursive: true }, (error) => {
105+
fs.mkdir(dirPath, { recursive: true }, (error: any) => {
100106
error ? reject(error) : resolve();
101107
});
102108
// mkdirp(dirPath).then(() => resolve(), reject);
@@ -143,12 +149,13 @@ export class JsonFileHelper {
143149
const filePath = this.getJsonFilePath();
144150

145151
return await new Promise((resolve, reject) => {
146-
fs.readFile(filePath, 'utf-8', (err, data) => {
152+
fs.readFile(filePath, 'utf-8', (err: any, data: string | any[]) => {
147153
if (err) {
148154
reject(err);
149155
} else {
150156
try {
151-
resolve(JSON.parse(data.length ? data : "{}"));
157+
// resolve(JSON.parse(data.length ? data : "{}"));
158+
resolve(JSON.parse(data ? (Array.isArray(data) ? data.join('') : data) : "{}"));
152159
} catch (err_1) {
153160
reject(err_1);
154161
}
@@ -193,7 +200,7 @@ export class JsonFileHelper {
193200
: resolve();
194201
});
195202
} else {
196-
fs.writeFile(filePath, data, (err_1) => {
203+
fs.writeFile(filePath, data, (err_1: any) => {
197204
return err_1 ? reject(err_1)
198205
: resolve();
199206
});

src/keyvalues.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { get as _get, set as _set, has as _has, unset as _unset } from 'lodash';
22

33
import './internal/types';
4-
import { JsonFileHelper } from './internal/JsonFileHelper'
4+
import { DEFAULT_DIR_NAME, DEFAULT_FILE_NAME, JsonFileHelper } from './internal/JsonFileHelper'
55

66
/** @internal */
77
const defaultOptions: Options = {
88
atomicSave: true,
9-
fileName: 'keyvalues.json',
9+
dir: DEFAULT_DIR_NAME,
10+
fileName: DEFAULT_FILE_NAME,
1011
prettify: false,
1112
numSpaces: 2,
1213
};

test/codium.test.ts

Lines changed: 0 additions & 64 deletions
This file was deleted.

test/complex.test.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Generated by CodiumAI
2+
3+
import { KeyValues } from '../src/keyvalues';
4+
import { JsonFileHelper } from '../src/internal/JsonFileHelper';
5+
6+
describe('Codium Complex Test', () => {
7+
it('healthCheck', () => {
8+
expect(1).toBe(1);
9+
});
10+
// Tests that the set method sets the value of a key
11+
it('should set the value of a key', async () => {
12+
const mockSaveKeyValues = jest.spyOn(JsonFileHelper.prototype, 'saveKeyValues').mockResolvedValue();
13+
14+
const kvs = new KeyValues({fileName: 'complex.json', prettify: true});
15+
await kvs.set('key', 'value');
16+
17+
expect(mockSaveKeyValues).toHaveBeenCalledWith({ key: 'value' });
18+
mockSaveKeyValues.mockRestore();
19+
});
20+
21+
// Tests that the set method sets the value of a nested key
22+
it('should set the value of a nested key', async () => {
23+
const mockLoadKeyValues = jest.spyOn(JsonFileHelper.prototype, 'loadKeyValues');
24+
const mockSaveKeyValues = jest.spyOn(JsonFileHelper.prototype, 'saveKeyValues');
25+
mockLoadKeyValues.mockResolvedValue({ parent: {} });
26+
27+
const kvs = new KeyValues({fileName: 'complex.json', prettify: true});
28+
await kvs.set('parent.child', 'value');
29+
30+
expect(mockLoadKeyValues).toHaveBeenCalled();
31+
expect(mockSaveKeyValues).toHaveBeenCalledWith({ parent: { child: 'value' } });
32+
mockLoadKeyValues.mockRestore();
33+
mockSaveKeyValues.mockRestore();
34+
});
35+
36+
// Tests that the unset method removes a key and its value
37+
it('should remove a key and its value', async () => {
38+
const mockLoadKeyValues = jest.spyOn(JsonFileHelper.prototype, 'loadKeyValues');
39+
const mockSaveKeyValues = jest.spyOn(JsonFileHelper.prototype, 'saveKeyValues');
40+
mockLoadKeyValues.mockResolvedValue({ key: 'value' });
41+
42+
const kvs = new KeyValues({fileName: 'complex.json', prettify: true});
43+
await kvs.unset('key');
44+
45+
expect(mockLoadKeyValues).toHaveBeenCalled();
46+
expect(mockSaveKeyValues).toHaveBeenCalledWith({});
47+
mockLoadKeyValues.mockRestore();
48+
mockSaveKeyValues.mockRestore();
49+
});
50+
51+
it('shold get complex value', () => {
52+
const color = {
53+
name: 'cerulean',
54+
code: {
55+
rgb: [0, 179, 230],
56+
hex: '#003BE6',
57+
},
58+
};
59+
const keyValues = new KeyValues({ fileName: 'color.json', prettify: true });
60+
keyValues.setSync('color', color);
61+
expect(keyValues.getSync('color')).toEqual(color);
62+
63+
expect(keyValues.getSync('color.name')).toEqual('cerulean');
64+
expect(keyValues.getSync('color.code.hex')).toEqual('#003BE6');
65+
expect(keyValues.getSync(['color', 'code'])).toEqual({ hex: '#003BE6', rgb: [0, 179, 230] });
66+
67+
expect(keyValues.getSync(['color', 'hue'])).toBeUndefined();
68+
});
69+
});

0 commit comments

Comments
 (0)