DictUnit
DictUnit is a type of Unit that only accepts plain, non-null, non-Array, key-value based object data type as its value. It ensures that at any point of time the value would always be a dictionary object. It's loosely based on Map. DictUnit has some similar methods as the Map, but that's where the similarities end. For one thing, the keys in DictUnit value can only be of type string since the underlying data structure is still a simple object not a Map.
It borrows some Map.prototype methods like set , has, get etc. However, these methods aren't exactly the same, there are some differences as we explained above.
It also implements non-deprecated Object.prototype methods like hasOwnProperty , and redirects them to the stored object value inside the Unit, so when you call DictUnit.hasOwnProperty('prop') , it'll be executed on the Unit's value instead of the Unit instance itself. But there's probably no need to call it, you can just use DictUnit.has('prop') instead.
It also borrows some static methods from Object like assign and entries, and implements them as instance members assign and objectEntries, respectively. These methods operate on the stored object value instead of the Unit instance itself.
See API reference for more details.
​
​
Default value
{}
Value data type
plain dictionary object

DictUnit vs object literal vs Map:

DictUnit can not be used as a drop-in replacement for a dictionary object or Map. You can not directly assign or access properties like unit[k] = v instead, you have to use unit.set('k', v) and unit.get('k'), respectively. Unlike a Map, the keys can only be of type string as the underlying data structure is still a simple object. See the below comparisons for more clarity.
1
// initialization
2
const obj = {a: 1}; // an object literal
3
const map = new Map([['a', 1]]); // a map with a key:value pair 'a':1
4
const unit = new DictUnit({initialValue: {a: 1}}); // value is {a: 1}
5
​
6
obj === map // false
7
obj === unit // false
8
map === unit // false
9
​
10
typeof obj === 'object' // true
11
typeof map === 'object' // true
12
typeof unit === 'object' // true
13
unit instanceof Map // false
14
​
15
// adding a property
16
obj.b = 2;
17
map.set('b') = 2;
18
unit.set('b') = 2; // this is reactive, creates and dispatches a new object
19
​
20
map.set(obj, unit) // works
21
map.get(obj) // returns unit
22
unit.set(obj, map) // won't work, the key has to be a number or string
23
// additionally, non serialzable values are not recommended,
24
// when you configure the Unit to be immutable,
25
// the Unit will create a clone of the value but the Map will not be cloned.
26
// And when you configure the Unit to be persistent,
27
// the Unit will save it to localStorage using JSON.stringify but Map doesn't
28
// work with JSON.stringify, it simply becomes "{}".
29
​
30
map.clear() // clears all the key-value pairs
31
unit.clearValue() // clears the value and emits the default value {},
32
// this is similar to map.clear()
33
​
34
unit.clear() // clears the value and emits the default value ie: empty {},
35
// it also clears the cache, and unfreezes the unit
Copied!
Last modified 11mo ago