Skip to content

Instantly share code, notes, and snippets.

@gthrm
gthrm / formatDuration.js
Created July 3, 2021 22:13
Returns the human-readable format of the given number of milliseconds.
const formatDuration = ms => {
if (ms < 0) ms = -ms;
const time = {
day: Math.floor(ms / 86400000),
hour: Math.floor(ms / 3600000) % 24,
minute: Math.floor(ms / 60000) % 60,
second: Math.floor(ms / 1000) % 60,
millisecond: Math.floor(ms) % 1000
};
return Object.entries(time)
@gthrm
gthrm / toKeyedArray.js
Created July 3, 2021 22:04
an object as an array
const toKeyedArray = obj => {
const methods = {
map(target) {
return callback =>
Object.keys(target).map(key => callback(target[key], key, target));
},
reduce(target) {
return (callback, accumulator) =>
Object.keys(target).reduce(
(acc, key) => callback(acc, target[key], key, target),
@gthrm
gthrm / getWeek.js
Created May 5, 2021 12:22
getWeek and getWeekYear for Date constructor
// This script is released to the public domain and may be used, modified and
// distributed without restrictions. Attribution not necessary but appreciated.
// Source: https://weeknumber.com/how-to/javascript
// Returns the ISO week of the date.
Date.prototype.getWeek = function() {
var date = new Date(this.getTime());
date.setHours(0, 0, 0, 0);
// Thursday in current week decides the year.
date.setDate(date.getDate() + 3 - (date.getDay() + 6) % 7);
@gthrm
gthrm / withProvideProps.js
Created March 19, 2021 13:48
react hoc - withProvideProps
import React from 'react'
/**
* Returns function that provide props for component
* @param {ComponentType} Component component for provided props
* @param {Object} compProps props for component
* @returns {Function} provide function
*/
export default function withProvideProps(Component, compProps) {
const wrapped = function WithProvideProps(props) {
@gthrm
gthrm / video-to-gif.sh
Created February 14, 2021 18:29
Bash script for converting Video to GIF with ffmpeg
#!/bin/sh
if [ ! -e "$1" ]; then
echo "ERROR! File doesn't exists"
exit
fi
read -p "Enter output filename (default: new.gif): " output
(($output)) || output=new
read -p "Enter seconds output (default: 5s): " second
@gthrm
gthrm / Singleton.js
Created February 11, 2021 20:15
Singleton
"use strict";
const Singleton = new (function () {
const single = this;
return function () {
return single;
};
})();
// Usage
@gthrm
gthrm / curry.js
Last active February 5, 2021 16:20
curry
const curry = (func) => (...param) => {
if (func.length > param.length) {
// Если количество переданных аргументов меньше, чем ожидается функцией
const temp = func.bind(null, ...param);
return curry(temp);
}
return func(...param);
};
function sum(param1, param2, param3) {
@gthrm
gthrm / deepMerge.md
Created February 3, 2021 13:06
Deep merge function with ramda js (merge objects in array)

Look to examples:

  1. Wrong
const oldData = {
  id: 1,
  data: [
     {id: 1, name: 'data1'},
     {id: 2, name: 'data2'},
import { mergeDeepWith } from 'ramda'
const withFunc = (left, right) => {
if (Array.isArray(left) && typeof left[0] === 'object') {
const array = left.length > right.length ? left : right
const temp = []
for (let index = 0; index < array.length; index++) {
const item = mergeDeepWith(withFunc, left[index], right[index])
temp.push(item)
}
@gthrm
gthrm / repack.sh
Last active April 12, 2023 08:37
repack node_modules
#!/bin/bash
set -e
CURRENT_COMMAND="yarn"
while getopts ":ny" opt_char; do
case $opt_char in
n)
CURRENT_COMMAND="npm"
;;