Skip to content

Instantly share code, notes, and snippets.

@rtmalone
rtmalone / create_apex_class.sh
Created September 20, 2023 20:17
Create Apex Class script
#!/bin/bash
# Function to extract sourceApiVersion from sfdx-project.json
get_source_api_version() {
local json_file="$1"
local version=$(jq -r '.sourceApiVersion' "$json_file")
echo "$version"
}
# Check if the correct number of arguments are provided
@rtmalone
rtmalone / gist:0357698feb741caba49706cc2ca6ac38
Created December 10, 2021 20:33
LWC Event callback to report validity of fields in a form
/**
* Uses a JS Map structure to easily set & enforce unique keys.
* Map Iterator is turned into an Array then ultimately reduced to
* a single boolean for the Save button to disable against
*
* @param {Event} event
*/
formValidityCallback(event) {
const { fieldId, isValid } = event.detail;
@rtmalone
rtmalone / validateEmail.js
Created June 3, 2019 15:16
Regex for valid email
// https://www.w3.org/TR/html5/forms.html#valid-e-mail-address
const remail = /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
const copyToClipboard = str => {
const el = document.createElement('textarea'); // Create a <textarea> element
el.value = str; // Set its value to the string that you want copied
el.setAttribute('readonly', ''); // Make it readonly to be tamper-proof
el.style.position = 'absolute';
el.style.left = '-9999px'; // Move outside the screen to make it invisible
document.body.appendChild(el); // Append the <textarea> element to the HTML document
const selected =
document.getSelection().rangeCount > 0 // Check if there is any content selected previously
? document.getSelection().getRangeAt(0) // Store selection if found
@rtmalone
rtmalone / responsivefySvg.js
Created February 2, 2018 20:08
Make D3 SVG responsive
function responsivefy(svg) {
// get container + svg aspect ratio
var container = d3.select(svg.node().parentNode),
width = parseInt(svg.style("width")),
height = parseInt(svg.style("height")),
aspect = width / height;
// add viewBox and preserveAspectRatio properties,
// and call resize so that svg resizes on inital page load
svg.attr("viewBox", "0 0 " + width + " " + height)
@rtmalone
rtmalone / dedup.js
Created July 14, 2017 16:39
Deduplicate Array
// Filters duplicates from an array of Primitives (String, Bool, Num, null, undefined, Symbol)
const returnedArray = array
.map(obj => obj.name)
.filter((elem, idx, arr) => arr.indexOf(elem) === idx);
@rtmalone
rtmalone / gist:48789c81c866ef5d41e1cf19a46c72fc
Last active July 13, 2017 15:26
Turn Firebase snapshot into Array
// as if in a utils.js file
const isObject = obj => {
return Object.prototype.toString.call(obj) === '[object Object]'
? true
: false;
};
export const toArray = snap => {
const array = [];
const _location = window.location
const uAgent = window.navigator.userAgent.toLowercase()
const query = <grab the query params according to your code base>
if (uAgent.match(/iphone|ipod|ipad/i)) {
handleIOS(location, query)
} else if (uAgent.match(/android/i)) {
handleAndroid(location, query) {
} else {
<User is on a desktop; I just logged a message to the console>
Name of thing Sorta like... Mounted? Can you even setState? What would you say... ya do here?
constructor initialize() nope nope init stuff NO side effects
componentWillMount beforeDomReady() nope yeah but don't Only needed in createClass now use constructor for most things
render render nope please no render stuff and don't set any state please
componentDidMount domReady() yup yup DOM is a go init jQuery plugins dispatch stuff
componentWillReceiveProps onChange() yup yup
@rtmalone
rtmalone / SampleComponent.js
Created April 12, 2017 04:47 — forked from yamadayuki/SampleComponent.js
Use keyframes property with React using inline style
import React from 'react';
import injectStyle from './path/to/injectStyle';
export default class SampleComponent extends React.Component {
constructor(props) {
super(props);
const keyframesStyle = `
@-webkit-keyframes pulse {
0% { background-color: #fecd6d; }