Skip to content

Instantly share code, notes, and snippets.

@westc
westc / JavaScript Snippet - random().md
Last active April 30, 2021 01:54
Provides a simple way to get a random number in a range or a random value from an array.

JavaScript Snippet - random()

Have you ever had to pick a random value from an array or a range of numbers and wished there was a simple one-liner way of doing it? Me too! The following function will get that done for you:

Parameters

This function will accept zero, one or two arguments:

  1. minOrArray {number | Array} - If a number is given and this is the only argument specified this will be used as the limit value while 0 will be used as the inclusive boundary for the range of numbers that can be returned. If a number is given and limit is not undefined or null this will be the the inclusive boundary for the range of numbers that can be returned. If an array is given a random value from this array will be returned.
  2. limit {number | undefined | null} - If a number is given it will represent the exclusive boundary of the range of numbers from which a random number can be chosen.
@westc
westc / chrome-app-getFullPath.js
Created December 1, 2014 14:14
Get full path for a directory or file entry.
function getFullPath(dirOrFileEntry, callback) {
chrome.fileSystem.getDisplayPath(dirOrFileEntry, callback);
}
@westc
westc / chrome-app-readDirectory.js
Created December 1, 2014 14:16
Get contents of a directory.
function readDirectory(dirEntry, callback) {
dirEntry.createReader().readEntries(callback);
}
@westc
westc / chrome-app-readFileAsText.js
Created December 1, 2014 14:18
Read file as text in Chrome app.
function readFileAsText(fileEntry, callback) {
fileEntry.file(function(file) {
var reader = new FileReader();
reader.onload = function(e) {
callback(e.target.result);
};
reader.readAsText(file);
});
}
@westc
westc / chrome-app-writeFileAsText.js
Created December 1, 2014 14:19
Write file as text in Chrome app.
function writeFileAsText(fileEntry, text, opt_callback) {
fileEntry.createWriter(function(writer) {
writer.onwriteend = opt_callback;
writer.write(new Blob([text], {type: 'text/plain'}));
});
}
@westc
westc / chrome-apps-recurseDirectory.js
Created December 1, 2014 15:50
Recursively retrieve directory contents of a specified directory.
/**
* Recursively retrieves the contents of the specified DirectoryEntry. All
* entries are appended to an array assigned to the `entries` key of each
* DirectoryEntry.
* @param {!DirectoryEntry} dirEntry The DirectoryEntry representing the root
* directory for which all entries should be retrieved recursively.
* @param {function(Array.<DirectoryEntry,FileEntry>)} callback Function
* called after all descendant directories have been traversed.
*/
function recurseDirectory(dirEntry, callback) {
@westc
westc / parseCSV.js
Last active November 19, 2024 05:14
Simple CSV Parser
/**
* Parses a string as if it was the contents of a CSV file.
* @param {string} strCSV
* @param {?parseCSV__Options=} options
* @return {*[][]}
* An array of arrays where each value comes from `strCSV`.
*/
function parseCSV(strCSV, options) {
// Get and validate the delimiter.
const delimiter = options?.delimiter ?? ',';
@westc
westc / parseCSV-test-with-apostrophes.js
Last active August 29, 2015 14:11
Test a CSV generated by Excel that contains apostrophes to prove that they don't break the parser.
// CSV contents generated in Excel.
var csv = "First Name,Last Name\nPat,O'Rielly\nSam,O'Brien\nJohn,Carter";
/**
* Takes a string representation of a CSV and parses it into an array of arrays
* or an array of objects (dictionaries).
* @param {string} strCSV
* String representation of a CSV.
* @param {boolean=} opt_headerRow
* Optional boolean defaulting to false which if set to true indicates that
@westc
westc / inherit.js
Last active August 29, 2015 14:11
A simple way to write a function to make subclass (S) inherit from class (C) prior to Object.create() of ES5.
function inherit(C,S){var p='prototype';function O(){}O[p]=C[p];S[p]=new O}
@westc
westc / simple-inheritance-before-es5.js
Last active August 29, 2015 14:11
Example of simple prototypal inheritance before ES5.
// Define a simple Being prototype with isLiving(), die(), and _living starting
// off as true.
function Being() {}
Being.prototype._living = true;
Being.prototype.isLiving = function() { return this._living; };
Being.prototype.die = function() { this._living = false; };
// Define a simple Person prototype which is constructed with a gender.
function Person(gender) { this._gender = gender; }