Skip to content

Instantly share code, notes, and snippets.

@monochromer
monochromer / ComponentHelper.rb
Last active Oct 24, 2019
Ruby on Rails view component helpers
View ComponentHelper.rb
# https://www.innoq.com/en/blog/rails-frontend-components/
# app/helpers
module ComponentHelper
def component(name, data: {}, &block)
render_component("#{name}/#{name}", { data: data }, &block)
end
private
def render_component(name, locals, &block)
@monochromer
monochromer / hash-lib.js
Last active Oct 17, 2019
node.js hashing files
View hash-lib.js
const crypto = require('crypto');
const fs = require('fs');
async function hashFile(inputStream, algorithm = 'md5', encoding = 'hex') {
return new Promise((resolve, reject) => {
const hash = crypto.createHash(algorithm);
inputStream
.on('error', reject)
.on('data', data => hash.update(data))
View concurrent.js
function concurrent(tasks, iteratee, onDone) {
let completed = 0;
function onFinish() {
if (++completed === tasks.length) {
onDone();
}
}
tasks.forEach((task, index) => {
@monochromer
monochromer / responsive-email-image.html
Last active Aug 16, 2019
responsive email html image
View responsive-email-image.html
<!--[if gte MSO 9]>
<table width="200"><tr><td>
<![endif]-->
<table align="center" width="100%" style="max-width: 200px;">
<tr>
<td align="center" style="text-align: center">
<img src="//link.com" alt="" width="200" height="140" style="display: block; border: 0; width: 100%; height: auto;">
</td>
</tr>
</table>
@monochromer
monochromer / createSplitStream.js
Last active Sep 19, 2019
node.js line number stream
View createSplitStream.js
// https://github.com/deanhume/streams
function createSplitStream(splitter) {
let buffer = '';
return new stream.Transform({
transform(chunk, encoding, next) {
buffer += chunk;
const parts = buffer.split(splitter);
parts.slice(0, -1).forEach(part => this.push(part));
@monochromer
monochromer / es6-proxy.md
Created Jun 19, 2019
A practical guide to Javascript Proxy (ES6 Proxy)
View es6-proxy.md
@monochromer
monochromer / future.js
Created May 16, 2019
Future: Асинхронность на фьючерах без состояния
View future.js
// © Timur Shemsedinov
// https://www.youtube.com/watch?v=22ONv3AGXdk
// https://github.com/HowProgrammingWorks/Future
class Future {
constructor(executor) {
this.executor = executor;
}
static of(value) {
@monochromer
monochromer / cluster.js
Created May 13, 2019
Node.js cluster demo
View cluster.js
const cluster = require('cluster');
const os = require('os');
const pid = process.pid;
const cpusCount = os.cpus().length;
if (cluster.isMaster) {
console.log(`CPUs: ${cpusCount}`);
console.log(`Master started. Pid: ${pid}`);
for (let i = 0; i < cpusCount - 1; i++) {
View html-include.html
<!-- HTML Includes That Work Today -->
<!-- https://www.filamentgroup.com/lab/html-includes/ -->
<iframe
src="signal.svg"
loading="lazy"
onload="this.before((this.contentDocument.body||this.contentDocument).children[0]);this.remove()"
></iframe>
View lazy-load-module.js
// https://habr.com/ru/post/443124/
const loadableModule = (loaderFunction) =>
class AsyncComponent extends React.Component {
state = {
ResultComponent: null,
error: false,
};
componentWillMount() {
loaderFunction
You can’t perform that action at this time.