Skip to content

Instantly share code, notes, and snippets.

View aykutyaman's full-sized avatar

Aykut Yaman aykutyaman

View GitHub Profile
// reverse a string using stack data structure
const reverse = s => {
let stack = [];
let reversed = '';
// push all characters of string to stack
for (var i = 0; i < s.length; i++) {
stack.push(s[i]);
}
const express = require('express');
const xl = require('excel4node');
const app = express();
const makeReport = (request, response) => {
const wb = new xl.Workbook();
const ws = wb.addWorksheet('Ödenmiş Mesai Raporu');
// Create a reusable style
const LinkedList = () => {
let length = 0;
let headNode = null;
let Node = (element) => ({
element,
next: null,
});
let size = () => length;
const sum = ([x, ...xs]) => (
xs.length === 0
? x
: x + sum(xs)
);
console.log(sum([2, 4, 6])); // 12
const count = ([x, ...xs]) => (
xs.length === 0
? 1
@aykutyaman
aykutyaman / combineReducers.js
Created November 20, 2018 13:14
redux combineReducer function
const combineReducers = reducers => (state = {}, action) =>
Object.keys(reducers).reduce((acc, key) => ({
...acc,
[key]: reducers[key](state[key], action)
}), {})
@aykutyaman
aykutyaman / isValidDate.js
Created December 20, 2018 12:52
check if a date is valid or not
// isValid -> Date -> Bool
const isValidDate = d => d instanceof Date && !isNaN(d.getTime())
@aykutyaman
aykutyaman / palindrome.js
Last active January 21, 2019 13:51
Recursive palindrome
// it works only with strings
const isPalindrome = ([head, ...tail]) => {
if (tail.length === 0) return true
if (head !== tail[tail.length - 1]) return false
return isPalindrome(tail.slice(0, tail.length - 1))
}
@aykutyaman
aykutyaman / short.js
Created January 22, 2019 14:07
short circuit reduce
const sumUntil = (a, target) =>
[...a].reduce((acc, n, _, arr) => n === target ? (arr.length = 0, acc) : acc + n)
sumUntil([1, 2, 3, 36, 4], 36) // 6
[
'Ankara',
'Milano',
'Berlin'
]
@aykutyaman
aykutyaman / server-cors.js
Created January 25, 2019 10:57
Simple node.js server example with cors enabled
const http = require('http')
const Countries = [
'Turkey', 'Italy', 'Germany'
]
const app = http.createServer((req, res) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end(JSON.stringify(Countries));