Skip to content

Instantly share code, notes, and snippets.

View Emilios1995's full-sized avatar

Emilio Srougo Emilios1995

View GitHub Profile
// -- BEFORE --
// In this first exmaple, the action dispatching and flow would be hard to test, because its highly coupled with
// the component and the way it interacts with the Linking API.
// login.js
import {Linking} from 'react-native'
export default class Login extends Component {
constructor(props) {
super(props);
this.handleDeepLink = this.handleDeepLink.bind(this);
// Recursive funcion with Tail Call Optimization
flatten = ([first, ...rest], work = []) => {
if (first === undefined) {
return work;
}
else if (!Array.isArray(first)) {
return flatten(rest, [...work, first]);
}
else {
// tpr = tiles per row
const calcTileDimensions = (deviceWidth, tpr) => {
const margin = deviceWidth / (tpr * 10);
const size = (deviceWidth - margin * (tpr * 2)) / tpr;
return { size, margin };
};
import React, { Component } from 'react';
import { Text, View, StyleSheet, Dimensions } from 'react-native';
const { width } = Dimensions.get("window");
export default class App extends Component {
render() {
const tileDimensions = calcTileDimensions(width, 2) // -> change this number and see!
const tiles = 'Lorem Ipsum Dolor Sit Amet'.split(' ')
return (
<View style={styles.container}>
// This goes after https://gist.github.com/DrBoolean/3f5ac08a5bf1c4673757
const setNameActionCretor = name => ({ type: "SET_NAME", payload: { name } });
const setName = localStorage
.chain(ls => safeProp("user", ls))
.chain(u => safeProp("name", u))
.map(n => n.toUpperCase())
.chain(printLn)
.map(m => m.toLowerCase())
sendNotification = function(data) {
var headers = {
"Content-Type": "application/json; charset=utf-8",
Authorization: "Basic NzkwODdjM2YtODMxNi00ODMyLTgwMWEtZTVkOTcyMzg4ZWRi"
};
var options = {
host: "onesignal.com",
port: 443,
path: "/api/v1/notifications",
// updateKeys :: (String -> String) -> Object -> Object
const updateKeys = f =>
R.compose(R.fromPairs, R.map(R.over(R.lensIndex(0), f)), R.toPairs);
// updateKeysWithMap :: Object -> Object -> Object
const updateKeysWithMap = map => updateKeys(k => map[k] || k);
// example:
updateKeysWithMap({ oldKey: "newKey" })({ oldKey: 2 }); // -> { newKey: 2 }
const concatMany = unapply(unnest)
concatMany([1,2,3], [4,5,6], [7,8,9])
// -> [1, 2, 3, 4, 5, 6, 7, 8, 9]
const isType = R.curry((type, value) => R.type(value) === type);
const rules = {
version: [[isType('Number')], 'Version must be a number'],
build: [[isType('Number')], 'Build must be a number'],
appName: [[isType('String'), 'appName must be a string']]
}
const typeMessage = (type, field) => `${field} has to be a ${type}`;
const typeRule = type => [
isType(type),
(val, field) => typeMessage(type, field)
];
const isString = typeRule("String");
const isNumber = typeRule("Number")
const rules = {
version: [isNumber],