Skip to content

Instantly share code, notes, and snippets.

@korniychuk
korniychuk / collection.ts
Created July 25, 2016 09:38
TypeScript: extends Array class example
'use strict';
class Collection<T> extends Array<T> {
// noinspection JSAnnotator
public constructor(...args: T[]) {
let array = super(...args);
Object.setPrototypeOf(array, this.constructor.prototype);
return array;
}
@korniychuk
korniychuk / encrypt-example.js
Last active October 8, 2016 04:17
Simple encryption algorithm
class Security {
static encrypt(text, password) {
password = String(password);
return String(text)
.split('')
.map((char, index) => String.fromCharCode(char.charCodeAt(0) + password.charCodeAt(index % password.length)))
.join('');
}
@korniychuk
korniychuk / di.ts
Created November 11, 2016 20:24
Dependency container example :: typescript
declare type Dependency = any;
declare type DependencyKey = string|number|{new(...args: any[])};
/**
* Dependency injection container
*
* Example:
*
* // There is a class
* class Auth { ... }
@korniychuk
korniychuk / encoder.ts
Last active January 17, 2017 01:39
Very simple encoder that supports UTF-16 characters
class Encoder {
public static encode(str: string, pass: string): string {
let res = '';
let j = 0, jlen = pass.length;
str = pass + str;
for (let i = 0, ilen = str.length; i < ilen; i++) {
let charCode = str.charCodeAt(i),
passCode = pass.charCodeAt(j),
sumCode = charCode + passCode;
@korniychuk
korniychuk / Str2Hex.js
Created January 23, 2017 10:57
Encode any string to hex codes and decode back
let str = 'My Secure String';
let enc = str.split('').map(char => '0x'+char.codePointAt(0).toString(16)).join(' ');
let dec = enc.split(' ').map(code => String.fromCodePoint(parseInt(code))).join('')
console.log(`Encoded: '${enc}'`);
console.log(`'${str}' === '${dec}'`, str === dec);
@korniychuk
korniychuk / rhombus.js
Created February 13, 2017 05:15
Drowing a rhombus using the symbol '*' in a single line
function rhombus(n) {
return n < 0 || n % 2 == 0 ? null :
[
(n - (n % 2)) / 2,
max => Array(max+1).fill(1).map((i, j) => ' '.repeat(max-j) + '*'.repeat(j*2+1)),
half => half.concat(half.slice(0, -1).reverse(), '').join('\n')
]
.reduce((curr, fn) => fn(curr));
}
}
<ul>
<li>item 1
<ul>
<li>sub-item 1</li>
<li>sub-item 2</li>
<li>sub-item 3</li>
</ul>
</li>
<li>item 2
<ol>
@korniychuk
korniychuk / to.ts
Created July 10, 2017 14:26
types converter
/**
* This is helper class for parse server response
*/
export class To {
public static number(raw: any): number {
const n = Number(raw);
return raw === undefined || raw === null || isNaN(n) ? null : n;
}
@korniychuk
korniychuk / bash-mysql_has-database.sh
Created September 2, 2017 14:53
check if mysql database exists
hasDatabase() {
DB_NAME=$1;
return $(MYSQL_PWD="${MYSQL_ROOT_PASSWORD}" mysql -u${DB_USER} --skip-column-names --batch -e "SHOW DATABASES LIKE '${DB_NAME}'" | wc -l)
}
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Encoder</title>
<script>
var numberSystem = +prompt('Введите систему счисления(число от 2 до 36):', '16');
if (numberSystem >= 2 && numberSystem <= 36) {
var data = prompt('Введите данные для обработки:');