diff --git a/.gitattributes b/.gitattributes index fb4c97e..ed94eb1 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,5 +1,7 @@ # git CLI **/*.js.map -diff +**/*.css.map -diff # GitHub **/*.js.map linguist-generated=true +**/*.css.map linguist-generated=true diff --git a/dist/bundle.css b/dist/bundle.css new file mode 100644 index 0000000..f097a6d --- /dev/null +++ b/dist/bundle.css @@ -0,0 +1,2 @@ +body{background-color:#ccc;color:#194c19;font-family:Arial,sans-serif}h1{font-size:3rem}label{cursor:pointer}input,select{border-radius:.35rem;margin:.5rem;padding-left:.35rem;height:1.75rem}select{background-color:#194c19;color:#a3f4f4;border:0}button{background-color:#00f;color:#feff99;font-size:1.5rem;border:0;border-radius:.35rem;margin-top:1rem;padding:.25rem .5rem;height:3rem;cursor:pointer}button:hover{background-color:#006400;color:#00feff}#result{background-color:#382d2d;color:#19e519;margin-left:10px;padding:.25rem .5rem}input[type=text]:read-only{background-color:#eee} +/*# sourceMappingURL=bundle.css.map */ \ No newline at end of file diff --git a/dist/bundle.css.map b/dist/bundle.css.map new file mode 100644 index 0000000..bfc74ba --- /dev/null +++ b/dist/bundle.css.map @@ -0,0 +1 @@ +{"version":3,"sources":["dist\\calc.css"],"names":[],"mappings":"AAMA,KACE,iBAAkB,KAClB,MAAO,QACP,YAAa,KAAK,CAAE,WAItB,GACE,UAAW,KAIb,MACE,OAAQ,QAIV,MAAO,OACL,cAAe,OACf,OAAQ,MACR,aAAc,OACd,OAAQ,QAMV,OACE,iBAAkB,QAClB,MAAM,QACN,OAAQ,EAGV,OACE,iBAAkB,KAClB,MAAO,QACP,UAAW,OACX,OAAQ,EACR,cAAe,OACf,WAAY,KACZ,QAAS,OAAQ,MACjB,OAAQ,KACR,OAAQ,QAKV,aACE,iBAAkB,QAClB,MAAO,QAST,QACE,iBAAkB,QAClB,MAAM,QAEN,YAAa,KACb,QAAS,OAAQ,MAInB,2BACE,iBAAkB","sourcesContent":["/* #container-calculate {\r\n padding-top: 1rem;\r\n} */\r\n\r\n/* TODO: try using PostCSS or similar to build some part of it dynamically */\r\n\r\nbody {\r\n background-color: hsl(0, 0%, 80%);\r\n color: hsl(120, 50%, 20%);\r\n font-family: Arial, sans-serif;\r\n /* line-height: 1rem; */\r\n}\r\n\r\nh1 {\r\n font-size: 3rem;\r\n /* text-align: center; */\r\n}\r\n\r\nlabel {\r\n cursor: pointer;\r\n /* display: block; */\r\n}\r\n\r\ninput, select {\r\n border-radius: 0.35rem;\r\n margin: 0.5rem;\r\n padding-left: 0.35rem;\r\n height: 1.75rem;\r\n /* display: block;\r\n padding: 5px;\r\n */\r\n}\r\n\r\nselect {\r\n background-color: hsl(120, 50%, 20%);\r\n color:hsl(180, 80%, 80%);\r\n border: 0rem;\r\n}\r\n\r\nbutton {\r\n background-color: hsl(240, 100%, 50%);\r\n color: hsl(60, 100%, 80%);\r\n font-size: 1.5rem;\r\n border: 0;\r\n border-radius: 0.35rem;\r\n margin-top: 1rem;\r\n padding: 0.25rem 0.5rem;\r\n height: 3rem;\r\n cursor: pointer;\r\n /* display: block; */\r\n /* width: 100%; */\r\n}\r\n\r\nbutton:hover {\r\n background-color: darkgreen;\r\n color: hsl(180, 100%, 50%)\r\n}\r\n\r\n/* #container-calculate {\r\n display: flex;\r\n justify-content: space-between;\r\n align-items: center;\r\n} */\r\n\r\n#result {\r\n background-color: hsl(0, 10%, 20%);\r\n color:hsl(120, 80%, 50%);\r\n /* font-size: 1.2em; */\r\n margin-left: 10px;\r\n padding: 0.25rem 0.5rem;\r\n /* text-align: center; */\r\n}\r\n\r\ninput[type=\"text\"]:read-only {\r\n background-color: #EEE;\r\n /* cursor: not-allowed; */\r\n}\r\n"]} \ No newline at end of file diff --git a/dist/bundle.js b/dist/bundle.js new file mode 100644 index 0000000..07cb923 --- /dev/null +++ b/dist/bundle.js @@ -0,0 +1,2 @@ +!function(t,e){"function"==typeof define&&define.amd?define([],e):"object"==typeof module&&module.exports?module.exports=e():t.calculate=e()}("undefined"!=typeof self?self:globalThis,function(){var r=function(t){var e,n;if("string"!=typeof t&&!Array.isArray(t)||(e=t,t=null),null!==t||null!==(t=function(t,e){var n,l,u,o;if(t=(n=i(e))!==null&&n!==void 0?n:t,!/\?/.test(t))return null;var a=new URL(t).searchParams;r.setFieldsData((l=a.get("num1"))!==null&&l!==void 0?l:"",(u=a.get("operator"))!==null&&u!==void 0?u:"",(o=a.get("num2"))!==null&&o!==void 0?o:"")}(r.href,e)))return t=(e=t=null==t?r.getFieldsData():t).num1,n=e.operator,e=e.num2,r.setFieldsData(t,n,e),t=parseFloat(String(t)),e=parseFloat(String(e)),(n={"to the power of":function(t,e){return Math.pow(t,e)},plus:function(t,e){return t+e},minus:function(t,e){return t-e},times:function(t,e){return t*e},"divided by":function(t,e){return t/e}}[n])?(n=n(t,e),r.output(n),n):r.warnAfterFocus("Please select an operator")},l={},i=function(t){var e,n,t=t?[t].flat():"undefined"!=typeof process?process.argv.slice(2):[],t=t.length?t.join(" ").replace(/[^a-z\d.+%-]/gi," ").trim():"",l=/\??\b(num[12]|operator)\s*[:= ]\s*[a-z\d.+%-]/i.test(t),u=t.split(/\s+/),l=l?(e=null==(l=t.match(/\bnum1\s*[:= ]\s*([a-z\d.+%-]*)/i))?void 0:l[1],n=null==(l=t.match(/\boperator\s*[:= ]\s*([a-z\d.+%-]*)/i))?void 0:l[1],null==(l=t.match(/\bnum2\s*[:= ]\s*([a-z\d.+%-]*)/i))?void 0:l[1]):(e=u.shift(),n=u.shift(),u.shift());return t.trim().length?"X:?num1=".concat(e,"&operator=").concat(n,"&num2=").concat(l):null};return r.getFieldsData=function(){return l},r.setFieldsData=function(t,e,n){l.num1=t,l.operator=e,l.num2=n,l.result=null,console.warn(l)},r.warnAfterFocus=function(t){console.error(t)},r.output=function(t){l.result=t,console.log(l)},r.href="",r});var calculate,qi=function(t){return document.getElementById(t)},num1El=qi("num1"),operatorEl=qi("operator"),num2El=qi("num2"),resultEl=qi("result"),calcEl=qi("calc-button"),main=(calculate.getFieldsData=function(){return{num1:num1El.value,operator:operatorEl.value,num2:num2El.value}},calculate.setFieldsData=function(t,e,n){num1El.value=t,operatorEl.value=e,num2El.value=n},calculate.warnAfterFocus=function(t){operatorEl.focus(),alert(t)},calculate.output=function(t){resultEl.value=t,resultEl.focus(),resultEl.select()},calculate.href=location.href.replace(location.hash,""),function(t,e,n){calcEl.onclick=function(){return calculate()},t.onload=e(function(){calculate(null)},n)});main(window,setTimeout,100); +//# sourceMappingURL=./bundle.js.map \ No newline at end of file diff --git a/dist/bundle.js.map b/dist/bundle.js.map new file mode 100644 index 0000000..ac7218b --- /dev/null +++ b/dist/bundle.js.map @@ -0,0 +1 @@ +{"version":3,"sources":["./dist/calc.js","./dist/browser.js"],"sourcesContent":["// calc.ts: loaded after defer-loaded \"calc.ts\"\n(function (root, factory) {\n typeof define === \"function\" && define.amd\n ? define([], factory)\n : typeof module === \"object\" && module.exports\n ? module.exports = factory()\n : (root.calculate = factory());\n})(typeof self !== \"undefined\" ? self : globalThis, function () {\n var calculate = function (data) {\n var nodeArgsMock;\n if (typeof data === \"string\" || Array.isArray(data)) {\n nodeArgsMock = data;\n data = null;\n }\n ;\n if (data === null) {\n data = getArgsData(calculate.href, nodeArgsMock);\n if (data === null) {\n return;\n }\n ;\n }\n ;\n if (data == null) {\n // default logic if undefined (getArgsData had args to parse, or button was clicked)\n data = calculate.getFieldsData();\n }\n ;\n var _a = data, num1 = _a.num1, operator = _a.operator, num2 = _a.num2;\n calculate.setFieldsData(num1, operator, num2); // for visual consistency if calculate({key:value})\n num1 = parseFloat(String(num1));\n num2 = parseFloat(String(num2));\n var operations = {\n \"to the power of\": function (num1, num2) { return Math.pow(num1, num2); },\n \"plus\": function (num1, num2) { return num1 + num2; },\n \"minus\": function (num1, num2) { return num1 - num2; },\n \"times\": function (num1, num2) { return num1 * num2; },\n \"divided by\": function (num1, num2) { return num1 / num2; },\n };\n var fn = operations[operator];\n if (!fn) {\n return calculate.warnAfterFocus(\"Please select an operator\");\n }\n ;\n var result = fn(num1, num2);\n calculate.output(result);\n return result;\n };\n var fieldsMock = {};\n var getFieldsData = function () {\n return fieldsMock;\n };\n var setFieldsData = function (num1, operator, num2) {\n fieldsMock.num1 = num1;\n fieldsMock.operator = operator;\n fieldsMock.num2 = num2;\n // delete fieldsMock.result;\n fieldsMock.result = null;\n console.warn(fieldsMock);\n };\n var warnAfterFocus = function (message) {\n console.error(message);\n };\n var output = function (result) {\n fieldsMock.result = result;\n console.log(fieldsMock);\n };\n var getArgsData = function (href, nodeArgsMock) {\n var _a, _b, _c, _d;\n href = (_a = getNodeArgsData(nodeArgsMock)) !== null && _a !== void 0 ? _a : href;\n if (!/\\?/.test(href))\n return null;\n // new URL(href), because new URLSearchParams(href) seems to skip the 1st param immediately after \"?\"\n var p = new URL(href).searchParams;\n calculate.setFieldsData((_b = p.get(\"num1\")) !== null && _b !== void 0 ? _b : \"\", // briefer than p.has(\"num1\") ? p.get(\"num1\") : \"\"\n (_c = p.get(\"operator\")) !== null && _c !== void 0 ? _c : \"\", (_d = p.get(\"num2\")) !== null && _d !== void 0 ? _d : \"\");\n // return undefined, to ensure getFieldsData parses the values we just moved into the fields\n return;\n };\n var getNodeArgsData = function (nodeArgsMock) {\n var _a, _b, _c;\n var argv = (nodeArgsMock\n ? [nodeArgsMock].flat()\n : typeof process !== \"undefined\"\n ? process.argv.slice(2)\n : []);\n var args = argv.length\n ? argv.join(\" \").replace(/[^a-z\\d.+%-]/gi, \" \").trim()\n : \"\";\n var hasNamedArgs = /\\??\\b(num[12]|operator)\\s*[:= ]\\s*[a-z\\d.+%-]/i.test(args);\n var orderedList = args.split(/\\s+/);\n var num1, operator, num2;\n if (hasNamedArgs) {\n num1 = (_a = args.match(/\\bnum1\\s*[:= ]\\s*([a-z\\d.+%-]*)/i)) === null || _a === void 0 ? void 0 : _a[1];\n operator = (_b = args.match(/\\boperator\\s*[:= ]\\s*([a-z\\d.+%-]*)/i)) === null || _b === void 0 ? void 0 : _b[1];\n num2 = (_c = args.match(/\\bnum2\\s*[:= ]\\s*([a-z\\d.+%-]*)/i)) === null || _c === void 0 ? void 0 : _c[1];\n }\n else {\n num1 = orderedList.shift();\n operator = orderedList.shift();\n num2 = orderedList.shift();\n }\n ;\n return (args.trim().length\n ? \"X:?num1=\".concat(num1, \"&operator=\").concat(operator, \"&num2=\").concat(num2)\n : null);\n };\n calculate.getFieldsData = getFieldsData;\n calculate.setFieldsData = setFieldsData;\n calculate.warnAfterFocus = warnAfterFocus;\n calculate.output = output;\n calculate.href = \"\";\n // calculate.href = \"https://example.com/api#?num1=3.x&num2=4&operator=times\"; // no automatic .replace(location.hash, \"\");\n // calculate.href = \"https://example.com/api?num1=3.x&num2=4&operator=times\";\n // calculate.calculate = calculate; // useless, it seems: calculate.default = calculate;\n return calculate;\n});\n// console.log(module.exports);\n// [Function: calculate] {\n// getFieldsData: [Function: getFieldsData],\n// setFieldsData: [Function: setFieldsData],\n// warnAfterFocus: [Function: warnAfterFocus],\n// output: [Function: output],\n// href: ''\n// }\n// const calco = globalThis.calculate || module.exports;\n// (null) = try Node CLI args, if that fails then will attempt querystring\n// console.log( calco(null) );\n//# sourceMappingURL=calc.js.map","// browser.ts: defer-loaded after \"calc.ts\"\nvar calculate;\nvar qi = function (id) { return document.getElementById(id); };\nvar num1El = qi(\"num1\");\nvar operatorEl = qi(\"operator\");\nvar num2El = qi(\"num2\");\nvar resultEl = qi(\"result\");\nvar calcEl = qi(\"calc-button\");\ncalculate.getFieldsData = function () {\n var num1 = num1El.value;\n var operator = operatorEl.value;\n var num2 = num2El.value;\n return { num1: num1, operator: operator, num2: num2 };\n};\ncalculate.setFieldsData = function (num1, operator, num2) {\n num1El.value = num1;\n operatorEl.value = operator;\n num2El.value = num2;\n};\ncalculate.warnAfterFocus = function (message) {\n operatorEl.focus();\n alert(message);\n};\ncalculate.output = function (result) {\n resultEl.value = result;\n resultEl.focus();\n resultEl.select();\n};\ncalculate.href = location.href.replace(location.hash, \"\");\nvar main = function (window, setTimeout, delay) {\n calcEl.onclick = function () { return calculate(); };\n // auto-calculate based on getArgsData only (silently skipped if no args passed)\n window.onload = setTimeout(function () {\n calculate(null);\n }, delay);\n};\nmain(window, setTimeout, 100); // ensure time enough that HTML fields are visible\n//# sourceMappingURL=browser.js.map"],"names":["root","factory","define","amd","module","exports","calculate","self","globalThis","data","num2","result","Array","isArray","nodeArgsMock","href","_a","_b","_c","_d","getNodeArgsData","test","p","URL","searchParams","setFieldsData","get","num1","getFieldsData","operator","parseFloat","String","fn","to the power of","Math","pow","plus","minus","times","divided by","output","warnAfterFocus","fieldsMock","argv","flat","process","slice","args","length","join","replace","trim","hasNamedArgs","orderedList","split","match","shift","concat","console","warn","message","error","log","qi","id","document","getElementById","num1El","operatorEl","num2El","resultEl","calcEl","main","value","focus","alert","select","location","hash","window","setTimeout","delay","onclick","onload"],"mappings":"AACA,CAAA,SAAWA,EAAMC,GACK,YAAlB,OAAOC,QAAyBA,OAAOC,IACjCD,OAAO,GAAID,CAAO,EACA,UAAlB,OAAOG,QAAuBA,OAAOC,QACjCD,OAAOC,QAAUJ,EAAQ,EACxBD,EAAKM,UAAYL,EAAQ,CACvC,EAAkB,aAAhB,OAAOM,KAAuBA,KAAOC,WAAY,WAChD,IAAIF,EAAY,SAAUG,GACtB,IAsBAC,EAaIC,EA7BJ,GALoB,UAAhB,OAAOF,GAAqBG,CAAAA,MAAMC,QAAQJ,CAAI,IAC9CK,EAAeL,EACfA,EAAO,MAGE,OAATA,GAEa,QADbA,EAmDU,SAAUM,EAAMD,GAC9B,IAAIE,EAAIC,EAAIC,EAAIC,EAEhB,GADAJ,GAAQC,EAAKI,EAAgBN,CAAY,KAAO,MAAQE,IAAO,KAAK,EAAIA,EAAKD,EACzE,CAAC,KAAKM,KAAKN,CAAI,EACf,OAAO,KAEX,IAAIO,EAAI,IAAIC,IAAIR,CAAI,EAAES,aACtBlB,EAAUmB,eAAeR,EAAKK,EAAEI,IAAI,MAAM,KAAO,MAAQT,IAAO,KAAK,EAAIA,EAAK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,UAAUmB,cAAgB,SAAUE,EAAME,EAAUnB,GAChDyD,OAAOM,MAAQ9C,EACfyC,WAAWK,MAAQ5C,EACnBwC,OAAOI,MAAQ/D,CACnB,EACAJ,UAAUmC,eAAiB,SAAUmB,GACjCQ,WAAWM,MAAM,EACjBC,MAAMf,CAAO,CACjB,EACAtD,UAAUkC,OAAS,SAAU7B,GACzB2D,SAASG,MAAQ9D,EACjB2D,SAASI,MAAM,EACfJ,SAASM,OAAO,CACpB,EACAtE,UAAUS,KAAO8D,SAAS9D,KAAKmC,QAAQ2B,SAASC,KAAM,EAAE,EAC7C,SAAUC,EAAQC,EAAYC,GACrCV,OAAOW,QAAU,WAAc,OAAO5E,UAAU,CAAG,EAEnDyE,EAAOI,OAASH,EAAW,WACvB1E,UAAU,IAAI,CAClB,EAAG2E,CAAK,CACZ,GACAT,KAAKO,OAAQC,WAAY,GAAG"} \ No newline at end of file diff --git a/index.html b/index.html index 2396f38..7db8537 100644 --- a/index.html +++ b/index.html @@ -5,7 +5,7 @@