Skip to content

Instantly share code, notes, and snippets.

@sgimeno
sgimeno / Gruntfile.js
Last active January 3, 2016 10:39
A Gruntfile for Apache Cordova app development. Mimics Cordova CLI commands. Usage: ''grunt exec:<command>[:platform]".
module.exports = function ( grunt ) {
/**
* Load required Grunt tasks. These are installed based on the versions listed
* in `package.json` when you do `npm install` in this directory.
*/
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-exec');
@sgimeno
sgimeno / nodeReadFile
Last active August 29, 2015 13:56
Async and sync ways to read files with FS in node.
var fs = require('fs');
var file = __dirname + '/test.json';
//Async read file
fs.readFile(file, 'utf8', function (err, data) {
if (err) {
console.log('Error: ' + err);
return;
}
@sgimeno
sgimeno / 0_reuse_code.js
Created March 11, 2014 06:48
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@sgimeno
sgimeno / localStorageUsage
Last active August 29, 2015 13:57
Getting localStorage usage
var usageInBytes = (encodeURI(JSON.stringify(localStorage)).split(/%..|./).length - 1);
var usageInKbytes = (encodeURI(JSON.stringify(localStorage)).split(/%..|./).length - 1)/1024;
@sgimeno
sgimeno / parallel.js
Last active December 4, 2016 14:36 — forked from joseraya/parallel.js
var Q = require('q');
function printNumber(n) {
return Q.fcall(function () {
console.log("Number: ", n);
return n;
});
}
function printNumbers(numbers) {
@sgimeno
sgimeno / promise-filter.angular.js
Last active August 29, 2015 14:00
AngularJS filter with cached data got from a service using promise API
app.filter("testf", function($timeout) {
var data = null; // DATA RECEIVED ASYNCHRONOUSLY AND CACHED HERE
var invoked = false
function realFilter(value) { // REAL FILTER LOGIC
return ...;
}
return function(value) { // FILTER WRAPPER TO COPE WITH ASYNCHRONICITY
if( data === null ) {
if (!invoked){
@sgimeno
sgimeno / jq-s3-upload.js
Created May 9, 2014 11:30
Script to upload image files to Amazon S3. Uses jQuery promise API.
function upload(imageURI, fileName) {
var deferred = $.Deferred(),
ft = new FileTransfer(),
options = new FileUploadOptions();
options.fileKey = "file";
options.fileName = fileName;
options.mimeType = "image/jpeg";
options.chunkedMode = false;
options.params = {
@sgimeno
sgimeno / node-static-https-server.js
Created May 31, 2014 15:56
Node http static server with SSL
const fs = require("fs"),
url = require("url"),
path = require("path"),
https = require('https');
var server = https.createServer({
key: fs.readFileSync('server.key'),
cert: fs.readFileSync('server.crt')
});
#!/usr/bin/env node
//this hook installs all your plugins
// add your plugins to this list--either
// the identifier, the filesystem location
// or the URL
var pluginlist = [
'https://github.com/cogitor/PhoneGap-OrientationLock.git',
'org.apache.cordova.camera',
@sgimeno
sgimeno / git-completition.sh
Created September 30, 2014 14:42
git completition script
#!bash
#
# bash/zsh completion support for core Git.
#
# Copyright (C) 2006,2007 Shawn O. Pearce <[email protected]>
# Conceptually based on gitcompletion (http://gitweb.hawaga.org.uk/).
# Distributed under the GNU General Public License, version 2.0.
#
# The contained completion routines provide support for completing:
#