Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View kfiil's full-sized avatar

Kenneth Fiil kfiil

View GitHub Profile
@kfiil
kfiil / js-pad-date-zero.js
Created March 17, 2019 12:36
Javascript add leading zeroes to date
// Source: https://stackoverflow.com/a/12550320
function pad(n){return n<10 ? '0'+n : n}
/* use a function for the exact format desired... */
function ISODateString(d){
function pad(n){return n<10 ? '0'+n : n}
return d.getUTCFullYear()+'-'
+ pad(d.getUTCMonth()+1)+'-'
+ pad(d.getUTCDate())+'T'
@kfiil
kfiil / value-is-null-or-undefined.js
Created December 18, 2018 11:14
Check for null, undefined, or blank variables in JavaScript
/**
Test for "value is null or undefined" is
**/
if ( some_variable == null ){
// some_variable is either null or undefined
}
// So these two lines are equivalent:
if ( typeof(some_variable) !== "undefined" && some_variable !== null ) {}
if ( some_variable != null ) {}
@kfiil
kfiil / guid.js
Created October 1, 2015 06:20
Create GUID / UUID in JavaScript
//For an rfc4122 version 4 compliant solution, this one-liner(ish) solution is the most compact I could come up with
//http://stackoverflow.com/a/2117523
'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
//http://guid.us/GUID/JavaScript
function S4() {
return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
@kfiil
kfiil / custom-directives-shared-scope.js
Last active August 29, 2015 14:13
AngularJS kursus, custom directives shared scope
var app = angular.module('directivesModule');
app.controller('Controller', ['$scope', function ($scope) {
$scope.customer = {
name: 'David',
street: '1234 Anywhere St.'
};
}]);
app.directive('sharedScope', function () {
@kfiil
kfiil / templates-and-ng-include.html
Created January 13, 2015 19:30
AngularJS kursus, templates og ng-include
<div id="task-users" class="panel panel-default" ng-include="'/Angular-App/projektweb3/task-details/partial-templates/users.tpl.html'"></div>
<html ng-app>
<!-- Body tag augmented with ngController directive -->
<body ng-controller="MyController">
<input ng-model="foo" value="bar">
<!-- Button tag with ng-click directive, and string expression 'buttonText' wrapped in "{{ }}" markup -->
<button ng-click="changeFoo()">{{buttonText}}</button>
<script src="angular.js">
</body>
</html>
@kfiil
kfiil / isolateScopeWithController.js
Created January 13, 2015 18:45
AngularJS kursus, custom directive isolate scope with controller
angular.module('directivesModule')
.directive('isolateScopeWithController',function(){
return {
restrict:'EA',
scope: {datasource:'=',add:'&'},
controller: function($scope){
function init() {
$scope.customers=angular.copy($scope.datasource);
}
init();
@kfiil
kfiil / custom-directives.js
Last active August 29, 2015 14:13
AngularJS kursus, custom directives
angular.module('moduleName')
.directive('myDirective', function () {
return {
restrict: 'EA', //E = element, A = attribute, C = class, M = comment
scope: {
//@ reads the attribute string value,
//= provides two-way binding,
//& works with functions
title: '@'
},
@kfiil
kfiil / filters.html
Created January 12, 2015 20:38
AngularJS kursus, filters
<input type="checkbox" ng-model="strict"><br>
<table id="searchObjResults">
<tr><th>Name</th><th>Phone</th></tr>
<tr ng-repeat="friendObj in friends | filter:search:strict">
<td>{{friendObj.name}}</td>
<td>{{friendObj.phone}}</td>
</tr>
</table>
@kfiil
kfiil / substring.md
Last active May 4, 2017 05:29
Check whether a string contains a substring in #JavaScript.

List of current possibilities:

1. indexOf - (see bottom)

    var string = "foo", 
        substring = "oo"; 
    alert(string.indexOf(substring) > -1);
@kfiil
kfiil / removeElementFromArray.js
Created July 22, 2014 06:49
Remove specific element from an array in #JavaScript
//Credit http://stackoverflow.com/a/5767357
var array = [2, 5, 9];
var index = array.indexOf(5);
if (index > -1) {
array.splice(index, 1);
}