Skip to content

Instantly share code, notes, and snippets.

View joeriks's full-sized avatar
💭
I may be slow to respond.

Jonas Eriksson joeriks

💭
I may be slow to respond.
View GitHub Profile
@joeriks
joeriks / member.peg
Created January 15, 2014 07:36
peg grammar for simple syntax name:type
start
= (member:MEMBER "\n"+ {return member})*
MEMBER = c:[a-z]+ " "* t:(":" t:MEMBERTYPE{return t})? {return {name:c.join(""),type:t}}
MEMBERTYPE = type:('key'/'number'/'string') {return type;}
/*
@joeriks
joeriks / gist:7959769
Created December 14, 2013 14:26
trying out SelectIf syntax for FnX
var result = someTest()
.SelectIf(someOtherTest)
.SelectIf(aThirdTest);
if any test fails, the tests will not continue.
@joeriks
joeriks / trigger.sql
Created December 4, 2013 14:24
sql trigger - revert value if condition is met
ALTER TRIGGER reset_deleted
ON mytable
AFTER update
AS
IF (SELECT count(*) FROM inserted WHERE [type]='sometype' AND [deleted]=1)>0
BEGIN
SET NOCOUNT ON;
UPDATE mytable SET [deleted]=0 WHERE id in (SELECT id FROM inserted WHERE [type]='sometype' AND [deleted]=1)
END
GO
@joeriks
joeriks / render.cs
Last active December 29, 2015 06:09
Render nunjucks template with the help of ClearScript
using (var engine = new V8ScriptEngine())
{
engine.Execute("var window = {}"); // easiest way I found to keep the nunjucks reference somewhere
using (var client = new WebClient())
{
// get the nunjucks js source
var nunjucks = client.DownloadString("http://jlongster.github.io/nunjucks/files/nunjucks.js");
// run it
engine.Evaluate(nunjucks);
@joeriks
joeriks / anonymous-logic.md
Last active December 28, 2015 21:29
Razor recommendations for Umbraco - Mimimize anonymous logic inside the view block

Mimimize anonymous logic inside the view block:

@if (Model.TopImageOrMedia!=116 && Model.MediaArea=="" && Model.TopImage!="") {
    <img ... />
}

instead specify the intention of the pieces of your code before you use them

@{

var showTopImageOrMedia = (Model.TopImageOrMedia==116); // dropdown const value

@joeriks
joeriks / index.cshtml
Last active December 28, 2015 20:49
The most basic (clear text, database free) type of authentication for an asp.net site, using Razor syntax. All .cshtml pages will be protected.
@* needs to be in the root (this makes asp net setup cshtml file type automatically) *@
@* this file and all .cshtml files (other than login) is protected *@
<html>
<body>
Welcome authenticated user
</body>
</html>
@joeriks
joeriks / memoize.cs
Last active December 27, 2015 18:19
Memoize by Joe Albahari https://t.co/TvXLvFqoOR
void Main()
{
Expensive(100).Dump(); // takes 2 secs
Expensive(100).Dump(); // immediate
ExpensiveOther(100).Dump(); // takes 2 secs
ExpensiveOther(100).Dump(); // immediate
}
@joeriks
joeriks / gist:7332368
Created November 6, 2013 07:42
Behaviour Driven Bugfixing
"This returns what it should"
.OldAssembly.ActuallyWorksWhen(...)
"But it obviously fails when we call it with x"
.OldAssembly.FailsWhen(...)
"New version should fix the errors"
.NewAssembly.ShouldStillWork()
.NewAssembly.ShouldBeFixed()
@joeriks
joeriks / gist:7193672
Created October 28, 2013 09:12
return filtered result with ServiceStack & F#
type ClientService() =
inherit Service()
member this.Any (req:ClientsRequest) =
Services.Data.Client.All()
|> filterType req
|> filterCity req
|> filterName req
@joeriks
joeriks / htmlviews.ts
Last active December 26, 2015 01:29
Sample creating HTML in code in Typescript
module HTML {
export function elem(tag:string, content:string){
return "<" + tag + ">" + content + "</" + tag + ">"
}
export function div(inner:string[]){
return elem("div",inner.join(""))
}
export function p(content:string){
return elem("p",content)