Skip to content

Commit 0f5fac3

Browse files
committed
fixed mapping return
1 parent 73de864 commit 0f5fac3

3 files changed

Lines changed: 148 additions & 7 deletions

File tree

src/data/Map.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ export default class DataMap<K = any, V = any> extends Map<K, V> {
2424
* Finds the first entry that matches the callback
2525
*/
2626
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;
27+
for (const [ key, value ] of this.entries()) {
28+
if (callback(value, key, this)) {
29+
return [ key, value ] as [ K, V ];
3030
}
3131
}
3232
return undefined;
@@ -60,11 +60,18 @@ export default class DataMap<K = any, V = any> extends Map<K, V> {
6060
return map;
6161
}
6262

63+
/**
64+
* Returns the data set as a plain array
65+
*/
66+
public toArray() {
67+
return Array.from(this.values());
68+
}
69+
6370
/**
6471
* Returns the data map as a plain object
6572
*/
6673
public toObject() {
67-
return Object.fromEntries(Object.entries(this));
74+
return Object.fromEntries(this) as Record<string, V>;
6875
}
6976

7077
/**

src/data/Set.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export default class DataSet<V = any> extends Set<V> {
2727
const values = this.toArray();
2828
for (let i = 0; i < values.length; i++) {
2929
if (callback(values[i], i, this)) {
30-
return [ i, values[i] ] as [number, V];
30+
return [ i, values[i] ] as [ number, V ];
3131
}
3232
}
3333
return undefined;

tests/Map.test.ts

Lines changed: 136 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,142 @@ import { expect } from 'chai';
33
//NOTE: no extensions in tests because it's excluded in tsconfig.json and
44
//we are testing in a typescript environment via `ts-mocha -r tsx` (esm)
55
import type { CallableMap, CallableSet } from '../src/types';
6-
import { map } from '../src/data/Map';
7-
import { set } from '../src/data/Set';
6+
import DataMap, { map } from '../src/data/Map';
7+
import DataSet, { set } from '../src/data/Set';
8+
9+
describe('DataMap Tests', () => {
10+
it('Should get by name', async () => {
11+
const store = new DataMap<string, number>();
12+
store.set('one', 1);
13+
store.set('two', 2);
14+
store.set('three', 3);
15+
16+
expect(store.has('one')).to.be.true;
17+
expect(store.has('two')).to.be.true;
18+
expect(store.has('three')).to.be.true;
19+
expect(store.has('four')).to.be.false;
20+
21+
expect(store.get('one')).to.equal(1);
22+
expect(store.get('two')).to.equal(2);
23+
expect(store.get('three')).to.equal(3);
24+
expect(store.get('four')).to.be.undefined;
25+
});
26+
27+
it('Should convert to plain object', async () => {
28+
const store = new DataMap<string, number>();
29+
store.set('one', 1);
30+
store.set('two', 2);
31+
store.set('three', 3);
32+
33+
const object = store.toObject();
34+
expect(object).to.deep.equal({ one: 1, two: 2, three: 3 });
35+
});
36+
37+
it('Should convert to JSON string', async () => {
38+
const store = new DataMap<string, number>();
39+
store.set('one', 1);
40+
store.set('two', 2);
41+
store.set('three', 3);
42+
43+
const json = store.toString();
44+
expect(json).to.equal('{"one":1,"two":2,"three":3}');
45+
});
46+
47+
it('Should filter entries', async () => {
48+
const store = new DataMap<string, number>();
49+
store.set('one', 1);
50+
store.set('two', 2);
51+
store.set('three', 3);
52+
53+
const filtered = store.filter((value) => value >= 2);
54+
expect(filtered.toObject()).to.deep.equal({ two: 2, three: 3 });
55+
});
56+
57+
it('Should find entries', async () => {
58+
const store = new DataMap<string, number>();
59+
store.set('one', 1);
60+
store.set('two', 2);
61+
store.set('three', 3);
62+
63+
const foundEntry = store.find((value) => value === 2);
64+
expect(foundEntry).to.deep.equal([ 'two', 2 ]);
65+
66+
const foundKey = store.findKey((value) => value === 3);
67+
expect(foundKey).to.equal('three');
68+
69+
const foundValue = store.findValue((value) => value === 1);
70+
expect(foundValue).to.equal(1);
71+
});
72+
73+
it('Should map entries', async () => {
74+
const store = new DataMap<string, number>();
75+
store.set('one', 1);
76+
store.set('two', 2);
77+
store.set('three', 3);
78+
79+
const mapped = store.map((value) => value * 10);
80+
expect(mapped.toObject()).to.deep.equal({
81+
one: 10,
82+
two: 20,
83+
three: 30
84+
});
85+
});
86+
});
87+
88+
describe('DataSet Tests', () => {
89+
it('Should convert to array', async () => {
90+
const store = new DataSet<string>();
91+
store.add('foo');
92+
store.add('bar');
93+
store.add('baz');
94+
95+
const arr = store.toArray();
96+
expect(arr).to.deep.equal([ 'foo', 'bar', 'baz' ]);
97+
});
98+
99+
it('Should convert to JSON string', async () => {
100+
const store = new DataSet<string>();
101+
store.add('foo');
102+
store.add('bar');
103+
store.add('baz');
104+
105+
const json = store.toString();
106+
expect(json).to.equal('["foo","bar","baz"]');
107+
});
108+
109+
it('Should filter entries', async () => {
110+
const store = new DataSet<number>();
111+
store.add(1);
112+
store.add(2);
113+
store.add(3);
114+
115+
const filtered = store.filter((value) => value >= 2);
116+
expect(filtered.toArray()).to.deep.equal([ 2, 3 ]);
117+
});
118+
119+
it('Should find entries', async () => {
120+
const store = new DataSet<number>();
121+
store.add(1);
122+
store.add(2);
123+
store.add(3);
124+
125+
const foundIndex = store.findIndex((value) => value === 2);
126+
expect(foundIndex).to.equal(1);
127+
128+
const foundValue = store.findValue((value) => value === 3);
129+
expect(foundValue).to.equal(3);
130+
});
131+
132+
it('Should map entries', async () => {
133+
const store = new DataSet<number>();
134+
store.add(1);
135+
store.add(2);
136+
store.add(3);
137+
138+
const mapped = store.map((value) => value * 10);
139+
expect(mapped.toArray()).to.deep.equal([ 10, 20, 30 ]);
140+
});
141+
});
8142

9143
describe('map() Tests', () => {
10144
it('Should be callable', async () => {

0 commit comments

Comments
 (0)