Skip to content

Instantly share code, notes, and snippets.

@lac5
lac5 / nat-sort.js
Last active June 17, 2024 15:03
Performs a natural sort based on two variables' string values.
/**
* 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
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();
* }
/**
* 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;
@lac5
lac5 / LICENSE
Created April 29, 2019 13:36
This license applies to all public gists at <https://gist.github.com/larryc5/>.
Copyright (c) 2019 larryc5 <larry.costigan5@gmail.com>
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
/** @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]`)
*/
/**
@lac5
lac5 / snip.js
Created June 13, 2019 19:56
A function that cuts down potentially long or infinite loops to prevent blocking the event loop.
/**
* 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!');
* });
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)}`;
}
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;
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() {
@lac5
lac5 / node-handlebars-worker.js
Created September 17, 2019 15:51
`worker_thread` example
/**
* @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');