Skip to content
This repository was archived by the owner on Aug 2, 2024. It is now read-only.

Commit aa422b6

Browse files
author
Valerii Radchenko
committed
- Added constructor attribute to record interface;
- Added ability to override default constructor on item set; - Added ability to use Array constructor for a key inside brackets [].
1 parent 5096add commit aa422b6

File tree

5 files changed

+45
-6
lines changed

5 files changed

+45
-6
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ambary",
3-
"version": "1.1.0",
3+
"version": "1.2.0",
44
"description": "Plain object in memory database",
55
"main": "./dist/index.js",
66
"types": "./dist/index.d.ts",

src/base-ambary.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { IAmbaryRecord, IAmbaryOptions, IAmbarySetOptions } from './interfaces';
1+
import {
2+
IAmbaryRecord,
3+
IAmbaryOptions,
4+
IAmbarySetOptions,
5+
Constructor,
6+
} from './interfaces';
27

38
export class BaseAmbary {
49
private index = new Map<string, IAmbaryRecord>();
@@ -23,15 +28,17 @@ export class BaseAmbary {
2328
return typeof this.get(fullPath) !== 'undefined';
2429
}
2530

26-
protected set({ key, value, path }: IAmbarySetOptions) {
31+
protected set({ key, value, path, Constructor = Object }: IAmbarySetOptions) {
2732
let parent = this.model;
2833

2934
for (const pathKey of path) {
3035
const level = parent[pathKey];
3136

3237
if (!level) {
33-
parent[pathKey] = {};
34-
parent = parent[pathKey];
38+
const result = this.overrideKeyConstructor(pathKey, Constructor);
39+
40+
parent[result.key] = new result.Constructor();
41+
parent = parent[result.key];
3542
continue;
3643
}
3744

@@ -68,6 +75,7 @@ export class BaseAmbary {
6875
path,
6976
type,
7077
group,
78+
constructor: value?.constructor || null,
7179
fullPath: [...path, key].join('.'),
7280
};
7381
}
@@ -95,7 +103,7 @@ export class BaseAmbary {
95103
return result;
96104
}
97105

98-
protected createRecordIterator = function*(
106+
protected createRecordIterator = function* (
99107
model: Record<string, any> = this.model,
100108
path = [],
101109
) {
@@ -126,6 +134,13 @@ export class BaseAmbary {
126134
return ['Object', 'Array'].includes(type) ? 'model' : 'value';
127135
}
128136

137+
private overrideKeyConstructor(key: string, DefaultConstructor: Constructor) {
138+
const isArray = key.charAt(0) + key.charAt(key.length - 1) === '[]';
139+
return isArray
140+
? { Constructor: Array, key: key.substr(1, key.length - 2) }
141+
: { Constructor: DefaultConstructor, key };
142+
}
143+
129144
toJSON() {
130145
return this.model;
131146
}

src/interfaces.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1+
export type Constructor<
2+
T = object,
3+
A extends any[] = any[],
4+
Static = {}
5+
> = (new (...a: A) => T) & Static;
6+
17
export interface IAmbaryRecord {
28
key: string;
39
value: any;
410
path: string[];
511
type: string;
612
group: 'model' | 'value';
13+
constructor: Constructor;
714
fullPath: string;
815
}
916

@@ -31,4 +38,5 @@ export interface IAmbarySetOptions {
3138
key: string;
3239
value: any;
3340
path: string[];
41+
Constructor?: Constructor;
3442
}

src/specs/ambary.spec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ describe('Ambary', () => {
8585
path: ['level1_2', 'level2_1'],
8686
type: 'Number',
8787
group: 'value',
88+
constructor: Number,
8889
fullPath: 'level1_2.level2_1.level3_2',
8990
});
9091
});
@@ -98,6 +99,7 @@ describe('Ambary', () => {
9899
path: ['level1_2', 'level2_1'],
99100
type: 'String',
100101
group: 'value',
102+
constructor: String,
101103
fullPath: 'level1_2.level2_1.level3_1',
102104
});
103105
});
@@ -113,6 +115,7 @@ describe('Ambary', () => {
113115
path: ['level1_2', 'level2_1'],
114116
type: 'Number',
115117
group: 'value',
118+
constructor: Number,
116119
fullPath: 'level1_2.level2_1.level3_2',
117120
});
118121
});

src/specs/base-ambary.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,19 @@ describe('BaseAmbary', () => {
8484
expect(result).toEqual('test set');
8585
});
8686

87+
it('should set ambary item to non-existing parent(Array)', () => {
88+
ambary.set({
89+
key: 'level4_1',
90+
value: 'test set',
91+
path: ['[level1_3]', '0', 'level3_1'],
92+
});
93+
94+
const result = ambary.get('level1_3.0.level3_1.level4_1');
95+
96+
expect(result).toEqual('test set');
97+
expect(Array.isArray(ambary.get('level1_3'))).toBeTruthy();
98+
});
99+
87100
it('should return index iterator', () => {
88101
ambary.makeIndex();
89102
const iterator = ambary.getIndexIterator();

0 commit comments

Comments
 (0)