Skip to content

Instantly share code, notes, and snippets.

{
"directory": "public/bower_components"
}
{
"directory": "public/bower_components"
}
@ranwahle
ranwahle / jsontryParse.js
Last active September 9, 2015 11:33
JSON-tryparse parses JSON (using JSON.parse) and returns promise to handle success / error of parsing
if (!JSON.tryParse)
{
JSON.tryParse = function (str) {
var successState=1, errorState = 2, result, errorResult,
promise = {
then: function(successCallback, errorCallback)
{
if (this.state === successState)
function getModule(moduleName){
console.log('Getting module ' + moduleName);
var module = angular.module(moduleName);
if (!module) {
consoile.error('Module ' + moduleName + ' was not found');
return;
}
for (var i = 0; i < module.requires.length; i++){
getModule(module.requires[i]);
}
@ranwahle
ranwahle / timepan.ts
Last active May 7, 2023 13:20
.Net-like timespan class in TypeScript
const MILLISECONDS_IN_A_SECOND: number = 1000;
const SECONDS_IN_A_MINUTE: number = 60;
const MINUTES_IN_AN_HOUR: number = 60;
const HOURS_IN_A_DAY: number = 24;
const DAYS_IN_A_WEEK: number = 7;
const MILLISECONDS_IN_A_MINUTE = MILLISECONDS_IN_A_SECOND * SECONDS_IN_A_MINUTE;
const MILLISECONDS_IN_AN_HOUR = MILLISECONDS_IN_A_MINUTE * MINUTES_IN_AN_HOUR;
const MILLISECONDS_IN_A_DAY = MILLISECONDS_IN_AN_HOUR * HOURS_IN_A_DAY;
const MILLISECONDS_IN_A_WEEK = MILLISECONDS_IN_A_DAY * DAYS_IN_A_WEEK;
if (window.fetch) {
const origFetch = fetch;
fetch = function(url, options) {
if (!options) {
options = {};
}
// Adding the creadentials option
options.credentials = 'include';
getFragments(url) {
if (url.endsWith('/')) {
url = url.substring(0, url.length - 1);
}
if (url.startsWith('/')) {
url = url.substring(1);
}
return url === '/' ? [''] : url.split('/');
@ranwahle
ranwahle / build-route-tree-from-fragments.js
Created May 12, 2019 11:38
Build routes tree from fragments
buildRoutesTreeFromFragments(urlFragments, routes) {
if (!routes.filter) {
throw new Error('Routes should be an array');
}
const candidates = routes.filter(route => this.isRouteSuitable(route, urlFragments));
if (candidates.length === 0) {
return null;
} else if (candidates.length === 1) {
return this.buildRouteObject(candidates[0], urlFragments);
} else {
@ranwahle
ranwahle / is-route-suitable.js
Created May 12, 2019 11:41
Is route suitable
isRouteSuitable(route, urlFragments) {
const routeURLFragments = this.getFragments(route.path);
let result = routeURLFragments.length === urlFragments.length;
for (let index = 0; result && index < routeURLFragments.length; index++) {
const fragment = routeURLFragments[index];
result = fragment.toLowerCase() === urlFragments[index].toLowerCase() || fragment.startsWith(':');
}
return result;
}
@ranwahle
ranwahle / use-self-routing-anchor.html
Created May 12, 2019 14:01
Use self routing anchor
<a is="self-routing-anchor" href='/path/to/your/target'>Inner content </a>