Skip to content

Instantly share code, notes, and snippets.

@hidao80
Last active May 29, 2024 22:04
Show Gist options
  • Save hidao80/27c0d384e8ac2382951daccb20be72e8 to your computer and use it in GitHub Desktop.
Save hidao80/27c0d384e8ac2382951daccb20be72e8 to your computer and use it in GitHub Desktop.
indolence.js - A shoddy library for pure JavaScript that doesn't require NPM.
/**
* Copyright (c) 2023 hidao80
* Released under the MIT license
* https://opensource.org/licenses/mit-license.php
*/
/**
* Returns a new element with the given tag name.
* @param {string} tagName
* @returns {HTMLElement}
*/
export const $$new = (tagName) => document.createElement(tagName);
/**
* Suppress debug printing.
*/
export const $$disableConsole = () => {
["log", "debug", "warn", "info", "error", "table"].forEach(v => {
window.console[v] = () => {};
});
};
/**
* Returns a element with the given selector name.
* @param {string} selector
* @returns {HTMLElement}
*/
export const $$one = (selector) => document.querySelector(selector);
/**
* Returns elements with the given selector name.
* @param {string} selector
* @returns {HTMLElement[]}
*/
export const $$all = (selector) => document.querySelectorAll(selector);
/**
* Returns a calendar that goes back the specified number of days
* from the specified last day or today as an array of strings.
* @param {string|null} lastDate Last day of this calendar. e.g.) "YYYY/MM/DD". If null, today is used.
* @returns {string[]} Array of strings representing the calendar for one month
*/
export const getDaysArray = (lastDate = null, days = 28) => {
const date = lastDate ? new Date(lastDate) : new Date();
const year = date.getFullYear();
const month = date.getMonth();
const today = date.getDate();
const daysArray = [];
for (let offset = 0; offset < days; offset++) {
const day = new Date(year, month, today - offset);
const YYYY_MM_DD = year + "/" + ("0" + (day.getMonth() + 1)).slice(-2) + "/" + ("0" + day.getDate()).slice(-2);
daysArray.push(YYYY_MM_DD);
}
// Reverse the array to display the calendar in ascending order.
return daysArray.reverse();
}
export const $$new=e=>document.createElement(e);export const $$disableConsole=()=>{["log","debug","warn","info","error","table"].forEach((o=>{window.console[o]=()=>{}}))};export const $$one=e=>document.querySelector(e);export const $$all=e=>document.querySelectorAll(e);export const getDaysArray=(e=null,t=28)=>{const o=e?new Date(e):new Date,n=o.getFullYear(),r=o.getMonth(),l=o.getDate(),c=[];for(let e=0;e<t;e++){const t=new Date(n,r,l-e),o=n+"/"+("0"+(t.getMonth()+1)).slice(-2)+"/"+("0"+t.getDate()).slice(-2);c.push(o)}return c.reverse()};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment