Skip to content

Instantly share code, notes, and snippets.

@joanllenas
joanllenas / class.mq.directive.ts
Last active January 28, 2017 00:24
Angular2 API for responsive ngClass, based on @angular/flex-layout
/**
* Toggles class based on media query breakpoint activation.
* Depends on @angular/flex-layout
* Usage:
* <span
* class="green"
* class.xs="my-xs-class"
* class.sm="my-sm-class"
* class.md="my-md-class"
* class.lg="my-lg-class">
@joanllenas
joanllenas / type-inference-1.ts
Created March 4, 2017 21:12
TS Type inference 1
let myBool = false; // Inferred boolean type
myBool = 1; // Error: Type '1' is not assignable to type 'boolean'.
let myBool2 = true;
myBool2 + 5 // Error: Operator '+' cannot be applied to types 'true' and '5'.
@joanllenas
joanllenas / app-code.ts
Last active March 5, 2017 00:59
TS type inference 2
const userService = new HttpService<User>();
const user = { id: 1, name: '' };
userService.save(user) // user type is inferred to be User
.then(resp => { // resp type is inferred to be HttpResponse
console.log(resp.data.name); // resp.data is inferred to be User
});
@joanllenas
joanllenas / library.ts
Created March 4, 2017 22:08
TS Type Inference 3
export type Url = string;
export interface HttpResponse<T> {
data: T;
}
export type PromisedHttpResponse<T> = Promise<HttpResponse<T>>;
export class HttpService<T> {
save(url: Url, data: T): PromisedHttpResponse<T> {
return Promise.resolve({data});
};
}
@joanllenas
joanllenas / nullable-types.ts
Created March 4, 2017 22:55
TS nullable types
// The compiler has the --strictNullChecks flag set
let cannotBeNull = "I can't be null nor undefined";
cannotBeNull = null; // error, 'null' is not assignable to 'string'
cannotBeNull = undefined; // error, 'undefined' is not assignable to 'string'
let nullable: string | null = "I can be null but not undefined";
nullable = null;
nullable = undefined; // error, 'undefined' is not assignable to 'string | null'
@joanllenas
joanllenas / reverse.js
Created March 21, 2017 19:47
Misc exercises
/**
* Reverse a list in one expression, without using Array.reverse()
*/
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
.reduce(
(acc, val) => [val, ...acc],
[]
);
@joanllenas
joanllenas / hello.elm
Last active January 2, 2018 20:58
Ejemplo 1 del post "Un sorbo de TEA"
module Main exposing (..)
import Html exposing (..)
sayHello user =
"Hello " ++ user.name ++ "!"
userRecord =
@joanllenas
joanllenas / tea.elm
Created January 2, 2018 21:47
Ejemplo 2 del post "Un sorbo de TEA"
module Counter exposing (..)
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
programParams =
{ model = modelo
, view = vista
@joanllenas
joanllenas / tea2.elm
Last active January 4, 2018 11:39
Ejemplo del post "Otro sorbo de TEA"
module Counter exposing (..)
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
programParams =
{ model = modelo
, view = vista
@joanllenas
joanllenas / MouseCoordinates.elm
Created January 7, 2018 16:06
Elm example: Capture div click mouse coordinates
-- https://ellie-app.com/fMXwpzt7va1/1
module MouseCoordinates exposing (..)
import Html exposing (Html, div, text, program)
import Html.Attributes exposing (style)
import Html.Events exposing (on)
import Json.Decode as Decode exposing (Decoder, map2, at, int)
-- MODEL