Skip to content

Instantly share code, notes, and snippets.

View pste's full-sized avatar
🎯
Focusing

Stefano Pirra pste

🎯
Focusing
View GitHub Profile
@pste
pste / handlebars-template.html
Last active June 12, 2017 08:11
Handlebars template
<!DOCTYPE html>
<html>
<meta charset="UTF-8">
<head>
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.10/handlebars.min.js" crossorigin="anonymous"></script>
</head>
<body>
<div id="main">
<script type="text/x-handlebars-template">
var items = [{name:"a", age:22}, { name:"b", age:11}, {name:"a", age: 10}]
var sortingfields = {name:true, age:true} // This is used as a dictionary. Sort by "name" DESC and "age" ASC
// do the items sort
items.sort((a,b) => {
for (col in sortingfields) { // added properties are positional
if (a[col] != b[col]) { // if they are equal, sorting is made on the next column
if (sortingfields[col] === true) return a[col] > b[col] // if ASC === true
else return a[col] < b[col]
}
<!DOCTYPE html>
<html>
<meta charset="UTF-8">
<head>
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
</head>
<body>
<button id="uploadbtn">Click me to upload a file ...</button>
<form id="uploadform" action="sendfile" method="post" enctype="multipart/form-data" style="display:none">
<input id="uploadfile" type="file" name="file1" >
@pste
pste / fileReadAndZip.cs
Created August 23, 2017 15:02
Read a file and zip its content in RAM
FileInfo fi = new FileInfo(fullpath);
String b64ZippedData = String.Empty;
using (FileStream filestream = fi.OpenRead()) // source
{
using (MemoryStream memorystream = new MemoryStream()) // destination
{
using (GZipStream gzipstream = new GZipStream(memorystream, CompressionMode.Compress, false)) // compression engine
{
filestream.CopyTo(gzipstream);
}
@pste
pste / node_working_dom_v1.js
Created August 31, 2017 13:39
Node.js DOM parsing with promises
var request = require('request');
var jsdom = require('jsdom');
var promise = require('promise');
var colors = require('colors');
var util = require('util');
function get(url) {
return new Promise(function (resolve, reject) {
console.log('>>>'.yellow, 'started', 'get'.cyan, '[', url.yellow, ']');
@pste
pste / select_latest_version.sql
Last active September 7, 2017 15:08
Comparison between "the most recent record" approaches
SET NOCOUNT ON
SET STATISTICS IO ON
DECLARE @t TABLE (
Id INT IDENTITY(1,1)
, MyValue INT
, MyDate DATE
)
INSERT INTO @t VALUES(1,'2015-01-01')
@pste
pste / filesniff.js
Last active April 13, 2018 12:50
sniffing list-of-files info (node.js)
var allstats = filelist.map(filename => ({name: filename, path: path.parse(filename), stats: fs.statSync(filename) }));
@pste
pste / pg-sequence.sql
Created September 6, 2019 08:03
Playing with PostgreSQL's sequences
-- lets play with sequences:
create table items (id serial primary key, name varchar(10));
insert into items(name) values ('foo');
-- manually insert into a serial is allowed BUT it breaks the sequence!
insert into items(id,name) values (2,'foo');
-- now what do you expect?
select currval(pg_get_serial_sequence('items', 'id')); -- ouch! nextval is 1!!
-- ok, let's fix it up!
SELECT setval(pg_get_serial_sequence('items', 'id'), (select max(id)+1 from public.items));
@pste
pste / vue-expand-datatable.vue
Created September 6, 2019 08:04
Vuetify 1.5.x expandable datatable (by an external button)
<v-btn @click="explodeAll">{{exploded}}</v-btn>
<v-data-table
:headers="headers"
:items="items"
item-key="name"
ref="itemsTable"
expand
>
(..)