Skip to content

Instantly share code, notes, and snippets.

View KristofferK's full-sized avatar

Kristoffer KristofferK

View GitHub Profile
@KristofferK
KristofferK / InBetween.cs
Created February 11, 2018 12:19
Gets the substring inbetween two other substrings. InBetween("Hello beautiful world", "Hello ", " world") => "beautiful"
namespace Utils
{
public static class Util
{
public static string InBetween(string haystack, string afterThis, string beforeThis)
{
return InBetween(haystack, afterThis, beforeThis, 1, 0);
}
public static string InBetween(string haystack, string afterThis, string beforeThis,
@KristofferK
KristofferK / UpperWords.cs
Created February 11, 2018 12:17
Capitalizes words. "hello world" => "Hello World"
namespace Utils
{
public static class Util
{
public static string UpperFirstChar(string s)
{
if (s == null) return null;
if (s.Length == 1) return s.ToUpper();
if (s.Length > 1) return char.ToUpper(s[0]) + s.Substring(1);
return s;
@KristofferK
KristofferK / GetFirstMatch.cs
Created February 11, 2018 12:16
Runs a regex on a string. Will return the first match.
namespace Utils
{
public static class Util
{
public static string GetFirstMatch(string pattern, string s, int index, bool removeDots = false)
{
if (s == null) return null;
Regex regex = new Regex(pattern);
Match m = regex.Match(s);
while (m.Success)
@KristofferK
KristofferK / StripTags.cs
Last active February 11, 2018 12:14
Converts a html string to a text string.
namespace Utils
{
public static class Util
{
public static string StripTags(object o, bool keepLinebreaks = false)
{
if (o == null) return null;
string s = o.ToString();
if (keepLinebreaks)
{
<?PHP
function generateToken($s) {
for ($i = 0; $i < 1000 + strlen($s) * 17; $i++) {
$s = hash_hmac('sha256', $s, 'Token #'.((7919+$i)/17).'static salt!');
}
return $s;
}
?>
@KristofferK
KristofferK / checkIfArrayHasNValuesOfSameDataTypeCohesively_oneLiner.js
Last active February 11, 2018 00:27
Checks if a given array has multiple items of the same data type next to each other. In the test code it checks if a given array has three numbers cohesively. Using reducer and only one line
const array1 = [1,2,'a',3,'b',4,5]
const array2 = [1,2,'a',3,'b',4,5,6,'k']
const checkIfArrayHasNValuesOfSameDataTypeCohesively = datatype => n => array => (array.reduce((acc, val) => (
{passed: acc.passed || (acc.n = typeof val != datatype ? 0 : acc.n + 1) >= n, n: acc.n}
), {n: 0, passed: false})).passed;
const hasThreeNumbersCohesively = checkIfArrayHasNValuesOfSameDataTypeCohesively('number')(3);
console.log(hasThreeNumbersCohesively(array1)); // false
console.log(hasThreeNumbersCohesively(array2)); // true
@KristofferK
KristofferK / checkIfArrayHasNValuesOfSameDataTypeCohesively2.js
Created February 10, 2018 21:53
Checks if a given array has multiple items of the same data type next to each other. In the test code it checks if a given array has three numbers cohesively. This time using array.reduce
const array1 = [1,2,'a',3,'b',4,5]
const array2 = [1,2,'a',3,'b',4,5,6,'k']
const checkIfArrayHasNValuesOfSameDataTypeCohesively = datatype => n => array => (array.reduce((acc, val) => {
if ((acc.n = typeof val != datatype ? 0 : acc.n + 1) >= n) acc.passed = true;
return acc;
}, {n: 0, passed: false})).passed;
console.log(checkIfArrayHasNValuesOfSameDataTypeCohesively('number')(3)(array1)); // false
console.log(checkIfArrayHasNValuesOfSameDataTypeCohesively('number')(3)(array2)); // true
@KristofferK
KristofferK / checkIfArrayHasNValuesOfSameDataTypeCohesively.js
Last active February 10, 2018 21:52
Checks if a given array has multiple items of the same data type next to each other. In the test code it checks if a given array has three numbers cohesively.
let array1 = [1,2,'a',3,'b',4,5]
let array2 = [1,2,'a',3,'b',4,5,6,'k']
const checkIfArrayHasNValuesOfSameDataTypeCohesively = datatype => n => array => {
let _n = 0;
for (let i = 0; i < array.length; i++) {
if (typeof array[i] == datatype) {
if (++_n >= n) return true;
}
else {
@KristofferK
KristofferK / GetPalindromePermutation.cs
Last active February 4, 2018 19:59
Attempts to get a permutation p of the string s, where p is a palindrome
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PermutationOfPalindrome
{
class Program
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PermutationOfPalindrome
{
class Program
{