Skip to content

Instantly share code, notes, and snippets.

View chzager's full-sized avatar

chzager

  • Germany
View GitHub Profile
@chzager
chzager / makeElement.js
Last active November 21, 2025 16:49
Create new HTML elements
/**
* Creates a new HTML element.
* @author Christoph Zager
* @license CC0-1.0
* @param {string} definition The tag of the desired HTML element and optionally an Id and css classes, in a _query selector_ like notation (i.e. `"div#id.class1.class2"`).
* @param {...string | number | HTMLElement | {[key: string]: string | number | boolean | Function} } [content] Content (or children) to be created on/in the HTML element. This may be text content, child HTML elements or a record of attributes or event handlers.
* @returns {HTMLElement} Returns the newly created HTML element with all its content and children.
*/
function makeElement (definition, ...content)
{
@chzager
chzager / stringHash.js
Created February 13, 2022 21:59
JavaScript string hash function
function stringHash (string)
{
/* This is based on https://github.com/darkskyapp/string-hash
Since the basic code is public domain, this function is public domain as well.
*/
let hash, i = string.length;
while (i)
{
hash = (hash * 33) ^ string.charCodeAt(--i);
};
@hyamamoto
hyamamoto / string.hashcode.js
Created September 30, 2016 07:19
JavaScript Implementation of String.hashCode() .
/**
* Returns a hash code for a string.
* (Compatible to Java's String.hashCode())
*
* The hash code for a string object is computed as
* s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
* using number arithmetic, where s[i] is the i th character
* of the given string, n is the length of the string,
* and ^ indicates exponentiation.
* (The hash value of the empty string is zero.)