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 / 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;
@SparK-Cruz
SparK-Cruz / chain.js
Created November 28, 2020 19:45
Allows you to chain calls for void methods in third party objects
exports.chain = function (subject) {
const proxy = new Proxy(subject, {
get(target, name, receiver) {
const member = Reflect.get(target, name, receiver);
if (typeof member === 'function') {
return function () {
const value = member.apply(subject, arguments);
if (typeof value !== 'undefined')
return value;
@SparK-Cruz
SparK-Cruz / c13date.js
Last active April 17, 2021 22:13
C13Date Object
const monthNames = [
'Capricornus',
'Aquarius',
'Pisces',
'Aries',
'Taurus',
'Gemini',
'Cancer',
'Leo',
'Virgo',
@SparK-Cruz
SparK-Cruz / locale.js
Created February 4, 2020 21:06
Locale detector and organizer for using with i18n property of vue root object using the vue-i18n plugin
import en from './en';
import pt from './pt';
import es from './es';
const Locale = {
sharedMessages: {
en,
pt,
es
}
@SparK-Cruz
SparK-Cruz / Trespasser.php
Last active January 31, 2019 15:28
PHP Trait to be used for testing when invoking private methods is necessary
<?php
namespace Test;
trait Trespasser
{
protected function forceCall($subject, $methodName, ...$parameters)
{
$reflection = new \ReflectionMethod($subject, $methodName);
$reflection->setAccessible(true);
return $reflection->invokeArgs($subject, $parameters);
@SparK-Cruz
SparK-Cruz / bingo.js
Last active June 22, 2018 14:15
Bingo random number picker
const Bingo = (function(){
const pool = [];
for(let i=0; i<79; i++) {
pool.push(i+1);
}
function next() {
const index = Math.round(Math.random() * (pool.length - 1));
return pool.splice(index, 1)[0];
}
@SparK-Cruz
SparK-Cruz / Requestable.php
Created June 20, 2018 18:42
My default curl usage
<?php
trait Requestable {
private function getRequest($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
try {
return [curl_exec($curl), curl_errno($curl)];