Skip to content
/ mosdef Public

Functional conveniences for property definitions.

Notifications You must be signed in to change notification settings

mikeal/mosdef

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

mosdef -- Functional conveniences for property definitions.

Add property getters.

let o = {}
mosdef(o, 'key', () => 'my value')

Set a single getter for many keys.

let p = {} // private object
let o = {}
mosdef(o, ['key1', 'key2'], key => p[key])

Can do setters as well.

let p = {} // private object
let o = {}
mosdef(o, ['key1', 'key2'], {
  get: key => p[key],
  set: (value, key) => { p[key] = value }
})

Can use a table for mapping instead. Can also run through multiple objects.

let p = {} // private object
let o = {}
let o2 = {}

let getPrivate = key => p[key]
let setPrivate = (value, key) => { p[key] = value }
let trySelf = (key, obj) => obj['_' + key] || p[key]

mosdef([o, o2], {
  key1: {get: getPrivate, set: setPrivate},
  key2: getPrivate,
  key3: trySelf
})

About

Functional conveniences for property definitions.

Resources

Stars

Watchers

Forks

Packages

No packages published