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 / index.mjs
Last active August 27, 2024 10:54
Converts CommonJS format packages to ESModule format
export * as default from "./index.js";
export * from "./index.js";
// 1. write your lib in CommonJS
// 2. add "module": "index.mjs" to you package.json
@SparK-Cruz
SparK-Cruz / example.js
Created August 12, 2024 14:46
Functions to read and write on graphs with dotted string keys
const obj = {
a: {
a: 5
},
b: {
a: 8
}
};
console.log(readGraph(obj, 'a.a')); // 5
@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;