UNPKG

@tgwf/co2

Version:
8 lines (7 loc) 4.27 kB
{ "version": 3, "sources": ["../../src/hosting-json.js"], "sourcesContent": ["\"use strict\";\n\n/**\n * Check if a string or array of domains has been provided\n * @param {string|array} domain - The domain to check, or an array of domains to be checked.\n */\nasync function check(domain, db) {\n // is it a single domain or an array of them?\n if (typeof domain === \"string\") {\n return checkInJSON(domain, db);\n } else {\n return checkDomainsInJSON(domain, db);\n }\n}\n\n/**\n * Check if a domain is hosted by a green web host by querying the database.\n * @param {string} domain - The domain to check.\n * @param {object} db - The database to check against.\n * @returns {boolean} - A boolean indicating whether the domain is hosted by a green web host.\n */\nfunction checkInJSON(domain, db) {\n if (db.indexOf(domain) > -1) {\n return true;\n }\n return false;\n}\n\n/**\n * Extract the green domains from the results of a green check.\n * @param {object} greenResults - The results of a green check.\n * @returns {array} - An array of domains that are hosted by a green web host.\n */\nfunction greenDomainsFromResults(greenResults) {\n const entries = Object.entries(greenResults);\n const greenEntries = entries.filter(([key, val]) => val.green);\n\n return greenEntries.map(([key, val]) => val.url);\n}\n\n/**\n * Check if an array of domains is hosted by a green web host by querying the database.\n * @param {array} domains - An array of domains to check.\n * @param {object} db - The database to check against.\n * @returns {array} - An array of domains that are hosted by a green web host.\n */\nfunction checkDomainsInJSON(domains, db) {\n let greenDomains = [];\n\n for (let domain of domains) {\n if (db.indexOf(domain) > -1) {\n greenDomains.push(domain);\n }\n }\n return greenDomains;\n}\n\n/**\n * Find the provided information a string or array of domains\n * @param {string|array} domain - The domain to check, or an array of domains to be checked.\n */\nfunction find(domain, db) {\n // is it a single domain or an array of them?\n if (typeof domain === \"string\") {\n return findInJSON(domain, db);\n } else {\n return findDomainsInJSON(domain, db);\n }\n}\n\n/**\n * Check if a domain is hosted by a green web host by querying the database.\n * @param {string} domain - The domain to check.\n * @param {object} db - The database to check against.\n * @returns {object} - An object representing the domain provided host information.\n */\nfunction findInJSON(domain, db) {\n if (db.indexOf(domain) > -1) {\n return domain;\n }\n return {\n url: domain,\n green: false,\n };\n}\n\n/**\n * Check if an array of domains is hosted by a green web host by querying the database.\n * @param {array} domains - An array of domains to check.\n * @param {object} db - The database to check against.\n * @returns {array} - A dictionary of domain to provided host information.\n */\nfunction findDomainsInJSON(domains, db) {\n const result = {};\n for (let domain of domains) {\n result[domain] = findInJSON(domain, db);\n }\n return result;\n}\n\nmodule.exports = {\n check,\n greenDomainsFromResults,\n find,\n};\n"], "mappings": ";AAMA,eAAe,MAAM,QAAQ,IAAI;AAE/B,MAAI,OAAO,WAAW,UAAU;AAC9B,WAAO,YAAY,QAAQ,EAAE;AAAA,EAC/B,OAAO;AACL,WAAO,mBAAmB,QAAQ,EAAE;AAAA,EACtC;AACF;AAQA,SAAS,YAAY,QAAQ,IAAI;AAC/B,MAAI,GAAG,QAAQ,MAAM,IAAI,IAAI;AAC3B,WAAO;AAAA,EACT;AACA,SAAO;AACT;AAOA,SAAS,wBAAwB,cAAc;AAC7C,QAAM,UAAU,OAAO,QAAQ,YAAY;AAC3C,QAAM,eAAe,QAAQ,OAAO,CAAC,CAAC,KAAK,GAAG,MAAM,IAAI,KAAK;AAE7D,SAAO,aAAa,IAAI,CAAC,CAAC,KAAK,GAAG,MAAM,IAAI,GAAG;AACjD;AAQA,SAAS,mBAAmB,SAAS,IAAI;AACvC,MAAI,eAAe,CAAC;AAEpB,WAAS,UAAU,SAAS;AAC1B,QAAI,GAAG,QAAQ,MAAM,IAAI,IAAI;AAC3B,mBAAa,KAAK,MAAM;AAAA,IAC1B;AAAA,EACF;AACA,SAAO;AACT;AAMA,SAAS,KAAK,QAAQ,IAAI;AAExB,MAAI,OAAO,WAAW,UAAU;AAC9B,WAAO,WAAW,QAAQ,EAAE;AAAA,EAC9B,OAAO;AACL,WAAO,kBAAkB,QAAQ,EAAE;AAAA,EACrC;AACF;AAQA,SAAS,WAAW,QAAQ,IAAI;AAC9B,MAAI,GAAG,QAAQ,MAAM,IAAI,IAAI;AAC3B,WAAO;AAAA,EACT;AACA,SAAO;AAAA,IACL,KAAK;AAAA,IACL,OAAO;AAAA,EACT;AACF;AAQA,SAAS,kBAAkB,SAAS,IAAI;AACtC,QAAM,SAAS,CAAC;AAChB,WAAS,UAAU,SAAS;AAC1B,WAAO,UAAU,WAAW,QAAQ,EAAE;AAAA,EACxC;AACA,SAAO;AACT;AAEA,OAAO,UAAU;AAAA,EACf;AAAA,EACA;AAAA,EACA;AACF;", "names": [] }