Skip to content

Instantly share code, notes, and snippets.

View vagnercardosoweb's full-sized avatar
🏠
Homework

Vagner Cardoso vagnercardosoweb

🏠
Homework
View GitHub Profile
@vagnercardosoweb
vagnercardosoweb / challenge.js
Last active May 6, 2023 18:52
Test result that happened on the Web Summit Rio da Codewars and Andela
const makeStaircase = (value) => {
const items = value.split(" ").filter(Boolean);
if (items.length === 0) return value;
let index = 0;
let result = "";
let spaceSize = 0;
for (let item of items) {
let space = " ".repeat(spaceSize);
import fs from 'fs';
import path from 'path';
import { mkdirp } from '@/utils';
interface RequestSave {
file: Express.Multer.File;
directory: string;
name: string;
}
@vagnercardosoweb
vagnercardosoweb / cpf.ts
Created April 4, 2022 04:51
validate and returns value formatted
export class Cpf {
constructor(private readonly value: string) {}
public toString(): string {
return this.unmask(this.value);
}
public isValid(): boolean {
const value = this.unmask(this.value);
@vagnercardosoweb
vagnercardosoweb / cnpj.ts
Created April 4, 2022 04:44
validate cnpj, format and to string only number
export class Cnpj {
constructor(private readonly value: string) {}
public toString(): string {
return this.unmask(this.value);
}
public isValid(): boolean {
const value = this.unmask(this.value);
@vagnercardosoweb
vagnercardosoweb / connect.ts
Last active March 28, 2022 03:08
knex connection with default model using postgresql
import knex from 'knex';
export const database = knex({
client: require('knex/lib/dialects/postgres'),
debug: process.env.DB_DEBUG !== 'off',
log: { enableColors: true },
connection: {
host: process.env.DB_HOST,
port: Number(process.env.DB_PORT),
database: process.env.DB_NAME,
@vagnercardosoweb
vagnercardosoweb / event-manager.ts
Created January 10, 2022 02:01
event manager in pure js
type Listener<T = any> = () => Promise<T> | T;
class EventManager {
protected listeners: Record<string, Listener[]> = {};
public on<T = any>(eventName: string, listener: Listener<T>) {
if (typeof this.listeners[eventName] !== 'object') {
this.listeners[eventName] = [];
}
@vagnercardosoweb
vagnercardosoweb / parse-fist-last-name.ts
Created December 27, 2021 16:39
parse fist and last name in javascript
export const getFirstLastName = (name: string) => {
const slitName = name.split(' ');
return {
firstName: slitName[0],
lastName: slitName
.slice(1)
.map(row => row.trim())
.join(' ')
.trim(),
@vagnercardosoweb
vagnercardosoweb / multer-file-filter-mimetypes.ts
Created November 10, 2021 03:54
multer filter mime types with express
import { Request } from 'express';
import multer from 'multer';
import { ForbiddenError } from '@src/errors';
export const multerFileFilter =
(mimeTypes: string[]) =>
(
_: Request,
file: Express.Multer.File,
@vagnercardosoweb
vagnercardosoweb / https-request.ts
Last active October 17, 2021 06:26
request https default node
import https, { RequestOptions } from 'https';
export interface HttpRequest extends RequestOptions {
url: string;
body?: string;
method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'OPTIONS';
}
interface Response<T = Buffer | any> {
body: T;
<?php
if (!function_exists('array_find')) {
/**
* @param array $array
* @param callable $callback
*
* @return mixed|null
*/
function array_find(array $array, callable $callback)