Skip to content

Instantly share code, notes, and snippets.

View Pho3nixHun's full-sized avatar
Charging up

László Simon Pho3nixHun

Charging up
View GitHub Profile
@Pho3nixHun
Pho3nixHun / getTablesAsRecord.js
Last active September 27, 2024 01:46
useragents.me to JSON converter
/**
* @typedef {string} ElementToStringConverterResult - The result of the ElementToStringConverter (string).
*/
/**
* @typedef {function(HTMLElement | null, number): ElementToStringConverterResult} ElementToStringConverter - A function that converts an element to an ElementToStringConverterResult (string).
*/
/**
* @typedef {Object.<ElementToStringConverterResult, ElementToStringConverterResult>} KeysAndElementsMapperResult - The result of the KeysAndElementsMapper (record).
*/
const require = async (url) => {
const scriptAlreadyRequired = document.querySelector(`script[data-origin="${url}"`);
if (scriptAlreadyRequired) {
return scriptAlreadyRequired;
}
const script = document.createElement('script');
const code = await fetch(url);
script.type = 'text/javascript';
script.setAttribute('data-origin', url);
script.innerText = await code.text();
@Pho3nixHun
Pho3nixHun / expect.js
Last active March 28, 2018 08:55
Will throw a TypeError if definition does not match with target.
/**
* Will throw a TypeError if definition does not match with target.
* @param {string} def The types to chec target against. Multiple types have to be separated by | (pipe).
* @param {Object.<string, string|Object|Array|number|boolean>} def The object of definitions the target object will be matched against.
* @param {[string|Object|Array|number|boolean]} def Target Array(!) will be matched against def[0].
* @param {number|boolean} def Target's value will be matched against the value of definition.
* @param {any} target Target to check.
* @example See shared/Utils.md
*/
function expect(def, target) {
@Pho3nixHun
Pho3nixHun / index.js
Created September 12, 2017 05:26
Algorithms
function test(func, target, iterations = 100000) {
const start = Date.now();
for (let i = 0; i < iterations; i++) {
func(target.slice(), i);
}
const end = Date.now();
return { duration: end - start, result: func(target.slice(), 0) };
}
function truncate(text, maxLength = 32) {
@Pho3nixHun
Pho3nixHun / request
Last active September 23, 2017 08:49
Simple request for browser.
(function(exports) {
'use strict';
class Request {
static get METHODS() {
return {
GET: 'GET',
POST: 'POST',
PUT: 'PUT',
DELETE: 'DELETE',