Skip to content

Instantly share code, notes, and snippets.

View dypsilon's full-sized avatar

Tim Navrotskyy dypsilon

View GitHub Profile
@dypsilon
dypsilon / .eslintrc.json
Created May 30, 2023 21:29
TypeScript ESLint Prettier Setup
{
"parser": "@typescript-eslint/parser",
"plugins": [
"@typescript-eslint",
"prettier"
],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
async function addComment(comment) {
const post = await fetchPostBySlug(comment.postSlug);
if (!post.isCommentingEnabled) {
throw new Error('Commenting is disabled.');
}
const isAllowed = await isUserAllowedToComment(post, comment.user);
if (!isAllowed) {
function addComment(comment) {
return fetchPostBySlug(comment.postSlug)
.then((post) => {
if (!post.isCommentingEnabled) {
return Promise.reject(new Error('Commenting is disabled.'));
}
return isUserAllowedToComment(post, comment.user)
.then((isAllowed) => ({ isAllowed, post }));
}).then((input) => {
function addComment(comment) {
return fetchPostBySlug(comment.postSlug).then((post) => {
if (!post.isCommentingEnabled) {
return Promise.reject(new Error('Commenting is disabled.'));
}
return isUserAllowedToComment(post, comment.user).then((isAllowed) => {
if (!isAllowed) {
return Promise.reject(
new Error('The user is not allowed to comment on this post.')
function addComment(comment, done) {
fetchPostBySlug(comment.postSlug, (err, post) => {
if (err) return done(err);
if (!post.isCommentingEnabled) {
return done(new Error('Commenting is disabled.'));
}
isUserAllowedToComment(post, comment.user, (err, isAllowed) => {
if (err) return done(err);
{
"user": "58bb380d222c2239a2b16ae9",
"postSlug": "is-learning-dangerous",
"comment": "A little learning is a dangerous thing."
}
@dypsilon
dypsilon / reader-t.js
Last active December 27, 2017 03:53
In this example, the save method produces an error and we use mapLower to call a special Future function "mapRej", which transforms all incoming errors.
const {tagged} = require('daggy');
// const
const K = (a) => (b) => a;
// Reader Transformer
module.exports = (M) => {
const ReaderT = tagged('run');
ReaderT.lift = ReaderT.prototype.lift = (m) => ReaderT(K(m));
@dypsilon
dypsilon / reader-t.js
Last active October 28, 2020 22:25
A simple example of using Reader transformer to combine reader and future.
const {tagged} = require('daggy');
// const
const K = (a) => (b) => a;
// Reader Transformer
module.exports = (M) => {
const ReaderT = tagged('run');
ReaderT.lift = (m) => ReaderT(K(m));
@dypsilon
dypsilon / reader_future.js
Created August 9, 2016 19:21
Reader + Future Monads Usage
/**
* This short program will encrypt the user password
* and insert a new record into a mock database.
*/
const Reader = require('fantasy-readers');
const Future = require('fluture');
const R = require('ramda');
const crypto = require('crypto');
// our mock database
@dypsilon
dypsilon / reader.js
Last active April 28, 2024 08:50
Example usage of the reader monad.
/**
* This short program will encrypt the user password
* and insert a new record into a mock database.
*/
const Reader = require('fantasy-readers');
const R = require('ramda');
const crypto = require('crypto');
// our mock database
const database = [