Skip to content

Instantly share code, notes, and snippets.

@MrChocolatine
MrChocolatine / TS - More precise return type method Date#toISOString.d.ts
Last active February 22, 2024 22:06
TypeScript – How to accurately type dates in ISO 8601 format
// In TS, interfaces are "open" and can be extended
interface Date {
/**
* Give a more precise return type to the method `toISOString()`:
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString
*/
toISOString(): TDateISO;
}
type TYear = `${number}${number}${number}${number}`;
@lukebrandonfarrell
lukebrandonfarrell / collapsable.js
Last active January 29, 2024 14:49
Collapsable header in React Native
import React, { useRef } from "react";
import { View, Animated, Image, ScrollView, Text } from "react-native";
const H_MAX_HEIGHT = 150;
const H_MIN_HEIGHT = 52;
const H_SCROLL_DISTANCE = H_MAX_HEIGHT - H_MIN_HEIGHT;
const CollapsibleHeader = () => {
const scrollOffsetY = useRef(new Animated.Value(0)).current;
const headerScrollHeight = scrollOffsetY.interpolate({
@ramsunvtech
ramsunvtech / groupBy.js
Created September 25, 2018 15:07
Group By - ES6
function groupBy(list, props) {
return list.reduce((a, b) => {
(a[b[props]] = a[b[props]] || []).push(b);
return a;
}, {});
}
// Usage.
groupBy([{
id: 1,
@lukasbach
lukasbach / deepReplace.js
Created July 5, 2018 17:55
Replace object members by member name in a deeply nested object
const deepReplace = (obj: object, keyName: string, replacer: (from: any) => string) => {
for (const key in obj) {
if (key === keyName) {
obj[key] = replacer(obj[key]);
} else if (Array.isArray(obj[key])) {
(obj[key] as any[]).forEach(member => deepReplace(member, keyName, replacer));
} else if (typeof obj[key] === "object") {
deepReplace(obj[key], keyName, replacer);
}
}
@mattiasghodsian
mattiasghodsian / functions.php
Last active April 28, 2023 08:38
Woocommerce Get product attributes
/**
* Title: Woocommerce Get product attributes
* Author: Mattias Ghodsian
* Description: Get product attributes in a simple way (array)
* Donate a cup of coffee: https://www.buymeacoffee.com/mattiasghodsian
* Donate Eth: 0xBBB96204E45D11C9799c6B12E6eE6F0d4A071Ef5
*
* @param integer $postID
* return array
**/
@kharakhordindemo
kharakhordindemo / form.php
Created December 22, 2017 15:21
Remove span in Contact Form 7
/*Contact form 7 remove span*/
add_filter('wpcf7_form_elements', function($content) {
$content = preg_replace('/<(span).*?class="\s*(?:.*\s)?wpcf7-form-control-wrap(?:\s[^"]+)?\s*"[^\>]*>(.*)<\/\1>/i', '\2', $content);
$content = str_replace('<br />', '', $content);
return $content;
});
@cryptexvinci
cryptexvinci / remove_checkout_fields.php
Last active March 3, 2024 00:43
Remove fields from WooCommerce checkout page.
<?php
add_filter( 'woocommerce_checkout_fields' , 'custom_remove_woo_checkout_fields' );
function custom_remove_woo_checkout_fields( $fields ) {
// remove billing fields
unset($fields['billing']['billing_first_name']);
unset($fields['billing']['billing_last_name']);
unset($fields['billing']['billing_company']);
@ivorpad
ivorpad / get_sc_attr.php
Created October 29, 2016 20:48
Grab all attributes for a given shortcode in a text
<?php
/**
* Grab all attributes for a given shortcode in a text
*
* @uses get_shortcode_regex()
* @uses shortcode_parse_atts()
* @param string $tag Shortcode tag
* @param string $text Text containing shortcodes
* @return array $out Array of attributes
@srikat
srikat / class-custom-featured-post.php
Last active April 3, 2022 16:29 — forked from GaryJones/readme.md
Custom Featured Posts Widget plugin: Skeleton for amending the output of the Genesis Featured Posts widget. https://sridharkatakam.com/custom-featured-post-widget-plugin/
<?php
/**
* Plugin Name
*
* @package Custom_Featured_Post_Widget
* @author Gary Jones
* @license GPL-2.0+
* @link http://gamajo.com/
* @copyright 2013 Gary Jones, Gamajo Tech
*/
@tyxla
tyxla / insert-widget-in-sidebar.php
Created August 24, 2016 10:43
Insert a widget in a sidebar programmatically in WordPress
<?php
/**
* Insert a widget in a sidebar.
*
* @param string $widget_id ID of the widget (search, recent-posts, etc.)
* @param array $widget_data Widget settings.
* @param string $sidebar ID of the sidebar.
*/
function insert_widget_in_sidebar( $widget_id, $widget_data, $sidebar ) {