Skip to content

Instantly share code, notes, and snippets.

View 73nko's full-sized avatar
💭
👨‍💻

73NKo 73nko

💭
👨‍💻
View GitHub Profile
@73nko
73nko / destructuring.js
Last active February 19, 2020 13:49
Some functional functions Based in array& object destructuring
const map = ([ head, ...tail ], fn) =>
(head === undefined && !tail.length) ? [] :
(tail.length === 0) ? [ fn(head) ] :
[ fn(head), ...map(tail, fn) ];
const filter = ([ head, ...tail ], fn) => {
const newHead = fn(head) ? [ head ] : [];
return tail.length ? [ ...newHead, ...(filter(tail, fn)) ] : newHead;
}
@73nko
73nko / setTimeout
Created March 13, 2018 14:24
A small function to promisify SetTimeout and use it with async await
const timer = time =>
new Promise(resolve => setTimeout(resolve, time));
@73nko
73nko / span.js
Created July 3, 2018 05:42
Creates an array with the span of numbers
/**
* Creates an array with the span of numbers going from `first` and ending at
* `last` if possible depending on the specified step value.
* @param {number} first
* First number that should appear in the returned array.
* @param {number} last
* Last number that should appear in the returned array.
* @param {number=} opt_step
* Defaults to `1` if not given or if `0` or `NaN` is specified. The
* difference between each subsequent number in the returned array.
@73nko
73nko / remove-duplicates.js
Created August 8, 2018 06:16
Function to remove ducplicates from array using reduce.
Array.prototype.removeDuplicates = function(){
return this.reduce((result,nextItem)=>result.includes(nextItem) ? result : result.concat(nextItem),[]);
}
@73nko
73nko / array-chunk.js
Created August 16, 2018 12:41
A method fro array chunk
function chunk(array, size) {
//declaring variable 'chunked' as an empty array
let chunked = []
//looping through the array until it has been entirely "manipulated" or split into our subarrays
while(array.length > 0) {
//taking the spliced segments completely out of our original array
//pushing these subarrays into our new "chunked" array
chunked.push(array.splice(0, size))
}
@73nko
73nko / sort-array-object.js
Created August 22, 2018 06:09
A function for array of objects sorting.
Array.prototype.sortBy = function(key, ascending = true) {
return this.sort((a, b) => {
if (ascending) {
return a[key] > b[key] ? 1 : a[key] < b[key] ? -1 : 0;
} else {
return a[key] > b[key] ? -1 : a[key] < b[key] ? 1 : 0;
}
});
};
var arr = [
@73nko
73nko / random-number.js
Created December 16, 2018 11:05
Get a random number with a defined size
Math.round(Math.random() * 1E2)
@73nko
73nko / sw.js
Created December 20, 2018 07:47 — forked from ireade/sw.js
Handle broken images with the service worker
self.addEventListener('install', (e) => {
e.waitUntil(
caches.open("precache").then((cache) => cache.add("/broken.png"))
);
});
function isImage(fetchRequest) {
return fetchRequest.method === "GET" && fetchRequest.destination === "image";
}
function downloadFile(data, fileName, type="text/plain") {
// Create an invisible A element
const a = document.createElement("a");
a.style.display = "none";
document.body.appendChild(a);
// Set the HREF to a Blob representation of the data to be downloaded
a.href = window.URL.createObjectURL(
new Blob([data], { type })
);
@73nko
73nko / algorithms-base-code.js
Created January 4, 2019 14:37
Base code for algorithms Course in Node JS
'use strict';
const fs = require('fs');
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;