Skip to content

Instantly share code, notes, and snippets.

View thekarel's full-sized avatar

Charles Szilagyi thekarel

View GitHub Profile
@thekarel
thekarel / watchobject.js
Created October 25, 2013 15:58
Angular - watch a whole object
$scope.$watch(function() {return $scope.bookingData;}, function() {
console && console.log(1);
Storage.set(CFG.cfg.cookies.bookingflow.name, $scope.bookingData);
}, true)
@thekarel
thekarel / clild.html
Created October 26, 2013 05:20
Angular form validation: class of parent based on child status
<div class="grp" ng-class="{passed: (theForm.fname.$dirty && theForm.fname.$valid) }">
app.directive('bubble', function() {
return function(scope, element, attrs) {
// Whatever the data-bubble attr is set to will
// be the expression we watch.
var expr = attrs.bubble;
scope.$watch(expr, function(val) {
if (val > 0) {
element.attr('data-bubble', val);
} else {
@thekarel
thekarel / valid.js
Created October 28, 2013 14:13
Watch form $valid in a child element in AngularJS
angular.module('foo', []).directive('enableifformisvalid', function() {
return {
restrict: 'A',
require: '^form',
link: function(scope, element, attrs, formCtrl) {
window.f = formCtrl;
scope.$watch(function() {return formCtrl.$valid;}, function(newVal, oldVal) {
console && console.log('newVal', newVal);
console && console.log('oldVal', oldVal);
});
var tweet = "Currently chilling out at W1B 2EL, then on to WC2E 8HA or maybe even L1 8JF! :-)";
// Here's a simple regex that tries to recognise postcode-like strings.
// See http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom#Validation
// for the rules on how UK postcodes are formatted.
var postcode_regex = /[A-Z]{1,2}[0-9][0-9A-Z]?\s?[0-9][A-Z]{2}/g;
var postcodes = tweet.match(postcode_regex);
console.log(postcodes);
@thekarel
thekarel / sumAny.js
Created November 1, 2013 16:08
A helper function that can add (sum) strings and numbers AS NUMBERS, ie. "1"+2 will return 3 and not "12". Requires _
var sumAny = function() {
var sum = 0;
_.each(arguments, function(arg) {
var _num;
_num = parseInt(arg);
if (!isNaN(_num)) sum += _num;
});
return sum;
}
@thekarel
thekarel / types.js
Created November 3, 2013 02:24
Testing functions for JS types -- more @ https://github.com/thekarel/jstypes
exports.isString = function(v) {
return (typeof v === 'string');
}
exports.isInteger = function(v) {
return (parseInt(v) === v)
}
exports.isFloat = function(v) {
return ((v % 1) > 0)
var re = new RegExp("regex","g");
// or
new RegExp(someObj.path).test(url)
// also
// this.appConfig.appPath.photo = 'photo/'
var re = new RegExp(this.appConfig.appPath.photo+'+.');
@thekarel
thekarel / boxsizing.css
Created November 20, 2013 15:14
box sizing
/*
The width and height properties include the padding and border, but not the margin.
https://developer.mozilla.org/en-US/docs/Web/CSS/box-sizing
Default: content-box
*/
element {
box-sizing: border-box;
}
function writeScreenshot(data, name) {
name = name || 'ss.png';
var screenshotPath = 'C:\\selenium_local_map\\';
fs.writeFileSync(screenshotPath + name, data, 'base64');
};
driver.takeScreenshot().then(function(data) {
writeScreenshot(data, 'out1.png');
});