Skip to content

Instantly share code, notes, and snippets.

View 3AHAT0P's full-sized avatar
:octocat:

3@H@T0P 3AHAT0P

:octocat:
View GitHub Profile
@3AHAT0P
3AHAT0P / sort_by_copy.ts
Last active April 5, 2023 12:45
Implementation custom sorting algorithm (sorting by copying to new array)
const binarySearch = (
array: number[],
element: number,
range: [left: number, right: number] = [0, array.length - 1],
): number => {
let left = range[0];
let right = range[1];
let halfIndex = -1;
while (left < right) {
@3AHAT0P
3AHAT0P / insane_fp_switch.ts
Created June 24, 2022 20:25
Insane FP Switch
const Selector = {
};
interface CaseLine<T, G> {
condition(value: T): boolean;
body(value: T): void | G;
}
interface Switcher<T, G> {
case(caseLine: CaseLine<T, G>): this;
@3AHAT0P
3AHAT0P / Selector.dart
Created December 11, 2021 19:02
Selector pattern on the dart
void main() {
final selector = Selector();
selector
.when((value) => value == 3).then((value) => value * 2)
.whenIsEqual((value) => value == 'QQQ').then((value) { print(value); return null; })
.fallback((value) => { 'x': 42 });
final selector2 = Selector<int, int>();
selector2
.when((value) => value == 3).then((value) => value * 2)
@3AHAT0P
3AHAT0P / Matcher.ts
Last active June 10, 2021 11:43
My implementation matcher pattern (switch/case or some sequence if/else analogue)
export type Listener<TContext> = (context: TContext) => Promise<void> | void;
export type MatcherFn<TEvent extends string> = (event: TEvent) => TEvent | null;
export class Matcher<
TEvent extends string, TAdditionalEvent extends string = 'DEFAULT', TContext = any,
> {
private _listeners: Map<TEvent | TAdditionalEvent, Listener<TContext>[]> = new Map();
private _customMatchers: MatcherFn<TEvent | TAdditionalEvent>[] = [];
@3AHAT0P
3AHAT0P / ChunkerTransformStream.js
Created August 19, 2020 13:25
I created a NodeJS Transform Stream which repack data chunks to necessary size chunks
const { Transform } = require('stream');
const sliceBySize = (buffer, size, callback) => {
let chunk = buffer.slice(0, size);
let index = size;
while (chunk.length >= size) {
callback(chunk);
chunk = buffer.slice(index, index + size);
index += size;
@3AHAT0P
3AHAT0P / iterable-hash.ts
Created June 4, 2020 07:30
It's Hash with low cost iterations
class IterableHash<G extends string | number, T> {
[key: string]: T;
[key: number]: T;
// @ts-ignore
private primaryKeyIndex: G[] = [];
// @ts-ignore
private data: Record<G, T | void> = {};
// @ts-ignore
@3AHAT0P
3AHAT0P / index.js
Last active April 12, 2019 10:57
Reverse string in JS
const isSupportSymbol = (symbolCode) => symbolCode === 55356 || (symbolCode >= 768 && symbolCode <= 879);
const reverse = (str) => {
let res = '';
let prevSymbol = null;
for (const symbol of str) {
if (isSupportSymbol(symbol.charCodeAt())) {
res = `${prevSymbol}${symbol}${res}`;
prevSymbol = null;
} else if (prevSymbol == null) {
prevSymbol = symbol;
(async () => {
const relation = (ModelClass, deserialize, serialize) => {
return (target, key, descriptor) => {
const proto = Reflect.getPrototypeOf(target);
if (!target.hasOwnProperty(Symbol.for('#meta'))) {
if (proto.hasOwnProperty(Symbol.for('#meta'))) {
Reflect.defineProperty(target, Symbol.for('#meta'), {value: {...proto[Symbol.for('#meta')]}});
} else {
Reflect.defineProperty(target, Symbol.for('#meta'), {value: {}});
}
import Ember from 'ember';
export default Ember.Component.extend({
componentName: 'rd-block',
mods: null,
classNameBindings: ['componentName', 'modClassNames'],
modClassNames: Ember.computed('componentName', 'mods', 'mods.[]', {
get() {
let componentName = this.get('componentName');
let mods = this.get('mods') || [];
@3AHAT0P
3AHAT0P / components.test-one.js
Created December 10, 2016 16:01 — forked from workmanw/components.test-one.js
Ember 2.10.0 - Bug with inline styles
import Ember from 'ember';
export default Ember.Component.extend({
attributeBindings: 'style'.w(),
init() {
this._super(...arguments);
Ember.run.later(this, () => {
this.set('isVisible', true);
}, 5000);