Skip to content

Instantly share code, notes, and snippets.

@igrek8
igrek8 / BaseConversion.java
Created December 10, 2016 19:37
Any base to any base number conversion
import java.util.Arrays;
public class HelloWorld {
public static String calcBase(final String value, final int fromBase, final int toBase) {
// схема по всех возможных цифр для любой системы счистелния (максимальная 64)
final char[] charArray = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray();
// схема цифр для изначальной системы счителния
final char[] fromRange = Arrays.copyOfRange(charArray, 0, fromBase);
export const resolveClasses = (rules: {
[className: string]: () => boolean
}, ...persistedClasses: string[]): string => {
let classes: string[] = [];
Object.keys(rules).forEach((className) => {
const isResolved = rules[className]();
if (isResolved) {
classes.push(className);
} else {
// should remove it
@igrek8
igrek8 / ip-mac-net.ts
Last active December 22, 2016 19:30
ip2num, ipv4 to number, mac to number, number to mac, num2ip
function ipv42num(ipString: string) {
const ipNumber = ipString.split('.');
return ((((((+ipNumber[0]) * 256) + (+ipNumber[1])) * 256) + (+ipNumber[2])) * 256) + (+ipNumber[3]);
}
function num2ipv4(ipNumber: number) {
let ipString = (ipNumber % 256).toString();
for (let i = 3; i > 0; i--) {
ipNumber = Math.floor(ipNumber / 256);
ipString = ipNumber % 256 + '.' + ipString;
@igrek8
igrek8 / buffer-offset.ts
Last active December 23, 2016 19:42
nodejs simple offset helper
export class SmartCursor {
private _cur: number;
private _memorized: number;
constructor(i: number = 0) {
this._cur = i;
this._memorized = i;
}
@igrek8
igrek8 / bitwise.ts
Last active December 24, 2016 11:01
nodejs simple class for bitwise ops
export class Bitwise {
private _i: number;
constructor(value: number) {
this._i = value;
}
test(mask = 0x01) {
return (mask ^ this._i) === 0;
@igrek8
igrek8 / event-emittable.ts
Created January 6, 2017 08:59
typescript class mixins
import { Mixin } from './mixin';
import { EventEmitter } from 'events';
// Define .h
export declare class EventEmittable {
protected events: EventEmitter;
}
// Implement .cpp
export const EventEmittableMixin = (mixin: Mixin) => class extends mixin {
@igrek8
igrek8 / state-object.ts
Last active January 8, 2017 14:22
simple immutable state object with reflection
import * as deepMerge from 'deepmerge';
import * as deepFreeze from 'deep-freeze';
function sealed(constructor: Function) {
Object.seal(constructor);
Object.seal(constructor.prototype);
}
@sealed
export class StateObject<T> {
@igrek8
igrek8 / form.jsx
Created February 9, 2018 07:36
Simple implementation of AutoComplete & Select in Material UI with Redux Form
import React from 'react';
import { Field, reduxForm } from 'redux-form';
import { AutoComplete, FlatButton } from 'material-ui';
// Component
const mapError = (
{
meta: { touched, error, warning } = {},
input,
@igrek8
igrek8 / recursive-redux-form.jsx
Created February 21, 2018 11:08
Recusive props in redux-form, Recursive render
/* eslint-disable react/prefer-stateless-function */
import React from 'react';
import { reduxForm, FieldArray, Field, FormSection } from 'redux-form';
import { TextField } from 'redux-form-material-ui';
const getDepth = (name) => {
if (name) {
const match = name.match(/children\[\d\]/g);
if (match) {
@igrek8
igrek8 / loader.css
Last active March 16, 2018 08:19
The simplest inline-block loader with inherit color
.loader {
color: inherit;
display: inline-block;
cursor: wait;
}
.loader > span {
animation-name: blink;
animation-duration: 1.4s;
animation-iteration-count: infinite;