Skip to content

Instantly share code, notes, and snippets.

View devlato's full-sized avatar
🐯
🦜🐨🦘

Denis Tokarev devlato

🐯
🦜🐨🦘
View GitHub Profile
@devlato
devlato / pluralizer.ts
Created April 25, 2024 13:11
Pluralizer/string formatter
export const format = (format: string, ...args: Serializable[]): string => {
args.forEach((arg, i) => {
assert(
typeof arg === 'string' ||
(typeof arg === 'number' && !isNaN(arg)) ||
(Array.isArray(arg) && arg.every((v) => typeof v === 'string' || !isNaN(v))),
`The argument #${i + 1} must be a valid number, a string, or an array of valid numbers or strings, got a ${typeof arg === 'number' ? 'NaN' : typeof arg} instead (${String(arg)})`,
);
});
@devlato
devlato / points_on_circle.js
Created November 7, 2023 06:42
Points on a circle with specified step in radians
const step = Math.PI / 20; // step in radians
const raduus = 2.0;
const center = { x: 3.0, y: 2.0 }; // [x, y]
const points = [];
for (let angle = 0; angle < Math.PI * 2; angle += step) {
const initPoint = { x: 0.0, y: raduus };
const rotatedPoint = {
@devlato
devlato / map_vs_obj_benchmark.js
Created September 26, 2023 02:11
Map vs obj read benchmark
async function checkAvgPerf() {
const numberOfReads = 100000;
const numberOfTests = 100;
const eps = 0.000001;
const mapAvg = [];
const objAvg = [];
let mapSlowerOnAvgTimes = 0;
let objSlowerOnAvgTimes = 0;
let equalTimes = 0;
@devlato
devlato / array_view.ts
Created September 18, 2023 04:24
ArrayView for JS Arrays
// `ReadonlyArrayView` class allows to avoid copying when creating array slices
// It can be used as a drop-in replacement instead of an array but only gives read access to the original array
// Worth remembering that this class doesn't track index change, element reordering, etc., so use it at your own risk
//
// Demo:
// https://www.typescriptlang.org/play?module=1#code/C4TwDgpgBASghgOwObQLxQN4FgBQUoBmATgPYC2AkggCYQAeAXFAgK5kBGERA3LvsCSq1GzNpx64Avrxy56YEkWBQAxgBs4AZ02wIcaiQRqQAQSJE4IAGoBLCAHcAPABUAfFBtkwaiGQgJgHTMLEAAZGwBrCBd3bDwoIj0DIxAoAG0EJlYOLgBdJmdcPigwIhsANzhgaBVDTWAiFhUBIgAKUoqq6ET9Q2MoOHNLArTcgBoSssrqhKS+1ItkCCZ4JYBKTEki+PqqmxUoEgIY1sGQkfGExBQV64g1lbmU4MtbBxjMYvxahHqocrs9ig6AQDl0vWeQ2sgJOZ0sE0WKDWMi+qjqygQhgAwnA1Bp2D5gVBWhtULFUfwABakIGgoEAUXMilaAHIcQhMcoVLi1FBgJSbDotANCCwEM0bIYWcjUdJtvh8IlgCwiAhmGCAAqkOggVoAhwTOIKhVwMDeEBZbE8uAEiBjClo34NJrAS0IHF4m0+e3xY20Ag2UFakiQJQW5hWz22n3G-C0HzVYOh0Buj3470Okj2BAAaQgIE0TBJwPcaRZPmQ-JZExZUQL1agLMqahYEE0DZZAgAyg1A0gWeMHShXcTgINhxMwBMAPqk8m+2M2AjEsAeNVjojDjZG2PGpUq9f
@devlato
devlato / generate_regexp.ts
Created May 3, 2023 07:47
Generate a regexp for a string
export const generateRegexp = (str: string): string => {
const ranges = uniq(
uniq([...str]).map((char) => {
if (char.match(/[a-z]/) != null) {
return 'a-z';
}
if (char.match(/[A-Z]/) != null) {
return 'A-Z';
}
@devlato
devlato / gentoo-custom.zsh-theme
Last active April 13, 2023 12:57
A custom, much more fast a responsive gentoo theme for `oh-my-zsh`
# Add this file to ~/.oh-my-zsh/themes/gentoo-custom.zsh-theme
# Then, in your ~/.zshrc, set ZSH_THEME to "gentoo-custom" like this:
# ZSH_THEME=gentoo-custom
function prompt_char {
if [ $UID -eq 0 ]; then echo "#"; else echo $; fi
}
git_branch() {
local branch=$(git rev-parse --abbrev-ref HEAD 2>&1)
@devlato
devlato / chooseConsole.sh
Last active April 13, 2023 12:58
Chooses console for you
#!/usr/bin/env bash
chooseConsole() {
declare -a consoles=('PS4' 'PS4 Pro' 'Xbox One' 'XBox One S', 'Nintendo Switch', 'PS5', 'Xbox Series X')
local index=$(( $[$RANDOM % ${#consoles[@]}] ))
echo "${consoles[$index]}"
}
echo "You should get $(chooseConsole)"