Skip to content

Instantly share code, notes, and snippets.

View davidwaterston's full-sized avatar

David Waterston davidwaterston

View GitHub Profile
@davidwaterston
davidwaterston / Increment number part of string
Created April 20, 2014 10:22
Javascript: increment the number part of a text field e.g. take a text field containing "abc15" and increment it by 1 to return "abc16", "xyz99" will become "xyz100", etc. Really shouldn't be doing this but if you have to then this will do it. See it in action: http://jsfiddle.net/davidwaterston/a8yXH/
var myString = "abc99";
newString = myString.replace(/\d+$/, function(n) { return ++n });
@davidwaterston
davidwaterston / no-object-prop-key-value-split.js
Last active August 29, 2015 14:18
ESLint custom rule: Disallow object property values from appearing on a different line from their key
/* global module */
"use strict";
module.exports = function (context) {
function checkObjectExpression(node) {
var props = node.properties;
var numberOfProperties = props.length;
@davidwaterston
davidwaterston / no-multi-object-properties-last-line.js
Last active August 29, 2015 14:18
ESLint custom rule: Disallow last property of a multiple property object to be declared on last line (no-multi-object-properties-last-line)
/* global module */
"use strict";
module.exports = function (context) {
function checkObjectExpression(node) {
var numberOfProperties = node.properties.length;
var objHasMultipleProperties = (numberOfProperties > 1);
@davidwaterston
davidwaterston / no-multi-object-properties-first-line.js
Created April 6, 2015 10:45
ESLint custom rule: Disallow first property of a multiple property object to be declared on first line (no-multi-object-properties-first-line)
/* global module */
"use strict";
module.exports = function (context) {
function checkObjectExpression(node) {
var numberOfProperties = node.properties.length;
var objHasMultipleProperties = (numberOfProperties > 1);
@davidwaterston
davidwaterston / no-multi-object-properties-one-line.js
Created April 6, 2015 10:46
ESLint custom rule: Disallow multiple object properties to be declared on one line (no-multi-object-properties-one-line)
/* global module */
"use strict";
module.exports = function (context) {
function checkObjectExpression(node) {
var multiplePropertiesOnOneLine;
var numberOfLines;
@davidwaterston
davidwaterston / no-multi-vars.js
Created April 6, 2015 10:47
ESLint custom rule: Disallow multiple variables per var declaration (no-multi-vars)
/* global module */
"use strict";
module.exports = function(context) {
function checkVarDeclarations(node) {
var hasMultipleVars = (node.declarations.length > 1);
@davidwaterston
davidwaterston / Javascript now() function
Created June 24, 2012 09:00
A cross-browser Javascript shim function to return the number of milliseconds elapsed since either the browser navigationStart event (using performance.now or browser equivalent) or the UNIX epoch, depending on availability.
var now = (function() {
// Returns the number of milliseconds elapsed since either the browser navigationStart event or
// the UNIX epoch, depending on availability.
// Where the browser supports 'performance' we use that as it is more accurate (microsoeconds
// will be returned in the fractional part) and more reliable as it does not rely on the system time.
// Where 'performance' is not available, we will fall back to Date().getTime().
// jsFiddle: http://jsfiddle.net/davidwaterston/xCXvJ
@davidwaterston
davidwaterston / gist:3741543
Created September 18, 2012 06:21
MySQL: Check if a table exists - version 1
select table_name as found
from information_schema.tables
where table_schema = '__database_name__'
and table_name = '__table_name__'
@davidwaterston
davidwaterston / gist:3741564
Created September 18, 2012 06:27
MySQL: Check if a column exists in a table
select column_name
from information_schema.columns
where table_schema = '__database_name__'
and table_name = '__table_name__'
and column_name = '__column_name__'
@davidwaterston
davidwaterston / gist:3741571
Created September 18, 2012 06:30
MySQL: Check if a table exists - version 2
show tables like '__table_name__'