Skip to content

Instantly share code, notes, and snippets.

// I was not expecting this. I changed a test using Node's assert.deepEqual() to
// Jest's toEqual().
// Node's deepEqual():
assert.deepEqual(transformed, plant);
// passed!
// Jest's toEqual():
expect(transformed).toEqual(plant);
// Fails:
@guyellis
guyellis / array-concat-push-speed-test.js
Created August 11, 2017 04:24
A speed test between using concat() and push() when reducing an array of arrays to a single array
// Which is faster depends on which version of Node you're using.
// See the bottom for the results.
const iterations = 1000000;
const arrayOfTen = [...Array(10).keys()];
const arrayOfArrays = [...Array(10).keys()].map(() => arrayOfTen);
const NS_PER_SEC = 1e9;
function a() {
for (let i = 1; i < iterations; i++) {
@guyellis
guyellis / master-lock-combo.js
Last active April 30, 2017 18:09
Generate combinations for a Master Combo Padlock after a reset went wrong
// After resetting the Master Combo Lock I was unable to unlock it.
// Assuming that I had been off on one or more on the new number/
// letters I needed to generate all of the combinations so I could
// open it.
// This is an example of the combo I thought I set it to:
const combo = '0AT';
// These are the numbers/letters available on the dial:
const letters = '0123456789ADEHJLNRST';
// I create a script to generate this groovy - i.e. did not write it by hand
mgmt = graph.openManagement();
if(!mgmt.containsPropertyKey('TEXT_key_prop')){
k = mgmt.makePropertyKey('TEXT_key_prop').dataType(String.class).cardinality(Cardinality.SINGLE).make();
mgmt.buildIndex('TEXT_index_name', Vertex.class).addKey(k, Mapping.TEXT.asParameter()).buildMixedIndex('search');
mgmt.commit();
graph.addVertex('TEXT_key_prop', 'The quick brown fox JUMPS oVeR the lazy dog');
graph.tx().commit();
@guyellis
guyellis / typeInference.js
Created April 20, 2015 20:39
How type inferencing can catch errors
var obj = {
one: 'one',
two: 'two'
};
// Forgot to add length after Object.keys(obj)
var isBig = Object.keys(obj) > 1;
console.log(isBig);
// false but should be true.
@guyellis
guyellis / async.eashSeries.js
Created February 4, 2015 13:38
async.eachSeries()
var async = require('async');
var arr = [1,2,3,4];
async.eachSeries(arr,function(item, cb){
setTimeout(function() {
console.log('#1: ', item);
return cb();
}, Math.random()*2000);
}, function(err){
@guyellis
guyellis / async.each.js
Last active August 29, 2015 14:14
async.each()
var async = require('async');
var arr = [1,2,3,4];
async.each(arr,function(item, cb){
setTimeout(function() {
console.log('#1: ', item);
return cb();
}, Math.random()*2000);
}, function(err){
@guyellis
guyellis / gist:2f9d6afdad8f44038eee
Created September 25, 2014 18:44
npm-debug.log
0 info it worked if it ends with ok
1 verbose cli [ 'node', '/home/guy/local/bin/npm', 'publish' ]
2 info using [email protected]
3 info using [email protected]
4 verbose publish [ '.' ]
5 verbose cache add [ '.', null ]
6 verbose cache add spec="." args=[".",null]
7 verbose parsed spec { raw: '.',
7 verbose parsed spec scope: null,
7 verbose parsed spec name: null,
@guyellis
guyellis / namegen1.js
Created June 5, 2014 18:43
Name generators
var vowels = ['a','e','i','o','u'];
var alphabet = 'bcdfghjklmnpqrstvwxyz';
for(var a=0; a<alphabet.length; a++) {
for(var v1=0; v1<vowels.length; v1++) {
for(var b=0; b<alphabet.length; b++) {
for(var c=0; c<alphabet.length; c++) {
for(var v2=0; v2<vowels.length; v2++) {
for(var d=0; d<alphabet.length; d++) {
console.log(alphabet[a] + vowels[v1] + alphabet[b] + alphabet[c] + vowels[v2] + alphabet[d]);
}
@guyellis
guyellis / parseEmailsFromString.js
Created May 1, 2014 16:34
Allows a list of space,comma,semicolon,tab and newline delimited emails to be parsed into an array.
// Accepts a single paramater which is one or more emails separated
// by space,comma,semicolon,tab, or newline.
// Returns an array of tokens that should be emails
// Does not validate emails to see if they are well formed.
exports.parseEmails = function(emails) {
return emails.toLowerCase().split(/[\s,;\t\n]+/);
};