Skip to content

Instantly share code, notes, and snippets.

@MeetMartin
MeetMartin / authReducer.js
Last active October 29, 2018 22:18
functional AuthReducer
import { SET_CURRENT_USER } from '../actions/types';
import { isEmpty } from '../../utilities/validations';
import match from 'conditional-expression';
const initialState = {
isAuthenticated: false,
user: {}
};
/**
import match from 'conditional-expression';
match('funny joke')
.equals('sad').then('I am false')
.with(/[a-z]/).then('I am true and I am the result')
.includes('joke').then('I am true but I am not evaluated')
.else('I just execute everything');
// returns 'I am true and I am the result'
class TextTransformation {
constructor(text) {
this.text = text;
}
makeImportant() {
this.text = this.text.trim(); // trim
this.text = this.text.toLowerCase(); // make lowercase
this.text = this.text.charAt(0).toUpperCase() + this.text.slice(1); // capitalize
this.text = this.text + '!'; // add !
return this.text;
import { assert } from 'chai';
import TextTransformation from '../src/text-transform-class';
describe('test makeImportant', () => {
it('Makes it capitalized, trimmed with ! at the end.',
() => assert.strictEqual(new TextTransformation (' pReSiDeNt ').makeImportant(), 'President!'));
});
describe('test makeUnimportant', () => {
import TextTransformation from '../src/text-transform-class';
cont textTransformation = new TextTransformation('bug');
console.log(textTransformation.makeImportant());
// output: Bug!
console.log(textTransformation.makeUnimportant());
// output: bug!.
class TextTransformation {
static capitalize(text) {
return text.charAt(0).toUpperCase() + text.slice(1);
}
static addExclamationMark(text) {
return text + '!';
}
static addPunctuation(text) {
return text + '.';
}
import { assert } from 'chai';
import TextTransformation from '../src/text-transform-oop';
describe('test atomic functions', () => {
it('capitalize(text) turns first character to Capital',
() => assert.strictEqual(TextTransformation.capitalize("pReSiDeNt"), 'PReSiDeNt'));
it('addExclamationMark(text) adds !',
() => assert.strictEqual(TextTransformation.addExclamationMark("text"), 'text!'));
it('addPunctuation(text) adds .',
const compose = (...fns) => fns.reduce((f, g) => (...args) => f(g(...args)));
export const trim = text => text.trim();
export const toLowerCase = text => text.toLowerCase();
export const capitalize = text => text.charAt(0).toUpperCase() + text.slice(1);
export const addExclamationMark = text => text + '!';
export const addPunctuation = text => text + '.';
export const makeImportant = compose(addExclamationMark, capitalize, toLowerCase, trim);
export const makeUnimportant = compose(addPunctuation, toLowerCase, trim);
import { assert } from 'chai';
import * as TextTransformation from '../src/text-transform-functional';
describe('test atomic functions', () => {
it('trim(text) trims spaces',
() => assert.strictEqual(TextTransformation.trim(' Text '), 'Text'));
it('trim(text) trims tabs',
() => assert.strictEqual(TextTransformation.trim(' Text '), 'Text'));
it('trim(text) trims new lines',
const [url, setUrl] = useState('https://myserver/api/v1');