diff --git a/index.1dc4dbca.css b/index.1dc4dbca.css new file mode 100644 index 0000000..7b1434a --- /dev/null +++ b/index.1dc4dbca.css @@ -0,0 +1,2 @@ +.green{color:#4caf50!important}.red{color:#f44336!important}form{width:80%;position:fixed;top:50%;left:50%;transform:translate(-50%,-50%)}.mdl-textfield{width:100%}.mdl-textfield__input,.mdl-textfield__label,.npc-result{letter-spacing:-.02em;text-align:center;outline:none;font-family:Roboto,Helvetica,Arial,sans-serif;font-size:8vmin;line-height:1}.mdl-spinner{width:20px;height:20px}@media only screen and (min-width:480px){.mdl-spinner{width:5vmin;height:5vmin}}.mdl-spinner,.npc-result{margin:0 auto;position:absolute;left:0;right:0}#npc-result-text,#npc-result-icon{color:#bbb;vertical-align:middle;font-size:6vmin}#npc-result-text{cursor:default;text-decoration:none}#npc-result-text.hover:hover{color:#aaa;cursor:pointer;border-bottom:1px solid;text-decoration:none} +/*# sourceMappingURL=index.1dc4dbca.css.map */ diff --git a/index.1dc4dbca.css.map b/index.1dc4dbca.css.map new file mode 100644 index 0000000..6ea6d55 --- /dev/null +++ b/index.1dc4dbca.css.map @@ -0,0 +1 @@ +{"mappings":"ACcA,+BAKA,6BASA,8EASA,0BAKA,yLAYA,oCAKA,yCACE,uCAOF,wEASA,mFAQA,qDAKA","sources":["index.1dc4dbca.css","src/style.css"],"sourcesContent":[".green {\n color: #4caf50 !important;\n}\n\n.red {\n color: #f44336 !important;\n}\n\nform {\n width: 80%;\n position: fixed;\n top: 50%;\n left: 50%;\n transform: translate(-50%, -50%);\n}\n\n.mdl-textfield {\n width: 100%;\n}\n\n.mdl-textfield__input, .mdl-textfield__label, .npc-result {\n letter-spacing: -.02em;\n text-align: center;\n outline: none;\n font-family: Roboto, Helvetica, Arial, sans-serif;\n font-size: 8vmin;\n line-height: 1;\n}\n\n.mdl-spinner {\n width: 20px;\n height: 20px;\n}\n\n@media only screen and (width >= 480px) {\n .mdl-spinner {\n width: 5vmin;\n height: 5vmin;\n }\n}\n\n.mdl-spinner, .npc-result {\n margin: 0 auto;\n position: absolute;\n left: 0;\n right: 0;\n}\n\n#npc-result-text, #npc-result-icon {\n color: #bbb;\n vertical-align: middle;\n font-size: 6vmin;\n}\n\n#npc-result-text {\n cursor: default;\n text-decoration: none;\n}\n\n#npc-result-text.hover:hover {\n color: #aaa;\n cursor: pointer;\n border-bottom: 1px solid;\n text-decoration: none;\n}\n/*# sourceMappingURL=index.1dc4dbca.css.map */\n","/**\n * Styles from Material Design Lite:\n * https://getmdl.io/components/index.html\n *\n * Icons from Material Icons:\n * https://google.github.io/material-design-icons/\n */\n@charset \"UTF-8\";\n\n/* ==========================================================================\n Colors\n ========================================================================== */\n\n/* success */\n.green {\n color: #4caf50 !important;\n}\n\n/* failure */\n.red {\n color: #f44336 !important;\n}\n\n/* ==========================================================================\n Main styles and overrides\n ========================================================================== */\n\n/* center input */\nform {\n position: fixed;\n top: 50%;\n left: 50%;\n transform: translate(-50%, -50%);\n width: 80%;\n}\n\n/* input takes full width of form */\n.mdl-textfield {\n width: 100%;\n}\n\n/* styling for input, label, and result */\n.mdl-textfield__input,\n.mdl-textfield__label,\n.npc-result {\n font-family: Roboto, Helvetica, Arial, sans-serif;\n font-size: 8vmin;\n letter-spacing: -0.02em;\n line-height: 1;\n outline: none;\n text-align: center;\n}\n\n/* loader */\n.mdl-spinner {\n width: 20px;\n height: 20px;\n}\n\n@media only screen and (min-width: 480px) {\n .mdl-spinner {\n width: 5vmin;\n height: 5vmin;\n }\n}\n\n/* styling for loader and result */\n.mdl-spinner,\n.npc-result {\n position: absolute;\n margin: 0 auto;\n right: 0;\n left: 0;\n}\n\n/* result text and icon */\n#npc-result-text,\n#npc-result-icon {\n color: #bbb;\n font-size: 6vmin;\n vertical-align: middle;\n}\n\n/* result text link */\n#npc-result-text {\n cursor: default;\n text-decoration: none;\n}\n\n#npc-result-text.hover:hover {\n color: #aaa;\n cursor: pointer;\n border-bottom: 1px solid;\n text-decoration: none;\n}\n"],"names":[],"version":3,"file":"index.1dc4dbca.css.map"} \ No newline at end of file diff --git a/index.7ba6dd4d.js b/index.7ba6dd4d.js new file mode 100644 index 0000000..4bb7e1e --- /dev/null +++ b/index.7ba6dd4d.js @@ -0,0 +1,2 @@ +!function(){let e,t;function n(e,t){return -1===e.className.indexOf(t)&&(e.className+=" "+t),e}function r(e,t){return e.className.indexOf(t)>-1&&(e.className=e.className.replace(t,"").trim()),e}function a(e,t,n){if(!e)throw Error("The first argument must be an element");return e[t]!==n&&(e[t]=n),e}function c(e,t){switch(t){case"error":r(e,"green"),n(e,"red"),a(e,"textContent","cancel");break;case"success":r(e,"red"),n(e,"green"),a(e,"textContent","check_circle");break;case"broken":r(e,"green"),n(e,"red"),a(e,"textContent","report_problem");break;default:r(e,"red"),r(e,"green"),a(e,"textContent","search")}}let o=document.getElementById("npc-package-name"),s=document.getElementById("npc-loading"),i=document.getElementById("npc-result-text"),l=document.getElementById("npc-result-icon");o.addEventListener("keyup",function(){var u;let m=o.value.toLowerCase();if(!m){r(s,"is-active"),a(i,"textContent",""),c(l,"default"),t="";return}if(m!==t){if(t=m,!/^[a-zA-Z0-9_-]+$/.test(m)||"_"===m[0]){r(s,"is-active"),a(i,"textContent","Invalid name."),c(l,"error");return}a(i,"textContent",""),a(l,"textContent",""),n(s,"is-active"),(u=async()=>{let e,t;try{e=await fetch(`https://corsmirror.com/v1?url=https://registry.npmjs.com/${encodeURIComponent(m)}`,{headers:{"Content-Type":"application/json"}}),t=await e.json(),a(i,"textContent","Name is taken."),a(i,"href",`https://www.npmjs.com/package/${m}`),a(i,"target","_blank"),n(i,"hover"),c(l,"error")}catch(e){e.status>=500&&(a(i,"textContent","Server error."),c(l,"broken"),console.error(e))}r(s,"is-active"),(e?.status===404||t?.time?.unpublished)&&(a(i,"textContent","Name is available."),c(l,"success"),a(i,"href","#"),a(i,"target",""),r(i,"hover"))},function(...t){clearTimeout(e),e=setTimeout(()=>{u.apply(this,t)},300)})()}},!1)}(); +//# sourceMappingURL=index.7ba6dd4d.js.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,EACvB,EDtH0C,ICuH5C,KFRF,EArFgD,CAAA,E","sources":["","src/main.ts","src/constants.ts","src/utils.ts"],"sourcesContent":["(function () {\nconst $c619454d2d0897a2$export$6fb729463a74e23 = 'https://www.npmjs.com/package';\nconst $c619454d2d0897a2$var$NPM_REGISTRY_URL = 'https://registry.npmjs.com';\nconst $c619454d2d0897a2$export$2e74004b635d08aa = `https://corsmirror.com/v1?url=${$c619454d2d0897a2$var$NPM_REGISTRY_URL}`;\nconst $c619454d2d0897a2$export$a3c4df769e264cca = 300;\n\n\n/**\n * Adds class to DOM element.\n *\n * @param element - The element.\n * @param className - The class.\n * @returns - The element.\n */ function $edb0d2ac61c14a64$export$d2cf6cd1dc7067d3(element, className) {\n if (element.className.indexOf(className) === -1) element.className += ' ' + className;\n return element;\n}\nfunction $edb0d2ac61c14a64$export$c2255604a80b4506(element, className) {\n if (element.className.indexOf(className) > -1) element.className = element.className.replace(className, '').trim();\n return element;\n}\nfunction $edb0d2ac61c14a64$export$a41c68a4eb5ff164(element, property, value) {\n if (!element) throw new Error('The first argument must be an element');\n /* eslint-disable @typescript-eslint/ban-ts-comment */ // @ts-ignore\n if (element[property] !== value) // @ts-ignore\n element[property] = value;\n /* eslint-enable @typescript-eslint/ban-ts-comment */ return element;\n}\nfunction $edb0d2ac61c14a64$export$307fb6e7cd9f153f(packageName) {\n if (/^[a-zA-Z0-9_-]+$/.test(packageName)) return packageName[0] !== '_';\n return false;\n}\nfunction $edb0d2ac61c14a64$export$8c7c179b1e9895e(element, type) {\n switch(type){\n case 'error':\n $edb0d2ac61c14a64$export$c2255604a80b4506(element, 'green');\n $edb0d2ac61c14a64$export$d2cf6cd1dc7067d3(element, 'red');\n $edb0d2ac61c14a64$export$a41c68a4eb5ff164(element, 'textContent', 'cancel');\n break;\n case 'success':\n $edb0d2ac61c14a64$export$c2255604a80b4506(element, 'red');\n $edb0d2ac61c14a64$export$d2cf6cd1dc7067d3(element, 'green');\n $edb0d2ac61c14a64$export$a41c68a4eb5ff164(element, 'textContent', 'check_circle');\n break;\n case 'broken':\n $edb0d2ac61c14a64$export$c2255604a80b4506(element, 'green');\n $edb0d2ac61c14a64$export$d2cf6cd1dc7067d3(element, 'red');\n $edb0d2ac61c14a64$export$a41c68a4eb5ff164(element, 'textContent', 'report_problem');\n break;\n default:\n $edb0d2ac61c14a64$export$c2255604a80b4506(element, 'red');\n $edb0d2ac61c14a64$export$c2255604a80b4506(element, 'green');\n $edb0d2ac61c14a64$export$a41c68a4eb5ff164(element, 'textContent', 'search');\n break;\n }\n}\nlet $edb0d2ac61c14a64$var$timeout;\nfunction $edb0d2ac61c14a64$export$61fc7d43ac8f84b0(callback, delay) {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return function(...args) {\n clearTimeout($edb0d2ac61c14a64$var$timeout);\n $edb0d2ac61c14a64$var$timeout = setTimeout(()=>{\n callback.apply(this, args);\n }, delay);\n };\n}\n\n\n// cache DOM nodes\nconst $02b2fc1f0fd6e808$var$inputElement = document.getElementById('npc-package-name');\n/* eslint-disable @typescript-eslint/no-non-null-assertion */ const $02b2fc1f0fd6e808$var$loadingElement = document.getElementById('npc-loading');\nconst $02b2fc1f0fd6e808$var$resultTextElement = document.getElementById('npc-result-text');\nconst $02b2fc1f0fd6e808$var$resultIconElement = document.getElementById('npc-result-icon');\n/* eslint-enable @typescript-eslint/no-non-null-assertion */ // store input value\nlet $02b2fc1f0fd6e808$var$inputValue;\n// check name when it is typed (with a debounce)\n$02b2fc1f0fd6e808$var$inputElement.addEventListener('keyup', $02b2fc1f0fd6e808$var$onKeyup, false);\n/**\n * Handles `keyup` event.\n *\n * @param event - The event handler.\n */ function $02b2fc1f0fd6e808$var$onKeyup() {\n const packageName = $02b2fc1f0fd6e808$var$inputElement.value.toLowerCase();\n // blank input\n if (!packageName) {\n (0, $edb0d2ac61c14a64$export$c2255604a80b4506)($02b2fc1f0fd6e808$var$loadingElement, 'is-active');\n (0, $edb0d2ac61c14a64$export$a41c68a4eb5ff164)($02b2fc1f0fd6e808$var$resultTextElement, 'textContent', '');\n (0, $edb0d2ac61c14a64$export$8c7c179b1e9895e)($02b2fc1f0fd6e808$var$resultIconElement, 'default');\n $02b2fc1f0fd6e808$var$inputValue = '';\n return;\n }\n // check whether input has changed\n if (packageName === $02b2fc1f0fd6e808$var$inputValue) return;\n else $02b2fc1f0fd6e808$var$inputValue = packageName;\n // invalid package name\n if (!(0, $edb0d2ac61c14a64$export$307fb6e7cd9f153f)(packageName)) {\n (0, $edb0d2ac61c14a64$export$c2255604a80b4506)($02b2fc1f0fd6e808$var$loadingElement, 'is-active');\n (0, $edb0d2ac61c14a64$export$a41c68a4eb5ff164)($02b2fc1f0fd6e808$var$resultTextElement, 'textContent', 'Invalid name.');\n (0, $edb0d2ac61c14a64$export$8c7c179b1e9895e)($02b2fc1f0fd6e808$var$resultIconElement, 'error');\n return;\n }\n // clear result and display loading spinner\n (0, $edb0d2ac61c14a64$export$a41c68a4eb5ff164)($02b2fc1f0fd6e808$var$resultTextElement, 'textContent', '');\n (0, $edb0d2ac61c14a64$export$a41c68a4eb5ff164)($02b2fc1f0fd6e808$var$resultIconElement, 'textContent', '');\n (0, $edb0d2ac61c14a64$export$d2cf6cd1dc7067d3)($02b2fc1f0fd6e808$var$loadingElement, 'is-active');\n (0, $edb0d2ac61c14a64$export$61fc7d43ac8f84b0)(async ()=>{\n let response;\n let data;\n try {\n response = await fetch(`${(0, $c619454d2d0897a2$export$2e74004b635d08aa)}/${encodeURIComponent(packageName)}`, {\n headers: {\n 'Content-Type': 'application/json'\n }\n });\n data = await response.json();\n // package name is taken\n (0, $edb0d2ac61c14a64$export$a41c68a4eb5ff164)($02b2fc1f0fd6e808$var$resultTextElement, 'textContent', 'Name is taken.');\n (0, $edb0d2ac61c14a64$export$a41c68a4eb5ff164)($02b2fc1f0fd6e808$var$resultTextElement, 'href', `${(0, $c619454d2d0897a2$export$6fb729463a74e23)}/${packageName}`);\n (0, $edb0d2ac61c14a64$export$a41c68a4eb5ff164)($02b2fc1f0fd6e808$var$resultTextElement, 'target', '_blank');\n (0, $edb0d2ac61c14a64$export$d2cf6cd1dc7067d3)($02b2fc1f0fd6e808$var$resultTextElement, 'hover');\n (0, $edb0d2ac61c14a64$export$8c7c179b1e9895e)($02b2fc1f0fd6e808$var$resultIconElement, 'error');\n } catch (error) {\n if (error.status >= 500) {\n (0, $edb0d2ac61c14a64$export$a41c68a4eb5ff164)($02b2fc1f0fd6e808$var$resultTextElement, 'textContent', 'Server error.');\n (0, $edb0d2ac61c14a64$export$8c7c179b1e9895e)($02b2fc1f0fd6e808$var$resultIconElement, 'broken');\n // eslint-disable-next-line no-console\n console.error(error);\n }\n }\n // remove loading spinner\n (0, $edb0d2ac61c14a64$export$c2255604a80b4506)($02b2fc1f0fd6e808$var$loadingElement, 'is-active');\n // package is available or unpublished\n if (response?.status === 404 || data?.time?.unpublished) {\n (0, $edb0d2ac61c14a64$export$a41c68a4eb5ff164)($02b2fc1f0fd6e808$var$resultTextElement, 'textContent', 'Name is available.');\n (0, $edb0d2ac61c14a64$export$8c7c179b1e9895e)($02b2fc1f0fd6e808$var$resultIconElement, 'success');\n (0, $edb0d2ac61c14a64$export$a41c68a4eb5ff164)($02b2fc1f0fd6e808$var$resultTextElement, 'href', '#');\n (0, $edb0d2ac61c14a64$export$a41c68a4eb5ff164)($02b2fc1f0fd6e808$var$resultTextElement, 'target', '');\n (0, $edb0d2ac61c14a64$export$c2255604a80b4506)($02b2fc1f0fd6e808$var$resultTextElement, 'hover');\n }\n }, (0, $c619454d2d0897a2$export$a3c4df769e264cca))();\n}\n\n})();\n//# sourceMappingURL=index.7ba6dd4d.js.map\n","import {\n DEBOUNCE_DELAY_IN_MILLISECONDS,\n NPM_PACKAGE_URL,\n REQUEST_URL,\n} from './constants';\nimport {\n addClass,\n debounce,\n isValidPackageName,\n removeClass,\n setProperty,\n setResultIcon,\n} from './utils';\n\n// cache DOM nodes\nconst inputElement = document.getElementById(\n 'npc-package-name',\n) as HTMLInputElement;\n\n/* eslint-disable @typescript-eslint/no-non-null-assertion */\nconst loadingElement = document.getElementById('npc-loading')!;\nconst resultTextElement = document.getElementById('npc-result-text')!;\nconst resultIconElement = document.getElementById('npc-result-icon')!;\n/* eslint-enable @typescript-eslint/no-non-null-assertion */\n\n// store input value\nlet inputValue: string;\n\n// check name when it is typed (with a debounce)\ninputElement.addEventListener('keyup', onKeyup, false);\n\n/**\n * Handles `keyup` event.\n *\n * @param event - The event handler.\n */\nfunction onKeyup(): void {\n const packageName = inputElement.value.toLowerCase();\n\n // blank input\n if (!packageName) {\n removeClass(loadingElement, 'is-active');\n setProperty(resultTextElement, 'textContent', '');\n setResultIcon(resultIconElement, 'default');\n inputValue = '';\n return;\n }\n\n // check whether input has changed\n if (packageName === inputValue) {\n return;\n } else {\n inputValue = packageName;\n }\n\n // invalid package name\n if (!isValidPackageName(packageName)) {\n removeClass(loadingElement, 'is-active');\n setProperty(resultTextElement, 'textContent', 'Invalid name.');\n setResultIcon(resultIconElement, 'error');\n return;\n }\n\n // clear result and display loading spinner\n setProperty(resultTextElement, 'textContent', '');\n setProperty(resultIconElement, 'textContent', '');\n addClass(loadingElement, 'is-active');\n\n debounce(async () => {\n let response;\n let data;\n\n try {\n response = await fetch(\n `${REQUEST_URL}/${encodeURIComponent(packageName)}`,\n {\n headers: {\n 'Content-Type': 'application/json',\n },\n },\n );\n data = await response.json();\n\n // package name is taken\n setProperty(resultTextElement, 'textContent', 'Name is taken.');\n setProperty(\n resultTextElement,\n 'href',\n `${NPM_PACKAGE_URL}/${packageName}`,\n );\n setProperty(resultTextElement, 'target', '_blank');\n addClass(resultTextElement, 'hover');\n setResultIcon(resultIconElement, 'error');\n } catch (error) {\n if (error.status >= 500) {\n setProperty(resultTextElement, 'textContent', 'Server error.');\n setResultIcon(resultIconElement, 'broken');\n // eslint-disable-next-line no-console\n console.error(error);\n }\n }\n\n // remove loading spinner\n removeClass(loadingElement, 'is-active');\n\n // package is available or unpublished\n if (response?.status === 404 || data?.time?.unpublished) {\n setProperty(resultTextElement, 'textContent', 'Name is available.');\n setResultIcon(resultIconElement, 'success');\n setProperty(resultTextElement, 'href', '#');\n setProperty(resultTextElement, 'target', '');\n removeClass(resultTextElement, 'hover');\n }\n }, DEBOUNCE_DELAY_IN_MILLISECONDS)();\n}\n","export const NPM_PACKAGE_URL = 'https://www.npmjs.com/package';\nconst NPM_REGISTRY_URL = 'https://registry.npmjs.com';\nexport const REQUEST_URL = `https://corsmirror.com/v1?url=${NPM_REGISTRY_URL}`;\nexport const DEBOUNCE_DELAY_IN_MILLISECONDS = 300;\n","/**\n * Adds class to DOM element.\n *\n * @param element - The element.\n * @param className - The class.\n * @returns - The element.\n */\nexport function addClass(element: HTMLElement, className: string) {\n if (element.className.indexOf(className) === -1) {\n element.className += ' ' + className;\n }\n return element;\n}\n\n/**\n * Removes class from DOM element.\n *\n * @param element - The element.\n * @param className - The class.\n * @returns - The element.\n */\nexport function removeClass(element: HTMLElement, className: string) {\n if (element.className.indexOf(className) > -1) {\n element.className = element.className.replace(className, '').trim();\n }\n return element;\n}\n\n/**\n * Sets property for DOM element.\n *\n * @param element - The element.\n * @param property - The property.\n * @param value - The value.\n * @returns - The element.\n */\nexport function setProperty(\n element: HTMLElement,\n property: keyof HTMLElement | keyof HTMLAnchorElement,\n value: string,\n) {\n if (!element) {\n throw new Error('The first argument must be an element');\n }\n\n /* eslint-disable @typescript-eslint/ban-ts-comment */\n // @ts-ignore\n if (element[property] !== value) {\n // @ts-ignore\n element[property] = value;\n }\n /* eslint-enable @typescript-eslint/ban-ts-comment */\n\n return element;\n}\n\n/**\n * Check if npm package name is valid.\n *\n * https://github.com/npm/validate-npm-package-name#naming-rules\n *\n * @param packageName - The package name.\n */\nexport function isValidPackageName(packageName: string): boolean {\n if (/^[a-zA-Z0-9_-]+$/.test(packageName)) {\n return packageName[0] !== '_';\n }\n return false;\n}\n\n/**\n * Changes result icon display type (success, error, default).\n *\n * @param element - The element.\n * @param type - The display type.\n */\nexport function setResultIcon(element: HTMLElement, type: string): void {\n switch (type) {\n case 'error':\n removeClass(element, 'green');\n addClass(element, 'red');\n setProperty(element, 'textContent', 'cancel');\n break;\n\n case 'success':\n removeClass(element, 'red');\n addClass(element, 'green');\n setProperty(element, 'textContent', 'check_circle');\n break;\n\n case 'broken':\n removeClass(element, 'green');\n addClass(element, 'red');\n setProperty(element, 'textContent', 'report_problem');\n break;\n\n default:\n removeClass(element, 'red');\n removeClass(element, 'green');\n setProperty(element, 'textContent', 'search');\n break;\n }\n}\n\nlet timeout: ReturnType;\n\n/**\n * Debounces a function call.\n *\n * https://remysharp.com/2010/07/21/throttling-function-calls\n *\n * @param callback - The function to debounce.\n * @param delay - The delay in milliseconds.\n * @returns - The debounced function.\n */\nexport function debounce(callback: () => void, delay?: number) {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return function (...args: any[]) {\n clearTimeout(timeout);\n timeout = setTimeout(() => {\n callback.apply(this, args);\n }, delay);\n };\n}\n"],"names":["$edb0d2ac61c14a64$var$timeout","$02b2fc1f0fd6e808$var$inputValue","$edb0d2ac61c14a64$export$d2cf6cd1dc7067d3","element","className","indexOf","$edb0d2ac61c14a64$export$c2255604a80b4506","replace","trim","$edb0d2ac61c14a64$export$a41c68a4eb5ff164","property","value","Error","$edb0d2ac61c14a64$export$8c7c179b1e9895e","type","$02b2fc1f0fd6e808$var$inputElement","document","getElementById","$02b2fc1f0fd6e808$var$loadingElement","$02b2fc1f0fd6e808$var$resultTextElement","$02b2fc1f0fd6e808$var$resultIconElement","addEventListener","callback","packageName","toLowerCase","test","response","data","fetch","encodeURIComponent","headers","json","error","status","console","time","unpublished","args","clearTimeout","setTimeout","apply"],"version":3,"file":"index.7ba6dd4d.js.map"} \ No newline at end of file diff --git a/index.9d94d6fb.js b/index.9d94d6fb.js new file mode 100644 index 0000000..fe88ca1 --- /dev/null +++ b/index.9d94d6fb.js @@ -0,0 +1,2 @@ +let e,t;function n(e,t){return -1===e.className.indexOf(t)&&(e.className+=" "+t),e}function r(e,t){return e.className.indexOf(t)>-1&&(e.className=e.className.replace(t,"").trim()),e}function a(e,t,n){if(!e)throw Error("The first argument must be an element");return e[t]!==n&&(e[t]=n),e}function c(e,t){switch(t){case"error":r(e,"green"),n(e,"red"),a(e,"textContent","cancel");break;case"success":r(e,"red"),n(e,"green"),a(e,"textContent","check_circle");break;case"broken":r(e,"green"),n(e,"red"),a(e,"textContent","report_problem");break;default:r(e,"red"),r(e,"green"),a(e,"textContent","search")}}const o=document.getElementById("npc-package-name"),s=document.getElementById("npc-loading"),i=document.getElementById("npc-result-text"),l=document.getElementById("npc-result-icon");o.addEventListener("keyup",function(){var m;let u=o.value.toLowerCase();if(!u){r(s,"is-active"),a(i,"textContent",""),c(l,"default"),t="";return}if(u!==t){if(t=u,!/^[a-zA-Z0-9_-]+$/.test(u)||"_"===u[0]){r(s,"is-active"),a(i,"textContent","Invalid name."),c(l,"error");return}a(i,"textContent",""),a(l,"textContent",""),n(s,"is-active"),(m=async()=>{let e,t;try{e=await fetch(`https://corsmirror.com/v1?url=https://registry.npmjs.com/${encodeURIComponent(u)}`,{headers:{"Content-Type":"application/json"}}),t=await e.json(),a(i,"textContent","Name is taken."),a(i,"href",`https://www.npmjs.com/package/${u}`),a(i,"target","_blank"),n(i,"hover"),c(l,"error")}catch(e){e.status>=500&&(a(i,"textContent","Server error."),c(l,"broken"),console.error(e))}r(s,"is-active"),(e?.status===404||t?.time?.unpublished)&&(a(i,"textContent","Name is available."),c(l,"success"),a(i,"href","#"),a(i,"target",""),r(i,"hover"))},function(...t){clearTimeout(e),e=setTimeout(()=>{m.apply(this,t)},300)})()}},!1); +//# sourceMappingURL=index.9d94d6fb.js.map diff --git a/index.9d94d6fb.js.map b/index.9d94d6fb.js.map new file mode 100644 index 0000000..6baccd7 --- /dev/null +++ b/index.9d94d6fb.js.map @@ -0,0 +1 @@ +{"mappings":"IGwGI,EF9EA,EEnBG,SAAS,EAAS,CAAoB,CAAE,CAAiB,EAI9D,OAH6C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AAI,CFRH,IESf,AAAmB,MAAnB,AFTe,CESJ,CAAC,EAAE,CFTe,CACpC,AAAA,EAAY,EAAgB,aAC5B,AAAA,EAAY,EAAmB,cAAe,iBAC9C,AAAA,EAAc,EAAmB,SACjC,MACF,CAGA,AAAA,EAAY,EAAmB,cAAe,IAC9C,AAAA,EAAY,EAAmB,cAAe,IAC9C,AAAA,EAAS,EAAgB,aAEzB,CE+CuB,EF/Cd,UACP,IAAI,EACA,EAEJ,GAAI,CACF,EAAW,MAAM,MACf,4DAAkB,mBAAmB,IAAc,CACnD,CACE,QAAS,CACP,eAAgB,kBAClB,CACF,GAEF,EAAO,MAAM,EAAS,IAAI,GAG1B,AAAA,EAAY,EAAmB,cAAe,kBAC9C,AAAA,EACE,EACA,OACA,iCAAsB,GAAa,EAErC,AAAA,EAAY,EAAmB,SAAU,UACzC,AAAA,EAAS,EAAmB,SAC5B,AAAA,EAAc,EAAmB,QACnC,CAAE,MAAO,EAAO,CACV,EAAM,MAAM,EAAI,MAClB,AAAA,EAAY,EAAmB,cAAe,iBAC9C,AAAA,EAAc,EAAmB,UAEjC,QAAQ,KAAK,CAAC,GAElB,CAGA,AAAA,EAAY,EAAgB,aAGxB,CAAA,GAAU,SAAW,KAAO,GAAM,MAAM,WAAA,IAC1C,AAAA,EAAY,EAAmB,cAAe,sBAC9C,AAAA,EAAc,EAAmB,WACjC,AAAA,EAAY,EAAmB,OAAQ,KACvC,AAAA,EAAY,EAAmB,SAAU,IACzC,AAAA,EAAY,EAAmB,SAEnC,EEIO,SAAU,GAAG,CAAW,EAC7B,aAAa,GACb,EAAU,WAAW,KACnB,EAAS,KAAK,CAAC,IAAI,CAAE,EACvB,EDtH0C,ICuH5C,KFRF,EArFgD,CAAA","sources":["","src/main.ts","src/constants.ts","src/utils.ts"],"sourcesContent":["const $19d416a1dabe61c4$export$6fb729463a74e23 = 'https://www.npmjs.com/package';\nconst $19d416a1dabe61c4$var$NPM_REGISTRY_URL = 'https://registry.npmjs.com';\nconst $19d416a1dabe61c4$export$2e74004b635d08aa = `https://corsmirror.com/v1?url=${$19d416a1dabe61c4$var$NPM_REGISTRY_URL}`;\nconst $19d416a1dabe61c4$export$a3c4df769e264cca = 300;\n\n\n/**\n * Adds class to DOM element.\n *\n * @param element - The element.\n * @param className - The class.\n * @returns - The element.\n */ function $d526951eb107ee9b$export$d2cf6cd1dc7067d3(element, className) {\n if (element.className.indexOf(className) === -1) element.className += ' ' + className;\n return element;\n}\nfunction $d526951eb107ee9b$export$c2255604a80b4506(element, className) {\n if (element.className.indexOf(className) > -1) element.className = element.className.replace(className, '').trim();\n return element;\n}\nfunction $d526951eb107ee9b$export$a41c68a4eb5ff164(element, property, value) {\n if (!element) throw new Error('The first argument must be an element');\n /* eslint-disable @typescript-eslint/ban-ts-comment */ // @ts-ignore\n if (element[property] !== value) // @ts-ignore\n element[property] = value;\n /* eslint-enable @typescript-eslint/ban-ts-comment */ return element;\n}\nfunction $d526951eb107ee9b$export$307fb6e7cd9f153f(packageName) {\n if (/^[a-zA-Z0-9_-]+$/.test(packageName)) return packageName[0] !== '_';\n return false;\n}\nfunction $d526951eb107ee9b$export$8c7c179b1e9895e(element, type) {\n switch(type){\n case 'error':\n $d526951eb107ee9b$export$c2255604a80b4506(element, 'green');\n $d526951eb107ee9b$export$d2cf6cd1dc7067d3(element, 'red');\n $d526951eb107ee9b$export$a41c68a4eb5ff164(element, 'textContent', 'cancel');\n break;\n case 'success':\n $d526951eb107ee9b$export$c2255604a80b4506(element, 'red');\n $d526951eb107ee9b$export$d2cf6cd1dc7067d3(element, 'green');\n $d526951eb107ee9b$export$a41c68a4eb5ff164(element, 'textContent', 'check_circle');\n break;\n case 'broken':\n $d526951eb107ee9b$export$c2255604a80b4506(element, 'green');\n $d526951eb107ee9b$export$d2cf6cd1dc7067d3(element, 'red');\n $d526951eb107ee9b$export$a41c68a4eb5ff164(element, 'textContent', 'report_problem');\n break;\n default:\n $d526951eb107ee9b$export$c2255604a80b4506(element, 'red');\n $d526951eb107ee9b$export$c2255604a80b4506(element, 'green');\n $d526951eb107ee9b$export$a41c68a4eb5ff164(element, 'textContent', 'search');\n break;\n }\n}\nlet $d526951eb107ee9b$var$timeout;\nfunction $d526951eb107ee9b$export$61fc7d43ac8f84b0(callback, delay) {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return function(...args) {\n clearTimeout($d526951eb107ee9b$var$timeout);\n $d526951eb107ee9b$var$timeout = setTimeout(()=>{\n callback.apply(this, args);\n }, delay);\n };\n}\n\n\n// cache DOM nodes\nconst $04177131cda29cd1$var$inputElement = document.getElementById('npc-package-name');\n/* eslint-disable @typescript-eslint/no-non-null-assertion */ const $04177131cda29cd1$var$loadingElement = document.getElementById('npc-loading');\nconst $04177131cda29cd1$var$resultTextElement = document.getElementById('npc-result-text');\nconst $04177131cda29cd1$var$resultIconElement = document.getElementById('npc-result-icon');\n/* eslint-enable @typescript-eslint/no-non-null-assertion */ // store input value\nlet $04177131cda29cd1$var$inputValue;\n// check name when it is typed (with a debounce)\n$04177131cda29cd1$var$inputElement.addEventListener('keyup', $04177131cda29cd1$var$onKeyup, false);\n/**\n * Handles `keyup` event.\n *\n * @param event - The event handler.\n */ function $04177131cda29cd1$var$onKeyup() {\n const packageName = $04177131cda29cd1$var$inputElement.value.toLowerCase();\n // blank input\n if (!packageName) {\n (0, $d526951eb107ee9b$export$c2255604a80b4506)($04177131cda29cd1$var$loadingElement, 'is-active');\n (0, $d526951eb107ee9b$export$a41c68a4eb5ff164)($04177131cda29cd1$var$resultTextElement, 'textContent', '');\n (0, $d526951eb107ee9b$export$8c7c179b1e9895e)($04177131cda29cd1$var$resultIconElement, 'default');\n $04177131cda29cd1$var$inputValue = '';\n return;\n }\n // check whether input has changed\n if (packageName === $04177131cda29cd1$var$inputValue) return;\n else $04177131cda29cd1$var$inputValue = packageName;\n // invalid package name\n if (!(0, $d526951eb107ee9b$export$307fb6e7cd9f153f)(packageName)) {\n (0, $d526951eb107ee9b$export$c2255604a80b4506)($04177131cda29cd1$var$loadingElement, 'is-active');\n (0, $d526951eb107ee9b$export$a41c68a4eb5ff164)($04177131cda29cd1$var$resultTextElement, 'textContent', 'Invalid name.');\n (0, $d526951eb107ee9b$export$8c7c179b1e9895e)($04177131cda29cd1$var$resultIconElement, 'error');\n return;\n }\n // clear result and display loading spinner\n (0, $d526951eb107ee9b$export$a41c68a4eb5ff164)($04177131cda29cd1$var$resultTextElement, 'textContent', '');\n (0, $d526951eb107ee9b$export$a41c68a4eb5ff164)($04177131cda29cd1$var$resultIconElement, 'textContent', '');\n (0, $d526951eb107ee9b$export$d2cf6cd1dc7067d3)($04177131cda29cd1$var$loadingElement, 'is-active');\n (0, $d526951eb107ee9b$export$61fc7d43ac8f84b0)(async ()=>{\n let response;\n let data;\n try {\n response = await fetch(`${(0, $19d416a1dabe61c4$export$2e74004b635d08aa)}/${encodeURIComponent(packageName)}`, {\n headers: {\n 'Content-Type': 'application/json'\n }\n });\n data = await response.json();\n // package name is taken\n (0, $d526951eb107ee9b$export$a41c68a4eb5ff164)($04177131cda29cd1$var$resultTextElement, 'textContent', 'Name is taken.');\n (0, $d526951eb107ee9b$export$a41c68a4eb5ff164)($04177131cda29cd1$var$resultTextElement, 'href', `${(0, $19d416a1dabe61c4$export$6fb729463a74e23)}/${packageName}`);\n (0, $d526951eb107ee9b$export$a41c68a4eb5ff164)($04177131cda29cd1$var$resultTextElement, 'target', '_blank');\n (0, $d526951eb107ee9b$export$d2cf6cd1dc7067d3)($04177131cda29cd1$var$resultTextElement, 'hover');\n (0, $d526951eb107ee9b$export$8c7c179b1e9895e)($04177131cda29cd1$var$resultIconElement, 'error');\n } catch (error) {\n if (error.status >= 500) {\n (0, $d526951eb107ee9b$export$a41c68a4eb5ff164)($04177131cda29cd1$var$resultTextElement, 'textContent', 'Server error.');\n (0, $d526951eb107ee9b$export$8c7c179b1e9895e)($04177131cda29cd1$var$resultIconElement, 'broken');\n // eslint-disable-next-line no-console\n console.error(error);\n }\n }\n // remove loading spinner\n (0, $d526951eb107ee9b$export$c2255604a80b4506)($04177131cda29cd1$var$loadingElement, 'is-active');\n // package is available or unpublished\n if (response?.status === 404 || data?.time?.unpublished) {\n (0, $d526951eb107ee9b$export$a41c68a4eb5ff164)($04177131cda29cd1$var$resultTextElement, 'textContent', 'Name is available.');\n (0, $d526951eb107ee9b$export$8c7c179b1e9895e)($04177131cda29cd1$var$resultIconElement, 'success');\n (0, $d526951eb107ee9b$export$a41c68a4eb5ff164)($04177131cda29cd1$var$resultTextElement, 'href', '#');\n (0, $d526951eb107ee9b$export$a41c68a4eb5ff164)($04177131cda29cd1$var$resultTextElement, 'target', '');\n (0, $d526951eb107ee9b$export$c2255604a80b4506)($04177131cda29cd1$var$resultTextElement, 'hover');\n }\n }, (0, $19d416a1dabe61c4$export$a3c4df769e264cca))();\n}\n\n\n//# sourceMappingURL=index.9d94d6fb.js.map\n","import {\n DEBOUNCE_DELAY_IN_MILLISECONDS,\n NPM_PACKAGE_URL,\n REQUEST_URL,\n} from './constants';\nimport {\n addClass,\n debounce,\n isValidPackageName,\n removeClass,\n setProperty,\n setResultIcon,\n} from './utils';\n\n// cache DOM nodes\nconst inputElement = document.getElementById(\n 'npc-package-name',\n) as HTMLInputElement;\n\n/* eslint-disable @typescript-eslint/no-non-null-assertion */\nconst loadingElement = document.getElementById('npc-loading')!;\nconst resultTextElement = document.getElementById('npc-result-text')!;\nconst resultIconElement = document.getElementById('npc-result-icon')!;\n/* eslint-enable @typescript-eslint/no-non-null-assertion */\n\n// store input value\nlet inputValue: string;\n\n// check name when it is typed (with a debounce)\ninputElement.addEventListener('keyup', onKeyup, false);\n\n/**\n * Handles `keyup` event.\n *\n * @param event - The event handler.\n */\nfunction onKeyup(): void {\n const packageName = inputElement.value.toLowerCase();\n\n // blank input\n if (!packageName) {\n removeClass(loadingElement, 'is-active');\n setProperty(resultTextElement, 'textContent', '');\n setResultIcon(resultIconElement, 'default');\n inputValue = '';\n return;\n }\n\n // check whether input has changed\n if (packageName === inputValue) {\n return;\n } else {\n inputValue = packageName;\n }\n\n // invalid package name\n if (!isValidPackageName(packageName)) {\n removeClass(loadingElement, 'is-active');\n setProperty(resultTextElement, 'textContent', 'Invalid name.');\n setResultIcon(resultIconElement, 'error');\n return;\n }\n\n // clear result and display loading spinner\n setProperty(resultTextElement, 'textContent', '');\n setProperty(resultIconElement, 'textContent', '');\n addClass(loadingElement, 'is-active');\n\n debounce(async () => {\n let response;\n let data;\n\n try {\n response = await fetch(\n `${REQUEST_URL}/${encodeURIComponent(packageName)}`,\n {\n headers: {\n 'Content-Type': 'application/json',\n },\n },\n );\n data = await response.json();\n\n // package name is taken\n setProperty(resultTextElement, 'textContent', 'Name is taken.');\n setProperty(\n resultTextElement,\n 'href',\n `${NPM_PACKAGE_URL}/${packageName}`,\n );\n setProperty(resultTextElement, 'target', '_blank');\n addClass(resultTextElement, 'hover');\n setResultIcon(resultIconElement, 'error');\n } catch (error) {\n if (error.status >= 500) {\n setProperty(resultTextElement, 'textContent', 'Server error.');\n setResultIcon(resultIconElement, 'broken');\n // eslint-disable-next-line no-console\n console.error(error);\n }\n }\n\n // remove loading spinner\n removeClass(loadingElement, 'is-active');\n\n // package is available or unpublished\n if (response?.status === 404 || data?.time?.unpublished) {\n setProperty(resultTextElement, 'textContent', 'Name is available.');\n setResultIcon(resultIconElement, 'success');\n setProperty(resultTextElement, 'href', '#');\n setProperty(resultTextElement, 'target', '');\n removeClass(resultTextElement, 'hover');\n }\n }, DEBOUNCE_DELAY_IN_MILLISECONDS)();\n}\n","export const NPM_PACKAGE_URL = 'https://www.npmjs.com/package';\nconst NPM_REGISTRY_URL = 'https://registry.npmjs.com';\nexport const REQUEST_URL = `https://corsmirror.com/v1?url=${NPM_REGISTRY_URL}`;\nexport const DEBOUNCE_DELAY_IN_MILLISECONDS = 300;\n","/**\n * Adds class to DOM element.\n *\n * @param element - The element.\n * @param className - The class.\n * @returns - The element.\n */\nexport function addClass(element: HTMLElement, className: string) {\n if (element.className.indexOf(className) === -1) {\n element.className += ' ' + className;\n }\n return element;\n}\n\n/**\n * Removes class from DOM element.\n *\n * @param element - The element.\n * @param className - The class.\n * @returns - The element.\n */\nexport function removeClass(element: HTMLElement, className: string) {\n if (element.className.indexOf(className) > -1) {\n element.className = element.className.replace(className, '').trim();\n }\n return element;\n}\n\n/**\n * Sets property for DOM element.\n *\n * @param element - The element.\n * @param property - The property.\n * @param value - The value.\n * @returns - The element.\n */\nexport function setProperty(\n element: HTMLElement,\n property: keyof HTMLElement | keyof HTMLAnchorElement,\n value: string,\n) {\n if (!element) {\n throw new Error('The first argument must be an element');\n }\n\n /* eslint-disable @typescript-eslint/ban-ts-comment */\n // @ts-ignore\n if (element[property] !== value) {\n // @ts-ignore\n element[property] = value;\n }\n /* eslint-enable @typescript-eslint/ban-ts-comment */\n\n return element;\n}\n\n/**\n * Check if npm package name is valid.\n *\n * https://github.com/npm/validate-npm-package-name#naming-rules\n *\n * @param packageName - The package name.\n */\nexport function isValidPackageName(packageName: string): boolean {\n if (/^[a-zA-Z0-9_-]+$/.test(packageName)) {\n return packageName[0] !== '_';\n }\n return false;\n}\n\n/**\n * Changes result icon display type (success, error, default).\n *\n * @param element - The element.\n * @param type - The display type.\n */\nexport function setResultIcon(element: HTMLElement, type: string): void {\n switch (type) {\n case 'error':\n removeClass(element, 'green');\n addClass(element, 'red');\n setProperty(element, 'textContent', 'cancel');\n break;\n\n case 'success':\n removeClass(element, 'red');\n addClass(element, 'green');\n setProperty(element, 'textContent', 'check_circle');\n break;\n\n case 'broken':\n removeClass(element, 'green');\n addClass(element, 'red');\n setProperty(element, 'textContent', 'report_problem');\n break;\n\n default:\n removeClass(element, 'red');\n removeClass(element, 'green');\n setProperty(element, 'textContent', 'search');\n break;\n }\n}\n\nlet timeout: ReturnType;\n\n/**\n * Debounces a function call.\n *\n * https://remysharp.com/2010/07/21/throttling-function-calls\n *\n * @param callback - The function to debounce.\n * @param delay - The delay in milliseconds.\n * @returns - The debounced function.\n */\nexport function debounce(callback: () => void, delay?: number) {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return function (...args: any[]) {\n clearTimeout(timeout);\n timeout = setTimeout(() => {\n callback.apply(this, args);\n }, delay);\n };\n}\n"],"names":["$d526951eb107ee9b$var$timeout","$04177131cda29cd1$var$inputValue","$d526951eb107ee9b$export$d2cf6cd1dc7067d3","element","className","indexOf","$d526951eb107ee9b$export$c2255604a80b4506","replace","trim","$d526951eb107ee9b$export$a41c68a4eb5ff164","property","value","Error","$d526951eb107ee9b$export$8c7c179b1e9895e","type","$04177131cda29cd1$var$inputElement","document","getElementById","$04177131cda29cd1$var$loadingElement","$04177131cda29cd1$var$resultTextElement","$04177131cda29cd1$var$resultIconElement","addEventListener","callback","packageName","toLowerCase","test","response","data","fetch","encodeURIComponent","headers","json","error","status","console","time","unpublished","args","clearTimeout","setTimeout","apply"],"version":3,"file":"index.9d94d6fb.js.map"} \ No newline at end of file diff --git a/index.html b/index.html new file mode 100644 index 0000000..ab47ef2 --- /dev/null +++ b/index.html @@ -0,0 +1 @@ +npm package name checker
search
\ No newline at end of file