Skip to content

Instantly share code, notes, and snippets.

@ArtemAvramenko
ArtemAvramenko / mask-email.js
Last active February 8, 2023 10:01
Masks email with asterisks/bullets in Node.js
/** Masks email, e.g. [email protected] as j•••e@g•••m **/
function maskEmail(email) {
function mask(segment) {
const hider = '•••';
let result = '';
if (segment) {
result = segment[0] + hider;
if (segment.length > 2) {
result += segment[segment.length - 1];
@ArtemAvramenko
ArtemAvramenko / parseTime.js
Last active March 7, 2023 10:27
Parses text as time in JavaScript
function parseTime(s, pmByDefault) {
const match = (s || '').toLowerCase().match(/^[^0-9]*([0-9]+)([^0-9]*([0-9]*)(.*))$/);
if (!match) {
return null;
}
let mins = 0;
let hours = 0;
if (match[3]) { // strings like '12:34'
@ArtemAvramenko
ArtemAvramenko / ExtractZip.cs
Last active April 5, 2023 20:34
Extract zip file with backslashes in .NET C#
private void ExtractZip(string zipPath, string folderPath)
{
using var zip = ZipFile.Open(zipPath, ZipArchiveMode.Read);
foreach (var entry in zip.Entries)
{
var correctPath = entry.FullName.Replace("\\", "/");
var extractedFileInfo = new FileInfo(Path.Combine(folderPath, correctPath));
var entryDirectory = extractedFileInfo.Directory!.FullName;
if (!Directory.Exists(entryDirectory))
{
@ArtemAvramenko
ArtemAvramenko / regex-validated-strings.ts
Last active August 7, 2024 19:34
Regex-validated strings in TypeScript
const isDevMode = () => true;
type ValidatedString<T extends string> = string & {
[K in keyof string] : K extends `as${string}String` ? never : string[K]
} & {
readonly __format: T
};
/*
eslint-disable
@ArtemAvramenko
ArtemAvramenko / GetAnsiEncodingByLCID.cs
Created April 24, 2023 18:30
Get ANSI Encoding by LCID in .NET Framework
using System.Runtime.InteropServices;
using System.Text;
namespace App.Encoding
{
class Program
{
[DllImport("kernel32.dll")]
static extern int GetSystemDefaultLCID();
@ArtemAvramenko
ArtemAvramenko / fullTextSearch.js
Last active October 23, 2024 10:06
A script for a heuristic search for a similar name
const expect = actual => {
const toBe = expected => {
let [a, b] = [actual, expected].map(JSON.stringify);
if (a !== b) {
console.error(a + ' should be ' + b);
}
};
return { toBe, toEqual: toBe };
};
@ArtemAvramenko
ArtemAvramenko / money-formatting.js
Last active April 1, 2025 11:28
Script for determining regional number formats
// type numberFormat =
'dot_with_comma_grouping' | // en-US
'dot_with_indian_grouping' | // en-IN
'dot_with_apostrophe_grouping' | // de-CH
'comma_with_dot_grouping' | // it-IT
'comma_with_space_grouping'; // pt-PT
// 100,000.50 - DEFAULT FORMAT except for Europe (en-US)
["United Kingdom", "Malta"]
var amount = 900m;
var currency = "μBTC";
// https://en.wikipedia.org/wiki/Micro-
int prefixIndex = currency.Length > 3 ? "mμnpf".IndexOf(currency[0]) : -1;
if (prefixIndex >= 0)
{
currency = currency[1..];
amount /= (decimal)Math.Pow(1000, prefixIndex + 1);
}
// In practical scenarios, such a transformation speeds up the search by dozens of times:
// Apple, Apricot, Banana => (?:Ap(?:ple|ricot)|Banana)
private static void GenerateRegex(StringBuilder result, IList<string> words, int level = 0)
{
const int minPrefix = 1;
const int maxLevel = 300;
void AppendLevel()
{
// sb.Append('\n');
@ArtemAvramenko
ArtemAvramenko / GenerateBasicLatinMapping.cs
Last active December 4, 2025 16:09
Generator for mapping national alphabets to the Basic Latin
// https://github.com/anyascii/anyascii/blob/master/unidecode/unidecode.tsv
var unidecodeLines = File.ReadAllLines(@"C:\Users\username\Desktop\Unidecode\unidecode.tsv");
var outputFile = @"C:\Users\username\Desktop\Unidecode\onlyletters.tsv";
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
var encodings = new[] {
10000, 10006, 10007, 10010, 10017, 10029, 10079, 10081, 10082,
1250, 1251, 1252, 1253, 1254, 1257, 1258
}.Select(i => Encoding.GetEncoding(i)).ToArray();
bool isSupportedEncoding(string s)