Skip to content

Instantly share code, notes, and snippets.

<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 / 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));
}
}
@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 / 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 / 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 / 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 / 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 / index.js
Created June 14, 2016 18:42
check correct order of the brackets in expression
/**
* @param {string} str testable string
*
* @return {boolean}
*/
function verify(str) {
if (typeof str !== 'string') return false;
var pairs = {
'(' : ')',
@korniychuk
korniychuk / composer.json
Last active April 1, 2016 23:37
Swift mailer simple example
{
"require": {
"swiftmailer/swiftmailer": "@stable"
}
}
@korniychuk
korniychuk / common\config\main-local.php
Created March 30, 2016 15:34
An easy example how to send feedback emails in yii2 (Advanced template)
<?php
return [
'components' => [
...
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '@common/mail',
// send all mails to a file by default. You have to set