This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Performs a natural sort based on two variables' string values. | |
* Example: | |
* ['C12', 'B', 'c2', 'a', 'c1'].sort(natSort); //=> ['a', 'B', 'c1', 'c2', 'C12'] | |
* | |
* [{name: 'Bob'}, {name: 'Zack'}, {name: 'Alex'}].sort(function(a, b) { | |
* return natSort(a, b, 'name', true); | |
* }); //=> [{name: 'Alex'}, {name: 'Bob'}, {name: 'Zack'}] | |
* | |
* @param {any} a |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { open, writeFile } from 'fs'; | |
/** | |
* Writes and overwrites a file in a continuous loop. | |
* | |
* Example: | |
* loopWriteFile('./date.txt', function*() { | |
* while (true) { | |
* yield new Date().toISOString(); | |
* } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Clones an object and all sub-objects. | |
* `into = from;` -> `into = deepClone(from);` | `deepClone(from, into);` | |
* @param {Object} from Getter object. | |
* @param {Object} [into = {}] Setter object. | |
* @param {boolean} [noOverwrite = false] Flag to not overwrite non-null values in `into` if true. | |
* @return {Object} Returns `into`. | |
*/ | |
export default function deepClone(from, into = {}, noOverwrite = false) { | |
let into = this == global ? Object.create(Object.getPrototypeOf(from)) : this; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Copyright (c) 2019 larryc5 <[email protected]> | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** @typedef {string|boolean|number|Function|void} nonObject */ | |
/** | |
* @typedef {[string, nonObject]} DenestedRow | |
* @property {string} path The dot/bracket notation of the variable. (Example: `a.b[0].c["d-e"].f`). Same as `this[0]`. | |
* @property {object} parent The object that holds this variable. | |
* @property {string} key The key to this variable. (`this.parent[this.key] === this[1]`) | |
*/ | |
/** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Cuts down potentially long or infinite loops to prevent blocking the event loop. | |
* | |
* Example: | |
* snip(10, 500, function*() { | |
* for (let i = 0; i < 1e100; i++, yield) { | |
* console.log(i); | |
* } | |
* console.log('Done!'); | |
* }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function julian(date) { | |
date = new Date(date); | |
let year = date.getFullYear(); | |
let days = Math.floor((date.valueOf() - new Date(year, 0, 0).valueOf()) / 86400000); | |
return `${year}${`000${days}`.slice(-3)}`; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let date = new Date(); | |
if (date.getHours() < 5) { | |
date.setDate(date.getDate() - 1); | |
} | |
let thisThu = new Date(date.valueOf() + ((7 - date.getDay()) % 7 - 3) * 86400000); | |
let firstDay = new Date(thisThu.getFullYear(), 0, 1); | |
let year = thisThu.getFullYear(); | |
let week = Math.ceil((thisThu.valueOf() - firstDay.valueOf() + 86400000) / 604800000); | |
let dow = date.getDay() || 7; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function getWeek() { | |
let date = this; | |
let thisThu = new Date(date.valueOf() + ((7 - date.getDay()) % 7 - 3) * 86400000); | |
let firstDay = new Date(thisThu.getFullYear(), 0, 1); | |
return Math.ceil((thisThu.valueOf() - firstDay.valueOf() + 86400000) / 604800000); | |
} | |
export function getUTCWeek() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @file This creates a worker thread that only handles template rendering. | |
* The purpose is to keep the main thread free so that processes like | |
* rendering don't block the event loop. | |
*/ | |
const { | |
Worker, isMainThread, parentPort | |
} = require('worker_threads'); |
OlderNewer