Skip to content

Instantly share code, notes, and snippets.

@Erushenko
Erushenko / formatMoney.js
Created December 15, 2020 16:39
format money
function formatMoney(value) {
const preparedValue =
typeof value === 'undefined' || value === null ? '' : value.toString()
let [dollars, cents = ''] = preparedValue.replace(/[^0-9.-]/g, '').split('.')
dollars = dollars.replace(/^0+/, '') || '0'
cents = `${cents}00`.slice(0, 2)
return `${dollars}.${cents}`.replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1 ')
@Erushenko
Erushenko / config.js
Created February 13, 2019 15:24
get and use connection string from env
module.exports = {
jwt: env('JWT_SECRET'),
pg: {
connection: `postgres://app:${env('PG_APP_PWD', 'app')}@${env('PG_HOST', 'localhost')}/db`,
client: 'pg',
},
pgTwo: {
connection: `postgres://app2:${env('PG_APP2_PWD', 'APP2')}@${env('PG_APP2_HOST', 'localhost:port')}/db2`,
client: 'pg',
},
@Erushenko
Erushenko / basic.sql
Created November 1, 2017 14:35 — forked from nesquena/basic.sql
PostgreSQL Common Utility Queries
/* How to calculate postgreSQL database size in disk ? */
SELECT pg_size_pretty(pg_database_size('thedbname'));
/* Calculate size of a table including or excluding the index */
SELECT pg_size_pretty(pg_total_relation_size('big_table'));
SELECT pg_size_pretty(pg_relation_size('big_table')); /* without index */
/* See indexes on a table with `\d tablename` */
@Erushenko
Erushenko / destructuring.js
Created August 8, 2017 09:10 — forked from mikaelbr/destructuring.js
Several demos and usages for ES6 destructuring. Runnable demos and slides about the same topic: http://git.mikaelb.net/presentations/bartjs/destructuring
// === Arrays
var [a, b] = [1, 2];
console.log(a, b);
//=> 1 2
// Use from functions, only select from pattern
var foo = () => [1, 2, 3];
@Erushenko
Erushenko / curl.md
Last active June 9, 2017 09:33 — forked from subfuzion/curl.md
curl POST examples

Common Options

-#, --progress-bar Make curl display a simple progress bar instead of the more informational standard meter.

-b, --cookie <name=data> Supply cookie with request. If no =, then specifies the cookie file to use (see -c).

-c, --cookie-jar <file name> File to save response cookies to.

@Erushenko
Erushenko / remove.js
Created May 15, 2017 16:30 — forked from max-frai/remove.js
Remove outgoing facebook friend requests
var interval = window.setInterval(function() {
var moreButton = document.querySelector('a[ajaxify*="outgoing/more"]');
if (moreButton)
{
moreButton.click();
console.log('loading more...');
}
else
{
//window.clearInterval(interval);
@Erushenko
Erushenko / json_postgres.js
Created April 14, 2017 12:59 — forked from lucdew/json_postgres.js
Example of json document storage in postgresql and querying (with knex.js)
var connectionString = 'postgres://localhost:5432/postgres';
var Promise=require('bluebird');
var knex = require('knex')({
client: 'pg',
connection: {
user: 'postgres',
database: 'postgres',
port: 5432,
@Erushenko
Erushenko / parser.js
Last active February 14, 2019 07:49
#bluebird #promise #parser
const cheerio = require("cheerio")
const Promise = require("bluebird")
const request = Promise.promisify(require("request"))
const csvStringify = require('csv-stringify')
const fs = require('fs')
const URL = 'https://dou.ua/forums'
request(URL)
.then(topics)
.mapSeries(topic => request(topic.url).then(res => topicContents(res, topic)))
@Erushenko
Erushenko / sumUpDiagonals.js
Created December 10, 2016 10:35
Get the sum diagonals in the matrix on javascript
var matrixExample = [
[ 1, 2, 3, 4 ],
[ 4, 5, 6, 5 ],
[ 7, 8, 9, 7 ],
[ 7, 8, 9, 7 ]
];
function sumUpDiagonals(matrix) {
var sumDiagonals = {main: 0, second: 0},
matrixLength = matrix.length;