Skip to content

Instantly share code, notes, and snippets.

View Josema's full-sized avatar

Josema Josema

View GitHub Profile
@Josema
Josema / supplant.js
Created September 7, 2023 14:53
supplant
function supplant(text, params) {
return text.replace(/\${([^{}]*)}/g, (a, b) => {
const r = params[b]
return typeof r === 'string' || typeof r === 'number' ? r : a
})
}
console.log(supplant("I'm ${age} years old!", { age: 29 }));
console.log(supplant("The ${a} says ${n}, ${n}, ${n}!", { a: 'cow', n: 'moo' }));
@Josema
Josema / createEventEmitter.js
Last active May 6, 2023 09:18
createEventEmitter
function createEventEmitter() {
const events = {}
return {
on: (name, listener) => {
if (typeof name !== 'string') {
throw new Error(`Not valid name ${name}:${typeof name}`)
}
if (typeof listener !== 'function') {
throw new Error(`Not valid listener ${name} ${typeof listener}`)
@Josema
Josema / convert-range.js
Created September 3, 2022 16:54 — forked from rijkvanzanten/convert-range.js
Convert number from one range to another
/**
* Convert value from one range to another
* @param {Number} value value to convert
* @param {Object} oldRange min, max of values range
* @param {Object} newRange min, max of desired range
* @return {Number} value converted to other range
*/
function convertRange(value, oldRange, newRange) {
return ((value - oldRange.min) * (newRange.max - newRange.min)) / (oldRange.max - oldRange.min) + newRange.min;
}
@Josema
Josema / README.md
Last active July 7, 2021 20:23
sortBy.js

Simple usage

var data = [
    {id:4, name:"Josema", age:34, work: {isworking:true} }, 
    {id:5, name:"Enzo", age:29, work: {isworking:true} }, 
    {id:2, name:"Josema", age:29, work: {isworking:false} }, 
    {id:1, name:"Enzo", age:29, work: {isworking:false} }, 
    {id:3, name:"Enzo", age:34, work: {isworking:false} }
];
async function fetchWrapper({ url, method, headers = {}, body }) {
const opts = { method, headers }
if (method === 'POST' && typeof body === 'string') {
headers['Content-Type'] = 'application/x-www-form-urlencoded'
opts.body = body
}
const response = await fetch(url, opts)
let output
export const h=(t,p,...c)=>({t,p,c,k:p&&p.key})
export const render=(e,d,t=d.t||(d.t={}),p,r,c)=>
// arrays
e.pop?e.map((e,p)=>render(e,d,t.o&&t.o[p])):
// components
e.t.call?(e.i=render((render.c=e).t({children:e.c,...e.p},e.s=t.s||{},t=>
render(Object.assign(e.s,t)&&e,d,e)),t.i||d,t&&t.i||{}),e):(
// create notes
e.d=t.d||(e.t?document.createElement(e.t):new Text(e.p)),
// diff props
-0.0740966796875 3.73828125 6.6953125 -0.0172119140625 -0.05413818359375 -0.002765655517578125
0.1337890625 4.76953125 8.1171875 -0.0172119140625 -0.05413818359375 -0.002765655517578125
0.2247314453125 4.80859375 8.6796875 -0.0172119140625 -0.05413818359375 -0.002765655517578125
0.1864013671875 4.8203125 8.7265625 -0.0172119140625 -0.05413818359375 -0.002765655517578125
0.1744384765625 4.8125 8.6953125 -0.0172119140625 -0.05413818359375 -0.002765655517578125
0.1793212890625 4.78125 8.625 -0.0172119140625 -0.05413818359375 -0.002765655517578125
0.1673583984375 4.71875 8.578125 -0.0172119140625 -0.05413818359375 -0.002765655517578125
0.12432861328125 4.734375 8.5234375 -0.0172119140625 -0.05413818359375 -0.002765655517578125
0.0980224609375 4.71875 8.53125 -0.0172119140625 -0.05413818359375 -0.002765655517578125
0.06689453125 4.68359375 8.5078125 -0.0172119140625 -0.05413818359375 -0.002765655517578125
We can't make this file beautiful and searchable because it's too large.
-0.308349609375,0.260498046875,9.8984375,-0.0036563873291015625,-0.0102996826171875,0.0012645721435546875
-0.298828125,0.2294921875,9.875,-0.0012140274047851562,-0.0127410888671875,0.0012645721435546875
-0.279541015625,0.2342529296875,9.9296875,0.000007450580596923828,0.00679779052734375,0.0012645721435546875
-0.279541015625,0.219970703125,9.953125,0.000007450580596923828,0.014129638671875,0.00004315376281738281
-0.284423828125,0.23193359375,9.953125,0.000007450580596923828,-0.0017518997192382812,0.00004315376281738281
-0.29150390625,0.2462158203125,9.9453125,-0.0012140274047851562,-0.0054168701171875,0.0012645721435546875
-0.30126953125,0.2222900390625,9.90625,0.000007450580596923828,0.004352569580078125,0.0012645721435546875
-0.298828125,0.239013671875,9.9140625,-0.0012140274047851562,0.0019121170043945312,0.0012645721435546875
-0.310791015625,0.2342529296875,9.890625,-0.0024356842041015625,-0.0029735565185546875,0.0024852752685546875
-0.308349609375,0.2080078125,9.9375,0.000007450580596923828,0.00191211700
@Josema
Josema / forEach.js
Last active July 7, 2021 20:22
forEachAsync
// https://2ality.com/2016/10/asynchronous-iteration.html
async function forEach(object, callback) {
if (Array.isArray(object)) {
for (let prop = 0; prop < object.length; ++prop) {
await callback(object[prop], prop)
}
} else {
for (const prop in object) {
await callback(object[prop], prop)
@Josema
Josema / arrowFunctionToClassic.js
Last active May 9, 2016 08:57
Convert arrow function into classic function in runtime
function isArrowFunction(f) {
return typeof f == 'function' && /^\([^)]*\)\s*=>/.test(f.toString());
}
function arrowFunctionToClassic(f) {
var string_function_converted = f.toString().replace(/\(([^)]*)\)\s*=>([\s\S]+)/, function(all, params, content) {
if ( content.substr(-1) != '}' )
content = '{return '+ content + ';}';