dict.set
This commit is contained in:
parent
c51030b3bd
commit
92ce43b508
|
|
@ -1,9 +1,16 @@
|
|||
import { type Value, toString, toValue } from 'reefvm'
|
||||
|
||||
export const dict = {
|
||||
keys: (dict: Record<string, any>) => Object.keys(dict),
|
||||
values: (dict: Record<string, any>) => Object.values(dict),
|
||||
entries: (dict: Record<string, any>) => Object.entries(dict).map(([k, v]) => ({ key: k, value: v })),
|
||||
'has?': (dict: Record<string, any>, key: string) => key in dict,
|
||||
get: (dict: Record<string, any>, key: string, defaultValue: any = null) => dict[key] ?? defaultValue,
|
||||
set: (dict: Value, key: Value, value: Value) => {
|
||||
const map = dict.value as Map<string, Value>
|
||||
map.set(toString(key), value)
|
||||
return dict
|
||||
},
|
||||
merge: (...dicts: Record<string, any>[]) => Object.assign({}, ...dicts),
|
||||
'empty?': (dict: Record<string, any>) => Object.keys(dict).length === 0,
|
||||
map: async (dict: Record<string, any>, cb: Function) => {
|
||||
|
|
@ -22,3 +29,7 @@ export const dict = {
|
|||
},
|
||||
'from-entries': (entries: [string, any][]) => Object.fromEntries(entries),
|
||||
}
|
||||
|
||||
// raw functions deal directly in Value types, meaning we can modify collection
|
||||
// careful - the MUST return a Value!
|
||||
; (dict.set as any).raw = true
|
||||
|
|
|
|||
|
|
@ -471,6 +471,12 @@ describe('dict operations', () => {
|
|||
await expect(`dict.get [a=1] 'b' 99`).toEvaluateTo(99, globals)
|
||||
})
|
||||
|
||||
test('dict.set sets value', async () => {
|
||||
await expect(`map = [a=1]; dict.set map 'b' 99; map.b`).toEvaluateTo(99, globals)
|
||||
await expect(`map = [a=1]; dict.set map 'a' 100; map.a`).toEvaluateTo(100, globals)
|
||||
})
|
||||
|
||||
|
||||
test('dict.empty? checks if dict is empty', async () => {
|
||||
await expect(`dict.empty? [=]`).toEvaluateTo(true, globals)
|
||||
await expect(`dict.empty? [a=1]`).toEvaluateTo(false, globals)
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user