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);
}
};
@abozhilov
abozhilov / gist:5249833
Last active June 10, 2020 23:36
2^1000 mod 100?
2^1 = 2 (mod 100)
-------------------
2^2 = 4 (mod 100)
2^3 = 8 (mod 100)
2^4 = 16 (mod 100)
2^5 = 32 (mod 100)
2^6 = 64 (mod 100)
2^7 = 28 (mod 100)
2^8 = 56 (mod 100)
2^9 = 12 (mod 100)
@abozhilov
abozhilov / gist:1779852
Created February 9, 2012 13:02
HashMap
var HashMap = (function () {
var PROTO_SUPPORT = !Object.prototype.isPrototypeOf({__proto__ : null}),
OBJECT_CREATE = typeof Object.create != 'undefined',
DONT_ENUM_BUG = !{'toString' : true}.propertyIsEnumerable('toString');
var hasOwnP = {}.hasOwnProperty,
dontEnums = [];
if (DONT_ENUM_BUG) {
dontEnums = [
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)
})