Skip to content

Instantly share code, notes, and snippets.

View nmenglund's full-sized avatar

Nils Magnus Englund nmenglund

  • Step One AS
  • Oslo, Norway
View GitHub Profile
@nmenglund
nmenglund / gather_discount_coeffs.php
Last active June 21, 2017 17:30
woocommerce-bulk-discount 2.4.5 performance improvements in gather_discount_coeffs
<?php
/**
* Performance improved gather_discount_coeffs function
* code adapted from woocommerce-bulk-discount 2.4.5
* by Nils Magnus Englund <nme@stepone.no>
*
* NOTE: The code below is only compatible with WooCommerce 3.0 as-is.
* Should be easy enough to backport.
*
@nmenglund
nmenglund / PersistentArray.php
Created November 6, 2014 12:50
PersistentArray, an ArrayObject extension to maintain state in a JSON file (for running batch scripts etc.)
<?php
/*
* Example:
*
* $store = PersistentArray::load('example.json');
* echo "Key is: " . (isset($store['key']) ? $store['key'] : 'not set') . "\n";
* $store['key'] = '123';
*
* Running this twice will yield:

Keybase proof

I hereby claim:

  • I am nmenglund on github.
  • I am nme (https://keybase.io/nme) on keybase.
  • I have a public key whose fingerprint is 5D8F 6D97 6BB4 3BA2 9BE2 071B 57FA 9ACC 8321 63A0

To claim this, I am signing this object:

@nmenglund
nmenglund / String.format.js
Created September 17, 2013 08:25
JS: Simple string formatter
String.prototype.format = function() {
var args = arguments;
return this.replace(/{(\d+)}/g, function(match, number) { return ((typeof args[number] != 'undefined') ? args[number]: match); });
};
@nmenglund
nmenglund / String.splice.js
Created September 17, 2013 08:25
JS: Insert a string into the given string at position idx, after removing 'rem' existing characters
// Insert a string into the given string at position idx, after removing 'rem' existing characters
String.prototype.splice = function(idx, rem, s) {
return (this.slice(0,idx) + s + this.slice(idx + Math.abs(rem)));
}
@nmenglund
nmenglund / String.repeat.js
Created September 17, 2013 08:24
JS: Repeat a given string 'num' times
// Repeat a given string 'num' times
String.prototype.repeat = function(num)
{
if (num == 1)
return this.toString();
else if (num < 1)
return "";
else
return new Array(+num + 1).join(this.toString());
}
@nmenglund
nmenglund / Number.formatNordic.js
Created September 17, 2013 08:23
Nordic number formatter; using , as decimal point when precision>0, and space as thousand separator otherwise (made for a pretty specific application)
// Nordic number formatter; using , as decimal point when precision>0,
// and space as thousand separator otherwise
Number.prototype.formatNordic = function(precision) {
precision = precision || 0;
var rounded = Math.round(+this * Math.pow(10, precision)) / Math.pow(10, precision);
var result = rounded.toString().replace(".",",");
if (+precision > 0) {
var pos = result.indexOf(',');
if (pos == -1)
result += "," + '0'.repeat(precision);