Skip to content

Instantly share code, notes, and snippets.

@stuartambient
stuartambient / asyncMap.js
Last active February 1, 2021 22:38
NodeJS Design Patters (third edition) challenges
function mapAsync(iterable, callback, concurrency, results) {
if (!iterable.length)
return Promise.all(results).then(results => console.log(results));
const queue = iterable.splice(0, concurrency - 1);
while (queue.length) {
const task = queue.shift();
callback(task, iterable, callback, concurrency, results);
}
@stuartambient
stuartambient / producer-consumer-promises.js
Created January 11, 2021 01:01
NodeJS Design Patters (third edition) challenges
class TaskQueue {
constructor(concurrency) {
this.taskQueue = [];
this.consumerQueue = [];
for (let i = 0; i < concurrency; i++) {
this.consumer();
}
}
@stuartambient
stuartambient / recursiveFind.js
Last active December 14, 2020 18:26
NodeJS Design Patterns (third edition) challenges
import { readdir, stat } from 'fs';
import { basename, join, resolve } from 'path';
import { TaskQueue } from './TaskQueue.js';
import { searchFile, processFile } from './searchFiles.js';
let results = [],
allFiles = [],
processed = null,
readFiles = false;
@stuartambient
stuartambient / challenge-3.4.js
Created November 18, 2020 02:12
NodeJS Design Patterns (third edition) challenges
const { EventEmitter } = require('events');
const ticker = (num, cb) => {
const emitter = new EventEmitter();
const counters = { timePassed: 0, count: 0 };
const timestamp = () => {
const time = Date.now();
return time % 5 === 0;
@stuartambient
stuartambient / challenge-3-2.js
Last active March 13, 2023 18:55
NodeJS Design Patterns (third edition) challenges
const { EventEmitter } = require('events');
const ticker = (num, cb) => {
let timePassed = 0,
count = 0;
const emitter = new EventEmitter();
const repeat = () => {
if (timePassed >= num) return process.nextTick(() => cb(count));
@stuartambient
stuartambient / image.md
Created September 17, 2020 16:10 — forked from prof3ssorSt3v3/image.md
Layered background image effects and clipping
@stuartambient
stuartambient / myPinkTruck.js
Created November 4, 2019 01:08 — forked from blogscot/myPinkTruck.js
Factory Pattern examples using ES6 syntax
class Car {
constructor(doors = 4, state = "brand new", color = "silver") {
this.doors = doors
this.state = state
this.color = color
}
}
class Truck {
# route -
get '/pag' do
@all_album_links = paginate
@pages = {}
@all_album_links.each_slice(10).with_index do |x,i|
@pages.store("page #{i+1 }", "#{x}")
end
slim :pag
#end
end
- num_links = @albums.page_count
- link_first_part = %q[<li class="page-item"><a class="page-link" href="#">]
- link_second_part = %q[</a></li>]
<nav aria-label="Page navigation example">
<ul class="pagination">
<li class="page-item"><a class="page-link" href="#">Previous</a></li>
- num_links.times do |i|
- i = 1
@stuartambient
stuartambient / gist:d04fc61c5747918541da1f37beda25f9
Created November 18, 2017 01:49
Inserting the album_sub_folder id conditional statement
# is it possible to have an if statement in the insert
# dataset and map works in bin/sequel so that is not the problem
check_ids = DB.fetch("SELECT * FROM album_sub_folders WHERE album_id = {@album_id}")
.....
.....
MUSIC_FILES.insert(file_name: File.basename(entry),
album_id: @album_id,
album_sub_folder_id: @asf_id if check_ids.map.count > 0,