Skip to content

Instantly share code, notes, and snippets.

View jbnv's full-sized avatar
🙂
Life is good.

Jay Bienvenu jbnv

🙂
Life is good.
View GitHub Profile
@jbnv
jbnv / circularprimes.js
Created September 13, 2016 14:16
Function to determine number of circular primes in a range.
/*
A number is called a circular prime if it is prime and all of its rotations are primes.
For example, the number 197 has two rotations: 971, and 719. Both of them are prime.
Another example: 1193 is a circular prime, since 1931, 9311 and 3119 all are also prime.
*/
function isPrime(n) {
if (n == 1) return false;
for (var i = 2; i <= Math.sqrt(n); i++) {
@jbnv
jbnv / matchesSequence.js
Created April 12, 2016 15:01
String prototype function that matches a sequence to a string if the string contains all characters in the sequence. (E.g. "love" matches "like a glove")
if (!String.prototype.matchesSequence) {
String.prototype.matchesSequence = function(sequence) {
if (!sequence) return false;
if (sequence === "") return true;
var pattern = sequence.split("").join(".*");
var exp = new RegExp(pattern);
return exp.test(this);
};
}
@jbnv
jbnv / polyfill.js
Created March 10, 2016 15:27
Polyfill for JavaScript.
if (!Array.prototype.includes) {
Array.prototype.includes = function(searchElement /*, fromIndex*/ ) {
'use strict';
var O = Object(this);
var len = parseInt(O.length) || 0;
if (len === 0) {
return false;
}
var n = parseInt(arguments[1]) || 0;
var k;
@jbnv
jbnv / compare_arrays_of_hashes.rb
Created March 1, 2016 21:36
A function that goes in Ruby unit tests to check an array of hashes by property. Supply a list of symbols to check.
def compare_arrays_of_hashes(expected_array,actual_array,symbols)
expected_array.each_with_index do |expected,index|
actual = actual_array[index]
symbols.each do |symbol|
assert_equal expected[symbol],actual[symbol],"#{symbol} of element #{index} doesn't match"
end
end
end
@jbnv
jbnv / keymap.cson
Last active June 27, 2017 14:27
Keymap for Atom.
# Mac
'atom-text-editor':
'ctrl-i': 'window:toggle-invisibles'
'ctrl-w': 'editor:toggle-soft-wrap'
'body':
'ctrl-w': 'editor:toggle-soft-wrap'
@jbnv
jbnv / prompt.sh
Last active July 3, 2017 13:14
My standard shell prompt script. Show current git branch in the prompt, if there is one. Show current path in the prompt. Replace home path with "~". Replace "/var/www" with "%".
parse_git_branch () {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'
}
parse_pwd () {
pwd | sed -e 's#/var/www/#%#' | sed -e 's#/home/myusername#~#'
}
prompt () {
echo "$BRANCH_COLOR\$(parse_git_branch)$USERNAME_COLOR`whoami`$PATH_COLOR\$(parse_pwd)$PROMPT_COLOR "
@jbnv
jbnv / Calendar.sql
Created November 30, 2015 22:45
SQL Server [Calendar] view.
/*
A view that creates a view of date information, in a range from 2000 to 2179.
Use this view to perform date amd time operations would otherwise be difficult with standard T-SQL methods.
*/
CREATE VIEW [dbo].[Calendar]
AS
SELECT [Date]
,DATEPART(year,[Date]) AS [Year]
,DATEPART(month,[Date]) AS [Month]
,DATEPART(day,[Date]) AS [Day]
@jbnv
jbnv / MenuHtmlHelper.cs
Created November 11, 2015 20:01
Menu HTML helper for ASP.Net MVC.
// Untested!
public class MenuItem
{
public bool Show { get; set; }
public MvcHtmlString Html { get; set; }
}
public class Menu
{
@jbnv
jbnv / FileSize.cs
Created October 2, 2015 14:40
Simple C# class for converting an integer into a file size with suffix.
public class FileSize
{
long _value;
long _reduced;
string _units;
public long Reduced { get { return _reduced; } }
public string Units { get { return _units; } }
@jbnv
jbnv / childView1.html
Created September 3, 2015 21:29
An example of view composition in Durandal. From Durandal's HTML Samples download.
<div>
<h2>Child View One</h2>
<span data-bind="text: $root.propertyOne"></span>
</div>