Skip to content

Instantly share code, notes, and snippets.

View zmts's full-sized avatar
🇺🇦
russian warship go f*uck yourself

Sasha Zmts zmts

🇺🇦
russian warship go f*uck yourself
View GitHub Profile
@zmts
zmts / merge_objects.md
Created March 7, 2024 15:02
merge objects

Merge objects in JavaScript (lodash)

const mergeWith = require('lodash/mergeWith')
const isArray = require('lodash/isArray')

function mergeObjects(obj1, obj2) {
  return mergeWith(obj1, obj2, (objValue, srcValue) => {
    if (isArray(objValue) && isArray(srcValue)) {
 return objValue.concat(srcValue);

Convert .env file to object

const isNumber = (value) => {
  return !isNaN(Number(value))
}

const getValue = (value) => {
  if (value === '') return value
  if (['true', 'false'].includes(value)) return JSON.parse(value)
@zmts
zmts / dns.md
Created December 5, 2023 11:01

Too slow response .local domains Mac OS

The Multicast DNS feature of Bonjour technology allows devices on a local network to connect to each other by name without a separate DNS server. By default, any hostname ending in .local is treated as a Bonjour host rather than by querying the DNS server entries in Network preferences.

Though the .local domain is not defined as a valid top-level domain on the Internet, some private networks have DNS servers that assign hostnames in the .local domain. By default Mac OS X does not use the DNS server to resolve these names. This may result in unexpected failures to connect to .local hostnames defined by your server.

To fix this problem you need to add IPv6 entries for each of your vhosts in your /etc/hosts file:

127.0.0.1       localhost
127.0.0.1 myproject.local
@zmts
zmts / buildTreeFromFlatArray.md
Created August 15, 2023 09:37
Build tree from flat array

Build tree from flat array

function buildTree(nodes, parentId = null) {
  const tree = [];
  for (const node of nodes) {
    if (node.parentId === parentId) {
      const children = buildTree(nodes, node.id);
      if (children.length) {
        node.children = children;
@zmts
zmts / parent_child_get_item_level.md
Last active June 23, 2023 15:16
SQL: Get item level (aka generation) from parent child hierarchy

SQL: get item level (aka generation) from parent child hierarchy

CREATE TABLE "public"."parent_child" (
    "id" int4 NOT NULL DEFAULT nextval('people_id_seq'::regclass),
    "first_name" varchar(100),
    "parent_id" int4,
    "city" varchar(100),
    PRIMARY KEY ("id")
);
@zmts
zmts / select_by_id_from_feild_array.md
Last active June 23, 2023 15:16
SQL: select rows where some id in array

SQL: select rows where some id in array

CREATE TABLE "public"."items" (
    "id" int4 NOT NULL DEFAULT nextval('item_id_seq'::regclass),
    "name" text,
    "has_access" _int4 NOT NULL DEFAULT '{}'::integer[],
    PRIMARY KEY ("id")
);

SQL: sum per day

CREATE TABLE "stat" (
    "id" int4 NOT NULL DEFAULT nextval('stat_id_seq'::regclass),
    "type" varchar(100),
    "datetime" timestamptz,
    "amount" int8,
    "wallet_id" int8,
 PRIMARY KEY ("id")
@zmts
zmts / pg_jsonb_push.md
Last active October 25, 2021 17:16
Insert(push) object to nested array. JSONB PostgreSQL

Insert(push) object to nested array. JSONB PostgreSQL

// posts table

| id | content          |
|----|------------------|
| 40 | jsonb data       |
@zmts
zmts / knex_ssl.md
Last active September 3, 2023 23:21
Can't connect to PostgreSQL. SSL error with Nodejs/Knexjs (Digital ocean)

Can't connect to PostgreSQL. SSL error with Nodejs/Knexjs (Digital ocean)

Issues:

Case 1

{
  client: 'pg',
  connection: {
    host: process.env.DB_HOST,
    port: process.env.DB_PORT,
@zmts
zmts / base64.md
Last active June 30, 2021 18:54
base64 on unix systems

base64 on Unix systems

How get base64 from any file ?

Write to output

cat var/tmp/myFile.txt | base64

Or write to file

cat var/tmp/myFile.txt | base64 > var/tmp/decodedFile.txt