Local Storage in the browser

Local storage lets you store things indefinitely in the user’s browser, specific to your site. This can be used for all kinds of things, like caching dropdown values, storing user information, keeping client-side shopping carts, whatever else you can think of. It’s a simple store, just string-value pairs, so if this doesn’t suit your needs, […]

Javascript promises array

“use strict”;const request = require(“request”);go((err, data) => { if (err) { console.error(`ERROR: ${err}`); } else { console.log(“———————————–“); data.forEach(x => { console.log(`success: ${x.success}`); console.log(`URL: ${x.url}`); console.log(`bodyLength: ${x.bodyLength}`); console.log(`error: ${x.err}`); console.log(“———————————–“); }); } console.log(“DONE”);});function go(callback) { var results = []; const urls = [ “http://www.google.com”, “http://www.yahoo.com”, “http://www.bing.com”, “asdf”, “http://www.apple.com” ]; let seq = Promise.resolve(); urls.forEach(url => { […]

NodeJS Request

“use strict”;const fs = require(“fs”);const request = require(“request”);// Simple GETrequest(“http://localhost:14519/Test/GetHtml”, (err, resp, body) => { console.log(“err: ” + err); console.log(“statusCode: ” + resp.statusCode); console.log(“body: ” + body);});// Output GET results to filerequest(“http://localhost:14519/Test/GetJson”).pipe(fs.createWriteStream(“result.json”));// POST with form parametersrequest.post({ url: “http://localhost:14519/Test/PostParams”, form: { name: “John Smith” },}, (err, resp, body) => { console.log(“err: ” + err); console.log(“statusCode: ” […]

JavaScript Destructuring

Destructuring allows you to take an object or array, and pull pieces of it out into local variables. This can be useful to keep code more readable and avoid holding a reference to an object when you don’t need it any longer. You can use this in both JavaScript and TypeScript. View code on GitHub

Upgrade Deno

Upgrading Deno is incredibly easy, with the deno upgrade command. You can also downgrade or upgrade to a dev channel just as easily. You can access the version programatically with Deno.version. View code on GitHub

NodeJS read/write text files

“use strict”;(function() { var fs = require(“fs”); var readline = require(“readline”); fs.readFile(“input.json”, function(error, data) { var obj = JSON.parse(data); fs.writeFile(“output.json”, JSON.stringify(obj), function(err) { if (err) { console.log(“Error: ” + err); } else { console.log(“Saved”); } }); }); (function() { var outputStream = fs.createWriteStream(“output.txt”); var lineReader = readline.createInterface({ input: fs.createReadStream(“input.txt”) }); lineReader.on(“line”, function(line) { console.log(“Line: ” […]

NodeJS Read/Write Files

const fs = require(“fs”);const readline = require(“readline”);// Read full filefs.readFile(“myfile.txt”, { encoding: “utf-8” } ,(err, data) => { console.log(“Full file contents:”); console.log(data); console.log(“===================”);});// Read file line-by-linevar lineReader = readline.createInterface({ input: fs.createReadStream(“myfile.txt”) });lineReader.on(“line”, line => { console.log(`Line: ${line}`); });lineReader.on(“close”, () => console.log(“DONE”));// Write full filefs.writeFile(“myfile.txt”, “some file contents”, err => { if (err) console.error(err);});// Write file […]

Javascript rest parameters

// collects parameters and turns them into an array// must be the last parameter// no IE support// no Safari supportfunction writeArguments(name, …values) { console.log(`my name is ${name}`); console.log(JSON.stringify(values)); // [1,2,3]}writeArguments(“john doe”, 1, 2, 3);