Skip to content

Instantly share code, notes, and snippets.

@krazov
krazov / flat-flatten-v1.js
Created April 7, 2021 15:17
When you have no access to `Array.prototype.flat`
const array = [1,[2,[[3], 4],5], [6]];
// the first approach, with two loops
function flatFlatten(array) {
const flatOne = [];
for (let i = 0, j = 0; i < array.length; i++, j = flatOne.length) {
const item = array[i];
if (Array.isArray(item)) {
@krazov
krazov / isValidLatinString.js
Last active December 5, 2020 10:08
Checker for a valid string in all latin-flavoured languages
// Ranges covered:
// \u0041-\u005a - ABCDEFGHIJKLMNOPQRSTUVWXYZ
// \u0061-\u007a - abcdefghijklmnopqrstuvwxyz
// \u00c0-\u00d6 - ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ
// \u00d8-\u00f6 - ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö
// \u00f8-\u00fe - øùúûüýþ
// \u0100-\u0131 - ĀāĂ㥹ĆćĈĉĊċČčĎďĐđĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħĨĩĪīĬĭĮįİı
// \u0134-\u0150 - ĴĵĶķĸĹĺĻļĽľĿŀŁłŃńŅņŇňʼnŊŋŌōŎŏŐő
// \u0154-\u017e - ŔŕŖŗŘřŚśŜŝŞşŠšŢţŤťŦŧŨũŪūŬŭŮůŰűŲųŴŵŶŷŸŹźŻżŽž
@krazov
krazov / total-time.js
Last active October 6, 2020 16:01
Sum times
function totalTime(...times) {
let hours = 0;
let minutes = 0;
for (const time of times) {
const [h, m] = time.split(':');
hours += Number(h);
minutes += Number(m);
}
@krazov
krazov / equation.js
Last active October 14, 2020 17:12
Equation function
function equation() {
let step = 1;
let solution = 0;
while (true) {
const x = solution + step;
const tmp = 3 * x ** 3 + 5 * x;
switch (true) {
case tmp == 16:
@krazov
krazov / array-shuffle.js
Last active December 20, 2019 10:36
Future useless.js
function shuffle(array) {
if (array.length === 0) {
return [];
}
const copy = [...array];
const result = [];
const rand = (min, max) => Math.floor(Math.random() * (max - min) + min);
do {
@krazov
krazov / random-number-average-table.js
Last active November 19, 2019 13:46
Random number average
setInterval((dto) => {
const rand = Math.random();
dto.count++;
dto.acc = dto.acc + rand;
console.table([{
cnt: dto.count,
rnd: rand,
acc: dto.acc,
@krazov
krazov / index.html
Created August 13, 2019 12:49
Form parser
<html>
<head>
<title>&lt;form&gt; parser</title>
<script src="./main.js" type=module defer></script>
<style>
textarea {
font-family: monospace;
}
</style>
@krazov
krazov / clean-prototype.js
Last active June 15, 2019 16:59
Clean prototype
// explanation: https://twitter.com/krazov/status/1139937821396000768
{
const cleanPrototype = (prototype) => Object.create(
Object.assign(
Object.create(null),
prototype,
)
);
const cleanObject = cleanPrototype({
@krazov
krazov / runtime-interface.js
Last active June 13, 2019 14:55
Runtime interface
// interface
var MyNotSoClass = {
[Symbol.hasInstance](instance) {
return Boolean(instance.data && instance.next);
}
};
var objectRight = {
data: 'yes',
next: 'also, yes',
@krazov
krazov / linked-list-fn.js
Last active May 4, 2020 19:43
LinkedList: The functional approach (WIP)
{
/*
Who-when: Krazov 2019
LinkedList object creator.
Instead of storing a list in array or other linear container, the data
is organized in a chain of linked items:
```
LinkedListCell {
value: any;
next: LinkedListCell | null;