Skip to content

Instantly share code, notes, and snippets.

View matfin's full-sized avatar
💭
November 2020: TypeScript and web components

Matt Finucane matfin

💭
November 2020: TypeScript and web components
View GitHub Profile
@matfin
matfin / Contentful Webhooks.md
Last active January 5, 2018 14:07
Dealing with custom Content-Types and POST requests in node.js and Meteor

This describes how to deal with custom content types and hooks in a Meteor JS based webapp which pulls its content from Contentful.

My goal was to reload content into a Meteor server side collection and have it update any time a user on Contentful.com updated content there. Contentful provide a very helpful hook mechanism, whereby a POST request is made to an endpoint of your choosing when content has been updated.

I was using the server side component of IronRouter to listen for incoming POST requests from Contentful and when they arrived, the request body was empty. This is because Contentful have a custom content type 'application/vnd.contentful.management.v1+json' and IronRouter does not know how to deal with that out of the box.

I wrote the above, using the nodejs connect and the body parser module. Want to know how to include these in your Meteor app? Look at this handy package - https://github.com/meteorhacks/npm which allows you to include NPM modules in meteor.

Look at line 15 in the above cod

@matfin
matfin / object_to_array.js
Last active August 29, 2015 14:25
A simple solution to convert an object map into an array
'use strict';
/**
* Given an object map like this one
*/
var map = {
objectOne: {
attributeOne: 1,
attributeTwo: 2
@matfin
matfin / oddness.js
Last active August 29, 2015 14:26
Iterate through and array and return the first number that occurs an odd number of times.
var numbers = [1, 1, 2, 3, 2, 3, 7, 8, 8];
var getOddOccurrence = function(numbers) {
var collected = [],
keys,
oddnum;
numbers.forEach(function(number) {
if(typeof collected[number] === 'undefined') {
collected[number] = [];
/**
* A practical example of using Fibers in Meteor JS
*/
var MeteorFiber = {
takeLong: function() {
var future = new this.Future();
Meteor.setTimeout(function() {
/**
@matfin
matfin / 4-ways-custom-components.html
Last active January 6, 2016 19:53
HTML5 Web Components - Snippets from learning
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<!-- Option One -->
<!--pluralsight-comment></pluralsight-comment-->
<script type="text/javascript">
@matfin
matfin / index.ts
Created April 17, 2020 17:34
Return a grouped array of strings that are annagrams
((): void => {
interface IItem {
original: string,
sorted: string
}
const test_inputs: string[] =[
'poh', 'own', 'won', 'cat', 'eti', 'pat',
'hpo', 'apt', 'now', 'tap', 'hop', 'tie'
];
const hammerTime = async (url, numCalls) => {
const start = performance.now();
const results = [];
for(let i = 0; i < numCalls; i++) {
const res = await fetch(url);
const data = await res.text();
results.push(data);
}
@matfin
matfin / studentCourses
Created January 17, 2021 19:25
Get a course for a student
const courses = [
{
"courseData" : [
{
"course" : {
"day" : "Tuesday",
"duration" : "10 weeks",
"language" : "German",
"location" : "Online",
"startdate" : "12th January",
@matfin
matfin / engineering.md
Last active December 13, 2021 12:26
Answers to common engineering related topics

Engineering questions

When's the right time to move functionality to a custom hook In React?

The main reasons to keep certain functinality outside of a hook are as follows:

  • It encourages reuse of code and keeps the complexity of a component down, by moving business logic outside of the component.
  • Moving functionality outside of a hook makes the component easier to test
  • Hooks are easy to initialise when needed, and their functionality can be discarded when no longer needed.

Here is an example of a custom hook I have written

@matfin
matfin / prioritise.ts
Created January 28, 2022 17:14
Prioritise a nested array of items
// From: https://twitter.com/rauschma/status/1487086371760136195?s=20&t=z_0I28Cjhgn1igKt90qmrw
import assert from 'assert/strict';
interface Character {
name: string;
}
interface PrioritisedItem {
name: string;
priority: number;