Skip to content

Instantly share code, notes, and snippets.

View ScottKaye's full-sized avatar
🌎

Scott Kaye ScottKaye

🌎
View GitHub Profile
@ScottKaye
ScottKaye / Hide URL Parameters.js
Last active April 18, 2024 12:30
Hide URL parameters using Javascript's history.replaceState methods. The server (PHP or whatever) will still see it of course, but this script will quickly 'hide' the parameters. Don't use this to hide anything sensitive - there shouldn't be anything sensitive in GET anyway, but I use this to hide various un-pretty things like ?error=invalidPass…
function getURLParameter(name) {
return decodeURI((RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]);
}
function hideURLParams() {
//Parameters to hide (ie ?success=value, ?error=value, etc)
var hide = ['success','error'];
for(var h in hide) {
if(getURLParameter(h)) {
history.replaceState(null, document.getElementsByTagName("title")[0].innerHTML, window.location.pathname);
}
@ScottKaye
ScottKaye / HandyDandyPrototypes.js
Last active July 10, 2023 19:16
A collection of potentially useful prototypes to make some things easier. Each of these should be crushable with http://www.iteral.com/jscrush/ . "Don't modify objects you don't own" is completely thrown out the window here in favour of coolness.
//Get a range of numbers between two numbers
//Usage: [1, 10].range returns [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Object.defineProperty(Array.prototype, "range", {
get: function () {
var range = [this[0]], i;
for (var i = this[0], len = this[1]; i < len; range.push(++i));
return range;
}
});
// Golfed TypeScript:
<svg class="loader" width="60" height="60" xmlns="http://www.w3.org/2000/svg">
<g>
<ellipse ry="25" rx="25" cy="30" cx="30" stroke-width="5" stroke="#000" fill="none"/>
</g>
</svg>
@ScottKaye
ScottKaye / TrimFileLines.cs
Created September 22, 2018 01:35
Reads X lines from a file, stopping each line after Y characters.
void Main()
{
var path = @"R:\test.txt";
const int MAX_LINE_LENGTH = 15;
const int MAX_LINE_COUNT = 15;
using (var stream = GetTruncatedFile(path, MAX_LINE_LENGTH, MAX_LINE_COUNT))
using (var reader = new StreamReader(stream))
{
void Main()
{
foreach (var file in Directory.GetFiles(@"X:\Scott\Projects\Test XML Files", "*.xml"))
{
var doc = XDocument.Load(file);
var expando = XmlToDict.Convert(doc.Root.Elements(), new LibertyTransformer());
expando.Dump(20);
JsonConvert.SerializeObject(expando, Newtonsoft.Json.Formatting.Indented).Dump();
void Main()
{
var bytes = new byte[] { 0, 0, 1, 0, 255, 64 };
var reader = new ByteReader(bytes);
reader.Read<int>().Dump();
reader.Read<byte>().Dump();
reader.Read<sbyte>().Dump();
reader.Read<byte>(4).Dump();
}
void Main()
{
string input = Console.ReadLine();
input.EatHorror("abcdefg");
input.Dump("Eaten");
(input + " - Appended").Dump("Fix?");
"hello".Dump("Try entering \"hello\"");
}
public static class Benchmark
{
/// <summary>
/// Returns the total number of milliseconds elapsed running a function [iterations] times.
/// </summary>
/// <see cref="http://stackoverflow.com/a/1048708/382456" />
public static double Run(int iterations, Action func)
{
Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
Thread.CurrentThread.Priority = ThreadPriority.Highest;
/// <summary>
/// An in-memory repository of T.
/// </summary>
/// <typeparam name="T">Type of entities to emulate.</typeparam>
public class FakeRepository<T> : IRepository<T>
where T : class
{
private readonly PropertyInfo _key;
private readonly Dictionary<int, T> _entities;
@ScottKaye
ScottKaye / getParams.js
Last active May 1, 2016 04:24
Getting the querystring into an array throughout the ages
// ES3
var getParams = function() {
var results = [], parts = window.location.search.slice(1).split("&"), i, len;
for (i = 0, len = parts.length; i < len; ++i) results.push(parts[i].split("="));
return results;
};
console.log(getParams());
// ES6
const getParams = () => window.location.search.slice(1).split("&").map(q => q.split("="));