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 / 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);
}
}
@SparK-Cruz
SparK-Cruz / otp.js
Last active March 5, 2021 01:42
Lib/function for One Time Padding strings
(() => {
const OTP = (() => {
function xorStrings(a, b) {
let c = "";
for (let i = 0; i < a.length; i++)
c += String.fromCharCode(a.charCodeAt(i) ^ b.charCodeAt(i));
return c;
}
@SparK-Cruz
SparK-Cruz / atena.js
Created January 21, 2021 02:02
Yet another browser AJAX lib
(function () {
function prmze() {
let thenCallback = null;
let catchCallback = null;
let child = null;
return {
ready: false,
then: function (callback) {
this.ready = true;