Skip to content

Instantly share code, notes, and snippets.

@ahoNerd
ahoNerd / randomArray.js
Last active February 28, 2023 06:29
Get random value from given array
const random = (opt) => {
return opt[Math.floor(Math.random() * opt.length)];
}
// how to use
const role = random(['User', 'Admin', 'Developer'])
const gender = random(['FEMALE', 'MALE'])
@ahoNerd
ahoNerd / index.js
Last active February 26, 2023 11:18
convert js object to formData
const objToFormData = function (obj, form, namespace) {
const formData = form || new FormData()
for (const property in obj) {
if (obj.hasOwnProperty(property)) {
const formKey = namespace ? (namespace + '[' + property + ']') : property
if (typeof obj[property] === 'object' && !(obj[property] instanceof File))
objToFormData(obj[property], formData, property) // if object
else
@ahoNerd
ahoNerd / server.js
Created February 4, 2023 16:38
Express.js example directory listing
const express = require('express')
const app = express()
const port = 3000
const fs = require('fs')
const path = require('path')
const dir = path.join(__dirname, 'Fruits')
let arr1 = []
@ahoNerd
ahoNerd / settings.json
Last active May 20, 2023 23:41
VSCode Settings
{
"editor.scrollBeyondLastLine": false,
"editor.wordWrap": "on",
"editor.multiCursorModifier": "ctrlCmd",
"editor.snippetSuggestions": "top",
"workbench.startupEditor": "welcomePage",
"workbench.iconTheme": "vscode-icons",
"workbench.colorCustomizations": { // https://code.visualstudio.com/api/references/theme-color
// "focusBorder": "#f92672",
"sash.hoverBorder": "#f92672",
@ahoNerd
ahoNerd / read.cs
Last active November 26, 2022 23:37
C# SQLite Database Connection, read more at https://ahonerd.com/csharp-sqlite-database-connection
using System.Data.SQLite;
string cs = @"URI=file:fruits.db";
using var con = new SQLiteConnection(cs);
con.Open();
string stm = "SELECT * FROM fruits LIMIT 50";
using var cmd = new SQLiteCommand(stm, con);
using SQLiteDataReader rdr = cmd.ExecuteReader();
@ahoNerd
ahoNerd / Readme.md
Created November 26, 2022 14:02 — forked from Gozala/Readme.md
Range hilighting code using getClientRects API

Highlight Selection Ranges

Code here takes a DOM Selection Range instance and creates a highlighting for it by using getClientRects. Approach was inspired by marks although here function attempts to find nearest positioned parent element to the commonAncestorContainer and draw all the highilighting rectangles there, this avoids issues with an overflowing content.

Issues

  • Approach ignores z-index which isn't great as some element might be overlaying the selection in which case it should not appear, but it does if we use high z-index value. If we use low z-index value then some elements (possibly ones containing selection) might end up overlaying selection itself.
  • Rendered selections are scattered all o
@ahoNerd
ahoNerd / App-00.vue
Last active November 20, 2022 14:04
Vue 2 with Vuetify Example, read more at https://ahonerd.com/vue-2-vuetify-example
<template>
<v-app>
<v-navigation-drawer
v-model="drawer"
app
clipped
floating
color="indigo lighten-5"
>
<!-- untuk navigasi -->
@ahoNerd
ahoNerd / setMultipleAttr_1.ts
Created November 14, 2022 04:30
Add Multiple Attribute to an Element using javaScript, read more at https://ahonerd.com/js-add-multiple-attribute
const setMultipleAttr = (elm: any, attributes: any) => {
for (const key in attributes)
elm.setAttribute(key, attributes[key])
}
@ahoNerd
ahoNerd / deluge-web.service
Last active May 20, 2023 10:04
Install Deluge on Raspberry Pi, read more at https://ahonerd.com/install-deluge-on-raspberry-pi
[Unit]
Description=Deluge Web Interface
After=network-online.target deluged.service
Wants=deluged.service
[Service]
Type=simple
User=debian-deluged
Group=debian-deluged
UMask=002