{"version":3,"sources":["../src/misc/switchValue.ts"],"names":["switchValue","value","switchers","options","defaultValue","typeMatchers","result","matchKey","matchValue","t","v","matcher"],"mappings":";;;;AA2CO,SAASA,EAAmBC,CAAQC,CAAAA,CAAAA,CAA6BC,EAA4B,CAChG,IAAMC,EAAeD,CAAS,EAAA,YAAA,CACxBE,EAAkD,CACpD,MAAA,CAAU,SACV,MAAU,CAAA,QAAA,CACV,SAAU,UACV,CAAA,MAAA,CAAU,SACV,OAAU,CAAA,SACd,CACGF,CAAAA,CAAAA,EAAS,cAAc,MAAO,CAAA,MAAA,CAAOE,EAAaF,CAAQ,CAAA,YAAY,EAEzE,IAAIG,CAAAA,CAAaF,EACjB,IAAU,GAAA,CAACG,EAASC,CAAU,CAAA,GAAK,OAAO,OAAQN,CAAAA,CAAS,EACvD,GAAG,KAAA,CAAM,OAAQD,CAAAA,CAAK,GAClB,GAAGM,CAAAA,CAAS,SAAS,OAAO,CAAA,CAAE,CAC1B,IAAME,CAAAA,CAAIJ,EAAaE,CAAS,CAAA,KAAA,CAAM,EAAE,EAAE,CAAC,EAC3C,GAAG,KAAA,CAAM,QAAQN,CAAK,CAAA,EAAMA,CAAM,CAAA,KAAA,CAAOS,GAAQ,OAAOA,CAAAA,EAAID,CAAC,CAAE,CAAA,CAC3DH,EAASE,CACT,CAAA,KACJ,CACJ,CAEGD,CAAAA,KAAAA,GAAAA,CAAAA,EAAYN,GAAS,OAAOM,CAAAA,EAAW,OAAON,CAAO,CAAA,CACpDK,EAASE,CACT,CAAA,KACJ,CAASD,KAAAA,GAAAA,CAAAA,EAAYN,GAAS,OAAOA,CAAAA,EAAQ,SAAS,CAClDK,CAAAA,CAASE,EACT,KACJ,CAAA,KAAA,GAASD,KAAYF,CAAa,CAAA,CAC9B,IAAMM,CAAUN,CAAAA,CAAAA,CAAaE,CAAQ,CACrC,CAAA,GAAG,OAAOI,CAAU,EAAA,UAAA,EAAcA,CAAQV,CAAAA,CAAK,EAAE,CAC7CK,CAAAA,CAASE,EACT,KACJ,CAAA,KAAA,GAASG,GAAW,OAAOV,CAAAA,CAAO,CAC9BK,CAASE,CAAAA,CAAAA,CACT,KACJ,CACJ,CAAA,KAAA,GAASD,GAAY,OACjB,CAAA,CAAA,GAAG,MAAM,OAAQN,CAAAA,CAAK,CAAE,CAAA,CACpBK,EAASE,CACT,CAAA,KACJ,UACKD,CAAS,CAAA,QAAA,CAAS,OAAO,CAAE,CAAA,CAChC,IAAME,CAAIJ,CAAAA,CAAAA,CAAaE,EAAS,KAAM,CAAA,CAAA,CAAE,EAAE,CAAC,CAAA,CAC3C,GAAG,KAAM,CAAA,OAAA,CAAQN,CAAK,CAAMA,EAAAA,CAAAA,CAAM,MAAOS,CAAQ,EAAA,OAAOA,GAAID,CAAC,CAAA,CAAE,CAC3DH,CAASE,CAAAA,CAAAA,CACT,KACJ,CACJ,CAIR,OAAO,OAAOF,CAAAA,EAAS,WAAaA,CAAOL,CAAAA,CAAK,EAAIK,CACxD","file":"chunk-H7IUQ3N2.mjs","sourcesContent":["/**\n \n * 对输入的值进行匹配，如果相同则返回对应的值\n * \n * switcher = {\n 1:\"a\",\n *    String:\"I am string\",\n *    Number:\"I am number\",\n *    Function:()=>{ return \"I am function\" },\n *    Object:\"I am object\",\n *    Array:\"I am array\",\n *    Boolean:\"I am boolean\",\n *    StringArray:\"I am string array\",\n *    NumberArray:\"I am number array\",\n *    BooleanArray:\"I am boolean array\", * \n * }\n * \n * switchValue(1,switcher)  // == \"a\"\n * \n * switchValue(\"1\",switcher)  // == \"I am string\",\n *  \n * switchValue(Symbol(),switcher,{defaultValue:1})  // == 1\n * \n * 也可以指定类型匹配器\n * \n * switchValue(new User(),{\n *      User: (value)=> user.name\n * },{\n *    typeMatchers:{\n *       User: (value)=> value instanceof User \n *    }\n * )\n * \n\n */\n\n\nexport interface SwitchValueOptions{\n    typeMatchers?: Record<string,string | Function>\n    defaultValue?:any\n}\n\n\nexport function switchValue<T=any>(value:T,switchers:Record<string,any>,options?:SwitchValueOptions){\n    const defaultValue = options?.defaultValue\n    const typeMatchers:SwitchValueOptions['typeMatchers'] = {\n        Number  : 'number',\n        String  : 'string',\n        Function: 'function',\n        Object  : 'object',\n        Boolean : 'boolean'            \n    }\n    if(options?.typeMatchers) Object.assign(typeMatchers,options.typeMatchers)\n\n    let result:any = defaultValue\n    for(const [matchKey,matchValue] of Object.entries(switchers)){                    \n        if(Array.isArray(value)){\n            if(matchKey.endsWith(\"Array\")){\n                const t = typeMatchers[matchKey.slice(0,-5)]\n                if(Array.isArray(value) &&  value.every((v:any)=>typeof(v)==t)){                \n                    result = matchValue\n                    break\n                }\n            }\n        }else{\n            if(matchKey == value && typeof(matchKey)==typeof(value)){\n                result = matchValue\n                break\n            }else if(matchKey == value && typeof(value)=='number'){\n                result = matchValue\n                break\n            }else if(matchKey in typeMatchers){   \n                const matcher = typeMatchers[matchKey]         \n                if(typeof(matcher)=='function' && matcher(value)){\n                    result = matchValue\n                    break\n                }else if(matcher == typeof(value)){\n                    result = matchValue\n                    break\n                }\n            }else if(matchKey == 'Array'){\n                if(Array.isArray(value)){\n                    result = matchValue\n                    break\n                }\n            }else if(matchKey.endsWith(\"Array\")){\n                const t = typeMatchers[matchKey.slice(0,-5)]\n                if(Array.isArray(value) &&  value.every((v:any)=>typeof(v)==t)){                \n                    result = matchValue\n                    break\n                }\n            }\n        }        \n    }\n\n    return typeof(result)=='function' ? result(value) : result        \n}\n\n// class User{\n//     name=\"a\"\n// }\n\n// const switcher = {\n//     1:\"I am 1\",\n//     current:\"current\",\n//     parent:\"parent\",\n//     String:\"I am string\",\n//     Number:\"I am number\",\n//     Function:()=>{ return \"I am function\" },\n//     Object:\"I am object\",\n//     Array:\"I am array\",\n//     Boolean:\"I am boolean\",\n//     StringArray:\"I am string array\",\n//     NumberArray:\"I am number array\",\n//     BooleanArray:\"I am boolean array\"     \n// }\n    \n//     console.log(\"1 = \",switchValue(1,switcher))  // == \"a\"\n//     console.log(\"current = \",switchValue(\"current\",switcher))  // == \"a\"\n//     console.log(\"parent = \",switchValue(\"parent\",switcher))  // == \"a\"\n//     console.log(\"xxxxx = \",switchValue(\"xxxxx\",switcher))\n//     console.log(\"100 = \",switchValue(100,switcher))\n//     console.log(\"true = \",switchValue(true,switcher))\n//     console.log(\"{} = \",switchValue({},switcher))\n//     console.log(\"[] = \",switchValue([],switcher))\n//     console.log(\"['String'] = \",switchValue([\"String\"],switcher))\n//     console.log(\"[100] = \",switchValue([100],switcher))\n//     console.log(\"[true] = \", switchValue([true],switcher))\n\n//     console.log(\"()=>{} = \", switchValue(()=>{},switcher))\n    \n//     console.log(\"Symbol() = \",switchValue(Symbol(),switcher,{defaultValue:1}))  // == 1\n//     console.log(\"new User() = \",switchValue(new User(),{\n//             User: \"I am user\"\n//         },{\n//             typeMatchers:{\n//                 User: (value:any)=> value instanceof User \n//             }\n//         }\n//     ))  // == \"I am user\""]}