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));
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
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){
@aeinbu
aeinbu / array-helpers.js
Last active October 28, 2023 22:36
Array operations for useReducer or Redux with ES6+
// Returns a new array, with the element at pos replaced with replacement item or calling the replacement function on arr[pos]
function changeAtIndex(arr, pos, replacement) {
if (typeof replacement == "function") {
replacement = replacement(arr[pos]);
}
return [...arr.slice(0, pos), replacement, ...arr.slice(pos + 1)];
}
// Returns a new array, with the first element matching the predicate function replaced with replacement item or calling the replacement function on arr[pos]
const angular = require("angular");
const _ = require("lodash");
angular
.module("modulename")
.directive("componentBinder", function($compile, $parse) {
return {
restrict: "E",
scope: {
componentName: "<",
},
@aeinbu
aeinbu / tasks.json
Last active October 16, 2018 12:47
Visual Studio Code task for running mocha tests with problem matcher
{
"version": "0.1.0",
"command": "mocha",
"isShellCommand": true,
"showOutput": "silent",
"args": [
"--reporter",
"tap",
"--colors"
],
@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");