@@ -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)
55import 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
9143describe ( 'map() Tests' , ( ) => {
10144 it ( 'Should be callable' , async ( ) => {
0 commit comments