Skip to content

Instantly share code, notes, and snippets.

View izelnakri's full-sized avatar
🇺🇦
Ember.js is the best.

Izel Nakri | izelnakri.eth izelnakri

🇺🇦
Ember.js is the best.
View GitHub Profile
@izelnakri
izelnakri / hbs.rs
Last active November 17, 2019 15:11
use hbs; // NOTE: this is the crate I need
pub fn compile(input: &str) -> <Result<(&str), Box<dyn Error>> { // NOTE: maybe also options argument
return hbs::compile(input)?; // NOTE: the interface I need from the crate
}
// "<Post @title={{this.firstTitle}}></Post><Post @title={{this.secondTitle}}></Post>" => "{ }"(The JS wire-format exp)
@izelnakri
izelnakri / ci.js
Last active August 13, 2020 14:16
node.js microservice
import axios from "axios";
import express from "express";
import bodyParser from "body-parser";
import Workflow from "./models/workflow.js";
const { ARGO_SERVER_URL, ARGO_SERVER_TOKEN, GITEA_SERVER_URL, GITEA_SERVER_TOKEN } = process.env;
const TARGET_PORT = process.env.PORT || 1234;
const CI_RUNNING_IMAGE =
process.env.TESTS_RUNNING_IMAGE || "https://media.giphy.com/media/8WeatsYCC54TC/giphy.gif";
const CI_SUCCEED_IMAGE =
@izelnakri
izelnakri / docker-compose.yml
Created August 19, 2020 21:31
Nginx reverse proxy on docker-compose.yml
# add your custom hosts to /etc/hosts: 127.0.0.1 serverone.com
version: '3.8'
services:
server-one:
image: server-one
tty: true
build:
context: .
dockerfile: ./Dockerfile
entrypoint: ["node", "/code/index.js"]
@izelnakri
izelnakri / Email.ex
Last active December 3, 2020 03:11
Without pipes(|>) certain APIs will not get built or used. Application code below is very easy to read/follow/maintain and write.
# Ecto composable changesets/validations:
defmodule Email do
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:address])
|> validate_required([:address])
|> validate_format(:address, ~r/@/)
|> validate_length(:address, max: 75)
|> unique_constraint(:address)
@izelnakri
izelnakri / changeset-and-query.ts
Last active February 25, 2021 00:57
Ember-data + Memserver + Ecto(ORM) API unification && Memserver + Express.js API unification
import { from, where, join, leftJoin, innerJoin } from 'memserver/query';
import { cast, validat cast, validateRequired, validateFormat, validateLength, uniqueConstraint, foreignKeyConstraint } from 'memserver/changeset';
let userFirstNames = User
|> where('u', 'u.id < 200') // 'm' because there could be multiple model references on |> sql joins()
|> select('u', 'u.first_name')
|> Repo.all
// or maybe some API like this as well: https://apidock.com/rails/ActiveRecord/QueryMethods/where
// or direct memserver like API User.findBy({});
@izelnakri
izelnakri / devtools.js
Last active February 9, 2021 10:15
This optionally exposes the developer to some global utility functions on google chrome dev console: routes(), route(), model(), service('$serviceName') etc.
import Service from '@ember/service';
import ENV from 'frontend/config/environment';
import { getOwner } from '@ember/application';
export default class DevToolService extends Service {
owner = getOwner(this);
constructor() {
super(...arguments);
import { assert } from 'chai';
import { Readable, PassThrough } from 'stream';
import * as readline from 'readline';
import { spawn } from 'child_process';
// Adaptor for Node 10 to adapt readline streams for async iteration.
// (via https://medium.com/@wietsevenema/node-js-using-for-await-to-read-lines-from-a-file-ead1f4dd8c6f)
function readLines(input: Readable): Readable {
let output = new PassThrough({ objectMode: true });
let lines = readline.createInterface({ input });
let stdin = process.stdin;
stdin.setRawMode(true);
stdin.resume();
stdin.setEncoding('utf8');
let targetInputs = {};
let inputs = [];
stdin.on('data', function(key){
type RequireOnlyOne<T, Keys extends keyof T = keyof T> = Pick<T, Exclude<keyof T, Keys>> &
{
[K in Keys]-?: Required<Pick<T, K>> & Partial<Record<Exclude<Keys, K>, undefined>>;
}[Keys];
export interface ModelRefShape {
id?: number;
uuid?: string;
[propName: string]: any;
}
@izelnakri
izelnakri / benchmark.js
Last active December 17, 2021 01:50
Benchmark boilerplate
import setup from "./setup-benchmark.js";
let Benchmark = setup();
Benchmark.add('RegExp#test', () => {
/o/.test('Hello World!');
});
Benchmark.add('String#indexOf', () => {
'Hello World!'.indexOf('o') > -1;
});