Skip to content

Instantly share code, notes, and snippets.

var sqlite3 = require('sqlite3').verbose();
var get_rows = function(query, callback){
var db = new sqlite3.Database('test.db');
db.serialize(function(){
db.all(query, function(err, rows){
callback(rows);
});
});
@codebubb
codebubb / parse_params.js
Created April 15, 2016 13:31
function which takes a URL and extracts key/value pairs based on positions in array.
var parse_params = function(url){
var pairs = url.match(/\/?\??(.*)/)[1].split('&');
var out = [];
pairs.forEach(function(chunk){
var keyvalues = chunk.split('=');
for(var i=0; i<keyvalues.length; i++){
if(i % 2 === 0){
var obj = {};
obj[keyvalues[i]] = keyvalues[i + 1]
out.push(obj);
let pp = this.proposalForm;
let $datefields = $('.date input').datepicker(
{
format: "dd/mm/yyyy"
}
);
$datefields.on('changeDate', function (e) {
let fcn = $(this).attr('formControlName');
let index = e;
@codebubb
codebubb / post_curl.php
Created December 27, 2018 14:35
Send a cURL request with PHP rather than Postmant.
$data = array("name" => "James", "age" => "36");
$data_string = json_encode($data);
$ch = curl_init('http://localhost/post.php');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
@codebubb
codebubb / arrayofwords.js
Created February 13, 2019 11:16
UpperCase an array of Words (using a for loop)
const arrayOfWords = ['the', 'quick', 'brown', 'fox'];
for (let i = 0; i < arrayOfWords.length; i += 1) {
arrayOfWords[i] = arrayOfWords[i][0].toUpperCase() + arrayOfWords[i].slice(1);
}
console.log(arrayOfWords); // ["The", "Quick", "Brown", "Fox"]
@codebubb
codebubb / nodemod.js
Created June 4, 2019 14:46
node mod collection
const axios = require('axios');
const chalk = require('chalk');
const commander = require('commander');
commander
.version('1.0.0')
.option('-t --type <type>', 'Lookup type')
.option('-n --number <number>', 'Number of results')
.parse(process.argv);
@codebubb
codebubb / mongodbrest.js
Created July 3, 2019 16:05
MongoDB RestFul calls
router.get('/', (req, res, next) => {
const users = req.app.locals.users;
users
.find({})
.toArray()
.then(data => res.json(data));
});
router.post('/', (req,res, next) => {
const users = req.app.locals.users;
// app.js
const MongoClient = require('mongodb').MongoClient;
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const ObjectID = require('mongodb').ObjectID;
MongoClient.connect('mongodb://localhost:27017/test', (err, client) => {
if (err) throw err;
const db = client.db('test');
const users = db.collection('users');
@codebubb
codebubb / decodecookie.js
Last active December 18, 2022 13:41
Decode Cookie string to a single object
document.cookie
.split(';')
.map(cookie => cookie.split('='))
.reduce((accumulator, [key, value]) => ({ ...accumulator, [key.trim()]: decodeURIComponent(value) }), {});
@codebubb
codebubb / submitform.html
Created August 21, 2019 12:54
Quick example of how to submit a form with JavaScript.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Submit a form with JavaScript</title>
<style>
html, body {
height: 100%;