Skip to content

Instantly share code, notes, and snippets.

View nadeesha's full-sized avatar

Nadeesha Cabral nadeesha

View GitHub Profile
@nadeesha
nadeesha / contibutors.js
Last active August 29, 2015 14:14
contibutors.js
'use strict';
var request = new XMLHttpRequest();
request.open('GET', 'https://api.github.com/repos/' + args[0] + '/contributors', true);
request.onreadystatechange = function()
{
if (request.readyState != 4)
return false;
@nadeesha
nadeesha / contributors.json
Last active August 29, 2015 14:14
contributors.json
{
"name": "ghcontribs3",
"endpoints": ["api.github.com"],
"scriptUrl": "https://rawgit.com/ncthis/6fa481eaceaf96ef6394/raw/0d8db4b2c6db266fed87b63d955d2947e2b1f0f2/contibutors.js"
}
@nadeesha
nadeesha / exampleStore.js
Last active March 28, 2016 01:53
Example Store Structure
{
user: {
name: "...",
email: "..."
},
orders: {
count: 10,
items: [...]
},
application: {
@nadeesha
nadeesha / linkState.js
Created June 7, 2016 04:35
Two way binding helper
import invariant from 'invariant';
const valueGetters = {
text: e => e.target.value,
checkbox: e => e.target.checked,
};
export default (self, key) => {
invariant(key, 'key is required');
invariant(self, 'self is required');
@nadeesha
nadeesha / callable-generic.ts
Created July 2, 2017 20:39
Callable generic params in TS
// from: http://www.typescriptlang.org/play/#src=function%20someFactory%3CT%3E()%20%7B%0D%0A%20%20return%20class%20%7B%0D%0A%20%20%20%20method(someParam%20%3A%20T)%20%7B%0D%0A%20%20%20%20%20%20%0D%0A%20%20%20%20%7D%0D%0A%20%20%7D%0D%0A%7D%0D%0A%0D%0A%2F%2F%20how%20to%20invoke%20this%20factory%20with%20a%20defined%20%3CT%3E%3F%0D%0A%0D%0Aconst%20SomeClass%20%3D%20someFactory%3C%7B%20whatever%3A%20string%20%7D%3E()%0D%0Aconst%20someInstance%20%3D%20new%20SomeClass()%0D%0AsomeInstance.method('will-error-here')%0D%0A
function someFactory<T>() {
return class {
method(someParam : T) {
}
}
}
const happyUsersWithHappiness
= _(users)
.map<IHappyUser>(user => ({ ...user, happiness: getHappiness(user.id) }))
.filter<RegExp>(/* filtering with RegExp */)
.reduce<HappyUsersObj>(/* some reducer, that will know iterator will be of type _.MemoIterator<HappyUsersObj, IHappyUser> */)
.value();
export type HOCType<P extends {}, V extends {}> = (
BaseComponent: React.StatelessComponent<P>,
) => React.StatelessComponent<V>;
function withAuthentication<OwnProps extends {}>():
HOCType<OwnProps & { user: IUser }, OwnProps> = /* ... /*
export const AuthenticatedPage = withAuthentication(Page); // typeof AuthenticatedPage ?
interface IPageProps {
title: string;
}
export const AuthenticatedPage = withAuthentication<IPageProps>(Page);
// typeof AuthenticatedPage is inferred as React.StatelessComponent<IPageProps>
// typeof IPageProps is inferred as React.StatelessComponent<IPageProps & { user: IUser }>
interface IBody {
payload: {
changedUser: {
id: string;
name: string;
username: string;
};
};
}