Skip to content

Instantly share code, notes, and snippets.

View ternavsky's full-sized avatar

Eugene Ternavsky ternavsky

  • https://simplecodesoftware.com/
  • Russia, Novocherkassk
View GitHub Profile
@ternavsky
ternavsky / rp.js
Created July 25, 2024 12:36
deprecated request-promise rewritten to use native "fetch" function.
const FormData = require('form-data');
const https = require('https');
const zlib = require('zlib');
async function rp(options) {
try {
// Check if options is a string URL
if (typeof options === 'string') {
options = { uri: options };
}
@ternavsky
ternavsky / str_mod.js
Created July 25, 2024 12:31
This tool lib allows to make strings checks and modification. It's useful for preparing requests for API calls to sanitize (str_mod) or validate (str_check) fields.
// Example usage
// Case 1
// const last6 = str_mod(trim(), last(6));
// console.log(last6(" test string ")); // returns "string"
// Case 2
// console.log(str_mod(" test string ", trim(), last(6))); // returns "string"
const trim = () => {
@ternavsky
ternavsky / formatJsonLogs.js
Last active January 25, 2023 16:47
VS Code macros: Format json logs - remove everything except json and format
const vscode = require('vscode');
/**
* Macro configuration settings
* { [name: string]: { ... Name of the macro
* no: number, ... Order of the macro
* func: ()=> string | undefined ... Name of the body of the macro function
* }
* }
*/
@ternavsky
ternavsky / nodeFilesWalk.js
Created December 8, 2020 16:56
Node.js read all files in folder and subfolders
const fs = require('fs');
const path = require('path');
const walkSync = (dir, callback) => {
const files = fs.readdirSync(dir);
files.forEach((file) => {
var filepath = path.join(dir, file);
const stats = fs.statSync(filepath);
if (stats.isDirectory()) {
walkSync(filepath, callback);
@ternavsky
ternavsky / Emitter.ts
Created August 2, 2018 07:56
Typescript Event Bus
import IEvent from "IEvent";
import IEventListener from "IEventListener";
export class EventEmitter
{
private listeners: { [key: string]: IEventListener[] } = {};
public on(event: Function, listener: IEventListener): void
{
if (!this.listeners[event.name]) this.listeners[event.name] = [];
@ternavsky
ternavsky / .php_cs
Created June 27, 2018 07:11
Visual Studio php-cs-fixer config. Put it somewhere and set path in php-cs-fixer extension settings.
<?php
return PhpCsFixer\Config::create()
->setRules([
'@PSR2' => true,
'array_syntax' => ['syntax' => 'short'],
'combine_consecutive_unsets' => true,
'method_separation' => true,
'no_multiline_whitespace_before_semicolons' => true,
'single_quote' => true,
@ternavsky
ternavsky / ApiController.php
Last active June 25, 2018 14:40
Base class for Laravel Api controller
<?php
use Backend\Classes\Controller;
use Illuminate\Http\Response;
class ApiController extends Controller {
protected $statusCode = Response::HTTP_OK;
public function setStatusCode($code)
@ternavsky
ternavsky / gist:36894cb0babd29c07e30590430c3120a
Created January 15, 2018 07:46
OctoberCMS file permissions production setup
#!/bin/bash
sudo chown -R www-data:www-data *
sudo find . -type d -exec chmod 755 {} \;
sudo find . -type f -exec chmod 664 {} \;
sudo chmod -R g+s storage themes
sudo chmod -R ug+rwx storage themes
@ternavsky
ternavsky / atom-config
Last active September 16, 2016 05:51
My Atom Configuration by http://atom.io/packages/sync-settings
test
@ternavsky
ternavsky / CountryCodes.php
Created December 8, 2015 09:27
Class allows to get country name by 2 letters code (getNameByCode) and code by name (getCodeByName). You can add several names for country separated by "|" (ex. United States|USA) in country array. getNameByCode will return first variant. getCodeByName will check all name variants in case-insensetive manner and return code if found.
<?php
class CountryCodes
{
private static $codes = array (
'AF' => 'Afghanistan',
'AX' => 'Åland Islands',
'AL' => 'Albania',
'DZ' => 'Algeria',
'AS' => 'American Samoa',