Skip to content

Instantly share code, notes, and snippets.

View aeinbu's full-sized avatar

Arjan Einbu aeinbu

View GitHub Profile
@aeinbu
aeinbu / cookieCutter.js
Last active November 15, 2015 14:59
A small utility to read ASP.NET issued cookies from javascript. (Cookies and sub-cookies)
;(function(){
function cookieCutter(cookieName, subCookieName) {
var name = cookieName + "=";
if (subCookieName) {
name += subCookieName + "=";
}
var cookie = document.cookie + ";";
var cookieValueStart = cookie.indexOf(name) + name.length;
var val = cookie.substring(cookieValueStart, cookie.indexOf(";", cookieValueStart));
const angular = require("angular");
angular
.module("modulename")
.directive('attachFocusFunction', function($parse) {
return {
scope: false,
link(scope, element, attributes) {
let expr = $parse(attributes.attachFocusFunction);
if(!expr.assign){
const angular = require("angular");
const _ = require("lodash");
angular
.module("modulename")
.directive("componentBinder", function($compile, $parse) {
return {
restrict: "E",
scope: {
componentName: "<",
},
@aeinbu
aeinbu / awaitable.js
Created April 12, 2017 11:06
Function to turn node style functions with callbacks into promises. This is useful when using async/await, since promises can be awaited upon.
// Function to turn node style functions with callbacks into promises.
// This is useful when using async/await, since promises can be awaited upon.
const callback = (resolve, reject) => (err, res) => err ? reject(err) : resolve(res);
const awaitable = async action => new Promise((resolve, reject) => action(callback(resolve, reject)))
module.exports = awaitable;
// Usage:
// const fs = require("fs");
// const awaitable = require("./awaitable");
define([], function () {
"use strict";
function parse(querystring) {
var obj = {};
if (querystring) {
var params = querystring.split(/&|\?/);
for (var i = 0; i < params.length; i++) {
var param = params[i];
@aeinbu
aeinbu / RedisExtensions.cs
Created September 28, 2015 13:16
Simple extension to make it easier to use Redis Caching and StackExchange.Redis package
using System;
using StackExchange.Redis;
namespace ConsoleApplication1
{
public static class RedisExtensions
{
public static RedisValue Get(this IDatabase db, RedisKey key, Func<RedisValue> loadFunc, TimeSpan slidingDuration)
{
var ret = db.StringGet(key);
import ko from 'knockout';
import $ from 'jquery';
var templateFromUrlLoader = {
loadTemplate(name, templateConfig, callback) {
if (templateConfig.fromUrl) {
var fullUrl = templateConfig.fromUrl;
$.get(fullUrl, markupString => {
ko.components.defaultLoader.loadTemplate(name, markupString, callback);
});
// traditional version
function toggleValueInArray1(val, arr) {
var ix = arr.indexOf(val);
if(ix === -1){
// add to array
arr.push(val);
}
else
{
// remove from array
@aeinbu
aeinbu / wrapInReadOnlyProxy.js
Last active July 26, 2017 08:26
This method creates a read only wrapper around a javascript object, so that you can pass large object without copying them and still avoiding that they are changed.
function wrapInReadOnlyProxy(orig, throwOnSet = false) {
if (typeof orig !== "object") {
return orig;
}
return new Proxy(orig, {
get: function(target, property) {
if (property in target) {
return wrapInReadOnlyProxy(target[property]);
}
@aeinbu
aeinbu / combinePredicates.js
Last active August 14, 2018 08:38
Combine predicates for filtering
const combinePredicates = (predicate1, predicate2, ...predicateRest) =>
(item) =>
predicate1(item) && (predicateRest.length > 0? combinePredicates(predicate2, predicateRest[0]): predicate2)(item);
// sample predicates
const exclude = (val) => (item) => item !== val;
const excludeRange = (from, to) => (item) => item < from || item > to;
// sample data
let arr = [1,2,3,4,5,6,7,8,9,10];