Skip to content

Commit fd0acfc

Browse files
committed
created a datamap and a dataset class that adds more basic methods
1 parent eb69a7e commit fd0acfc

14 files changed

Lines changed: 475 additions & 104 deletions

File tree

package.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@
4848
"require": "./cjs/data/cookie.js",
4949
"import": "./esm/data/cookie.js"
5050
},
51-
"./map": {
52-
"require": "./cjs/data/map.js",
53-
"import": "./esm/data/map.js"
51+
"./Map": {
52+
"require": "./cjs/data/Map.js",
53+
"import": "./esm/data/Map.js"
5454
},
55-
"./set": {
56-
"require": "./cjs/data/set.js",
57-
"import": "./esm/data/set.js"
55+
"./Set": {
56+
"require": "./cjs/data/Set.js",
57+
"import": "./esm/data/Set.js"
5858
},
5959
"./Nest": {
6060
"require": "./cjs/data/Nest.js",
@@ -131,8 +131,8 @@
131131
"Reflection": [ "./cjs/Reflection.d.ts" ],
132132
"Status": [ "./cjs/Status.d.ts" ],
133133
"cookie": [ "./cjs/data/cookie.d.ts" ],
134-
"map": [ "./cjs/data/map.d.ts" ],
135-
"set": [ "./cjs/data/set.d.ts" ],
134+
"Map": [ "./cjs/data/Map.d.ts" ],
135+
"Set": [ "./cjs/data/Set.d.ts" ],
136136
"Nest": [ "./cjs/data/Nest.d.ts" ],
137137
"ReadonlyMap": [ "./cjs/data/ReadonlyMap.d.ts" ],
138138
"ReadonlyNest": [ "./cjs/data/ReadonlyNest.d.ts" ],

src/data/Map.ts

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import type {
2+
CallableMap,
3+
DataMapFilter,
4+
DataMapIterator
5+
} from '../types.js';
6+
7+
/**
8+
* Adding extra utility methods to the native Map class
9+
* (that should have been there in the first place...)
10+
*/
11+
export default class DataMap<K = any, V = any> extends Map<K, V> {
12+
/**
13+
* Filters the data map (returns a new DataMap instance)
14+
*/
15+
public filter(callback: DataMapFilter<K, V, this>) {
16+
const entries = Array.from(this.entries()).filter(
17+
entry => callback(entry[1], entry[0], this)
18+
);
19+
const constructor = this.constructor as new(map: [K, V][]) => this;
20+
return new constructor(entries);
21+
}
22+
23+
/**
24+
* Finds the first entry that matches the callback
25+
*/
26+
public find(callback: DataMapFilter<K, V, this>) {
27+
for (const entry of this.entries()) {
28+
if (callback(entry[1], entry[0], this)) {
29+
return entry;
30+
}
31+
}
32+
return undefined;
33+
}
34+
35+
/**
36+
* Finds the first key that matches the callback
37+
*/
38+
public findKey(callback: DataMapFilter<K, V, this>) {
39+
return this.find(callback)?.[0];
40+
}
41+
42+
/**
43+
* Finds the first value that matches the callback
44+
*/
45+
public findValue(callback: DataMapFilter<K, V, this>) {
46+
return this.find(callback)?.[1];
47+
}
48+
49+
/**
50+
* Maps the data map values to a new data map
51+
*/
52+
public map<T>(callback: DataMapIterator<K, V, this, T>) {
53+
const constructor = this.constructor as new() => DataMap<K, T>;
54+
const map = new constructor();
55+
for (const entry of this.entries()) {
56+
map.set(entry[0], callback(entry[1], entry[0], this));
57+
}
58+
return map;
59+
}
60+
};
61+
62+
export function map<K = any, V = any> (data?: [K, V][]): CallableMap<K, V> {
63+
const store = new DataMap<K, V>(data);
64+
const callable = Object.assign(
65+
(name: K) => store.get(name),
66+
{
67+
clear() {
68+
return store.clear();
69+
},
70+
delete(name: K) {
71+
return store.delete(name);
72+
},
73+
entries() {
74+
return store.entries();
75+
},
76+
filter(callback: DataMapFilter<K, V, typeof store>) {
77+
return store.filter(callback);
78+
},
79+
find(callback: DataMapFilter<K, V, typeof store>) {
80+
return store.find(callback);
81+
},
82+
findKey(callback: DataMapFilter<K, V, typeof store>) {
83+
return store.findKey(callback);
84+
},
85+
findValue(callback: DataMapFilter<K, V, typeof store>) {
86+
return store.findValue(callback);
87+
},
88+
forEach(callback: DataMapIterator<K, V, Map<K, V>, void>) {
89+
return store.forEach(callback);
90+
},
91+
get(name: K) {
92+
return store.get(name);
93+
},
94+
has(name: K) {
95+
return store.has(name);
96+
},
97+
keys() {
98+
return store.keys();
99+
},
100+
map<T>(callback: DataMapIterator<K, V, typeof store, T>) {
101+
return store.map<T>(callback);
102+
},
103+
set(name: K, value: V) {
104+
return store.set(name, value);
105+
},
106+
values() {
107+
return store.values();
108+
}
109+
} as DataMap<K, V>
110+
);
111+
//magic size property
112+
Object.defineProperty(callable, 'size', { get: () => store.size });
113+
return callable;
114+
};

src/data/Nest.ts

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -286,21 +286,43 @@ export function nest<M extends UnknownNest = UnknownNest>(data?: M): CallableNes
286286
const callable = Object.assign(
287287
<T = any>(...path: Key[]) => store.get<T>(...path),
288288
{
289-
clear: () => store.clear(),
290-
delete: (...path: Key[]) => store.delete(...path),
291-
entries: () => store.entries(),
292-
forEach: (...path: Key[]) => store.forEach(...path),
293-
get: <T = any>(...path: Key[]) => store.get<T>(...path),
294-
has: (...path: Key[]) => store.has(...path),
295-
keys: () => store.keys(),
296-
path: <T = any>(path: string, defaults?: TypeOf<T>) => store.path<T>(path, defaults),
297-
set: (...path: any[]) => store.set(...path),
298-
toString: () => store.toString(),
299-
values: () => store.values(),
300289
withArgs: store.withArgs,
301290
withFormData: store.withFormData,
302291
withPath: store.withPath,
303-
withQuery: store.withQuery
292+
withQuery: store.withQuery,
293+
clear() {
294+
return store.clear();
295+
},
296+
delete(...path: Key[]) {
297+
return store.delete(...path);
298+
},
299+
entries() {
300+
return store.entries();
301+
},
302+
forEach(...path: Key[]) {
303+
return store.forEach(...path);
304+
},
305+
get<T = any>(...path: Key[]) {
306+
return store.get<T>(...path);
307+
},
308+
has(...path: Key[]) {
309+
return store.has(...path);
310+
},
311+
keys() {
312+
return store.keys();
313+
},
314+
path<T = any>(path: string, defaults?: TypeOf<T>) {
315+
return store.path<T>(path, defaults);
316+
},
317+
set(...path: any[]) {
318+
return store.set(...path);
319+
},
320+
toString() {
321+
return store.toString();
322+
},
323+
values() {
324+
return store.values();
325+
}
304326
} as Nest<M>
305327
);
306328
//magic size/data property

src/data/ReadonlyMap.ts

Lines changed: 67 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
1-
export default class ReadonlyMap<S = any, T = any> {
1+
import DataMap from './Map.js';
2+
3+
export default class ReadonlyMap<K = any, V = any> {
24
//map of map
3-
protected _map: Map<S, T>;
5+
protected _map: DataMap<K, V>;
6+
7+
/**
8+
* Returns the size of the map
9+
*/
10+
public get size() {
11+
return this._map.size;
12+
}
413

514
/**
615
* Sets the initial values of the map
716
*/
8-
constructor(init?: [S, T][]) {
9-
this._map = new Map(init);
17+
constructor(init?: [K, V][]) {
18+
this._map = new DataMap<K, V>(init);
1019
}
1120

1221
/**
@@ -16,11 +25,55 @@ export default class ReadonlyMap<S = any, T = any> {
1625
return this._map.entries();
1726
}
1827

28+
/**
29+
* Filters the data map (returns a new DataMap instance)
30+
*/
31+
public filter(callback: (
32+
value: V,
33+
key?: K,
34+
map?: DataMap<K, V>
35+
) => boolean) {
36+
return this._map.filter(callback);
37+
}
38+
39+
/**
40+
* Finds the first entry that matches the callback
41+
*/
42+
public find(callback: (
43+
value: V,
44+
key?: K,
45+
map?: DataMap<K, V>
46+
) => boolean) {
47+
return this._map.find(callback);
48+
}
49+
50+
/**
51+
* Finds the first key that matches the callback
52+
*/
53+
public findKey(callback: (
54+
value: V,
55+
key?: K,
56+
map?: DataMap<K, V>
57+
) => boolean) {
58+
return this._map.findKey(callback);
59+
}
60+
61+
/**
62+
* Finds the first value that matches the callback
63+
*/
64+
public findValue(callback: (
65+
value: V,
66+
key?: K,
67+
map?: DataMap<K, V>
68+
) => boolean) {
69+
return this._map.findValue(callback);
70+
}
71+
1972
/**
2073
* Iterates over the map
2174
*/
2275
public forEach(
23-
iterator: (value: T, key: S, set: Map<S, T>) => void,
76+
iterator: (value: V, key: K, set: Map<K, V>) => void,
2477
arg?: unknown
2578
) {
2679
return this._map.forEach(iterator, arg);
@@ -29,14 +82,14 @@ export default class ReadonlyMap<S = any, T = any> {
2982
/**
3083
* Returns the value of the map
3184
*/
32-
public get(key: S) {
85+
public get(key: K) {
3386
return this._map.get(key);
3487
}
3588

3689
/**
3790
* Returns whether the map has the value
3891
*/
39-
public has(key: S) {
92+
public has(key: K) {
4093
return this._map.has(key);
4194
}
4295

@@ -48,10 +101,14 @@ export default class ReadonlyMap<S = any, T = any> {
48101
}
49102

50103
/**
51-
* Returns the size of the map
104+
* Maps the data map values to a new data map
52105
*/
53-
public get size() {
54-
return this._map.size;
106+
public map<T>(callback: (
107+
value: V,
108+
key?: K,
109+
map?: DataMap<K, V>) => T
110+
) {
111+
return this._map.map(callback);
55112
}
56113

57114
/**

0 commit comments

Comments
 (0)