Skip to content

Instantly share code, notes, and snippets.

View Suppenhuhn79's full-sized avatar

Suppenhuhn79

  • Germany
View GitHub Profile
@Suppenhuhn79
Suppenhuhn79 / 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);
};
@Suppenhuhn79
Suppenhuhn79 / localeIsoDateString.js
Created July 14, 2021 08:35
javascript date toISOString with timezone
function localeIsoDateString(date = new Date())
{
let timezoneOffsetHours = (new Date()).getTimezoneOffset() * 60 * 1000;
let timezoneAsString = ["+", "+", "-"][Math.sign(timezoneOffsetHours) + 1] + (new Date(Math.abs(timezoneOffsetHours)).toISOString().substr(11, 5));
return new Date(date.getTime() - timezoneOffsetHours).toISOString().substr(0, 23) + timezoneAsString;
};
// Expected output (for Central European Summer Time): 2021-07-14T10:34:01.830+02:00
@Suppenhuhn79
Suppenhuhn79 / README.md
Created July 6, 2021 13:09
JavaScript Gradient Color Calculation

JavaScript Gradient Color Calculation

This is a small function to calculate a color (RGB) at a specific gradient location.

function gradientColor(percentage, stops)
  • percentage is the desired position. It is a number between (including) 0 and 1. Values less then 0 will be set to 0, values greater than 1 will be set to 1.
  • stops is an array of the gradient stops (see example).
prettyPrintXml = function (xmlString)
{
/* linearize */
let result = xmlString.replace(/>\s*</g, "><").trim();
/* simplify empty tags */
result = result.replace(/<(\S+)([^>]*)><\/\1>/g, "<$1$2/>");
/* remove spaces at tag end */
result = result.replace(/\s+(\/?>)/g, "$1");
/* remove too many spaces before attributes */
result = result.replace(/\s+(\s\S+="\S+")/g, "$1");