Skip to content

Instantly share code, notes, and snippets.

@zaus
zaus / FormRepo.js
Last active January 13, 2023 01:38
Preserve form values across page loads -- i.e. persistent forms. Uses `localStorage` to persist, `jQuery` for utilities.
var FormRepo = function (namespace) {
/// <summary>Persistent form values, saves to localStorage</summary>
/// <param name="namespace" type="String">the namespace to store values in localStorage</param>
// should also protect per page, since we could have the same forms in various places
this.N = namespace + '.' + window.location.pathname;
};
$.extend(FormRepo.prototype, {
namespace: function (key) {
return this.N + '.' + key;
@zaus
zaus / bkmklt-replace-in-selected-text.js
Last active August 4, 2022 11:20
Bookmarklet - Replace in Selected Text
(function() {
function getSelectionText() {
// https://stackoverflow.com/questions/5379120/get-the-highlighted-selected-text
var text = "";
if (window.getSelection) {
text = window.getSelection().toString();
} else if (document.selection && document.selection.type != "Control") {
text = document.selection.createRange().text;
}
return text;
@zaus
zaus / demo-output-GetPropertyName.linqpad.cs
Last active October 2, 2021 20:20
From StackOverflow discussion http://stackoverflow.com/questions/671968/retrieving-property-name-from-lambda-expression/17220748#17220748 -- demonstrating the behavior of various solutions via LinqPad to answer questions in the comments. Usage: LinqPad as "C# Program".
void Main()
{
// from comment discussion http://stackoverflow.com/questions/671968/retrieving-property-name-from-lambda-expression/17220748#17220748
Expression<Func<O, int>> exprId = o => o.Id;
Expression<Func<O, string>> exprName = o => o.Name;
Expression<Func<O, int[]>> exprItems = o => o.Items;
Expression<Func<O, int>> exprItemsLength = o => o.Items.Length;
Expression<Func<O, Subclass>> exprChild = o => o.Child;
Expression<Func<O, string>> exprChildName = o => o.Child.Name;
@zaus
zaus / +enumerate files method comparisons.md
Last active April 6, 2021 11:56
EnumerateFiles filtering on multiple extensions -- performance comparison of various methods. From discussion at http://stackoverflow.com/questions/163162/can-you-call-directory-getfiles-with-multiple-filters
@zaus
zaus / LinqPad.MyExtensions.cs
Last active February 20, 2021 15:42
Linqpad Extensions - `.Title()`, `.DumpFormat(tokens)`, `.Perf(task)`, `.Vs(tasks)`
/// <summary>Silly convenience initializer for creating Vs tasks with a dictionary;
/// can use <code>new Perf { { "task1", n => whatever }, {"task2", n => bar}...}.Vs()</code>
/// <para>see http://stackoverflow.com/a/14000325/1037948 </para>
/// </summary>
/// <remarks>Doesn't automatically call <see cref="Vs"/> because...</remarks>
public class Perf : Dictionary<string, Action<int>> {}
/// <summary>Silly convenience initializer for creating Vs tasks with a dictionary that can also provides some output;
/// can use <code>new Perf&lt;T&gt; { { "task1", n => whatever }, {"task2", n => bar}...}.Vs()</code>
/// <para>see http://stackoverflow.com/a/14000325/1037948 </para>
@zaus
zaus / WpPluginUpgradeBase.php
Last active October 2, 2020 17:10
Wordpress Activation/Upgrade Hook Helper
<?php
if(!class_exists('WpPluginUpgradeBase')) :
abstract class WpPluginUpgradeBase {
function __construct() {
add_action( 'admin_init', array(&$this, 'load_plugin') );
}
@zaus
zaus / ConfigurableJsMinify.cs
Last active January 10, 2020 02:16
How to create a custom BundleTransform in .NET MVC4, specifically for the purposes of not renaming variables. Since I could not find this after an hour of concentrated interweb searching...
using System.IO;
using Microsoft.Ajax.Utilities;
/// <summary>
/// Minify javascript files with externally overridable configuration settings.
/// </summary>
public class ConfigurableJsMinify : StandardFileBundleTransform {
protected bool includeFilenamesInOutput;
@zaus
zaus / DelayedRenderExtensions.cs
Created September 27, 2013 18:06
RenderSections in PartialViews -- delayed rendering of any HTML content. See SO answer: http://stackoverflow.com/a/18790222/1037948
public static class HtmlRenderExtensions {
/// <summary>
/// Delegate script/resource/etc injection until the end of the page
/// <para>@via http://stackoverflow.com/a/14127332/1037948 and http://jadnb.wordpress.com/2011/02/16/rendering-scripts-from-partial-views-at-the-end-in-mvc/ </para>
/// </summary>
private class DelayedInjectionBlock : IDisposable {
/// <summary>
/// Unique internal storage key
/// </summary>
/// <summary>
/// PGP round-trip encryption -- http://blogs.microsoft.co.il/kim/2009/01/23/pgp-zip-encrypted-files-with-c/#comment-611002
/// </summary>
public class PGPEncryptDecrypt
{
/**
* A simple routine that opens a key ring file and loads the first available key suitable for
* encryption.
*
* @param in
@zaus
zaus / $.stripClass.js
Created September 27, 2013 20:31
jQuery.removeClass companion -- strips out matching segments
$.fn.stripClass = function (partialMatch, endOrBegin) {
/// <summary>
/// The way removeClass should have been implemented -- accepts a partialMatch (like "btn-") to search on and remove
/// </summary>
/// <param name="partialMatch">the class partial to match against, like "btn-" to match "btn-danger btn-active" but not "btn"</param>
/// <param name="endOrBegin">omit for beginning match; provide a 'truthy' value to only find classes ending with match</param>
/// <returns type=""></returns>
var x = new RegExp((!endOrBegin ? "\\b" : "\\S+") + partialMatch + "\\S*", 'g');
// http://stackoverflow.com/a/2644364/1037948