diff --git a/README.md b/README.md index a27c773..a041a86 100644 --- a/README.md +++ b/README.md @@ -496,6 +496,10 @@ swich function accepts two parameters: array of *Pattern*, *Result* tuples and * ## Changelog +### 1.2.0 +- Improved types +- Added types exports + ### 1.1.0 - Added fallThrough functionality diff --git a/build/index.js b/build/index.js index ce6af5a..0ed3e8e 100644 --- a/build/index.js +++ b/build/index.js @@ -1,6 +1,6 @@ /*! * - * swich v1.1.2 + * swich v1.2.0 * https://github.com/Lukasz-pluszczewski/swich.git * * Copyright (c) Ɓukasz Pluszczewski (https://github.com/Lukasz-pluszczewski) and project contributors. @@ -9,5 +9,5 @@ * LICENSE file in the root directory of this source tree. * */ -!function(t,r){"object"==typeof exports&&"object"==typeof module?module.exports=r():"function"==typeof define&&define.amd?define([],r):"object"==typeof exports?exports.Swich=r():t.Swich=r()}(this,(function(){return(()=>{"use strict";var t={d:(r,n)=>{for(var e in n)t.o(n,e)&&!t.o(r,e)&&Object.defineProperty(r,e,{enumerable:!0,get:n[e]})},o:(t,r)=>Object.prototype.hasOwnProperty.call(t,r),r:t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})}},r={};function n(t,r){return function(t){if(Array.isArray(t))return t}(t)||function(t,r){var n=t&&("undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"]);if(null==n)return;var e,o,u=[],i=!0,c=!1;try{for(n=n.call(t);!(i=(e=n.next()).done)&&(u.push(e.value),!r||u.length!==r);i=!0);}catch(t){c=!0,o=t}finally{try{i||null==n.return||n.return()}finally{if(c)throw o}}return u}(t,r)||function(t,r){if(!t)return;if("string"==typeof t)return e(t,r);var n=Object.prototype.toString.call(t).slice(8,-1);"Object"===n&&t.constructor&&(n=t.constructor.name);if("Map"===n||"Set"===n)return Array.from(t);if("Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))return e(t,r)}(t,r)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function e(t,r){(null==r||r>t.length)&&(r=t.length);for(var n=0,e=new Array(r);no,defaultResultGetter:()=>u,createSwich:()=>i,gt:()=>c,gte:()=>a,lt:()=>f,lte:()=>l,default:()=>d});var o=function(t){return function(r,n){if(n instanceof RegExp)return"string"==typeof r&&n.test(r);if("function"==typeof n){if(!t.catchFunctionErrors)return t.acceptTruthyFunctionReturn?!!n(r):!0===n(r);try{return t.acceptTruthyFunctionReturn?!!n(r):!0===n(r)}catch(t){return!1}}return t.strict?n===r:n==r}},u=function(t){return function(r,n,e){var o=t.runResultFunction&&"function"==typeof e?e(r):e;return n instanceof RegExp?function(r,n,e){return"string"==typeof r&&"string"==typeof e&&t.performReplaceOnRegex?r.replace(n,e):e}(r,n,o):o}},i=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},r=t.returnMany,e=void 0!==r&&r,i=t.strict,c=void 0===i||i,a=t.acceptTruthyFunctionReturn,f=void 0===a||a,l=t.catchFunctionErrors,d=void 0===l||l,p=t.performReplaceOnRegex,s=void 0!==p&&p,v=t.runResultFunction,y=void 0===v||v,h=t.stopFallThrough,g=void 0!==h&&h,m=t.matcher,b=void 0===m?o:m,R=t.resultGetter,F=void 0===R?u:R;return function(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},o=r.returnMany,u=void 0===o?e:o,i=r.strict,a=void 0===i?c:i,l=r.acceptTruthyFunctionReturn,p=void 0===l?f:l,v=r.catchFunctionErrors,h=void 0===v?d:v,m=r.performReplaceOnRegex,R=void 0===m?s:m,S=r.runResultFunction,x=void 0===S?y:S,O=r.stopFallThrough,T=void 0===O?g:O,j=r.matcher,w=void 0===j?b:j,E=r.resultGetter,M=void 0===E?F:E;return function(){var r=!(arguments.length>0&&void 0!==arguments[0])||arguments[0],e={returnMany:u,strict:a,acceptTruthyFunctionReturn:p,catchFunctionErrors:h,performReplaceOnRegex:R,runResultFunction:x,stopFallThrough:T},o=!1,i=u?[]:null,c=function(t){var r=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];o=!!r||o,u?i.push(t):i=t},f=w(e),l=M(e),d=!1;return t.forEach((function(t,e){var i=n(t,3),a=i[0],p=i[1],s=i[2];if(!o||u||d)return 1!==t.length||o&&!d?1!==t.length&&(d&&!T||f(r,a))?(d=!!s,c(l(r,a,p))):void(d=!T&&!!s):c(l(r,void 0,a),!1)})),i}}},c=function(t){return function(r){return r>t}},a=function(t){return function(r){return r>=t}},f=function(t){return function(r){return r1&&void 0!==arguments[1]?arguments[1]:{};return i()(t,r)};return r})()})); +!function(t,r){"object"==typeof exports&&"object"==typeof module?module.exports=r():"function"==typeof define&&define.amd?define([],r):"object"==typeof exports?exports.Swich=r():t.Swich=r()}(this,(function(){return(()=>{"use strict";var t={d:(r,e)=>{for(var n in e)t.o(e,n)&&!t.o(r,n)&&Object.defineProperty(r,n,{enumerable:!0,get:e[n]})},o:(t,r)=>Object.prototype.hasOwnProperty.call(t,r),r:t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})}},r={};function e(t,r){var e=Object.keys(t);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(t);r&&(n=n.filter((function(r){return Object.getOwnPropertyDescriptor(t,r).enumerable}))),e.push.apply(e,n)}return e}function n(t){for(var r=1;rt.length)&&(r=t.length);for(var e=0,n=new Array(r);ec,defaultResultGetter:()=>f,gt:()=>l,gte:()=>p,lt:()=>y,lte:()=>s,createSwich:()=>d,default:()=>h});var c=function(t){return function(r,e){if(e instanceof RegExp)return"string"==typeof r&&e.test(r);if("function"==typeof e){if(!t.catchFunctionErrors)return t.acceptTruthyFunctionReturn?!!e(r):!0===e(r);try{return t.acceptTruthyFunctionReturn?!!e(r):!0===e(r)}catch(t){return!1}}return t.strict?e===r:e==r}},f=function(t){return function(r,e,n){var o=t.runResultFunction&&"function"==typeof n?n(r):n;return e instanceof RegExp?function(r,e,n){return"string"==typeof r&&"string"==typeof n&&t.performReplaceOnRegex?r.replace(e,n):n}(r,e,o):o}};function a(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},e=r.returnMany,n=void 0!==e&&e,o=r.strict,i=void 0===o||o,a=r.acceptTruthyFunctionReturn,l=void 0===a||a,p=r.catchFunctionErrors,y=void 0===p||p,s=r.performReplaceOnRegex,d=void 0!==s&&s,h=r.runResultFunction,v=void 0===h||h,b=r.stopFallThrough,g=void 0!==b&&b,O=r.matcher,m=void 0===O?c:O,j=r.resultGetter,R=void 0===j?f:j;return function(){var r=!(arguments.length>0&&void 0!==arguments[0])||arguments[0],e={returnMany:n,strict:i,acceptTruthyFunctionReturn:l,catchFunctionErrors:y,performReplaceOnRegex:d,runResultFunction:v,stopFallThrough:g},o=!1,c=n?[]:null,f=function(t){var r=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];o=!!r||o,n?c.push(t):c=t},a=m(e),p=R(e),s=!1;return t.forEach((function(t,e){var i=u(t,3),c=i[0],l=i[1],y=i[2];if(!o||n||s)return 1!==t.length||o&&!s?1!==t.length&&(s&&!g||a(r,c))?(s=!!y,f(p(r,c,l))):void(s=!g&&!!y):f(p(r,void 0,c),!1)})),c}}var l=function(t){return function(r){return r>t}},p=function(t){return function(r){return r>=t}},y=function(t){return function(r){return r1&&void 0!==arguments[1]?arguments[1]:{};return a(r,n(n({},t),e))}}const h=a;return r})()})); //# sourceMappingURL=index.js.map \ No newline at end of file diff --git a/build/index.js.map b/build/index.js.map index 9fa60db..6144bbb 100644 --- a/build/index.js.map +++ b/build/index.js.map @@ -1 +1 @@ -{"version":3,"sources":["webpack://Swich/webpack/universalModuleDefinition","webpack://Swich/webpack/bootstrap","webpack://Swich/webpack/runtime/define property getters","webpack://Swich/webpack/runtime/hasOwnProperty shorthand","webpack://Swich/webpack/runtime/make namespace object","webpack://Swich/./src/lib/index.ts"],"names":["root","factory","exports","module","define","amd","this","__webpack_require__","definition","key","o","Object","defineProperty","enumerable","get","obj","prop","prototype","hasOwnProperty","call","Symbol","toStringTag","value","defaultMatcher","config","valueToMatch","pattern","RegExp","test","catchFunctionErrors","acceptTruthyFunctionReturn","error","strict","defaultResultGetter","result","resultToBeSet","runResultFunction","replacement","performReplaceOnRegex","replace","getRegexResult","createSwich","returnMany","defaultReturnMany","defaultStrict","defaultAcceptTruthyFunctionReturn","defaultCatchFunctionErrors","defaultPerformReplaceOnRegex","defaultRunResultFunction","stopFallThrough","defaultStopFallThrough","matcher","defaultMatcherValue","resultGetter","defaultResultGetterValue","patterns","found","setResult","setFound","push","verifyValue","getResult","fallingThrough","forEach","matchArr","index","fallthrough","length","undefined","gt","compareValue","gte","lt","lte"],"mappings":"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","file":"index.js","sourcesContent":["(function webpackUniversalModuleDefinition(root, factory) {\n\tif(typeof exports === 'object' && typeof module === 'object')\n\t\tmodule.exports = factory();\n\telse if(typeof define === 'function' && define.amd)\n\t\tdefine([], factory);\n\telse if(typeof exports === 'object')\n\t\texports[\"Swich\"] = factory();\n\telse\n\t\troot[\"Swich\"] = factory();\n})(this, function() {\nreturn ","// The require scope\nvar __webpack_require__ = {};\n\n","// define getter functions for harmony exports\n__webpack_require__.d = (exports, definition) => {\n\tfor(var key in definition) {\n\t\tif(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {\n\t\t\tObject.defineProperty(exports, key, { enumerable: true, get: definition[key] });\n\t\t}\n\t}\n};","__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))","// define __esModule on exports\n__webpack_require__.r = (exports) => {\n\tif(typeof Symbol !== 'undefined' && Symbol.toStringTag) {\n\t\tObject.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });\n\t}\n\tObject.defineProperty(exports, '__esModule', { value: true });\n};","import { Object } from \"ts-toolbelt\";\n\ntype Pattern =\n | RegExp\n | ((value: TValue) => boolean)\n | ((value: TValue) => any)\n | any;\ntype Result = TOutput | ((param: TValue) => TOutput);\ntype CaseElement = [\n Pattern,\n Result,\n true?\n];\ntype DefaultCaseElement = [Result];\ntype CaseElements = (\n | CaseElement\n | DefaultCaseElement\n)[];\n\ntype Matcher = (\n config: Object.Omit, \"matcher\" | \"resultGetter\">\n) => (valueToMatch: TValue, pattern: Pattern) => boolean;\ntype ResultGetter = (\n config: Object.Omit, \"matcher\" | \"resultGetter\">\n) => (valueToMatch: TValue, pattern: Pattern, result: any) => TOutput;\n\ntype Config = {\n returnMany: boolean;\n strict: boolean;\n acceptTruthyFunctionReturn: boolean;\n catchFunctionErrors: boolean;\n performReplaceOnRegex: boolean;\n runResultFunction: boolean;\n stopFallThrough: boolean;\n matcher: Matcher;\n resultGetter: ResultGetter;\n};\ntype BasicConfig = Object.Omit<\n Config,\n \"matcher\" | \"resultGetter\"\n>;\n\nexport const defaultMatcher =\n (config: BasicConfig) =>\n (valueToMatch: TValue, pattern: Pattern): boolean => {\n if (pattern instanceof RegExp) {\n return typeof valueToMatch === \"string\"\n ? pattern.test(valueToMatch)\n : false;\n }\n if (typeof pattern === \"function\") {\n if (config.catchFunctionErrors) {\n try {\n if (!config.acceptTruthyFunctionReturn) {\n return pattern(valueToMatch) === true;\n } else {\n return !!pattern(valueToMatch);\n }\n } catch (error) {\n return false;\n }\n } else {\n if (!config.acceptTruthyFunctionReturn) {\n return pattern(valueToMatch) === true;\n } else {\n return !!pattern(valueToMatch);\n }\n }\n }\n\n if (config.strict) {\n return pattern === valueToMatch;\n }\n return pattern == valueToMatch;\n };\n\nexport const defaultResultGetter =\n (\n config: Object.Omit, \"matcher\" | \"resultGetter\">\n ) =>\n (valueToMatch: TValue, pattern: Pattern, result: any): TOutput => {\n const getRegexResult = (\n valueToMatch: TValue,\n pattern: Pattern,\n replacement: any\n ) => {\n if (\n typeof valueToMatch === \"string\" &&\n typeof replacement === \"string\" &&\n config.performReplaceOnRegex\n ) {\n return valueToMatch.replace(pattern, replacement);\n }\n return replacement;\n };\n\n const resultToBeSet =\n config.runResultFunction && typeof result === \"function\"\n ? result(valueToMatch)\n : result;\n\n return pattern instanceof RegExp\n ? getRegexResult(valueToMatch, pattern, resultToBeSet)\n : resultToBeSet;\n };\n\nexport const createSwich =\n ({\n returnMany: defaultReturnMany = false,\n strict: defaultStrict = true,\n acceptTruthyFunctionReturn: defaultAcceptTruthyFunctionReturn = true,\n catchFunctionErrors: defaultCatchFunctionErrors = true,\n performReplaceOnRegex: defaultPerformReplaceOnRegex = false,\n runResultFunction: defaultRunResultFunction = true,\n stopFallThrough: defaultStopFallThrough = false,\n matcher: defaultMatcherValue = defaultMatcher,\n resultGetter: defaultResultGetterValue = defaultResultGetter,\n }: Object.Optional> = {}) =>\n (\n patterns: CaseElements,\n {\n returnMany = defaultReturnMany,\n strict = defaultStrict,\n acceptTruthyFunctionReturn = defaultAcceptTruthyFunctionReturn,\n catchFunctionErrors = defaultCatchFunctionErrors,\n performReplaceOnRegex = defaultPerformReplaceOnRegex,\n runResultFunction = defaultRunResultFunction,\n stopFallThrough = defaultStopFallThrough,\n matcher = defaultMatcherValue,\n resultGetter = defaultResultGetterValue,\n }: Object.Optional> = {}\n ) =>\n (valueToMatch: TValue | boolean = true) => {\n const config: BasicConfig = {\n returnMany,\n strict,\n acceptTruthyFunctionReturn,\n catchFunctionErrors,\n performReplaceOnRegex,\n runResultFunction,\n stopFallThrough,\n };\n\n let found = false;\n let result: TOutput | TOutput[] = returnMany ? [] : null;\n\n const setResult = (value: TOutput, setFound = true) => {\n found = setFound ? true : found;\n\n if (returnMany) {\n (result as TOutput[]).push(value);\n } else {\n result = value;\n }\n };\n\n const verifyValue = matcher(config);\n const getResult = resultGetter(config);\n\n let fallingThrough = false;\n patterns.forEach((matchArr, index) => {\n const [pattern, result, fallthrough] = matchArr;\n if (found && !returnMany && !fallingThrough) {\n return;\n }\n if (matchArr.length === 1 && (!found || fallingThrough)) {\n return setResult(\n getResult(valueToMatch as TValue, undefined, pattern),\n false\n );\n }\n\n if (\n matchArr.length !== 1 &&\n ((fallingThrough && !stopFallThrough) ||\n verifyValue(valueToMatch as TValue, pattern))\n ) {\n fallingThrough = !!fallthrough;\n return setResult(getResult(valueToMatch as TValue, pattern, result));\n }\n\n fallingThrough = stopFallThrough ? false : !!fallthrough;\n });\n\n return result;\n };\n\nexport const gt = (compareValue: number) => (value: number) =>\n value > compareValue;\nexport const gte = (compareValue: number) => (value: number) =>\n value >= compareValue;\nexport const lt = (compareValue: number) => (value: number) =>\n value < compareValue;\nexport const lte = (compareValue: number) => (value: number) =>\n value <= compareValue;\n\nconst defaultSwich = (\n patterns: CaseElements,\n config: Object.Optional> = {}\n) => createSwich()(patterns, config);\n\nexport default defaultSwich;\n"],"sourceRoot":""} \ No newline at end of file +{"version":3,"sources":["webpack://Swich/webpack/universalModuleDefinition","webpack://Swich/webpack/bootstrap","webpack://Swich/webpack/runtime/define property getters","webpack://Swich/webpack/runtime/hasOwnProperty shorthand","webpack://Swich/webpack/runtime/make namespace object","webpack://Swich/./src/lib/index.ts"],"names":["root","factory","exports","module","define","amd","this","__webpack_require__","definition","key","o","Object","defineProperty","enumerable","get","obj","prop","prototype","hasOwnProperty","call","Symbol","toStringTag","value","defaultMatcher","config","valueToMatch","pattern","RegExp","test","catchFunctionErrors","acceptTruthyFunctionReturn","error","strict","defaultResultGetter","result","resultToBeSet","runResultFunction","replacement","performReplaceOnRegex","replace","getRegexResult","swich","patterns","returnMany","stopFallThrough","matcher","resultGetter","found","setResult","setFound","push","verifyValue","getResult","fallingThrough","forEach","matchArr","index","fallthrough","length","undefined","gt","compareValue","gte","lt","lte","createSwich","defaultConfig"],"mappings":";;;;;;;;;;;CAAA,SAA2CA,EAAMC,GAC1B,iBAAZC,SAA0C,iBAAXC,OACxCA,OAAOD,QAAUD,IACQ,mBAAXG,QAAyBA,OAAOC,IAC9CD,OAAO,GAAIH,GACe,iBAAZC,QACdA,QAAe,MAAID,IAEnBD,EAAY,MAAIC,IARlB,CASGK,MAAM,WACT,M,mBCTA,IAAIC,EAAsB,CCA1B,EAAwB,CAACL,EAASM,KACjC,IAAI,IAAIC,KAAOD,EACXD,EAAoBG,EAAEF,EAAYC,KAASF,EAAoBG,EAAER,EAASO,IAC5EE,OAAOC,eAAeV,EAASO,EAAK,CAAEI,YAAY,EAAMC,IAAKN,EAAWC,MCJ3E,EAAwB,CAACM,EAAKC,IAAUL,OAAOM,UAAUC,eAAeC,KAAKJ,EAAKC,GCClF,EAAyBd,IACH,oBAAXkB,QAA0BA,OAAOC,aAC1CV,OAAOC,eAAeV,EAASkB,OAAOC,YAAa,CAAEC,MAAO,WAE7DX,OAAOC,eAAeV,EAAS,aAAc,CAAEoB,OAAO,M,ywDCqChD,IAAMC,EACX,SAAkBC,GAAlB,OACA,SAACC,EAAsBC,GACrB,GAAIA,aAAmBC,OACrB,MAA+B,iBAAjBF,GACVC,EAAQE,KAAKH,GAGnB,GAAuB,mBAAZC,EAAwB,CACjC,IAAIF,EAAOK,oBAWT,OAAKL,EAAOM,6BAGDJ,EAAQD,IAFgB,IAA1BC,EAAQD,GAXjB,IACE,OAAKD,EAAOM,6BAGDJ,EAAQD,IAFgB,IAA1BC,EAAQD,GAIjB,MAAOM,GACP,OAAO,GAWb,OAAIP,EAAOQ,OACFN,IAAYD,EAEdC,GAAWD,IAGTQ,EACX,SACET,GADF,OAGA,SAACC,EAAsBC,EAA0BQ,GAC/C,IAeMC,EACJX,EAAOY,mBAAuC,mBAAXF,EAC/BA,EAAOT,GACPS,EAEN,OAAOR,aAAmBC,OApBH,SACrBF,EACAC,EACAW,GAEA,MAC0B,iBAAjBZ,GACgB,iBAAhBY,GACPb,EAAOc,sBAEAb,EAAac,QAAQb,EAASW,GAEhCA,EASLG,CAAef,EAAcC,EAASS,GACtCA,IAgBR,SAASM,EACLC,GAYA,6DAD8C,GAC9C,IAVEC,kBAUF,aATEX,cASF,aAREF,kCAQF,aAPED,2BAOF,aANES,6BAMF,aALEF,yBAKF,aAJEQ,uBAIF,aAHEC,eAGF,MAHYtB,EAGZ,MAFEuB,oBAEF,MAFiBb,EAEjB,EACF,OAAO,WAAwC,IAAvCR,IAAuC,yDACvCD,EAAuC,CAC3CmB,aACAX,SACAF,6BACAD,sBACAS,wBACAF,oBACAQ,mBAGEG,GAAQ,EACRb,EAAUS,EAAa,GAAK,KAE1BK,EAAY,SAAC1B,GAAgC,IAApB2B,IAAoB,yDACjDF,IAAQE,GAAkBF,EAEtBJ,EACDT,EAAqBgB,KAAK5B,GAE1BY,EAAqBZ,GAIpB6B,EAAcN,EAAQrB,GACtB4B,EAAYN,EAAatB,GAE3B6B,GAAiB,EAyBrB,OAxBAX,EAASY,SAAQ,SAACC,EAAUC,GAC1B,QAAuCD,EAAvC,GAAO7B,EAAP,KAAgBQ,EAAhB,KAAwBuB,EAAxB,KACA,IAAIV,GAAUJ,GAAeU,EAG7B,OAAwB,IAApBE,EAASG,QAAkBX,IAASM,EAQlB,IAApBE,EAASG,SACPL,IAAmBT,GACnBO,EAAY1B,EAAwBC,KAEtC2B,IAAmBI,EACZT,EAAUI,EAAU3B,EAAwBC,EAASQ,UAG9DmB,GAAiBT,KAA4Ba,GAfpCT,EACLI,EAAU3B,OAAwBkC,EAAWjC,IAC7C,MAgBCQ,GAIJ,IAAM0B,EAAK,SAACC,GAAD,OAA0B,SAACvC,GAAD,OAC1CA,EAAQuC,IACGC,EAAM,SAACD,GAAD,OAA0B,SAACvC,GAAD,OAC3CA,GAASuC,IACEE,EAAK,SAACF,GAAD,OAA0B,SAACvC,GAAD,OAC1CA,EAAQuC,IACGG,EAAM,SAACH,GAAD,OAA0B,SAACvC,GAAD,OAC3CA,GAASuC,IAgBJ,SAASI,EAA2CC,GACzD,OAAO,SACLxB,GADK,IAELlB,EAFK,uDAE8C,GAF9C,OAIFiB,EAAMC,EAAD,OAAgBwB,GAAkB1C,KAG9C,U","file":"index.js","sourcesContent":["(function webpackUniversalModuleDefinition(root, factory) {\n\tif(typeof exports === 'object' && typeof module === 'object')\n\t\tmodule.exports = factory();\n\telse if(typeof define === 'function' && define.amd)\n\t\tdefine([], factory);\n\telse if(typeof exports === 'object')\n\t\texports[\"Swich\"] = factory();\n\telse\n\t\troot[\"Swich\"] = factory();\n})(this, function() {\nreturn ","// The require scope\nvar __webpack_require__ = {};\n\n","// define getter functions for harmony exports\n__webpack_require__.d = (exports, definition) => {\n\tfor(var key in definition) {\n\t\tif(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {\n\t\t\tObject.defineProperty(exports, key, { enumerable: true, get: definition[key] });\n\t\t}\n\t}\n};","__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))","// define __esModule on exports\n__webpack_require__.r = (exports) => {\n\tif(typeof Symbol !== 'undefined' && Symbol.toStringTag) {\n\t\tObject.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });\n\t}\n\tObject.defineProperty(exports, '__esModule', { value: true });\n};","import { Object } from \"ts-toolbelt\";\n\nexport type Pattern =\n | RegExp\n | ((value: TValue) => boolean)\n | ((value: TValue) => any)\n | any;\nexport type Result = TOutput | ((param: TValue) => TOutput);\nexport type CaseElement = [\n Pattern,\n Result,\n true?\n];\nexport type DefaultCaseElement = [Result];\nexport type CaseElements = (\n | CaseElement\n | DefaultCaseElement\n)[];\n\nexport type Matcher = (\n config: Object.Omit, \"matcher\" | \"resultGetter\">\n) => (valueToMatch: TValue, pattern: Pattern) => boolean;\nexport type ResultGetter = (\n config: Object.Omit, \"matcher\" | \"resultGetter\">\n) => (valueToMatch: TValue, pattern: Pattern, result: any) => TOutput;\n\nexport type Config = {\n returnMany: boolean;\n strict: boolean;\n acceptTruthyFunctionReturn: boolean;\n catchFunctionErrors: boolean;\n performReplaceOnRegex: boolean;\n runResultFunction: boolean;\n stopFallThrough: boolean;\n matcher: Matcher;\n resultGetter: ResultGetter;\n};\nexport type BasicConfig = Object.Omit<\n Config,\n \"matcher\" | \"resultGetter\"\n>;\n\nexport const defaultMatcher =\n (config: BasicConfig) =>\n (valueToMatch: TValue, pattern: Pattern): boolean => {\n if (pattern instanceof RegExp) {\n return typeof valueToMatch === \"string\"\n ? pattern.test(valueToMatch)\n : false;\n }\n if (typeof pattern === \"function\") {\n if (config.catchFunctionErrors) {\n try {\n if (!config.acceptTruthyFunctionReturn) {\n return pattern(valueToMatch) === true;\n } else {\n return !!pattern(valueToMatch);\n }\n } catch (error) {\n return false;\n }\n } else {\n if (!config.acceptTruthyFunctionReturn) {\n return pattern(valueToMatch) === true;\n } else {\n return !!pattern(valueToMatch);\n }\n }\n }\n\n if (config.strict) {\n return pattern === valueToMatch;\n }\n return pattern == valueToMatch;\n };\n\nexport const defaultResultGetter =\n (\n config: Object.Omit, \"matcher\" | \"resultGetter\">\n ) =>\n (valueToMatch: TValue, pattern: Pattern, result: TOutput): TOutput => {\n const getRegexResult = (\n valueToMatch: TValue,\n pattern: Pattern,\n replacement: TOutput\n ) => {\n if (\n typeof valueToMatch === \"string\" &&\n typeof replacement === \"string\" &&\n config.performReplaceOnRegex\n ) {\n return valueToMatch.replace(pattern, replacement);\n }\n return replacement;\n };\n\n const resultToBeSet =\n config.runResultFunction && typeof result === \"function\"\n ? result(valueToMatch)\n : result;\n\n return pattern instanceof RegExp\n ? getRegexResult(valueToMatch, pattern, resultToBeSet)\n : resultToBeSet;\n };\n\ntype SwichReturnMany = (valueToMatch?: TValue | true) => TOutput[];\ntype SwichReturnOne = (valueToMatch?: TValue | true) => TOutput;\n\nfunction swich(\n patterns: CaseElements,\n config: Object.Optional> & { returnMany: true }\n): SwichReturnMany;\n\nfunction swich(\n patterns: CaseElements,\n config?: Object.Optional> & { returnMany?: false }\n): SwichReturnOne;\n\nfunction swich(\n patterns: CaseElements,\n {\n returnMany = false,\n strict = true,\n acceptTruthyFunctionReturn = true,\n catchFunctionErrors = true,\n performReplaceOnRegex = false,\n runResultFunction = true,\n stopFallThrough = false,\n matcher = defaultMatcher,\n resultGetter = defaultResultGetter,\n }: Object.Optional> = {}\n ) {\n return (valueToMatch: TValue | true = true) => {\n const config: BasicConfig = {\n returnMany,\n strict,\n acceptTruthyFunctionReturn,\n catchFunctionErrors,\n performReplaceOnRegex,\n runResultFunction,\n stopFallThrough,\n };\n\n let found = false;\n let result = (returnMany ? [] : null) as (TOutput[] | TOutput);\n\n const setResult = (value: any, setFound = true) => {\n found = setFound ? true : found;\n\n if (returnMany) {\n (result as TOutput[]).push(value);\n } else {\n (result as TOutput) = value;\n }\n };\n\n const verifyValue = matcher(config);\n const getResult = resultGetter(config);\n\n let fallingThrough = false;\n patterns.forEach((matchArr, index) => {\n const [pattern, result, fallthrough] = matchArr;\n if (found && !returnMany && !fallingThrough) {\n return;\n }\n if (matchArr.length === 1 && (!found || fallingThrough)) {\n return setResult(\n getResult(valueToMatch as TValue, undefined, pattern),\n false\n );\n }\n\n if (\n matchArr.length !== 1 &&\n ((fallingThrough && !stopFallThrough) ||\n verifyValue(valueToMatch as TValue, pattern))\n ) {\n fallingThrough = !!fallthrough;\n return setResult(getResult(valueToMatch as TValue, pattern, result));\n }\n\n fallingThrough = stopFallThrough ? false : !!fallthrough;\n });\n\n return result;\n }\n}\n\nexport const gt = (compareValue: number) => (value: number) =>\n value > compareValue;\nexport const gte = (compareValue: number) => (value: number) =>\n value >= compareValue;\nexport const lt = (compareValue: number) => (value: number) =>\n value < compareValue;\nexport const lte = (compareValue: number) => (value: number) =>\n value <= compareValue;\n\ntype CreateSwichReturnMany = (\n patterns: CaseElements,\n config?: Object.Optional>\n) => SwichReturnMany\n\ntype CreateSwichReturnOne = (\n patterns: CaseElements,\n config?: Object.Optional>\n) => SwichReturnOne\n\nexport function createSwich(defaultConfig: Object.Optional> & { returnMany: true }): CreateSwichReturnMany;\n// @ts-ignore\nexport function createSwich(defaultConfig: Object.Optional> & { returnMany?: false }): CreateSwichReturnOne;\n\nexport function createSwich(defaultConfig: Object.Optional>) {\n return (\n patterns: CaseElements,\n config: Object.Optional> = {}\n // @ts-ignore\n ) => swich(patterns, { ...defaultConfig, ...config });\n}\n\nexport default swich;\n"],"sourceRoot":""} \ No newline at end of file diff --git a/build/types/index.d.ts b/build/types/index.d.ts index 1d09c20..6201a6f 100644 --- a/build/types/index.d.ts +++ b/build/types/index.d.ts @@ -1,16 +1,16 @@ import { Object } from "ts-toolbelt"; -declare type Pattern = RegExp | ((value: TValue) => boolean) | ((value: TValue) => any) | any; -declare type Result = TOutput | ((param: TValue) => TOutput); -declare type CaseElement = [ +export declare type Pattern = RegExp | ((value: TValue) => boolean) | ((value: TValue) => any) | any; +export declare type Result = TOutput | ((param: TValue) => TOutput); +export declare type CaseElement = [ Pattern, Result, true? ]; -declare type DefaultCaseElement = [Result]; -declare type CaseElements = (CaseElement | DefaultCaseElement)[]; -declare type Matcher = (config: Object.Omit, "matcher" | "resultGetter">) => (valueToMatch: TValue, pattern: Pattern) => boolean; -declare type ResultGetter = (config: Object.Omit, "matcher" | "resultGetter">) => (valueToMatch: TValue, pattern: Pattern, result: any) => TOutput; -declare type Config = { +export declare type DefaultCaseElement = [Result]; +export declare type CaseElements = (CaseElement | DefaultCaseElement)[]; +export declare type Matcher = (config: Object.Omit, "matcher" | "resultGetter">) => (valueToMatch: TValue, pattern: Pattern) => boolean; +export declare type ResultGetter = (config: Object.Omit, "matcher" | "resultGetter">) => (valueToMatch: TValue, pattern: Pattern, result: any) => TOutput; +export declare type Config = { returnMany: boolean; strict: boolean; acceptTruthyFunctionReturn: boolean; @@ -21,43 +21,27 @@ declare type Config = { matcher: Matcher; resultGetter: ResultGetter; }; -declare type BasicConfig = Object.Omit, "matcher" | "resultGetter">; +export declare type BasicConfig = Object.Omit, "matcher" | "resultGetter">; export declare const defaultMatcher: (config: import("ts-toolbelt/out/Object/Omit")._Omit, "matcher" | "resultGetter">) => (valueToMatch: TValue, pattern: any) => boolean; -export declare const defaultResultGetter: (config: import("ts-toolbelt/out/Object/Omit")._Omit, "matcher" | "resultGetter">) => (valueToMatch: TValue, pattern: any, result: any) => TOutput; -export declare const createSwich: ({ returnMany: defaultReturnMany, strict: defaultStrict, acceptTruthyFunctionReturn: defaultAcceptTruthyFunctionReturn, catchFunctionErrors: defaultCatchFunctionErrors, performReplaceOnRegex: defaultPerformReplaceOnRegex, runResultFunction: defaultRunResultFunction, stopFallThrough: defaultStopFallThrough, matcher: defaultMatcherValue, resultGetter: defaultResultGetterValue, }?: { - returnMany?: boolean; - strict?: boolean; - acceptTruthyFunctionReturn?: boolean; - catchFunctionErrors?: boolean; - performReplaceOnRegex?: boolean; - runResultFunction?: boolean; - stopFallThrough?: boolean; - matcher?: Matcher; - resultGetter?: ResultGetter; -}) => (patterns: CaseElements, { returnMany, strict, acceptTruthyFunctionReturn, catchFunctionErrors, performReplaceOnRegex, runResultFunction, stopFallThrough, matcher, resultGetter, }?: { - returnMany?: boolean; - strict?: boolean; - acceptTruthyFunctionReturn?: boolean; - catchFunctionErrors?: boolean; - performReplaceOnRegex?: boolean; - runResultFunction?: boolean; - stopFallThrough?: boolean; - matcher?: Matcher; - resultGetter?: ResultGetter; -}) => (valueToMatch?: boolean | TValue) => TOutput[]; +export declare const defaultResultGetter: (config: import("ts-toolbelt/out/Object/Omit")._Omit, "matcher" | "resultGetter">) => (valueToMatch: TValue, pattern: any, result: TOutput) => TOutput; +declare type SwichReturnMany = (valueToMatch?: TValue | true) => TOutput[]; +declare type SwichReturnOne = (valueToMatch?: TValue | true) => TOutput; +declare function swich(patterns: CaseElements, config: Object.Optional> & { + returnMany: true; +}): SwichReturnMany; +declare function swich(patterns: CaseElements, config?: Object.Optional> & { + returnMany?: false; +}): SwichReturnOne; export declare const gt: (compareValue: number) => (value: number) => boolean; export declare const gte: (compareValue: number) => (value: number) => boolean; export declare const lt: (compareValue: number) => (value: number) => boolean; export declare const lte: (compareValue: number) => (value: number) => boolean; -declare const defaultSwich: (patterns: CaseElements, config?: { - returnMany?: boolean; - strict?: boolean; - acceptTruthyFunctionReturn?: boolean; - catchFunctionErrors?: boolean; - performReplaceOnRegex?: boolean; - runResultFunction?: boolean; - stopFallThrough?: boolean; - matcher?: Matcher; - resultGetter?: ResultGetter; -}) => (valueToMatch?: boolean | TValue) => TOutput[]; -export default defaultSwich; +declare type CreateSwichReturnMany = (patterns: CaseElements, config?: Object.Optional>) => SwichReturnMany; +declare type CreateSwichReturnOne = (patterns: CaseElements, config?: Object.Optional>) => SwichReturnOne; +export declare function createSwich(defaultConfig: Object.Optional> & { + returnMany: true; +}): CreateSwichReturnMany; +export declare function createSwich(defaultConfig: Object.Optional> & { + returnMany?: false; +}): CreateSwichReturnOne; +export default swich; diff --git a/package.json b/package.json index 7e7e5ef..00624c0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "swich", - "version": "1.1.2", + "version": "1.2.0", "description": "Switch, but better", "main": "build/index.js", "types": "build/types/index.d.ts", diff --git a/src/lib/index.ts b/src/lib/index.ts index f7aad2d..d38ffd7 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -1,30 +1,30 @@ import { Object } from "ts-toolbelt"; -type Pattern = +export type Pattern = | RegExp | ((value: TValue) => boolean) | ((value: TValue) => any) | any; -type Result = TOutput | ((param: TValue) => TOutput); -type CaseElement = [ +export type Result = TOutput | ((param: TValue) => TOutput); +export type CaseElement = [ Pattern, Result, true? ]; -type DefaultCaseElement = [Result]; -type CaseElements = ( +export type DefaultCaseElement = [Result]; +export type CaseElements = ( | CaseElement | DefaultCaseElement )[]; -type Matcher = ( +export type Matcher = ( config: Object.Omit, "matcher" | "resultGetter"> ) => (valueToMatch: TValue, pattern: Pattern) => boolean; -type ResultGetter = ( +export type ResultGetter = ( config: Object.Omit, "matcher" | "resultGetter"> ) => (valueToMatch: TValue, pattern: Pattern, result: any) => TOutput; -type Config = { +export type Config = { returnMany: boolean; strict: boolean; acceptTruthyFunctionReturn: boolean; @@ -35,7 +35,7 @@ type Config = { matcher: Matcher; resultGetter: ResultGetter; }; -type BasicConfig = Object.Omit< +export type BasicConfig = Object.Omit< Config, "matcher" | "resultGetter" >; @@ -78,11 +78,15 @@ export const defaultResultGetter = ( config: Object.Omit, "matcher" | "resultGetter"> ) => - (valueToMatch: TValue, pattern: Pattern, result: any): TOutput => { + ( + valueToMatch: TValue, + pattern: Pattern, + result: TOutput + ): TOutput => { const getRegexResult = ( valueToMatch: TValue, pattern: Pattern, - replacement: any + replacement: TOutput ) => { if ( typeof valueToMatch === "string" && @@ -104,33 +108,38 @@ export const defaultResultGetter = : resultToBeSet; }; -export const createSwich = - ({ - returnMany: defaultReturnMany = false, - strict: defaultStrict = true, - acceptTruthyFunctionReturn: defaultAcceptTruthyFunctionReturn = true, - catchFunctionErrors: defaultCatchFunctionErrors = true, - performReplaceOnRegex: defaultPerformReplaceOnRegex = false, - runResultFunction: defaultRunResultFunction = true, - stopFallThrough: defaultStopFallThrough = false, - matcher: defaultMatcherValue = defaultMatcher, - resultGetter: defaultResultGetterValue = defaultResultGetter, - }: Object.Optional> = {}) => - ( - patterns: CaseElements, - { - returnMany = defaultReturnMany, - strict = defaultStrict, - acceptTruthyFunctionReturn = defaultAcceptTruthyFunctionReturn, - catchFunctionErrors = defaultCatchFunctionErrors, - performReplaceOnRegex = defaultPerformReplaceOnRegex, - runResultFunction = defaultRunResultFunction, - stopFallThrough = defaultStopFallThrough, - matcher = defaultMatcherValue, - resultGetter = defaultResultGetterValue, - }: Object.Optional> = {} - ) => - (valueToMatch: TValue | boolean = true) => { +type SwichReturnMany = ( + valueToMatch?: TValue | true +) => TOutput[]; +type SwichReturnOne = ( + valueToMatch?: TValue | true +) => TOutput; + +function swich( + patterns: CaseElements, + config: Object.Optional> & { returnMany: true } +): SwichReturnMany; + +function swich( + patterns: CaseElements, + config?: Object.Optional> & { returnMany?: false } +): SwichReturnOne; + +function swich( + patterns: CaseElements, + { + returnMany = false, + strict = true, + acceptTruthyFunctionReturn = true, + catchFunctionErrors = true, + performReplaceOnRegex = false, + runResultFunction = true, + stopFallThrough = false, + matcher = defaultMatcher, + resultGetter = defaultResultGetter, + }: Object.Optional> = {} +) { + return (valueToMatch: TValue | true = true) => { const config: BasicConfig = { returnMany, strict, @@ -142,15 +151,15 @@ export const createSwich = }; let found = false; - let result: TOutput | TOutput[] = returnMany ? [] : null; + let result = (returnMany ? [] : null) as TOutput[] | TOutput; - const setResult = (value: TOutput, setFound = true) => { + const setResult = (value: any, setFound = true) => { found = setFound ? true : found; if (returnMany) { (result as TOutput[]).push(value); } else { - result = value; + (result as TOutput) = value; } }; @@ -184,6 +193,7 @@ export const createSwich = return result; }; +} export const gt = (compareValue: number) => (value: number) => value > compareValue; @@ -194,9 +204,34 @@ export const lt = (compareValue: number) => (value: number) => export const lte = (compareValue: number) => (value: number) => value <= compareValue; -const defaultSwich = ( +type CreateSwichReturnMany = ( patterns: CaseElements, - config: Object.Optional> = {} -) => createSwich()(patterns, config); + config?: Object.Optional> +) => SwichReturnMany; + +type CreateSwichReturnOne = ( + patterns: CaseElements, + config?: Object.Optional> +) => SwichReturnOne; + +export function createSwich( + defaultConfig: Object.Optional> & { returnMany: true } +): CreateSwichReturnMany; +// @ts-ignore +export function createSwich( + defaultConfig: Object.Optional> & { + returnMany?: false; + } +): CreateSwichReturnOne; + +export function createSwich( + defaultConfig: Object.Optional> +) { + return ( + patterns: CaseElements, + config: Object.Optional> = {} + // @ts-ignore + ) => swich(patterns, { ...defaultConfig, ...config }); +} -export default defaultSwich; +export default swich; diff --git a/src/lib/tests/index.test.ts b/src/lib/tests/index.test.ts index 24025f9..d5e9e96 100644 --- a/src/lib/tests/index.test.ts +++ b/src/lib/tests/index.test.ts @@ -218,7 +218,7 @@ describe('swich', () => { resultGetter: (config) => (valueToMatch, pattern, result) => defaultResultGetter(config)(valueToMatch, pattern, typeof result === 'object' ? result.value : result), }); - const instance = customSwich([ + const instance = customSwich([ ['foo', { value: 'Type is foo' }], ['bar', { value: 'Type is bar' }], ['Unknown type'],