From c1142e121bd6642ea3827de382e5c962864028e7 Mon Sep 17 00:00:00 2001 From: bjarneo Date: Tue, 9 Apr 2024 20:38:30 +0200 Subject: [PATCH] BREAKING CHANGE: use ESM only --- README.md | 16 ++++------------ dist/extract-domain.cjs | 2 -- dist/extract-domain.cjs.map | 1 - dist/extract-domain.modern.js | 2 -- dist/extract-domain.modern.js.map | 1 - dist/extract-domain.module.js.map | 2 +- package-lock.json | 4 ++-- package.json | 9 +++------ tests/index.test.js | 2 +- 9 files changed, 11 insertions(+), 28 deletions(-) delete mode 100644 dist/extract-domain.cjs delete mode 100644 dist/extract-domain.cjs.map delete mode 100644 dist/extract-domain.modern.js delete mode 100644 dist/extract-domain.modern.js.map diff --git a/README.md b/README.md index 1e7e1d7..6a81e45 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,9 @@ Learn more about [What is a URL](https://developer.mozilla.org/en-US/docs/Learn/ ## Supports -Both the browser and nodejs. +- ESM +- Node.js +- Browser ## Usage @@ -35,22 +37,12 @@ bun test:watch - @param {GetDomainOptions} opts `{ tld: true }` permit to get Top Level Domain like `*.co.uk` - @returns {Urls | Promise} Returns URL(s) or a promise of URL(s) if the PSL lib is being used -```js -const res = extractDomain(urls, options); -``` - -ES6 Import +ESM import ```js import extractDomain from 'extract-domain'; ``` -CommonJS Require - -```js -const extractDomain = require('extract-domain'); -``` - Examples ```js diff --git a/dist/extract-domain.cjs b/dist/extract-domain.cjs deleted file mode 100644 index 522318e..0000000 --- a/dist/extract-domain.cjs +++ /dev/null @@ -1,2 +0,0 @@ -function r(r){if(r&&r.__esModule)return r;var e=Object.create(null);return r&&Object.keys(r).forEach(function(t){if("default"!==t){var n=Object.getOwnPropertyDescriptor(r,t);Object.defineProperty(e,t,n.get?n:{enumerable:!0,get:function(){return r[t]}})}}),e.default=r,e}var e=["/",":","?","#"],t=[".","/","@"];function n(n,o){for(var i,u=0,f=0,s=0,a=n.length,c=0;a--&&++c&&!(u&&e.indexOf(n[c])>-1);)"."===n[c]&&(++u,f=c);for(i=c,c=f;c--;)if(-1!==t.indexOf(n[c])){s=c+1;break}if(0===s&&i>3)return n;if(s>0&&s<2)return"";if(o.tld){for(var l=0,p=["/","@"],v=f;v--;)if(p.indexOf(n[v])>-1){l=v+1;break}return Promise.resolve().then(function(){/*#__PURE__*/return r(require("psl"))}).then(function(r){return Promise.resolve(r.get(n.slice(l,i)))}).catch(function(r){throw console.error(r),Error("You must install psl library (https://www.npmjs.com/package/psl) to use `tld` option")})}return n.slice(s,i)}module.exports=function(r,e){if(void 0===e&&(e={}),"string"==typeof r)return n(r,e);if(Array.isArray(r)){for(var t=[],o=r.length,i=0;i;\ntype ReturnUrls = Array | Array>;\ntype ReturnUrl = Url | Promise;\n\n/**\n * Options to extract domain.\n */\ntype GetDomainOptions = {\n tld?: boolean;\n};\n\n/**\n * @param {Url} url\n * @param {GetDomainOptions} opts `{ tld: true }` permit to get Top Level Domain like `*.co.uk`\n * @returns {ReturnUrl} Returns a URL or a promise of a URL if the PSL lib is being used\n */\nfunction getDomainFromUrl(url: Url, opts: GetDomainOptions): ReturnUrl {\n let domainInc: number = 0;\n let offsetDomain: number = 0;\n let offsetStartSlice: number = 0;\n let offsetPath: number = 0;\n let len: number = url.length;\n let i: number = 0;\n\n // Find end offset of domain\n while (len-- && ++i) {\n if (domainInc && endings.indexOf(url[i]) > -1) {\n break;\n }\n\n if (url[i] !== '.') {\n continue;\n }\n\n ++domainInc;\n\n offsetDomain = i;\n }\n\n offsetPath = i;\n\n i = offsetDomain;\n\n // Find offset before domain name.\n while (i--) {\n // Look for sub domain, protocol or basic auth\n if (starters.indexOf(url[i]) === -1) {\n continue;\n }\n\n offsetStartSlice = i + 1;\n\n break;\n }\n\n if (offsetStartSlice === 0 && offsetPath > 3) {\n return url;\n }\n\n if (offsetStartSlice > 0 && offsetStartSlice < 2) {\n return '';\n }\n\n // Very customized if statement for tlds\n if (opts.tld) {\n let offsetStart: number = 0;\n const starters: Array = ['/', '@'];\n let i: number = offsetDomain;\n\n while (i--) {\n if (starters.indexOf(url[i]) > -1) {\n offsetStart = i + 1;\n\n break;\n }\n }\n\n return import('psl')\n .then((psl) => Promise.resolve(psl.get(url.slice(offsetStart, offsetPath))))\n .catch((error) => {\n console.error(error);\n\n throw Error(\n 'You must install psl library (https://www.npmjs.com/package/psl) to use `tld` option'\n );\n });\n }\n\n // Tried several approaches slicing a string. Can't get it any faster than this.\n return url.slice(offsetStartSlice, offsetPath);\n}\n\n/**\n * @param {Urls} urls [\"https://www.google.com\", \"https://www.github.com\"] or \"https://www.google.com\"\n * @param {GetDomainOptions} opts `{ tld: true }` permit to get Top Level Domain like `*.co.uk`\n * @returns {Urls | Promise} Return URLs or a promise of URLs if the PSL lib is being used\n */\nexport default function extractDomain(\n urls: Urls,\n opts: GetDomainOptions = {}\n): ReturnUrl | ReturnUrls | null {\n if (typeof urls === 'string') {\n return getDomainFromUrl(urls, opts);\n } else if (Array.isArray(urls)) {\n // lazy type checking (^o^)\n const extractedUrls: any = [];\n const len: number = urls.length;\n let i: number = 0;\n\n for (; i < len; i++) {\n const url = getDomainFromUrl(urls[i], opts);\n\n extractedUrls.push(url);\n }\n\n return extractedUrls;\n } else {\n throwTypeError();\n }\n}\n"],"names":["endings","starters","getDomainFromUrl","url","opts","offsetPath","domainInc","offsetDomain","offsetStartSlice","len","length","i","indexOf","tld","offsetStart","Promise","resolve","then","_interopNamespace","require","psl","get","slice","error","console","Error","urls","Array","isArray","extractedUrls","push","TypeError","throwTypeError"],"mappings":"8QAIA,IAAMA,EAAU,CAAC,IAAK,IAAK,IAAK,KAC1BC,EAAW,CAAC,IAAK,IAAK,KAmB5B,SAASC,EAAiBC,EAAUC,GAShC,IARA,IAGIC,EAHAC,EAAoB,EACpBC,EAAuB,EACvBC,EAA2B,EAE3BC,EAAcN,EAAIO,OAClBC,EAAY,EAGTF,OAAWE,KACVL,GAAaN,EAAQY,QAAQT,EAAIQ,KAAO,IAI7B,MAAXR,EAAIQ,OAINL,EAEFC,EAAeI,GAQnB,IALAN,EAAaM,EAEbA,EAAIJ,EAGGI,KAEH,IAAkC,IAA9BV,EAASW,QAAQT,EAAIQ,IAAzB,CAIAH,EAAmBG,EAAI,EAEvB,KAJC,CAOL,GAAyB,IAArBH,GAA0BH,EAAa,EACvC,OAAOF,EAGX,GAAIK,EAAmB,GAAKA,EAAmB,EAC3C,MAAO,GAIX,GAAIJ,EAAKS,IAAK,CAKV,IAJA,IAAIC,EAAsB,EACpBb,EAA0B,CAAC,IAAK,KAClCU,EAAYJ,EAETI,KACH,GAAIV,EAASW,QAAQT,EAAIQ,KAAO,EAAG,CAC/BG,EAAcH,EAAI,EAElB,KACH,CAGL,OAAaI,QAAAC,UAAAC,KAAA,wBAAA,OAAAC,EAAAC,QAAC,OAAM,GACfF,KAAK,SAACG,UAAQL,QAAQC,QAAQI,EAAIC,IAAIlB,EAAImB,MAAMR,EAAaT,IAAa,GAAC,MACrE,SAACkB,GAGJ,MAFAC,QAAQD,MAAMA,GAERE,MACF,uFAER,EACP,CAGD,OAAOtB,EAAImB,MAAMd,EAAkBH,EACvC,gBAOwB,SACpBqB,EACAtB,GAEA,YAFAA,IAAAA,EAAyB,CAAE,GAEP,iBAATsB,EACP,OAAOxB,EAAiBwB,EAAMtB,GAC3B,GAAIuB,MAAMC,QAAQF,GAAO,CAM5B,IAJA,IAAMG,EAAqB,GACrBpB,EAAciB,EAAKhB,OACrBC,EAAY,EAETA,EAAIF,EAAKE,IAAK,CACjB,IAAMR,EAAMD,EAAiBwB,EAAKf,GAAIP,GAEtCyB,EAAcC,KAAK3B,EACtB,CAED,OAAO0B,CACV,EA5HL,WACI,MAAM,IAAIE,UAAU,kEACxB,CA2HQC,EAER"} \ No newline at end of file diff --git a/dist/extract-domain.modern.js b/dist/extract-domain.modern.js deleted file mode 100644 index 76ae8ee..0000000 --- a/dist/extract-domain.modern.js +++ /dev/null @@ -1,2 +0,0 @@ -const r=["/",":","?","#"],t=[".","/","@"];function e(e,n){let o=0,i=0,s=0,f=0,l=e.length,a=0;for(;l--&&++a&&!(o&&r.indexOf(e[a])>-1);)"."===e[a]&&(++o,i=a);for(f=a,a=i;a--;)if(-1!==t.indexOf(e[a])){s=a+1;break}if(0===s&&f>3)return e;if(s>0&&s<2)return"";if(n.tld){let r=0;const t=["/","@"];let n=i;for(;n--;)if(t.indexOf(e[n])>-1){r=n+1;break}return import("psl").then(t=>Promise.resolve(t.get(e.slice(r,f)))).catch(r=>{throw console.error(r),Error("You must install psl library (https://www.npmjs.com/package/psl) to use `tld` option")})}return e.slice(s,f)}function n(r,t={}){if("string"==typeof r)return e(r,t);if(Array.isArray(r)){const n=[],o=r.length;let i=0;for(;i;\ntype ReturnUrls = Array | Array>;\ntype ReturnUrl = Url | Promise;\n\n/**\n * Options to extract domain.\n */\ntype GetDomainOptions = {\n tld?: boolean;\n};\n\n/**\n * @param {Url} url\n * @param {GetDomainOptions} opts `{ tld: true }` permit to get Top Level Domain like `*.co.uk`\n * @returns {ReturnUrl} Returns a URL or a promise of a URL if the PSL lib is being used\n */\nfunction getDomainFromUrl(url: Url, opts: GetDomainOptions): ReturnUrl {\n let domainInc: number = 0;\n let offsetDomain: number = 0;\n let offsetStartSlice: number = 0;\n let offsetPath: number = 0;\n let len: number = url.length;\n let i: number = 0;\n\n // Find end offset of domain\n while (len-- && ++i) {\n if (domainInc && endings.indexOf(url[i]) > -1) {\n break;\n }\n\n if (url[i] !== '.') {\n continue;\n }\n\n ++domainInc;\n\n offsetDomain = i;\n }\n\n offsetPath = i;\n\n i = offsetDomain;\n\n // Find offset before domain name.\n while (i--) {\n // Look for sub domain, protocol or basic auth\n if (starters.indexOf(url[i]) === -1) {\n continue;\n }\n\n offsetStartSlice = i + 1;\n\n break;\n }\n\n if (offsetStartSlice === 0 && offsetPath > 3) {\n return url;\n }\n\n if (offsetStartSlice > 0 && offsetStartSlice < 2) {\n return '';\n }\n\n // Very customized if statement for tlds\n if (opts.tld) {\n let offsetStart: number = 0;\n const starters: Array = ['/', '@'];\n let i: number = offsetDomain;\n\n while (i--) {\n if (starters.indexOf(url[i]) > -1) {\n offsetStart = i + 1;\n\n break;\n }\n }\n\n return import('psl')\n .then((psl) => Promise.resolve(psl.get(url.slice(offsetStart, offsetPath))))\n .catch((error) => {\n console.error(error);\n\n throw Error(\n 'You must install psl library (https://www.npmjs.com/package/psl) to use `tld` option'\n );\n });\n }\n\n // Tried several approaches slicing a string. Can't get it any faster than this.\n return url.slice(offsetStartSlice, offsetPath);\n}\n\n/**\n * @param {Urls} urls [\"https://www.google.com\", \"https://www.github.com\"] or \"https://www.google.com\"\n * @param {GetDomainOptions} opts `{ tld: true }` permit to get Top Level Domain like `*.co.uk`\n * @returns {Urls | Promise} Return URLs or a promise of URLs if the PSL lib is being used\n */\nexport default function extractDomain(\n urls: Urls,\n opts: GetDomainOptions = {}\n): ReturnUrl | ReturnUrls | null {\n if (typeof urls === 'string') {\n return getDomainFromUrl(urls, opts);\n } else if (Array.isArray(urls)) {\n // lazy type checking (^o^)\n const extractedUrls: any = [];\n const len: number = urls.length;\n let i: number = 0;\n\n for (; i < len; i++) {\n const url = getDomainFromUrl(urls[i], opts);\n\n extractedUrls.push(url);\n }\n\n return extractedUrls;\n } else {\n throwTypeError();\n }\n}\n"],"names":["endings","starters","getDomainFromUrl","url","opts","domainInc","offsetDomain","offsetStartSlice","offsetPath","len","length","i","indexOf","tld","offsetStart","import","then","psl","Promise","resolve","get","slice","catch","error","console","Error","extractDomain","urls","Array","isArray","extractedUrls","push","TypeError","throwTypeError"],"mappings":"AAIA,MAAMA,EAAU,CAAC,IAAK,IAAK,IAAK,KAC1BC,EAAW,CAAC,IAAK,IAAK,KAmB5B,SAASC,EAAiBC,EAAUC,GAChC,IAAIC,EAAoB,EACpBC,EAAuB,EACvBC,EAA2B,EAC3BC,EAAqB,EACrBC,EAAcN,EAAIO,OAClBC,EAAY,EAGhB,KAAOF,OAAWE,KACVN,GAAaL,EAAQY,QAAQT,EAAIQ,KAAO,IAI7B,MAAXR,EAAIQ,OAINN,EAEFC,EAAeK,GAQnB,IALAH,EAAaG,EAEbA,EAAIL,EAGGK,KAEH,IAAkC,IAA9BV,EAASW,QAAQT,EAAIQ,IAAzB,CAIAJ,EAAmBI,EAAI,EAEvB,KAJC,CAOL,GAAyB,IAArBJ,GAA0BC,EAAa,EACvC,OAAOL,EAGX,GAAII,EAAmB,GAAKA,EAAmB,EAC3C,MAAO,GAIX,GAAIH,EAAKS,IAAK,CACV,IAAIC,EAAsB,EAC1B,MAAMb,EAA0B,CAAC,IAAK,KACtC,IAAIU,EAAYL,EAEhB,KAAOK,KACH,GAAIV,EAASW,QAAQT,EAAIQ,KAAO,EAAG,CAC/BG,EAAcH,EAAI,EAElB,KACH,CAGL,OAAOI,OAAO,OACTC,KAAMC,GAAQC,QAAQC,QAAQF,EAAIG,IAAIjB,EAAIkB,MAAMP,EAAaN,MAC7Dc,MAAOC,IAGJ,MAFAC,QAAQD,MAAMA,GAERE,MACF,uFAAsF,EAGrG,CAGD,OAAOtB,EAAIkB,MAAMd,EAAkBC,EACvC,CAOwB,SAAAkB,EACpBC,EACAvB,EAAyB,CAAA,GAEzB,GAAoB,iBAATuB,EACP,OAAOzB,EAAiByB,EAAMvB,GACvBwB,GAAAA,MAAMC,QAAQF,GAAO,CAE5B,MAAMG,EAAqB,GACrBrB,EAAckB,EAAKjB,OACzB,IAAIC,EAAY,EAEhB,KAAOA,EAAIF,EAAKE,IAAK,CACjB,MAAMR,EAAMD,EAAiByB,EAAKhB,GAAIP,GAEtC0B,EAAcC,KAAK5B,EACtB,CAED,OAAO2B,CACV,EA5HL,WACI,MAAM,IAAIE,UAAU,kEACxB,CA2HQC,EAER"} \ No newline at end of file diff --git a/dist/extract-domain.module.js.map b/dist/extract-domain.module.js.map index beacdc1..dbd5bb1 100644 --- a/dist/extract-domain.module.js.map +++ b/dist/extract-domain.module.js.map @@ -1 +1 @@ -{"version":3,"file":"extract-domain.module.js","sources":["../index.ts"],"sourcesContent":["function throwTypeError(): never {\n throw new TypeError('The given URL is not a string. Please verify your string|array.');\n}\n\nconst endings = ['/', ':', '?', '#'];\nconst starters = ['.', '/', '@'];\n\ntype Url = string;\ntype Urls = string | Array;\ntype ReturnUrls = Array | Array>;\ntype ReturnUrl = Url | Promise;\n\n/**\n * Options to extract domain.\n */\ntype GetDomainOptions = {\n tld?: boolean;\n};\n\n/**\n * @param {Url} url\n * @param {GetDomainOptions} opts `{ tld: true }` permit to get Top Level Domain like `*.co.uk`\n * @returns {ReturnUrl} Returns a URL or a promise of a URL if the PSL lib is being used\n */\nfunction getDomainFromUrl(url: Url, opts: GetDomainOptions): ReturnUrl {\n let domainInc: number = 0;\n let offsetDomain: number = 0;\n let offsetStartSlice: number = 0;\n let offsetPath: number = 0;\n let len: number = url.length;\n let i: number = 0;\n\n // Find end offset of domain\n while (len-- && ++i) {\n if (domainInc && endings.indexOf(url[i]) > -1) {\n break;\n }\n\n if (url[i] !== '.') {\n continue;\n }\n\n ++domainInc;\n\n offsetDomain = i;\n }\n\n offsetPath = i;\n\n i = offsetDomain;\n\n // Find offset before domain name.\n while (i--) {\n // Look for sub domain, protocol or basic auth\n if (starters.indexOf(url[i]) === -1) {\n continue;\n }\n\n offsetStartSlice = i + 1;\n\n break;\n }\n\n if (offsetStartSlice === 0 && offsetPath > 3) {\n return url;\n }\n\n if (offsetStartSlice > 0 && offsetStartSlice < 2) {\n return '';\n }\n\n // Very customized if statement for tlds\n if (opts.tld) {\n let offsetStart: number = 0;\n const starters: Array = ['/', '@'];\n let i: number = offsetDomain;\n\n while (i--) {\n if (starters.indexOf(url[i]) > -1) {\n offsetStart = i + 1;\n\n break;\n }\n }\n\n return import('psl')\n .then((psl) => Promise.resolve(psl.get(url.slice(offsetStart, offsetPath))))\n .catch((error) => {\n console.error(error);\n\n throw Error(\n 'You must install psl library (https://www.npmjs.com/package/psl) to use `tld` option'\n );\n });\n }\n\n // Tried several approaches slicing a string. Can't get it any faster than this.\n return url.slice(offsetStartSlice, offsetPath);\n}\n\n/**\n * @param {Urls} urls [\"https://www.google.com\", \"https://www.github.com\"] or \"https://www.google.com\"\n * @param {GetDomainOptions} opts `{ tld: true }` permit to get Top Level Domain like `*.co.uk`\n * @returns {Urls | Promise} Return URLs or a promise of URLs if the PSL lib is being used\n */\nexport default function extractDomain(\n urls: Urls,\n opts: GetDomainOptions = {}\n): ReturnUrl | ReturnUrls | null {\n if (typeof urls === 'string') {\n return getDomainFromUrl(urls, opts);\n } else if (Array.isArray(urls)) {\n // lazy type checking (^o^)\n const extractedUrls: any = [];\n const len: number = urls.length;\n let i: number = 0;\n\n for (; i < len; i++) {\n const url = getDomainFromUrl(urls[i], opts);\n\n extractedUrls.push(url);\n }\n\n return extractedUrls;\n } else {\n throwTypeError();\n }\n}\n"],"names":["endings","starters","getDomainFromUrl","url","opts","offsetPath","domainInc","offsetDomain","offsetStartSlice","len","length","i","indexOf","tld","offsetStart","import","then","psl","Promise","resolve","get","slice","error","console","Error","extractDomain","urls","Array","isArray","extractedUrls","push","TypeError","throwTypeError"],"mappings":"AAIA,IAAMA,EAAU,CAAC,IAAK,IAAK,IAAK,KAC1BC,EAAW,CAAC,IAAK,IAAK,KAmB5B,SAASC,EAAiBC,EAAUC,GAShC,IARA,IAGIC,EAHAC,EAAoB,EACpBC,EAAuB,EACvBC,EAA2B,EAE3BC,EAAcN,EAAIO,OAClBC,EAAY,EAGTF,OAAWE,KACVL,GAAaN,EAAQY,QAAQT,EAAIQ,KAAO,IAI7B,MAAXR,EAAIQ,OAINL,EAEFC,EAAeI,GAQnB,IALAN,EAAaM,EAEbA,EAAIJ,EAGGI,KAEH,IAAkC,IAA9BV,EAASW,QAAQT,EAAIQ,IAAzB,CAIAH,EAAmBG,EAAI,EAEvB,KAJC,CAOL,GAAyB,IAArBH,GAA0BH,EAAa,EACvC,OAAOF,EAGX,GAAIK,EAAmB,GAAKA,EAAmB,EAC3C,MAAO,GAIX,GAAIJ,EAAKS,IAAK,CAKV,IAJA,IAAIC,EAAsB,EACpBb,EAA0B,CAAC,IAAK,KAClCU,EAAYJ,EAETI,KACH,GAAIV,EAASW,QAAQT,EAAIQ,KAAO,EAAG,CAC/BG,EAAcH,EAAI,EAElB,KACH,CAGL,OAAaI,OAAC,OACTC,KAAK,SAACC,UAAQC,QAAQC,QAAQF,EAAIG,IAAIjB,EAAIkB,MAAMP,EAAaT,IAAa,GAAC,MACrE,SAACiB,GAGJ,MAFAC,QAAQD,MAAMA,GAERE,MACF,uFAER,EACP,CAGD,OAAOrB,EAAIkB,MAAMb,EAAkBH,EACvC,CAOwB,SAAAoB,EACpBC,EACAtB,GAEA,YAFAA,IAAAA,EAAyB,CAAE,GAEP,iBAATsB,EACP,OAAOxB,EAAiBwB,EAAMtB,GAC3B,GAAIuB,MAAMC,QAAQF,GAAO,CAM5B,IAJA,IAAMG,EAAqB,GACrBpB,EAAciB,EAAKhB,OACrBC,EAAY,EAETA,EAAIF,EAAKE,IAAK,CACjB,IAAMR,EAAMD,EAAiBwB,EAAKf,GAAIP,GAEtCyB,EAAcC,KAAK3B,EACtB,CAED,OAAO0B,CACV,EA5HL,WACI,MAAM,IAAIE,UAAU,kEACxB,CA2HQC,EAER"} \ No newline at end of file +{"version":3,"file":"extract-domain.module.js","sources":["../index.ts"],"sourcesContent":["function throwTypeError(): never {\n throw new TypeError('The given URL is not a string. Please verify your string|array.');\n}\n\nconst endings = ['/', ':', '?', '#'];\nconst starters = ['.', '/', '@'];\n\ntype Url = string;\ntype Urls = string | Array;\ntype ReturnUrls = Array | Array>;\ntype ReturnUrl = Url | Promise;\n\n/**\n * Options to extract domain.\n */\ntype GetDomainOptions = {\n tld?: boolean;\n};\n\n/**\n * @param {Url} url\n * @param {GetDomainOptions} opts `{ tld: true }` permit to get Top Level Domain like `*.co.uk`\n * @returns {ReturnUrl} Returns a URL or a promise of a URL if the PSL lib is being used\n */\nfunction getDomainFromUrl(url: Url, opts: GetDomainOptions): ReturnUrl {\n let domainInc: number = 0;\n let offsetDomain: number = 0;\n let offsetStartSlice: number = 0;\n let offsetPath: number = 0;\n let len: number = url.length;\n let i: number = 0;\n\n // Find end offset of domain\n while (len-- && ++i) {\n if (domainInc && endings.indexOf(url[i]) > -1) {\n break;\n }\n\n if (url[i] !== '.') {\n continue;\n }\n\n ++domainInc;\n\n offsetDomain = i;\n }\n\n offsetPath = i;\n\n i = offsetDomain;\n\n // Find offset before domain name.\n while (i--) {\n // Look for sub domain, protocol or basic auth\n if (starters.indexOf(url[i]) === -1) {\n continue;\n }\n\n offsetStartSlice = i + 1;\n\n break;\n }\n\n if (offsetStartSlice === 0 && offsetPath > 3) {\n return url;\n }\n\n if (offsetStartSlice > 0 && offsetStartSlice < 2) {\n return '';\n }\n\n // Very customized if statement for tlds\n if (opts.tld) {\n let offsetStart: number = 0;\n const starters: Array = ['/', '@'];\n let i: number = offsetDomain;\n\n while (i--) {\n if (starters.indexOf(url[i]) > -1) {\n offsetStart = i + 1;\n\n break;\n }\n }\n\n return import('psl')\n .then((psl) => Promise.resolve(psl.get(url.slice(offsetStart, offsetPath))))\n .catch((error) => {\n console.error(error);\n\n throw Error(\n 'You must install psl library (https://www.npmjs.com/package/psl) to use `tld` option'\n );\n });\n }\n\n // Tried several approaches slicing a string. Can't get it any faster than this.\n return url.slice(offsetStartSlice, offsetPath);\n}\n\n/**\n * @param {Urls} urls [\"https://www.google.com\", \"https://www.github.com\"] or \"https://www.google.com\"\n * @param {GetDomainOptions} opts `{ tld: true }` permit to get Top Level Domain like `*.co.uk`\n * @returns {Urls | Promise} Return URLs or a promise of URLs if the PSL lib is being used\n */\nexport default function extractDomain(\n urls: Urls,\n opts: GetDomainOptions = {}\n): ReturnUrl | ReturnUrls | null {\n if (typeof urls === 'string') {\n return getDomainFromUrl(urls, opts);\n } else if (Array.isArray(urls)) {\n // lazy type checking (^o^)\n const extractedUrls: any = [];\n const len: number = urls.length;\n let i: number = 0;\n\n for (; i < len; i++) {\n const url = getDomainFromUrl(urls[i], opts);\n\n extractedUrls.push(url);\n }\n\n return extractedUrls;\n } else {\n throwTypeError();\n }\n}\n"],"names":["endings","starters","getDomainFromUrl","url","opts","offsetDomain","offsetStartSlice","len","length","i","domainInc","indexOf","offsetPath","tld","offsetStart","_starters","_i","import","then","psl","resolve","get","slice","error","console","Error","extractDomain","urls","Array","isArray","extractedUrls","push","TypeError","throwTypeError"],"mappings":"AAIA,IAAaA,EAAG,CAAC,IAAK,IAAK,IAAK,KAC1BC,EAAW,CAAC,IAAK,IAAK,KAmB5B,SAAAC,EAA0BC,EAAUC,GAShC,IARA,QAAwB,EACpBC,EAAuB,EACPC,EAAW,EAE3BC,EAAcJ,EAAIK,SACN,EAGTD,OAAWE,KACVC,GAAaV,EAAQW,QAAQR,EAAIM,KAAO,IAI7B,MAAXN,EAAIM,OAINC,EAEFL,EAAeI,GAQnB,IALAG,EAAaH,EAEbA,EAAIJ,EAGGI,KAEH,IAAkC,IAA9BR,EAASU,QAAQR,EAAIM,IAAzB,CAIAH,EAAmBG,EAAI,EAEvB,KAJC,CAOL,GAAyB,IAArBH,GAA0BM,EAAa,EACvC,OAAOT,EAGX,GAAIG,EAAmB,GAAKA,EAAmB,EAC3C,MAAO,GAIX,GAAIF,EAAKS,IAAK,CAKV,IAJA,IAAeC,EAAW,EACZC,EAAkB,CAAC,IAAK,KACjCC,EAAWX,EAETI,KACH,GAAIR,EAASU,QAAQR,EAAIM,KAAO,EAAG,CAC/BK,EAAcL,EAAI,EAElB,KACH,CAGL,OAAcQ,OAAA,OACTC,KAAK,SAACC,GAAD,eAAiBC,QAAQD,EAAIE,IAAIlB,EAAImB,MAAMR,EAAaF,IAAxD,GACC,MAAA,SAACW,GAGJ,MAFAC,QAAQD,MAAMA,GAEHE,MACP,uFAEP,EACR,CAGD,SAAWH,MAAMhB,EAAkBM,EACtC,UAOuBc,EACpBC,EACAvB,GAEA,YAFAA,IAAAA,EAAyB,CAAE,GAEP,iBAAhBuB,EACA,OAAuBzB,EAACyB,EAAMvB,MACvBwB,MAAMC,QAAQF,GAAO,CAM5B,IAJA,IAAmBG,EAAQ,KACPH,EAAKnB,OACpBC,EAAW,EAETA,EAAIF,EAAKE,IAAK,CACjB,MAAYP,EAAiByB,EAAKlB,GAAIL,GAEtC0B,EAAcC,KAAK5B,EACtB,CAED,OAAO2B,CACV,EA5HL,WACI,UAAME,UAAc,kEACvB,CA2HOC,EAEP"} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 96d2419..d9e7312 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "extract-domain", - "version": "4.1.4", + "version": "5.0.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "extract-domain", - "version": "4.1.4", + "version": "5.0.0", "license": "MIT", "devDependencies": { "@types/psl": "^1.1.0", diff --git a/package.json b/package.json index 5d87d5e..af56fb3 100644 --- a/package.json +++ b/package.json @@ -1,20 +1,17 @@ { "name": "extract-domain", - "version": "4.1.5", + "version": "5.0.0", "description": "Extract domain name from URL", "type": "module", "source": "index.ts", "exports": { - "types": "./dist/index.d.ts", - "require": "./dist/extract-domain.cjs", - "default": "./dist/extract-domain.modern.js" + "types": "./dist/index.d.ts" }, - "main": "./dist/extract-domain.cjs", "module": "./dist/extract-domain.module.js", "author": "Bjarne Oeverli", "license": "MIT", "scripts": { - "build": "npm run pretty && bunx --bun microbundle -i extractDomain -f modern,esm,cjs index.ts", + "build": "npm run pretty && bunx --bun microbundle -i extractDomain -f esm index.ts", "test": "bun test tests/", "test:watch": "bun test --watch tests/", "pretty": "prettier --tab-width=4 --print-width=100 --single-quote --trailing-comma=es5 --write *.ts *.ts", diff --git a/tests/index.test.js b/tests/index.test.js index 43d7a9c..8ca6f22 100644 --- a/tests/index.test.js +++ b/tests/index.test.js @@ -1,7 +1,7 @@ import assert from 'assert'; import { test } from 'bun:test'; import extractDomain from '../index.ts'; -// import extractDomain from '../dist/extract-domain.modern.js'; +// import extractDomain from '../dist/extract-domain.module.js'; const urls = [ 'https://www.npmjs.com/package/extract-domain',