Skip to content

Instantly share code, notes, and snippets.

View jimmielemontgomery's full-sized avatar

Jim Montgomery jimmielemontgomery

View GitHub Profile
@jimmielemontgomery
jimmielemontgomery / api-stub-response-example.js
Last active September 27, 2018 23:48
api-stub-response-example
/*
a simple generalized example of stubbing an API response to save the effort of standing up or proxying APIs
for repeatable UI work prior to wiring up or staging
simply handle the success or fail response object, in this example it's just success
an object's data attribute has the desired API response body like: {data: API-response-JSON-here}
this specific case was derived from the jobs redux global handling and could easily be repurposed for any approach
note prexisting responses can be found via integration/staging/etc just
pretty-print app.js or whatever in dev tools, search and set breakpoints for fetch handling
*/
function mkjob(i, item={}){ return Object.assign({}, {
"id":"01539731542362","name":"0 job 1539731542362","triggerType":"USER_TRIGGERED","creator":"SYSTEM","submitDate":1539731538041,"startDate":1539731541128,"endDate":1539731542362,"status":"Completed"
}, item) }
function randomInclusive(max=1, min=0){
max = Math.ceil(max);
min = Math.floor(min);
// random <= [0, 0.9999] never 1
return Math.floor(Math.random() * (max - min + 1) + min);
}
function mkjobs(config={}){
@jimmielemontgomery
jimmielemontgomery / timezone
Last active November 2, 2018 21:46
localize and internationalize times including testing for desired result for valid times
/* samples LTR and RTL; Persian, Hebrew, Arabic, Thai, English forms, Korean, Chinese, Japanese:
"11/2/2018, 12:03:27 PM PDT"
"02/11/2018, 12:03:27 GMT-7"
"2018. 11. 2. 오후 12시 3분 27초 GMT-7"
"٢‏/١١‏/٢٠١٨ ١٢:٠٣:٢٧ م غرينتش-٧"
"2018/11/2 GMT-7 下午12:03:27"
"۱۳۹۷/۸/۱۱،‏ ۱۲:۰۳:۲۷ (‎−۷ گرینویچ)"
"30/11/2 12:03:27 GMT-7"
"2/11/2018 12.03.27 GMT-7"
"2.11.2018, 12:03:27 GMT-7‎"
/*
timeouts and other signaling
https://developers.google.com/web/updates/2017/09/abortable-fetch
basics
https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
*/
function fetchr(url, req={}){
const headers = Object.assign({
'X-Auth-Token': localStorage.authToken || ''
,'X-Organization-Id': localStorage.orgId || ''
@jimmielemontgomery
jimmielemontgomery / URL.searchParams
Last active November 14, 2018 19:07
URL composition related
https://developer.mozilla.org/en-US/docs/Web/API/URL
https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams
https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
https://developers.google.com/web/updates/2016/01/urlsearchparams?hl=en
var url = new URL(location.hash.replace(/^#/,''), location.origin);
url.searchParams.set('x',1234)
url.search
"?x=1234"
@jimmielemontgomery
jimmielemontgomery / proofs
Last active November 26, 2018 21:36
checks for invalidating assertions
// perf of keys and key length, generally Object.keys vs _.keys
function test(o, a, b, len=10){
var i = 0, name = a.name || 'run1';
console.time(name);
while(len > i++){
a(o).length;
}
console.timeEnd(name);
i = 0, name = b.name || 'run2';
console.time(name);
@jimmielemontgomery
jimmielemontgomery / utils
Last active November 29, 2018 21:22
shared utilities
/*
copy this into your devtools and run it;
for more info using devtools see: https://developers.google.com/web/tools/chrome-devtools/console/get-started#javascript
possibly save to a snippet in the devtools, or come back to the gist for updates
https://gist.github.com/jimmielemontgomery/ => choose "utils"
*/
function linkFinder(...flowIds){
var $layers = document.querySelector('.srd-link-layer') || {};
var react = Object.entries($layers).find((it)=>{ return /react/i.test(it[0]) }) || [];
return !react.length ? [] : react[1]._currentElement.props.children
@jimmielemontgomery
jimmielemontgomery / http-conventions
Last active February 10, 2019 09:42
HTTP error and status code handling
params-conventions:
pagination:
page=0 base zero; ui can adjust as-needed;
pageSize=3;
API sets defaults and limits as-desired;
eg https://app.elementanalytics.com/jobs/v1/org/1061177/runs?page=0&pageSize=3
'use strict';
/*
transform JSX to tagged template literals
history:
2019-01-25 initial working draft
available types:
JSX_TYPES = [