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]);
}
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>
@ranwahle
ranwahle / define-custom-element.js
Created May 12, 2019 14:04
Define custom element
customElements.define('self-routing-anchor', SelfRoutingAnchor, {extends: 'a'})