Skip to content

Instantly share code, notes, and snippets.

View caracal7's full-sized avatar

Dmitrii Vasilev caracal7

  • This planet
View GitHub Profile
@caracal7
caracal7 / from-now.js
Created January 20, 2024 13:07 — forked from davidrleonard/from-now.js
moment.fromNow() implemented in vanilla JavaScript as a simple, standalone function. Runs in the browser. No dependencies required.
/**
* Implements all the behaviors of moment.fromNow(). Pass a
* valid JavaScript Date object and the method will return the
* time that has passed since that date in a human-readable
* format. Passes the moment test suite for `fromNow()`.
* See: https://momentjs.com/docs/#/displaying/fromnow/
*
* @example
*
* var pastDate = new Date('2017-10-01T02:30');
@caracal7
caracal7 / string.hashcode.js
Created March 29, 2023 12:10 — forked from hyamamoto/ string.hashcode.js
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.)
@caracal7
caracal7 / mysql_escape_string.js
Last active February 6, 2020 15:53 — forked from sstur/mysql_escape_string.js
Escape string for MySQL
const mysql_escape_string = str => str.replace(/[\0\x08\x09\x1a\n\r"'\\\%]/g, ch => {
switch (ch) {
case "\0": return "\\0";
case "\x08": return "\\b";
case "\x09": return "\\t";
case "\x1a": return "\\z";
case "\n": return "\\n";
case "\r": return "\\r";
case "\"":
case "'":
@caracal7
caracal7 / scroll.js
Last active June 15, 2019 09:48 — forked from thetutlage/scroll.js
Scroll to the bottom of an element with smooth animation - Pure Javascript
function scrollTo (element, duration) {
if (!element) return;
var target = element.scrollHeight;
target = Math.round(target);
duration = Math.round(duration);
if (duration < 0) return false;
if (duration === 0) {
element.scrollTop = target;
return true;
}
@caracal7
caracal7 / json-syntax-highlighter.js
Last active August 27, 2019 10:19 — forked from twilson63/json-syntax-highlighter.js
Javascript JSON syntax highlight
const isISODate = date => new Date(date) !== "Invalid Date" && !isNaN(new Date(date)) && date == new Date(date).toISOString();
const syntaxHighlight = json => {
json = JSON.stringify(json, null, ' ')
json = json.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
var result = json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) {
var cls = '';
if (/^"/.test(match)) {
if (/:$/.test(match)) {
cls = 'key';
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
@caracal7
caracal7 / objectpath.js
Created March 24, 2019 19:18 — forked from alanthai/objectpath.js
Object Path
// Gets value of object given a string path
// Example: objectPathGet({a: {b: {c: {d: "hello"}}}}, "a.b.c.d") // returns "hello"
function objectPathGet(obj, path, _default) {
try {
var keys = path.split(".");
return (function _get(obj) {
var child = obj[keys.shift()];
return keys.length ? _get(child) : child;
})(obj);
} catch(err) {
@caracal7
caracal7 / async-foreach.js
Created March 24, 2019 14:43 — forked from Atinux/async-foreach.js
JavaScript: async/await with forEach()
const waitFor = (ms) => new Promise(r => setTimeout(r, ms))
const asyncForEach = (array, callback) => {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array)
}
}
const start = async () => {
await asyncForEach([1, 2, 3], async (num) => {
await waitFor(50)
@caracal7
caracal7 / xor.js
Created March 13, 2019 10:49 — forked from axjs/xor.js
XOR crypt
const encrypt = (str, key) => str
.split('')
.map(s=>(s.charCodeAt()^key).toString(16))
.join('g')
;
const decrypt = (str, key) => str
.split('g')
.filter(Boolean)
.map(s=> String.fromCharCode(parseInt(s,16)^key) )
.join('')
@caracal7
caracal7 / object-watch.js
Created February 16, 2019 10:26 — forked from eligrey/object-watch.js
object.watch polyfill in ES5
/*
* object.watch polyfill
*
* 2012-04-03
*
* By Eli Grey, http://eligrey.com
* Public Domain.
* NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
*/