Php Transliterator

<?php
function slugify($string) {
    $translit = "Any-Latin; NFD; [:Nonspacing Mark:] Remove; NFC; [:Punctuation:] Remove; Lower();";
    $string = transliterator_transliterate($translit, $string);
    $string = preg_replace('/[-\s]+/', '-', $string);
    return trim($string, '-');
}

echo slugify("Я люблю PHP!");
23 января 2021, 21:09

Generate date ranges in postgresql

SELECT d::date
FROM generate_series(
  timestamp without time zone '2021-01-01',
  timestamp without time zone '2021-02-01',
  '1 day'
) AS gs(d);

19 января 2021, 12:33

Knex join with subquery

knex(
  knex('A').where('A.id',1).as('t1')
).leftJoin(
  knex('B').where('B.id', 2).as('t2'), 
  't1.c', 
  't2.d'
)
15 января 2021, 10:33

Postgresql reset sequence

SELECT setval('your_table_id_seq', COALESCE((SELECT MAX(id)+1 FROM your_table), 1), false)

or 

SELECT
	setval(pg_get_serial_sequence('tbl', 'id'), COALESCE(max(id) + 1, 1), FALSE)
FROM
	tbl;
19 декабря 2020, 20:04

Javascript end of week

function endOfWeek(date) {
    const lastday = date.getDate() - (date.getDay() - 1) + 6;
    return new Date(date.setDate(lastday));
}

const dt = new Date(); 
console.log(endOfWeek(dt).toString());
24 ноября 2020, 19:07

Manual installation LetsEncrypt

certbot run -a manual -i nginx -d example.com
5 ноября 2020, 22:19

Show enum types in postgress

select enum_range(null::my_type)
16 сентября 2020, 18:20

Show list of all services on Ubuntu/Debian

service --status-all
31 августа 2020, 10:17

Javascript sleep promise

const sleep = (ms) => new Promise( (r) => setTimeout(r, ms));
29 августа 2020, 22:22

Update multiple rows with one query in postgresql

CREATE TABLE table1 ( 
	"id" Integer NOT NULL,
	"a" Text NOT NULL,
	"b" Text NOT NULL );
insert into table1 values  
(1, 'a', 'b'),
(2, 'a', 'b')
update table1 as t set 
  a = data.a,
  b = data.b
from (values
  (1, 'a1', 'b1'),
  (2, 'a2', 'b2')
) as data(id, a, b)
where t.id = data.id
29 августа 2020, 21:41

How to edit local commit message

git commit --amend 
27 августа 2020, 13:10

Javascript replace all

function replaceAll(string, search, replace) {
  return string.split(search).join(replace);
}
11 августа 2020, 11:09

Difference between ROW_NUBER(), RANK(), DENSE_RANK() in postgresql

WITH T(StyleID, ID)
     AS (SELECT 1,1 UNION ALL
         SELECT 1,1 UNION ALL
         SELECT 1,1 UNION ALL
         SELECT 1,2)
SELECT *,
       RANK() OVER(PARTITION BY StyleID ORDER BY ID)       AS 'RANK',
       ROW_NUMBER() OVER(PARTITION BY StyleID ORDER BY ID) AS 'ROW_NUMBER',
       DENSE_RANK() OVER(PARTITION BY StyleID ORDER BY ID) AS 'DENSE_RANK'
FROM   T  
Returns

StyleID     ID       RANK      ROW_NUMBER      DENSE_RANK
----------- -------- --------- --------------- ----------
1           1        1         1               1
1           1        1         2               1
1           1        1         3               1
1           2        4         4               2
6 августа 2020, 20:48

Add one second to date in javascript

const currentDate = new Date("2020-07-28T17:23:47.568Z");
const dateSub = new Date(currentDate.getTime() + 1000);
console.log(dateSub.toISOString());
28 июля 2020, 20:22

Run nodejs application in docker

docker run \
  -e "NODE_ENV=production" \
  -u "node" \
  -m "300M" --memory-swap "1G" \
  -w "/home/node/app" \
  --name "my-nodejs-app" \
  node [script]
19 июля 2020, 18:59

Что занимает порт?

sudo netstat -ltnp | grep -w ':80'
19 июля 2020, 18:46

Restart all docker containers

docker restart $(docker ps -a -q)
25 июня 2020, 16:23

SplFileObject read file

$file = new SplFileObject("file.txt");
while (!$file->eof()) {
    echo $file->fgets();
}
function lines($filename) {
    $file = new SplFileObject($filename);
    while (!$file->eof()) {
        yield $file->fgets();
    }
}

foreach (lines('German.txt') as $line) {
    echo $line;
}
15 июня 2020, 20:37

Javascript transliteration library

Iuliia
import iuliia from "iuliia";

// list all supported schemas
for (let schemaName of iuliia.Schemas.names()) {
    console.log(schemaName);
}

// transliterate using specified schema
let source = "Юлия Щеглова";
iuliia.translate(source, iuliia.ICAO_DOC_9303);
// "Iuliia Shcheglova"

// or pick schema by name
let schema = iuliia.Schemas.get("wikipedia");
iuliia.translate(source, schema);
// "Yuliya Shcheglova"
20 мая 2020, 15:17

Compare directories

diff -rq DIR1 DIR2
5 апреля 2020, 20:15