Skip to content

Instantly share code, notes, and snippets.

View stemar's full-sized avatar

Steven Marshall stemar

View GitHub Profile
@stemar
stemar / blank.rb
Last active May 23, 2016 23:01
blank? method in Ruby
def blank?
self.respond_to?(:empty?) ? self.empty? : !self
end
@stemar
stemar / is_blank.py
Last active October 1, 2015 12:48
is_blank() method in Python
def is_blank(value):
return False if value is not False and value in [0, 0.0] else not value
@stemar
stemar / array_keep.php
Last active March 27, 2026 19:41
Select a subset of an associative array by providing the keys.
<?php
/**
* Select a subset of an associative array by providing the keys.
*
* @param array $assoc
* @param array $keys
* @return array
*/
function array_keep(array $assoc, array $keys) {
return array_intersect_key($assoc, array_flip($keys));
@stemar
stemar / mb_substr_replace.php
Last active October 24, 2024 09:33
Multibyte substr_replace(). The mbstring library PHP < 7.x doesn’t come with a multibyte equivalent of substr_replace(). This function behaves exactly like substr_replace() even when the arguments are arrays.
<?php
function mb_substr_replace($string, $replacement, $start, $length=NULL) {
if (is_array($string)) {
$num = count($string);
// $replacement
$replacement = is_array($replacement) ? array_slice($replacement, 0, $num) : array_pad(array($replacement), $num, $replacement);
// $start
if (is_array($start)) {
$start = array_slice($start, 0, $num);
foreach ($start as $key => $value)
@stemar
stemar / gist:327e34a0b2bebd11adb5
Last active January 18, 2024 10:50
Adding target="_blank" or any other attribute to a Markdown link
Markdown:
[JP Markdown](https://wordpress.org/plugins/jetpack-markdown){.target-blank .rel-nofollow}
JavaScript (just before the closing </body> tag):
<script>
jQuery('.target-blank').attr('target', '_blank').removeClass('target-blank').filter('[title=""]').removeAttr('title').filter('[class=""]').removeAttr('class');
jQuery('.rel-nofollow').attr('rel', 'nofollow').removeClass('rel-nofollow').filter('[title=""]').removeAttr('title').filter('[class=""]').removeAttr('class');
</script>
Post-jQuery HTML:
@stemar
stemar / p.js
Last active August 29, 2015 14:16
JavaScript function that replaces named placeholders in a string template. http://jsfiddle.net/mobc4vss/8/
// Usage: "abc{def}ghi".p({def:"xyz"}); => "abcxyzghi"
String.prototype.p = function(obj) {
return this.replace(/\{[^\}]+\}/g, function(key) {
return obj[key.replace(/^\{([^\}]+)\}$/, "$1")] || key;
});
};
@stemar
stemar / JavaScript window.location
Last active August 29, 2015 14:16
Extend JavaScript window.location with 2 properties
window.location.href returns the URL of the current page
http://www.example.org:8888/foo/bar/index.html?q=baz&w=io#bang
window.location.origin returns the protocol, hostname and port number of a URL
http://www.example.org:8888
window.location.protocol returns the web protocol used (http:// or https://)
http:
window.location.hostname returns the domain name of the web host
@stemar
stemar / trim.js
Created March 16, 2015 23:03
trim.js
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g, "");
};
@stemar
stemar / timezone_abbreviation.js
Last active April 14, 2021 18:01
Get timezone abbreviation from date object or date string
var timezoneAbbreviation = function(timezoneString) {
var timezoneAbbreviations = {
"Afghanistan Time": "AFT",
"Alaska Daylight Time": "AKDT",
"Alaska Standard Time": "AKST",
"Amazon Summer Time": "AMST",
"Amazon Time": "AMT",
"Arabia Standard Time": "AST",
"Argentina Time": "ART",
"Armenia Summer Time": "AMST",
@stemar
stemar / filter_html_attribute.rb
Last active September 15, 2019 04:34
Ruby helpers to filter out unwanted HTML class names or style properties, including example to filter out Microsoft Office classes and styles
class String
def filter_html_attribute(attribute_key, args={})
self.gsub!(/#{attribute_key}="([^"]*)"/) do |attribute|
return "" if attribute.nil?
attribute_value = $1.split(args[:split]).delete_if {|i| i.match(args[:match]) }.join(args[:join])
attribute_value += args[:append].to_s unless attribute_value.empty?
attribute.replace("#{attribute_key}=\"#{attribute_value}\"")
end
self.gsub!(/ (#{attribute_key})=""/, "")
end