Skip to content

Instantly share code, notes, and snippets.

View lowfront's full-sized avatar
:electron:

Low Front lowfront

:electron:
View GitHub Profile
@lowfront
lowfront / getContain.ts
Created February 14, 2024 02:54
getContain.ts
function getContain(
targetWidth: number,
targetHeight: number,
containerWidth: number,
containerHeight: number,
xOffset = 0.5,
yOffset = 0.5,
) {
const targetAspectRatio = targetWidth / targetHeight;
const containerAspectRatio = containerWidth / containerHeight;
class Singleton {
static #instance = null;
constructor() {
if (this.constructor.#instance) return this.constructor.#instance;
else this.constructor.#instance = this;
}
}
const AbstractSingleton = (() => {
const constructors = new Set();
return class AbstractSingleton {
constructor() {
if (constructors.has(this.constructor)) throw new Error(`Already created instance: ${this.constructor.name}`);
constructors.add(this.constructor);
}
};
})();
const camelToSnake1 = (str) => str.replace(/[\w]([A-Z])/g, m => m[0] + '_' + m[1]).toLowerCase();
const CAMEL_REGEX = /[\w]([A-Z])/g;
const cb = m => m[0] + '_' + m[1];
const camelToSnake2 = (str) => str.replace(CAMEL_REGEX, cb).toLowerCase();
const camelToSnake3 = (() => {
const CAMEL_REGEX = /[\w]([A-Z])/g;
const cb = m => m[0] + '_' + m[1];
return (str) => str.replace(CAMEL_REGEX, cb).toLowerCase();
const CAMEL_REGEX = /[\w]([A-Z])/g;
const cb = m => m[0] + '_' + m[1];
const camelToSnake1 = (str: string) => str.replace(CAMEL_REGEX, cb).toLowerCase();
const camelToSnake2 = (str: string) => {
const { length } = str;
let result = '', code = 0, i = 0;
for (i = 0; i < length; i++) {
code = str[i].charCodeAt();
if (65 <= code && code <= 90) {
const crypto = require('crypto');
const fs = require('fs');
const path = require('path');
const zlib = require('zlib');
const AppendInitVect = require('./appendInitVect');
const getCipherKey = require('./getCipherKey');
function encrypt({ file, password }) {
// Generate a secure, pseudo random initialization vector.
const crypto = require('crypto');
const fs = require('fs');
const path = require('path');
const zlib = require('zlib');
const getCipherKey = require('./getCipherKey');
function decrypt({ file, password }) {
// First, get the initialization vector from the file.
const readInitVect = fs.createReadStream(file, { end: 15 });
@lowfront
lowfront / crypto-stream.js
Created June 16, 2022 02:33 — forked from chris-rock/crypto-stream.js
Encrypt and decrypt streams
// Part of https://github.com/chris-rock/node-crypto-examples
// Nodejs encryption of buffers
var crypto = require('crypto'),
algorithm = 'aes-256-ctr',
password = 'd6F3Efeq';
var fs = require('fs');
var zlib = require('zlib');
const heap = Array.from(Array(100), () => Math.floor(Math.random() * 100));
const main = () => {
for (let i = 1; i < heap.length; i++) run(heap, i);
};
const run = (array, target) => {
const root = Math.floor((target - 1) / 2);
if (array[root] < array[target]) {
let temp = array[root];
array[root] = array[target];
function quickSortinPlace(array: number[]) {
const stack: [number, number][] = [[0, array.length - 1]];
let target: [number, number];
let temp: number;
while (target = stack.shift()) {
const [firstIndex, lastIndex] = target;
const middleIndex = Math.floor((firstIndex + lastIndex) / 2);
const pivot = array[middleIndex];