Skip to content

Instantly share code, notes, and snippets.

View techieshark's full-sized avatar

Peter W techieshark

View GitHub Profile
@techieshark
techieshark / is-non-empty-string.ts
Created September 25, 2023 03:40
Is string s a non empty string?
/**
* Is `s` both 1) defined, 2) a string, and 3) not empty?
* Can also be used as a TS type predicate to remove `undefined` values.
*/
export function isNonEmptyString(s?: string): s is string {
return Boolean(
typeof s === 'string' &&
s.length > 0
);
@techieshark
techieshark / findItemIndices.ts
Created December 12, 2022 10:02
Return a Map of items to their indices in the array #typescript
/**
* Return a Map of items to their indices in the array.
*
* The Map will have an entry for each item in A, with the item as the key and
* an array of indices as the value.
*
* There will be as many items in the Map as there are unique items in `A`.
*
* @param A an array of items of any type `T`
*
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@techieshark
techieshark / animation.css
Created June 22, 2021 05:32
slide-in text for website
@keyframes prep {
0% {
transform: translate(-150%);
}
100% {
transform: translate(-150%);
}
}
@keyframes slide {
@techieshark
techieshark / package.json
Last active December 29, 2020 05:31
Snowpack issue
{
"name": "anything",
"version": "0.1.0",
"dependencies": {
"@mapbox/mapbox-sdk": "^0.10.0",
"@mapbox/polyline": "^1.1.0",
"@material-ui/core": "^4.8.2",
"@material-ui/icons": "^4.5.1",
"@turf/circle": "^6.2.0-alpha.1",
"@turf/distance": "^6.2.0-alpha.1",
@techieshark
techieshark / __mocks__stripe.ts
Created August 30, 2019 05:15
jest Stripe mock
// __mocks__/stripe.ts: Jest Mock for Stripe class
/**
* Fake a response from stripe.customers.list().
* @example
* mockStripeCustomerList(1) === { data: ['fake customer'], object: 'list', … }
* @param count number of fake customers to return
*/
export const mockStripeCustomerList = (count: number) => ({
data: (new Array(count)).fill('fake customer'),
@techieshark
techieshark / send204.ts
Created August 29, 2019 01:11
node: send204()
import { NO_CONTENT } from 'http-status-codes';
const send204 = (req: Request, res: Response, next: NextFunction) => {
res.status(NO_CONTENT);
res.end();
};
@techieshark
techieshark / setAllowHeader.ts
Created August 29, 2019 01:10
node: setAllowHeader()
import { NextFunction, Request, Response } from 'express';
/**
* Sets Allow header.
*
* "The Allow header lists the set of methods support by a resource.
* This header must be sent if the server responds with a 405 Method Not Allowed
* status code to indicate which request methods can be used."
*
*
@techieshark
techieshark / index.js
Created February 22, 2019 20:50
browserFeatures
/**
* Returns whether the current browser supports
* the `locales` argument of Number.prototype.toLocaleString().
* IE < 11 does not.
* Copied from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString#Checking_for_support_for_locales_and_options_arguments
* @return {boolean} true if browser supports it, otherwise false.
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString#Browser_compatibility
*/
function toLocaleStringSupportsLocales() {
const number = 0;
@techieshark
techieshark / objectValues.js
Created February 22, 2019 04:10
Flow-typed version of Object.values()
// @flow
// Flow-typed version of Object.values()
// @see: problem - https://github.com/facebook/flow/issues/2221
// @see: this solution - https://stackoverflow.com/a/51757027/1024811
export default function objectValues<A, B>(obj: {[key: A]: B}): Array<B> {
return ((Object.values(obj): any): Array<B>);
}