Skip to content

Instantly share code, notes, and snippets.

View bitsmanent's full-sized avatar

Claudio Alessi bitsmanent

View GitHub Profile
@bitsmanent
bitsmanent / react-native-sprite-sheets.js
Last active September 25, 2021 17:18
Simple sprite sheets for React Native
import React from "react";
import {ImageBackground} from "react-native";
export function Sprite(props) {
const [w, h] = [props.geometry.width, props.geometry.height];
const r = Math.min(w / (props.width / props.cols), h / (props.height / props.rows));
return (
<ImageBackground
source={props.source}
@bitsmanent
bitsmanent / dtree.js
Last active November 30, 2021 18:07
Extremely simple decision tree implementation
function dtree_decide(tree, ...argv) {
var i, len, r;
for(i = 0, len = tree.length; i < len; i++)
if((r = tree[i](...argv)))
return r;
return false;
}
function dtree_make(tree) {
@bitsmanent
bitsmanent / verscmp.js
Created December 21, 2020 15:35
Compare software release version
/* E.g. verscmp("1.2.3", "1.2.2") */
function verscmp(a, b) {
var spa = a.split('.').map(x => Number(x));
var spb = b.split('.').map(x => Number(x));
var part = [], r = 0;
while(!r && spa.length && spb.length) {
part = [spa.shift(), spb.shift()];
if(part[0] == part[1])
continue;
@bitsmanent
bitsmanent / t9n.js
Last active July 23, 2023 22:12
Simple translations with parametrized strings and infinite plural forms
/* Do not use code you have not read first.
* Original file: https://gist.github.com/clamiax/ee6eea3010021bc62d188739b42c86cd */
let T9N_DEFLANG = null;
function _() {
if(!T9N_DEFLANG)
return "";
return t9n_format(T9N_DEFLANG, ...Array.from(arguments));
}
@bitsmanent
bitsmanent / xmltojson.js
Created December 23, 2019 09:19
Parse an XML object into JSON
function xmltojson(xml, attkey = "$") {
var obj = {}, t, len, i;
switch(xml.nodeType) {
case Node.ELEMENT_NODE:
len = xml.attributes.length;
if(len) {
obj[attkey] = {};
for(i = 0; i < len; ++i) {
t = xml.attributes[i];
@bitsmanent
bitsmanent / cmazes.js
Last active June 19, 2019 07:52
Build random mazes of arbitrary size and draw them in console
(() => {
/*
* Build mazes of arbitrary size using a randomized version of the Deep-first
* search algorhitm. Cells are accessed using the row/column-major order
* method. Mazes are printed in console.
*
* To understand everything else, start reading main().
*/
"use strict";
@bitsmanent
bitsmanent / isleap.js
Last active June 9, 2021 12:46
Check if the given number is a leap year
function isleap(year) {
return !(year % 4) && ((year % 100) || !(year % 400));
}
@bitsmanent
bitsmanent / applytodom.js
Last active February 4, 2022 22:30
Universal HTML rendering routine (work in progress) [Superseded by domapply]
/* Superseded by domapply:
* https://gist.github.com/clamiax/e053d486c28e5101ddf6a1739a5151c7 */
/* This may eventually be included in utils.js[0] at some point, if everything
* will works as expected.
* [0] https://gist.github.com/clamiax/e1db98deaf94bb1ccf8556d993245afb */
function applytodom(dst, src) {
var par, len, atts, i, has;
if(!(dst && src))
@bitsmanent
bitsmanent / getheight.js
Created December 19, 2018 17:28
Compute full height of an element
function getheight(el) {
var props = ["height", "marginTop", "marginBottom", "borderTop"];
var s = getComputedStyle(el), ret = 0;
if(!s) return 0;
props.forEach((p) => ret += parseFloat(s[p]));
return ret;
}
@bitsmanent
bitsmanent / clearall.js
Last active November 2, 2018 12:42
Clear all timers set with setTimeout() or setInterval()
/* clear all timers */
[...Array(100000).keys()].forEach(function(i) {
++i; /* timer IDs starts from 1 */
clearTimeout(i);
clearInterval(i);
});