Skip to content

Instantly share code, notes, and snippets.

View SparK-Cruz's full-sized avatar

Cruz SparK-Cruz

  • Brasil
View GitHub Profile
@SparK-Cruz
SparK-Cruz / caller.js
Created June 14, 2024 10:20
Get the caller context for any function after arguments.callee.caller deprecation
/**
* Usage:
* import callerProp from "./caller.js";
* Object.defineProperty(global, "__caller", callerProp);
*
* function A() {
* return B();
* }
* function B() {
* console.log(__caller.name); // A
@SparK-Cruz
SparK-Cruz / named_apply.js
Last active May 27, 2024 22:33
This extension allows functions to be called via apply with an object as argument for simulating named parameter calls.
/**
* Usage:
* Similar to native "Function.prototype.apply" but supporting an object as second argument.
*
* Given the following function:
* ```
* function myFunction(a, b = 2, c = 3, options = {complex: true}) {
* console.log(a, b, c, options);
* }
* ```
@SparK-Cruz
SparK-Cruz / nano_stamp.html
Last active June 15, 2023 04:34
Nano Address Stamps
<!DOCTYPE html>
<html>
<head>
<title>Nano Stamp</title>
<style>
img {
image-rendering: pixelated;
border: 1px solid black;
}
</style>
@SparK-Cruz
SparK-Cruz / pipe.ts
Created January 18, 2023 04:42
Pipe so you don't pile .catch handlers in Promises
export const pipe = function<T>(promise: Promise<T>, ...callbacks: ((value: T) => T|Promise<T>)[]): Promise<T> {
let subject = promise;
for (const i in callbacks) {
subject = subject.then(callbacks[i])
}
return subject;
}
@SparK-Cruz
SparK-Cruz / arrayflip.js
Created September 28, 2021 19:05
Array monkey-patch to flip keys and values into an object
Array.prototype.flip = function() {
const obj = {};
this.forEach((e, i) => {
obj[e] = i;
});
return obj;
}
@SparK-Cruz
SparK-Cruz / unchain.js
Last active July 26, 2022 14:08
Lib for chaining any method by using the ._. operator
exports.unchain = function unchain(subject) {
const proxy = new Proxy(subject, {
get(target, name, receiver) {
if (name === '_') {
return new Proxy(subject, {
get(target, name, receiver) {
const member = Reflect.get(target, name, receiver);
if (typeof member === 'function') {
return function () {
@SparK-Cruz
SparK-Cruz / array_delete.php
Created September 3, 2021 19:08
Ruby's array#delete for PHP
<?php
function array_delete(&$array, $key) {
if (!isset($array[$key])) {
return null;
}
$value = $array[$key];
unset($array[$key]);
return $value;
}
@SparK-Cruz
SparK-Cruz / index.js
Created July 14, 2021 12:56
Most Visited Sequence
const SEPARATOR = ':::';
module.exports = (function() {
const cls = function() {
this.userVisits = {};
};
const parseSequences = function(userVisits, length) {
const sequences = {};
const size = length - 1;
@SparK-Cruz
SparK-Cruz / scope.js
Last active July 26, 2022 14:14
Deferrence in JavaScript
(function(){
module.exports = function scope(block) {
const deferred = [];
const defer = function(callback, args) {
deferred.unshift({target: this, callback, args});
}
try {
return block(defer);
} finally {
@SparK-Cruz
SparK-Cruz / rng.ts
Created March 9, 2021 12:45
Seedable Pseudo-Random Number Generator for TypeScript
export namespace RNG {
let seq = 1;
export function random(seed ?:number) {
if (typeof seed != 'undefined') {
seq = seed;
}
let x = Math.sin(seq++) * 10000;
return x - Math.floor(x);
}
}