Skip to content

Instantly share code, notes, and snippets.

View fronterior's full-sized avatar
:electron:

fronterior fronterior

:electron:
View GitHub Profile
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 });
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 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 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 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);
}
};
})();
class Singleton {
static #instance = null;
constructor() {
if (this.constructor.#instance) return this.constructor.#instance;
else this.constructor.#instance = this;
}
}
@fronterior
fronterior / 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;