Skip to content

Instantly share code, notes, and snippets.

View dmshvetsov's full-sized avatar
🪄

Dmitry Shvetsov dmshvetsov

🪄
View GitHub Profile
@dmshvetsov
dmshvetsov / collection-utils.ts
Created October 11, 2023 16:18
all sort every day utils for TypeScript
function groupBy<T extends Record<string, string | number>, K extends keyof T>(list: T[], groupByKey: K): Record<T[K], T[]> {
return list.reduce((acc, item) => {
const groupValue = item[groupByKey]
if (!acc[groupValue]) {
acc[groupValue] = [item]
} else {
acc[groupValue].push(item)
}
return acc
}, {} as Record<T[K], T[]>)
module 0xA::pyth_oracle_n_coin_transfer_a {
struct State {
// a place where depositor, asset and amount is stored
}
public entry fun colatarize_asset<Asset>(depositor: &signer, amount: u64) {
let resource_account = get_resource_account();
coint::transfe<Asset>(
depositor,
resource_account,
@dmshvetsov
dmshvetsov / my_lodash_fn.ts
Last active August 4, 2023 07:00
type safe implementation lodash like functions (WIP)
type DotPrefix<T extends string> = T extends '' ? '' : `.${T}`;
type DotNestedKey<T> = (
T extends object
? {
[K in Exclude<keyof T, symbol>]: `${K}${DotPrefix<
DotNestedKey<T[K]>
>}`;
}[Exclude<keyof T, symbol>]
: ''
) extends infer D
import dotenv from "dotenv";
dotenv.config();
import assert from "assert";
import * as web3 from "@solana/web3.js";
import * as fs from "fs";
/**
* Constants
*/
@dmshvetsov
dmshvetsov / Rakefile
Last active July 30, 2022 00:02
Use rails webpacker gem with npm instead of yarn, Rails recipe.
# Rakefile
require File.expand_path('../config/application', __FILE__)
Rails.application.load_tasks
# Replace yarn with npm
Rake::Task['yarn:install'].clear if Rake::Task.task_defined?('yarn:install')
Rake::Task['webpacker:yarn_install'].clear
Rake::Task['webpacker:check_yarn'].clear
@dmshvetsov
dmshvetsov / mac-os-setup.sh
Last active June 24, 2022 07:45
DEPRECATED checkout my dotfiles/install script for the up to date version
# DEPRECATED checkout my dotfiles/install script for the up to date version
# https://github.com/dmshvetsov/dotfiles/blob/master/install
set -e
ssh -q git@github.com | true
if [ $? -eq 0 ]; then
echo "SSH to github OK"
else
echo "Cannot connect to github via SSH. Are you added your SSH key to your github account?"
@dmshvetsov
dmshvetsov / do-if-exist-pattern.js
Created April 26, 2021 07:04
Do if values exist pattern
function doIfExist(obj, predicateValuesPaths, fn) {
const values = predicateValuesPaths.map((keyPath) => get(obj, keyPath))
if (all(values, truthy)) {
return fn(...values)
}
return
}
@dmshvetsov
dmshvetsov / roman_num.rb
Created December 28, 2020 07:37
Integer to Roman Number string conversion (kata, exercise)
class Roman
NUMS = [
[10, 'X'],
[9, 'IX'],
[5, 'V'],
[4, 'IV'],
[1, 'I']
].freeze
def self.from(val)
@dmshvetsov
dmshvetsov / make-derived-calculations-template.js
Last active December 21, 2020 06:17
WIP; Consider using with Quokka.js
const K = 1000
const M = 1000 * K
const KB = 1000
const MB = 1000 * KB
const GB = 1000 * MB
const TB = 1000 * GB
const PB = 1000 * TB
function Calc () {
this.value = 100 * K
@dmshvetsov
dmshvetsov / attach-download-multer-wrapper-azure.js
Last active December 11, 2020 23:26
Rewrite of multer-azure-storage package with additional helper function (used in Express app)
import { resolve } from 'path';
import fs from 'fs';
import { createBlobService } from 'azure-storage';
import config from 'config';
/**
* Service to handle files downloads requests
*
*/