The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)

Ever wonder about that mysterious Content-Type tag? You know, the one you’re supposed to put in HTML and you never quite know what it should be? Did you ever get an email from your friends in…

The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)

Oldies but goldies. This article is 20 years old, but still very relevant

Continue reading

Prevent sleep/suspend when not logged in

Recently I’ve been logged into a laptop remotely a lot. Logging out from “main” session makes my Gnome on Debian 12 go to suspend after a while. To avoid it you just have to tell:

sudo  dbus-launch gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-ac-type 'nothing'

Thanks to WinEunuuchs2Unix for the answer I took from Prevent sleep/suspend when not logged in to a specific account; the only change is that GDM on Debian runs as root.

See also https://wiki.debian.org/Suspend#Disable_suspend_and_hibernation:

For systems which should never attempt any type of suspension, these targets can be disabled at the systemd level with the following:

sudo systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target

To re-enable hibernate and suspend use the following command:

sudo systemctl unmask sleep.target suspend.target hibernate.target hybrid-sleep.target

A modern alternative approach for disabling suspend and hibernation is to create /etc/systemd/sleep.conf.d/nosuspend.conf as

[Sleep]
AllowSuspend=no
AllowHibernation=no
AllowSuspendThenHibernate=no
AllowHybridSleep=no

The above technique works on Debian 10 Buster and newer. See systemd-sleep.conf(5) for details.

If you just want to prevent suspending when the lid is closed you can set the following options in /etc/systemd/logind.conf:

[Login]
HandleLidSwitch=ignore
HandleLidSwitchDocked=ignore

Then run systemctl restart systemd-logind.service or reboot.

Search and replace in multiple files using vim

Search and replace in multiple files using vim – (source Stack Overflow)

It is as simple as

Use:

:set aw
:argdo %s/happy999/happy111/g

The first line sets auto-write mode, so when you switch between files, vim will write the file if it has changed.

The second line does your global search and replace.

Note that it doesn’t use wq! since that exits. If you don’t want to use auto-write, then you could use:

:argdo %s/happy999/happy111/g | w

This avoids terminating vim at the end of editing the first file.

javascript – Can (a== 1 && a ==2 && a==3) ever evaluate to true? – Stack Overflow

javascript – Can (a== 1 && a ==2 && a==3) ever evaluate to true? – Stack Overflow

Yes, it can. IMHO it is one of the several undesirable consequences of loosely typed languages. In fact, according to an almost anonymous user:

If you take advantage of how == works, you could simply create an object with a custom toString (or valueOf) function that changes what it returns each time it is used such that it satisfies all three conditions.

const a = {
  i: 1,
  toString: function () {
    return a.i++;
  }
}

if(a == 1 && a == 2 && a == 3) {
  console.log('Hello World!');
}

The reason this works is due to the use of the loose equality operator. When using loose equality, if one of the operands is of a different type than the other, the engine will attempt to convert one to the other. In the case of an object on the left and a number on the right, it will attempt to convert the object to a number by first calling valueOf if it is callable, and failing that, it will call toString. I used toString in this case simply because it’s what came to mind, valueOf would make more sense. If I instead returned a string from toString, the engine would have then attempted to convert the string to a number giving us the same end result, though with a slightly longer path.

Worst, it is also possible using the === operator!

var i = 0;

with({
  get a() {
    return ++i;
  }
}) {
  if (a == 1 && a == 2 && a == 3)
    console.log("wohoo");
}

This uses a getter inside of a with statement to let a evaluate to three different values.

… this still does not mean this should be used in real code…

Even worse, this trick will also work with the use of ===.

  var i = 0;

  with({
    get a() {
      return ++i;
    }
  }) {
    if (a !== a)
      console.log("yep, this is printed.");
  }

Versioning data in Postgres? Testing a git like approach – Specfy

Versioning data in Postgres? Testing a git like approach – Specfy is fashinating but I think that most of the time these two proposed alternatives fit most of the needs:

  1. In-Table versioning, the WordPress way of doing thing. Add a a column version (or modify date) and SELECT the maximum version. It’s simple and doesn’t require maintaining multiple schema or resources. However it has massive drawback in term of performance, and query simplicity. The table will inevitably grow, and SELECT needs to have an order by which can make joining/grouping/aggregating harder or slower.
  2. Copy table versioning: the most simple and efficient alternative. Create a quasi equivalent copy of the table your are versioning, migration is almost 1:1. However you still need to add metadata fields, disable or rename primary key to allow the same id to be inserted multiple times. And you obviously need one table per versionned table.
Continue reading