date badge

node v12.18.3
version: 4.0.0
endpointsharetweet
/* * AUTHOR: https://github.com/sugatoray/ * ADAPTED FROM: * - https://runkit.com/amio/cal-badge * - https://cal-badge-icd0onfvrxx6.runkit.sh * ENDPOINT USAGE * mrjlip20e4ba * - https://date-badge-mrjlip20e4ba.runkit.sh * - https://date-badge-mrjlip20e4ba.runkit.sh/:timezone * * AVAILABLE TIMEZONES * https://en.wikipedia.org/wiki/List_of_tz_database_time_zones * NOTE: use entries from column: "TZ databse name" where ("status" != "deprecated") * EXAMPLE: "US/Central" is deprecated. Use "America/Chicago" instead. * * EXAMPLE * API * - https://date-badge-mrjlip20e4ba.runkit.sh * - https://date-badge-mrjlip20e4ba.runkit.sh/America/Chicago?name=sugatoray&start_year=2020&color=green * - https://date-badge-mrjlip20e4ba.runkit.sh/America/Chicago+++name=sugatoray&start_year=2020&color=green * - https://date-badge-mrjlip20e4ba.runkit.sh/?name=sugatoray&start_year=2020&color=green * WITH TIMEZONE: * - https://date-badge-mrjlip20e4ba.runkit.sh/Asia/Shanghai * - https://date-badge-mrjlip20e4ba.runkit.sh/America/Chicago * - https://date-badge-mrjlip20e4ba.runkit.sh/+++name=sugatoray&start_year=2020&color=green * * BADGE: RUNKIT-endpoint (https://badgen.net/runkit) * - https://badgen.net/runkit/date-badge-mrjlip20e4ba * - https://badgen.net/runkit/date-badge-mrjlip20e4ba/+++name=sugatoray * - https://badgen.net/runkit/date-badge-mrjlip20e4ba/+++name=sugatoray&start_year=2018 * - https://badgen.net/runkit/date-badge-mrjlip20e4ba/+++name=sugatoray&start_year=2018&color=orange * WITH TIMEZONE: * - https://badgen.net/runkit/date-badge-mrjlip20e4ba/Asia/Shanghai * - https://badgen.net/runkit/date-badge-mrjlip20e4ba/America/Chicago+++name=sugatoray&start_year=2018 * * BADGE: HTTPS-endpoint (https://badgen.net/https) * - https://badgen.net/https/date-badge-mrjlip20e4ba.runkit.sh/ * - https://badgen.net/https/date-badge-mrjlip20e4ba.runkit.sh/+++name=username * - https://badgen.net/https/date-badge-mrjlip20e4ba.runkit.sh/+++name=sugatoray&color=orange * - https://badgen.net/https/date-badge-mrjlip20e4ba.runkit.sh/+++name=sugatoray&start_year=2020 * - https://badgen.net/https/date-badge-mrjlip20e4ba.runkit.sh/+++name=sugatoray&start_year=2020&color=orange * WITH TIMEZONE: * - https://badgen.net/https/date-badge-mrjlip20e4ba.runkit.sh/America/Chicago * - https://badgen.net/https/date-badge-mrjlip20e4ba.runkit.sh/America/Chicago+++name=sugatoray&color=green */ /* ToDo: Add locale based time-info using "Date.toLocalTimeString()" Ref: 1. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString 2. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocalTimeString */ /* EXPECTED QUERY PARAMETERS 1. name: str| default: '' (empty string) 2. start_year: str (format YYYY) | default: null (auto-detects current year) 3. color: str | default: 'green' 4. num_spaces: int | default: 1 (must be a positive integer) 5. message: str| default: 'Copyright \u00A9' (\u00A9 == ©) */ const url = require('url') // add url library const send = (res, json) => res.end(JSON.stringify(json, null, 2)) const currentMDY = (timeZone) => (new Date).toLocaleDateString('en-US', { timeZone }).split('/') const defaults = { message: 'Copyright \u00A9', name: '', current_year: null, color: 'green', error: { message: 'Copyright Badge', color: 'grey' }, space_char: ' ', num_spaces: 1, sep: '+++', } exports.endpoint = async function (req, res) { try { //const sep = '+++' const true_url = (req.url.replace(defaults.sep, '?') || req.url) // replace +++ by ? in req.url // Parse URL and get query (search-part: comes after "?") const q = url.parse(true_url, true) const qdata = q.query // Get the part of URL that comes before the search-part const reqpath = true_url.split("?").shift() // req.url.split("?").shift() // Original code (which was modified here): // const [month, day, year] = currentMDY(req.url.replace('/', '') || undefined) // Retreive date-related information and other parameters from URL const [month, day, year] = currentMDY(reqpath.replace('/', '') || undefined) const date = (year + '-' + month + '-' + day) qdata.num_spaces = parseInt(qdata.num_spaces); const condition = (qdata.num_spaces==undefined || isNaN(qdata.num_spaces) || qdata.num_spaces==null || qdata.num_spaces==0); const num_spaces = (condition ? defaults.num_spaces : Math.abs(Number(qdata.num_spaces))); const space = defaults.space_char const message = (qdata.message==undefined ? defaults.message : qdata.message) const name = (qdata.name==undefined ? defaults.name : qdata.name) defaults.current_year = Number(year) const start_year = (qdata.start_year==undefined ? defaults.current_year : Number(qdata.start_year)) const end_year = defaults.current_year // To increase to next year: (Number(year)+1) const color = (qdata.color==undefined ? defaults.color : qdata.color) const subject = (message + space.repeat(num_spaces) + name) // ('Copyright' + ' \u00A9 ' + name) const status = (start_year==end_year ? start_year : (start_year + '-' + end_year)) const dateinfo = { date: (year + '-' + month + '-' + day), year: year, month: month, day: day, } const meta = { reqpath: reqpath, url: String(req.url), num_spaces: num_spaces, // the actual num_spaces used } send(res, { subject: subject, status: status, color: color, search: qdata, dateinfo: dateinfo, defaults: defaults, meta: meta, }) } catch (e) { if (e instanceof RangeError) { send(res, { subject: defaults.error.message, status: 'unknown timezone', color: defaults.error.color, search: {}, dateinfo: {}, defaults: defaults, meta: meta, }) } else { send(res, { subject: defaults.error.message, status: e.toString(), color: defaults.error.color, search: {}, dateinfo: {}, defaults: defaults, meta: meta, }) } } }
Loading…

no comments

    sign in to comment