Skip to content

Instantly share code, notes, and snippets.

@abozhilov
abozhilov / .js
Created November 6, 2022 18:22
JSON custom types serialization/parsing
const OBJECT_TYPE = '[object Object]';
const TYPE_PROP = '__type__';
const VALUE_PROP = '__value__';
const jsParseMap = {
'[object BigInt]': BigInt,
};
const jsSerializeMap = {
'[object BigInt]': (v) => `${v}`,
@abozhilov
abozhilov / .ts
Created February 5, 2022 19:24
Cached Intl.DateTimeFormat
const formatter = new Intl.DateTimeFormat('en-US', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit'
});
function format(date: Date) {
const mapParts = Object.create(null);
@abozhilov
abozhilov / .ts
Created February 5, 2022 19:20
Intl.DateTimeFormat
function format(date: Date) {
const formatter = new Intl.DateTimeFormat('en-US', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit'
});
const mapParts = Object.create(null);
const parts = formatter.formatToParts(date);
const create = (() => {
const handler = {
get: (obj, property, reciever) => {
if (!Reflect.has(obj, property)) {
throw new ReferenceError(`Object has no property ${property}`);
}
return Reflect.get(obj, property);
}
};
function fromKeys(iterable, value) {
let obj = {};
for (let i of iterable) {
if (typeof value === 'function') {
obj[i] = value(i)
}
else {
obj[i] = value;
}
@abozhilov
abozhilov / .js
Created December 11, 2017 16:20
Merge sorted iterators
'use strict';
function* merge(iterable1, iterable2) {
let iter1 = iterable1[Symbol.iterator]();
let iter2 = iterable2[Symbol.iterator]();
let a = iter1.next();
let b = iter2.next();
let isFirst;
while(!a.done && !b.done) {
if (a.value <= b.value) {
| | 1 2 3
| 1 | 2 3
| 1 2 | 3
| 1 2 3 |
1 | | 2 3
1 | 2 | 3
1 | 2 3 |
1 2 | | 3
1 2 | 3 |
1 2 3 | |
@abozhilov
abozhilov / .js
Last active September 29, 2016 14:03
scroll.js
var pos = 0;
window.addEventListener('scroll', function () {
pos = document.body.scrollTop * (100 / document.body.scrollHeight)
})
window.addEventListener('resize', function () {
var scrollTop = pos * (document.body.scrollHeight / 100)
window.scrollTo(0, scrollTop)
})
var sort = (function () {
function defCompare(a, b) {
var aStr = String(a),
bStr = String(b);
if (aStr > bStr) {
return 1;
}
else if (aStr < bStr) {
return -1;
@abozhilov
abozhilov / .js
Created June 8, 2016 14:39
case + break
switch(x) {
case 1:
//do something
break;
case 2:
//do something
break;
}