Skip to content

Instantly share code, notes, and snippets.

View mbenedettini's full-sized avatar

Mariano Benedettini mbenedettini

View GitHub Profile
@mbenedettini
mbenedettini / invoice_html2pdf.js
Created March 26, 2021 16:02
Generate invoice with barcode from html2pdf
Invoice.prototype.getBarcode = function() {
const CUIT = this.tenant().cuit;
const baseCode = `${CUIT}\
${(this.type + '').padStart(3, '0')}\
${(this.salesPoint + '').padStart(5, '0')}\
${this.cae}${moment(this.caeDueDate).format('YYYYMMDD')}`;
let oddSum = 0,
evenSum = 0;
baseCode.split('').forEach((digit, index) => {
@mbenedettini
mbenedettini / html2pdf.js
Created March 26, 2021 13:18
html2pdf in node using phantomjs
// 2017 version based on phantomjs, probably much easier nowadays with puppeteer
'use strict';
const Promise = require('bluebird');
const fs = require('fs');
const readFile = Promise.promisify(fs.readFile);
const unlink = Promise.promisify(fs.unlink);
const phantom = require('phantom');
const logger = require('logger');
@mbenedettini
mbenedettini / calculo_cuit.js
Created December 20, 2018 13:51
Calculo cuit
function getCuit(document_number, gender) {
/**
* Cuil format is: AB - document_number - C
* Author: Nahuel Sanchez, Woile
*
* @param {str} document_number -> string solo digitos
* @param {str} gender -> debe contener HOMBRE, MUJER o SOCIEDAD
*
* @return {str}
**/
@mbenedettini
mbenedettini / Dockerfile
Last active October 23, 2018 15:28
Cache node_modules across builds
FROM marianobe/node-base:latest
EXPOSE 3000 3001 3002
RUN npm install -g --unsafe-perm @angular/cli pushstate-server
RUN mkdir -p /usr/src/app/client
WORKDIR /usr/src/app
# Loopback app
COPY package.json /usr/src/app
@mbenedettini
mbenedettini / loopback-query-relations.js
Created September 15, 2018 15:29
Loopback relations: querying relations with/without include
/* Previously defined in customer-user.json:
"relations": {
"roles": {
"type": "hasMany",
"model": "Role",
"foreignKey": "principalId",
"through": "RoleMapping"
}
},
@mbenedettini
mbenedettini / postgres-docker-backup.sh
Last active May 20, 2018 23:57
Postgresql docker backup custom format
# Backups database $DATABASE running in container $CONTAINER
docker exec -u postgres $(docker ps | grep $CONTAINER | cut -f 1 -d " ") pg_dump -Fc $DATABASE > "$OUTPUTDIR/$DATABASE-$(date "+%u").pg
@mbenedettini
mbenedettini / server.js
Last active April 10, 2018 17:25
loopback angular html5 mode
/*
I usually insert this snippet right at the beginning of server.js
*/
const path = require('path');
const staticRoot = path.resolve(__dirname, '..', 'client-dist');
app.all('/*', function(req, res, next) {
if (req.url.startsWith('/api')) {
return next();
}
@mbenedettini
mbenedettini / locale-date-adapter.ts
Created November 14, 2017 18:56
Localized date adapter for Angular 4 Material DatePicker
import {Inject, Injectable, Optional} from '@angular/core';
import {DateAdapter, MAT_DATE_LOCALE, MatDateFormats} from '@angular/material';
import * as moment from 'moment';
/** Creates an array and fills it with values. */
function range<T>(length: number, valueFunction: (index: number) => T): T[] {
const valuesArray = Array(length);
for (let i = 0; i < length; i++) {
valuesArray[i] = valueFunction(i);
}
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
@mbenedettini
mbenedettini / flatten.js
Created June 26, 2017 18:00
Flatten nested array of integers
/* I've chosen Javascript as it has become my main language nowadays. I tried
to avoid high level functions () and stick to just the basic stuff.
*/
function flatten(object) {
return object.reduce((a, b) => {
if (Array.isArray(b)) {
return a.concat(flatten(b));
} else {
return a.concat(b);
}