This file contains hidden or 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 getContain( | |
| targetWidth: number, | |
| targetHeight: number, | |
| containerWidth: number, | |
| containerHeight: number, | |
| xOffset = 0.5, | |
| yOffset = 0.5, | |
| ) { | |
| const targetAspectRatio = targetWidth / targetHeight; | |
| const containerAspectRatio = containerWidth / containerHeight; |
This file contains hidden or 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
| class Singleton { | |
| static #instance = null; | |
| constructor() { | |
| if (this.constructor.#instance) return this.constructor.#instance; | |
| else this.constructor.#instance = this; | |
| } | |
| } |
This file contains hidden or 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
| 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); | |
| } | |
| }; | |
| })(); |
This file contains hidden or 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
| 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(); |
This file contains hidden or 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
| 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) { |
This file contains hidden or 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
| 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. |
This file contains hidden or 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
| 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 }); |
This file contains hidden or 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
| // 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'); |
This file contains hidden or 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
| 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]; |
This file contains hidden or 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 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]; |
NewerOlder