Skip to content

Instantly share code, notes, and snippets.

@zkat
Last active February 7, 2017 08:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zkat/ea7cfb20b918b5b2310f180471338664 to your computer and use it in GitHub Desktop.
Save zkat/ea7cfb20b918b5b2310f180471338664 to your computer and use it in GitHub Desktop.
Sketch of Figgy Pudding Options Processor
// Requirements:
// Unidirectional (data flows down)
// Composable (detailed definitions can be specified at a lower level)
// Declarative types and defaults
// Minimal consing
// No bubbling of side effects to parents or siblings
// Efficient
// Warn about unspecified options being read or assigned? (or make it available to run in tests?)
// opts objects readable like plain objects (maybe?)
// Example:
const CacacheOpts = figgyPudding({
cache: {
type: String
},
log: {
default: require('npmlog')
}
})
const RegistryClientOpts = figgyPudding({
retry: {
// TODO - how should *nested* (vs derived) opts work?
type: RetryOpts
},
log: {
default: require('npmlog')
}
}).deriving(RequestOpts)
const PacoteOpts = figgyPudding({
log: {
default: require('npmlog')
},
where: {
type: Path,
default: './'
}
})
// Deriving:
// * allows reading of same-name parent opts
// * allows passed-in opts to be parsed even though parent doesn't handle them
// * parent cannot read/write child-defined opts it does not share
// * will throw validation errors from children on *initial* instantiation (not on child instantiation)
.deriving(CacacheOpts)
.deriving(RegistryClientOpts)
let myOpts = {
cache: './cache',
log: require('./silent-log'),
auth: auth
}
myFn(1, 2, myOpts)
function myFn (arg1, arg2, _opts) {
let opts = PacoteOpts(_opts)
let cacOpts = CacacheOpts(opts) // defaults only processed at this level, and only affect this one
let regOpts = RegistryClientOpts(opts) // defaults for same name can be different
cacOpts.cache // => './cache'
cacOpts.log.silly('lol', 'yay') // logging!
cacOpts.where // => undefined (testable warning?)
cacOpts.where = './eh' // './eh' (assign anything? warn?)
opts.where // => './' (unaltered by child)
opts.cache // => undefined (test warn?)
}
PacoteOpts.allOpts() // => { log: { type: ... }, where: ..., retry: ..., cache: ..., auth: ... }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment