Urara-Blog/node_modules/.pnpm-store/v3/files/e2/ac2bb1e4866d1ad412f14e71e704e9d2ad9e97b3bb256b693f6a25010c0a5f7945ebf87668ff2770a1e96e8858411c2b1d84d99ccde0b5893334ce761b0265
2022-08-14 01:14:53 +08:00

61 lines
1.5 KiB
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

var own = {}.hasOwnProperty
/**
* @callback Handler
* @param {...unknown} value
* @return {unknown}
*
* @typedef {Record<string, Handler>} Handlers
*
* @typedef {Object} Options
* @property {Handler} [unknown]
* @property {Handler} [invalid]
* @property {Handlers} [handlers]
*/
/**
* Handle values based on a property.
*
* @param {string} key
* @param {Options} [options]
*/
export function zwitch(key, options) {
var settings = options || {}
/**
* Handle one value.
* Based on the bound `key`, a respective handler will be called.
* If `value` is not an object, or doesnt have a `key` property, the special
* “invalid” handler will be called.
* If `value` has an unknown `key`, the special “unknown” handler will be
* called.
*
* All arguments, and the context object, are passed through to the handler,
* and its result is returned.
*
* @param {...unknown} [value]
* @this {unknown}
* @returns {unknown}
* @property {Handler} invalid
* @property {Handler} unknown
* @property {Handlers} handlers
*/
function one(value) {
var fn = one.invalid
var handlers = one.handlers
if (value && own.call(value, key)) {
fn = own.call(handlers, value[key]) ? handlers[value[key]] : one.unknown
}
if (fn) {
return fn.apply(this, arguments)
}
}
one.handlers = settings.handlers || {}
one.invalid = settings.invalid
one.unknown = settings.unknown
return one
}