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 / 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 / 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 / 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 / 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 / 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 };
}